From 919cac4c7890531bce4dbb47b53fd7cc664f481f Mon Sep 17 00:00:00 2001 From: Freddie Witherden Date: Sat, 29 Dec 2007 12:13:18 +0000 Subject: [PATCH] Commit the first part of patch #901. This ports the ping functions over to the new netcode. git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@3218 4a71c877-e1ca-e34f-864e-861f7616d084 --- src/multiplay.c | 2 +- src/multirecv.h | 2 +- src/multisync.c | 108 +++++++++++++++++++++++++++++++----------------- 3 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/multiplay.c b/src/multiplay.c index 7989695a8..69925b01a 100644 --- a/src/multiplay.c +++ b/src/multiplay.c @@ -726,7 +726,7 @@ BOOL recvMessage(void) recvDestroyFeature(&msg); break; case NET_PING: // diagnostic ping msg. - recvPing(&msg); + recvPing(); break; case NET_DEMOLISH: // structure demolished. recvDemolishFinished(); diff --git a/src/multirecv.h b/src/multirecv.h index 0ecc57c86..1c2a9f6e7 100644 --- a/src/multirecv.h +++ b/src/multirecv.h @@ -40,7 +40,7 @@ extern BOOL recvBuildFinished (); extern BOOL recvTemplate (NETMSG *pMsg); extern BOOL recvDestroyFeature (NETMSG *pMsg); extern BOOL recvDemolishFinished (); -extern BOOL recvPing (NETMSG *pMsg); +extern BOOL recvPing (); extern BOOL recvRequestDroid (NETMSG *pMsg); extern BOOL recvTextMessage (NETMSG *pMsg); extern BOOL recvDroidSecondary (NETMSG *pMsg); diff --git a/src/multisync.c b/src/multisync.c index e02387004..ca122e13a 100644 --- a/src/multisync.c +++ b/src/multisync.c @@ -1070,77 +1070,109 @@ UDWORD averagePing(void) BOOL sendPing(void) { - NETMSG ping; - UDWORD i; - static UDWORD lastPing=0; // last time we sent a ping. - static UDWORD lastav=0; // last time we updated average. + BOOL new = TRUE; + uint8_t player = selectedPlayer; + int i; + static UDWORD lastPing = 0; // Last time we sent a ping + static UDWORD lastav = 0; // Last time we updated average - // only ping every so often. - if(lastPing > gameTime)lastPing= 0; - if((gameTime-lastPing) < PING_FREQUENCY) + // Only ping every so often + if (lastPing > gameTime) + { + lastPing = 0; + } + + if (gameTime - lastPing < PING_FREQUENCY) { return TRUE; } + lastPing = gameTime; - /// if host, also update the average ping stat for joiners. - if(NetPlay.bHost) + // If host, also update the average ping stat for joiners + if (NetPlay.bHost) { - if(lastav > gameTime)lastav= 0; - if((gameTime-lastav) > AV_PING_FREQUENCY) + if (lastav > gameTime) { - NETsetGameFlags(2,averagePing()); - lastav=gameTime; + lastav = 0; + } + + if (gameTime - lastav > AV_PING_FREQUENCY) + { + NETsetGameFlags(2, averagePing()); + lastav = gameTime; } } - // before we send the ping, if any player failed to respond to the last one - // we should re-enumerate the players. + /* + * Before we send the ping, if any player failed to respond to the last one + * we should re-enumerate the players. + */ - for (i=0; ibody[1] ==1) // if this is a new ping + // If this is a new ping, respond to it + if (new) { - reply.body[0] = (char) selectedPlayer; - reply.body[1] = 0; - reply.size = 2; - reply.type = NET_PING; - - NETsend(&reply, player2dpid[(int) ping->body[0]], FALSE); // reply to it + NETbeginEncode(NET_PING, player2dpid[sender]); + // We are responding to a new ping + new = FALSE; + + NETuint8_t(&us); + NETbool(&new); + NETend(); } - else // else it's returned, so store it. + // They are responding to one of our pings + else { - ingame.PingTimes[(int) ping->body[0]] = (gameTime2 - PingSend[(int) ping->body[0]] ) /2 ; - PingSend[(int) ping->body[0]] = 0; // note we've recvd it! + // Work out how long it took them to respond + ingame.PingTimes[sender] = (gameTime2 = PingSend[sender]) / 2; + + // Note that we have received it + PingSend[sender] = 0; } + return TRUE; }