net: less-trivial, but not non-trivial changes

git-svn-id: https://svn.eduke32.com/eduke32@2679 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-05-19 16:25:52 +00:00
parent a59e5f40fe
commit 341f8e7432

View file

@ -231,7 +231,7 @@ void Net_RestoreMapState(netmapstate_t *save)
{ {
if (save != NULL) if (save != NULL)
{ {
int32_t i, x; int32_t i;
intptr_t j; intptr_t j;
char phealth[MAXPLAYERS]; char phealth[MAXPLAYERS];
@ -477,15 +477,11 @@ void Net_Disconnect(void)
if (g_netServer) if (g_netServer)
{ {
ENetPeer *currentPeer; int32_t i;
ENetEvent event; ENetEvent event;
for (currentPeer = g_netServer -> peers; for (i=0; i<(signed)g_netServer->peerCount; i++)
currentPeer < & g_netServer -> peers [g_netServer -> peerCount]; enet_peer_disconnect_later(&g_netServer->peers[i], 0);
++ currentPeer)
{
enet_peer_disconnect_later(currentPeer, 0);
}
while (enet_host_service(g_netServer, & event, 3000) > 0) while (enet_host_service(g_netServer, & event, 3000) > 0)
{ {
@ -512,6 +508,7 @@ static void Net_SendVersion(ENetPeer *client)
buf[0] = PACKET_VERSION; buf[0] = PACKET_VERSION;
buf[1] = BYTEVERSION; buf[1] = BYTEVERSION;
// XXX: s_buildDate is outdated and useless; uint8 is not enough :/
buf[2] = (uint8_t)atoi(s_buildDate); buf[2] = (uint8_t)atoi(s_buildDate);
buf[3] = myconnectindex; buf[3] = myconnectindex;
@ -563,13 +560,13 @@ void Net_SendUserMapName(void)
return; return;
packbuf[0] = PACKET_USER_MAP; packbuf[0] = PACKET_USER_MAP;
packbuf[1] = 0;
Bcorrectfilename(boardfilename,0); Bcorrectfilename(boardfilename,0);
j = Bstrlen(boardfilename); // user map name is sent with a NUL at the end
boardfilename[j++] = 0; j = Bstrlen(boardfilename)+1;
Bstrcat(packbuf+1,boardfilename); Bmemcpy(&packbuf[1], boardfilename, j);
j++;
packbuf[j++] = myconnectindex; packbuf[j++] = myconnectindex;
@ -670,8 +667,12 @@ void Net_SyncPlayer(ENetEvent *event)
S_PlaySound(DUKE_GETWEAPON2); S_PlaySound(DUKE_GETWEAPON2);
// open a new slot if necessary and save off the resulting slot # for future reference // open a new slot if necessary and save off the resulting slot # for future reference
for (TRAVERSE_CONNECT(i)) if (g_player[i].playerquitflag == 0) break; for (TRAVERSE_CONNECT(i))
event->peer->data = (void *)((intptr_t)(i = (i == -1 ? playerswhenstarted++ : i))); if (g_player[i].playerquitflag == 0)
break;
if (i == -1)
i = playerswhenstarted++;
event->peer->data = (void *)i;
g_player[i].netsynctime = totalclock; g_player[i].netsynctime = totalclock;
g_player[i].playerquitflag = 1; g_player[i].playerquitflag = 1;
@ -702,7 +703,7 @@ void Net_SyncPlayer(ENetEvent *event)
if (g_player[0].ps->gm & MODE_GAME) if (g_player[0].ps->gm & MODE_GAME)
{ {
alloc_multimapstate(i); alloc_multimapstate(i);
if (g_multiMapState[i])
{ {
char *buf = (char *)Bmalloc(sizeof(netmapstate_t)+512); char *buf = (char *)Bmalloc(sizeof(netmapstate_t)+512);
@ -1402,6 +1403,15 @@ void Net_ParseClientPacket(ENetEvent *event)
} }
} }
static void display_betascreen(void)
{
rotatesprite_fs(0,0,65536,0,BETASCREEN,0,0,2+8+16+64);
rotatesprite_fs(160<<16,(104)<<16,60<<10,0,DUKENUKEM,0,0,2+8);
rotatesprite_fs(160<<16,(129)<<16,30<<11,0,THREEDEE,0,0,2+8);
if (PLUTOPAK) // JBF 20030804
rotatesprite_fs(160<<16,(151)<<16,30<<11,0,PLUTOPAKSPRITE+1,0,0,2+8);
}
void Net_GetPackets(void) void Net_GetPackets(void)
{ {
@ -1432,6 +1442,8 @@ void Net_GetPackets(void)
// dispatch any pending events from the local packet queue // dispatch any pending events from the local packet queue
while (enet_host_check_events(g_netServer, &event) > 0) while (enet_host_check_events(g_netServer, &event) > 0)
{ {
const intptr_t playeridx = (intptr_t)event.peer->data;
switch (event.type) switch (event.type)
{ {
case ENET_EVENT_TYPE_CONNECT: case ENET_EVENT_TYPE_CONNECT:
@ -1456,35 +1468,40 @@ void Net_GetPackets(void)
*/ */
Net_ParseClientPacket(&event); Net_ParseClientPacket(&event);
// broadcast takes care of enet_packet_destroy itself // broadcast takes care of enet_packet_destroy itself
// we set the state to disconnected so enet_host_broadcast doesn't send the player back his own packets // we set the state to disconnected so enet_host_broadcast
if ((event.channelID == CHAN_GAMESTATE && event.packet->data[0] > PACKET_BROADCAST) || event.channelID == CHAN_CHAT) // doesn't send the player back his own packets
if ((event.channelID == CHAN_GAMESTATE && event.packet->data[0] > PACKET_BROADCAST)
|| event.channelID == CHAN_CHAT)
{ {
const ENetPacket *pak = event.packet;
event.peer->state = ENET_PEER_STATE_DISCONNECTED; event.peer->state = ENET_PEER_STATE_DISCONNECTED;
enet_host_broadcast(g_netServer, event.channelID, enet_host_broadcast(g_netServer, event.channelID,
enet_packet_create(event.packet->data, event.packet->dataLength, event.packet->flags & ENET_PACKET_FLAG_RELIABLE)); enet_packet_create(pak->data, pak->dataLength, pak->flags&ENET_PACKET_FLAG_RELIABLE));
event.peer->state = ENET_PEER_STATE_CONNECTED; event.peer->state = ENET_PEER_STATE_CONNECTED;
} }
enet_packet_destroy(event.packet); enet_packet_destroy(event.packet);
g_player[(intptr_t)event.peer->data].ping = (event.peer->lastRoundTripTime + event.peer->roundTripTime)/2; g_player[playeridx].ping = (event.peer->lastRoundTripTime + event.peer->roundTripTime)/2;
break; break;
case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT:
numplayers--; numplayers--;
ud.multimode--; ud.multimode--;
P_RemovePlayer((intptr_t)event.peer->data); P_RemovePlayer(playeridx);
packbuf[0] = PACKET_PLAYER_DISCONNECTED; packbuf[0] = PACKET_PLAYER_DISCONNECTED;
packbuf[1] = (intptr_t)event.peer->data; packbuf[1] = playeridx;
packbuf[2] = numplayers; packbuf[2] = numplayers;
packbuf[3] = ud.multimode; packbuf[3] = ud.multimode;
packbuf[4] = playerswhenstarted; packbuf[4] = playerswhenstarted;
packbuf[5] = myconnectindex; packbuf[5] = myconnectindex;
enet_host_broadcast(g_netServer, CHAN_GAMESTATE, enet_packet_create(packbuf, 6, ENET_PACKET_FLAG_RELIABLE)); enet_host_broadcast(g_netServer, CHAN_GAMESTATE,
enet_packet_create(packbuf, 6, ENET_PACKET_FLAG_RELIABLE));
initprintf("%s disconnected.\n", g_player[(intptr_t)event.peer->data].user_name); initprintf("%s disconnected.\n", g_player[playeridx].user_name);
event.peer->data = NULL; event.peer->data = NULL;
break; break;
@ -1524,63 +1541,57 @@ void Net_GetPackets(void)
{ {
g_netSync = 1; g_netSync = 1;
buf = (uint8_t *)Bcalloc(1, sizeof(netmapstate_t)+512); buf = (uint8_t *)Bcalloc(1, sizeof(netmapstate_t)+512);
if (buf == NULL) // TODO: G_GameExit should send a "bye" packet?
G_GameExit("OUT OF MEMORY in Net_GetPackets:sync");
} }
alloc_multimapstate(0); alloc_multimapstate(0);
rotatesprite(0,0,65536L,0,BETASCREEN,0,0,2+8+16+64,0,0,xdim-1,ydim-1); display_betascreen();
rotatesprite(160<<16,(104)<<16,60<<10,0,DUKENUKEM,0,0,2+8,0,0,xdim-1,ydim-1); Bassert(datasiz+event.packet->dataLength <= sizeof(netmapstate_t)+512);
rotatesprite(160<<16,(129)<<16,30<<11,0,THREEDEE,0,0,2+8,0,0,xdim-1,ydim-1);
if (PLUTOPAK) // JBF 20030804
rotatesprite(160<<16,(151)<<16,30<<11,0,PLUTOPAKSPRITE+1,0,0,2+8,0,0,xdim-1,ydim-1);
if (buf && event.packet->dataLength == SYNCPACKETSIZE) if (event.packet->dataLength == SYNCPACKETSIZE)
{ {
char tbuf[64]; char tbuf[64];
Bmemcpy((uint8_t *)(buf)+datasiz, event.packet->data, event.packet->dataLength);
Bmemcpy(buf+datasiz, event.packet->data, SYNCPACKETSIZE);
datasiz += SYNCPACKETSIZE; datasiz += SYNCPACKETSIZE;
Bsprintf(tbuf, "Received %d bytes\n", (int32_t)datasiz); Bsprintf(tbuf, "Received %d bytes\n", (int32_t)datasiz);
gametext(160,190,tbuf,14,2); gametext(160,190,tbuf,14,2);
} }
// last packet of mapstate sequence else
else if (buf)
{ {
Bmemcpy((uint8_t *)(buf)+datasiz, event.packet->data, event.packet->dataLength); // last packet of mapstate sequence
Bmemcpy(buf+datasiz, event.packet->data, event.packet->dataLength);
datasiz = 0; datasiz = 0;
if (qlz_size_decompressed((const char *)buf) == sizeof(netmapstate_t)) if (qlz_size_decompressed((const char *)buf) == sizeof(netmapstate_t))
{ {
qlz_decompress((const char *)buf, g_multiMapState[0], state_decompress); qlz_decompress((const char *)buf, g_multiMapState[0], state_decompress);
Bfree(buf);
buf = NULL;
packbuf[0] = PACKET_REQUEST_GAMESTATE; packbuf[0] = PACKET_REQUEST_GAMESTATE;
packbuf[1] = myconnectindex; packbuf[1] = myconnectindex;
enet_peer_send(g_netClientPeer, CHAN_GAMESTATE, enet_packet_create(&packbuf[0], 2, ENET_PACKET_FLAG_RELIABLE)); enet_peer_send(g_netClientPeer, CHAN_GAMESTATE,
enet_packet_create(&packbuf[0], 2, ENET_PACKET_FLAG_RELIABLE));
gametext(160,190,"Transfer Complete",14,2); gametext(160,190,"Transfer Complete",14,2);
} }
else else
{ {
initprintf("Invalid map state from server! Decompressed to %ld bytes, expected %ld.\n", initprintf("Invalid map state from server! Decompressed to %ld bytes, expected %ld.\n",
qlz_size_decompressed((const char *)buf),sizeof(netmapstate_t)); qlz_size_decompressed((char *)buf), sizeof(netmapstate_t));
Bfree(buf);
buf = NULL;
g_netDisconnect = 1; g_netDisconnect = 1;
g_netSync = 0; g_netSync = 0;
gametext(160,190,"Transfer Error",14,2); gametext(160,190,"Transfer Error",14,2);
} }
}
else
{
initprintf("Error allocating buffer for map state!\n");
g_netDisconnect = 1;
g_netSync = 0;
gametext(160,190,"Transfer Error",14,2); Bfree(buf);
buf = NULL;
} }
nextpage(); nextpage();
@ -1848,10 +1859,10 @@ void Net_UpdateClients(void)
void Net_StreamLevel(void) void Net_StreamLevel(void)
{ {
int16_t i, nexti; int32_t pi;
int32_t i, nexti;
int32_t siz = 0; int32_t siz = 0;
int32_t zz, zj, k = 0, l; int32_t zz, zj, k = 0, l;
ENetPeer * currentPeer;
usize_t osize = sizeof(netmapstate_t); usize_t osize = sizeof(netmapstate_t);
if (!g_netServer || numplayers < 2) if (!g_netServer || numplayers < 2)
@ -1860,10 +1871,12 @@ void Net_StreamLevel(void)
if (!streamoutput) if (!streamoutput)
streamoutput = (netmapstate_t *)Bcalloc(1, sizeof(netmapstate_t)); streamoutput = (netmapstate_t *)Bcalloc(1, sizeof(netmapstate_t));
for (currentPeer = g_netServer->peers; for (pi=0; pi<(signed)g_netServer->peerCount; pi++)
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{ {
ENetPeer *const currentPeer = &g_netServer->peers[pi];
const intptr_t playeridx = (intptr_t)currentPeer->data;
if (currentPeer->state != ENET_PEER_STATE_CONNECTED) if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue; continue;
@ -1875,7 +1888,7 @@ void Net_StreamLevel(void)
Net_SaveMapState(g_multiMapRevisions[g_netMapRevision&(NET_REVISIONS-1)]); Net_SaveMapState(g_multiMapRevisions[g_netMapRevision&(NET_REVISIONS-1)]);
xd3_encode_memory((const uint8_t *)g_multiMapRevisions[g_netMapRevision&(NET_REVISIONS-1)], sizeof(netmapstate_t), xd3_encode_memory((const uint8_t *)g_multiMapRevisions[g_netMapRevision&(NET_REVISIONS-1)], sizeof(netmapstate_t),
(const uint8_t *)g_multiMapState[(intptr_t)currentPeer->data], sizeof(netmapstate_t), (const uint8_t *)g_multiMapState[playeridx], sizeof(netmapstate_t),
(uint8_t *)streamoutput, &osize, sizeof(netmapstate_t), XD3_COMPLEVEL_1|XD3_NOCOMPRESS); (uint8_t *)streamoutput, &osize, sizeof(netmapstate_t), XD3_COMPLEVEL_1|XD3_NOCOMPRESS);
g_netMapRevision++; g_netMapRevision++;
@ -1898,13 +1911,14 @@ void Net_StreamLevel(void)
initprintf("revision %u: final packet size: %d\n", g_netMapRevision-1, siz); initprintf("revision %u: final packet size: %d\n", g_netMapRevision-1, siz);
enet_peer_send (currentPeer, CHAN_GAMESTATE, enet_packet_create(packbuf, siz, ENET_PACKET_FLAG_RELIABLE)); enet_peer_send(currentPeer, CHAN_GAMESTATE, enet_packet_create(packbuf, siz, ENET_PACKET_FLAG_RELIABLE));
} }
} }
void faketimerhandler(void) void faketimerhandler(void)
{ {
if (((uintptr_t)g_netServer|(uintptr_t)g_netClient) == (uintptr_t)NULL) return; if (g_netServer==NULL && g_netClient==NULL)
return;
enet_host_service(g_netServer ? g_netServer : g_netClient, NULL, 0); enet_host_service(g_netServer ? g_netServer : g_netClient, NULL, 0);
} }
@ -2073,12 +2087,7 @@ void Net_WaitForServer(void)
{ {
if (quitevent || keystatus[1]) G_GameExit(""); if (quitevent || keystatus[1]) G_GameExit("");
rotatesprite(0,0,65536L,0,BETASCREEN,0,0,2+8+16+64,0,0,xdim-1,ydim-1); display_betascreen();
rotatesprite(160<<16,(104)<<16,60<<10,0,DUKENUKEM,0,0,2+8,0,0,xdim-1,ydim-1);
rotatesprite(160<<16,(129)<<16,30<<11,0,THREEDEE,0,0,2+8,0,0,xdim-1,ydim-1);
if (PLUTOPAK) // JBF 20030804
rotatesprite(160<<16,(151)<<16,30<<11,0,PLUTOPAKSPRITE+1,0,0,2+8,0,0,xdim-1,ydim-1);
gametext(160,170,"Waiting for server",14,2); gametext(160,170,"Waiting for server",14,2);
nextpage(); nextpage();