Skip to content

Commit

Permalink
Truncate game state CRC sent over net to 16 bits.
Browse files Browse the repository at this point in the history
This should save a tiny bit of bandwidth, at least 20 bytes/second.
  • Loading branch information
Cyp committed Apr 29, 2012
1 parent 5e3d4bc commit 0499825
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 45 deletions.
12 changes: 6 additions & 6 deletions lib/gamelib/gtime.cpp
Expand Up @@ -216,8 +216,8 @@ void gameTimeUpdate(bool mayUpdate)
updateLatency();
if (crcError)
{
debug(LOG_ERROR, "Synch error, gameTimes were: {%s}", listToString("%10u", ", ", gameQueueCheckTime, gameQueueCheckTime + game.maxPlayers).c_str());
debug(LOG_ERROR, "Synch error, CRCs were: {%s}", listToString("0x%08X", ", ", gameQueueCheckCrc, gameQueueCheckCrc + game.maxPlayers).c_str());
debug(LOG_ERROR, "Synch error, gameTimes were: {%s}", listToString("%7u", ", ", gameQueueCheckTime, gameQueueCheckTime + game.maxPlayers).c_str());
debug(LOG_ERROR, "Synch error, CRCs were: {%s}", listToString(" 0x%04X", ", ", gameQueueCheckCrc, gameQueueCheckCrc + game.maxPlayers).c_str());
crcError = false;
}
}
Expand Down Expand Up @@ -362,7 +362,7 @@ void sendPlayerGameTime()
unsigned player;
uint32_t latencyTicks = discreteChosenLatency / GAME_TICKS_PER_UPDATE;
uint32_t checkTime = gameTime;
uint32_t checkCrc = nextDebugSync();
GameCrcType checkCrc = nextDebugSync();

for (player = 0; player < game.maxPlayers; ++player)
{
Expand All @@ -374,7 +374,7 @@ void sendPlayerGameTime()
NETbeginEncode(NETgameQueue(player), GAME_GAME_TIME);
NETuint32_t(&latencyTicks);
NETuint32_t(&checkTime);
NETuint32_tLarge(&checkCrc);
NETuint16_t(&checkCrc);
NETuint16_t(&wantedLatency);
NETend();
}
Expand All @@ -384,12 +384,12 @@ void recvPlayerGameTime(NETQUEUE queue)
{
uint32_t latencyTicks = 0;
uint32_t checkTime = 0;
uint32_t checkCrc = 0;
GameCrcType checkCrc = 0;

NETbeginDecode(queue, GAME_GAME_TIME);
NETuint32_t(&latencyTicks);
NETuint32_t(&checkTime);
NETuint32_tLarge(&checkCrc);
NETuint16_t(&checkCrc);
NETuint16_t(&wantedLatencies[queue.index]);
NETend();

Expand Down
10 changes: 5 additions & 5 deletions lib/netplay/netplay.cpp
Expand Up @@ -3398,7 +3398,7 @@ void resetSyncDebug()
syncDebugNumDumps = 0;
}

uint32_t nextDebugSync(void)
GameCrcType nextDebugSync()
{
uint32_t ret = syncDebugLog[syncDebugNext].getCrc();

Expand All @@ -3409,7 +3409,7 @@ uint32_t nextDebugSync(void)
syncDebugNext = (syncDebugNext + 1)%MAX_SYNC_HISTORY;
syncDebugLog[syncDebugNext].clear();

return ret;
return (GameCrcType)ret;
}

static void dumpDebugSync(uint8_t *buf, size_t bufLen, uint32_t time, unsigned player)
Expand Down Expand Up @@ -3453,7 +3453,7 @@ static void recvDebugSync(NETQUEUE queue)
dumpDebugSync(debugSyncTmpBuf, bufLen, time, queue.index);
}

bool checkDebugSync(uint32_t checkGameTime, uint32_t checkCrc)
bool checkDebugSync(uint32_t checkGameTime, GameCrcType checkCrc)
{
if (checkGameTime == syncDebugLog[syncDebugNext].getGameTime()) // Can't happen - and syncDebugGameTime[] == 0, until just before sending the CRC, anyway.
{
Expand All @@ -3466,7 +3466,7 @@ bool checkDebugSync(uint32_t checkGameTime, uint32_t checkCrc)
{
if (syncDebugLog[logIndex].getGameTime() == checkGameTime)
{
if (syncDebugLog[logIndex].getCrc() == checkCrc) // Invert bits, since everyone else seems to do that with CRCs...
if ((GameCrcType)syncDebugLog[logIndex].getCrc() == checkCrc)
{
return true; // Check passed. (So far... There might still be more players to compare CRCs with.)
}
Expand All @@ -3477,7 +3477,7 @@ bool checkDebugSync(uint32_t checkGameTime, uint32_t checkCrc)

if (logIndex >= MAX_SYNC_HISTORY && syncDebugExtraGameTime == checkGameTime)
{
if (syncDebugExtraCrc == checkCrc)
if ((GameCrcType)syncDebugExtraCrc == checkCrc)
{
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/netplay/netplay.h
Expand Up @@ -352,8 +352,9 @@ void _syncDebugIntList(const char *function, const char *str, int *ints, size_t
#define syncDebugBacktrace() do { _syncDebugBacktrace(__FUNCTION__); } while(0)
void _syncDebugBacktrace(const char *function); ///< Adds a backtrace to syncDebug, if the platform supports it. Can be a bit slow, don't call way too often, unless desperate.

void resetSyncDebug(void); ///< Resets the syncDebug, so syncDebug from a previous game doesn't cause a spurious desynch dump.
uint32_t nextDebugSync(void); ///< Returns a CRC corresponding to all syncDebug() calls since the last nextDebugSync() or resetSyncDebug() call.
bool checkDebugSync(uint32_t checkGameTime, uint32_t checkCrc); ///< Dumps all syncDebug() calls from that gameTime, if the CRC doesn't match.
typedef uint16_t GameCrcType; // Truncate CRC of game state to 16 bits, to save a bit of bandwidth.
void resetSyncDebug(); ///< Resets the syncDebug, so syncDebug from a previous game doesn't cause a spurious desynch dump.
GameCrcType nextDebugSync(); ///< Returns a CRC corresponding to all syncDebug() calls since the last nextDebugSync() or resetSyncDebug() call.
bool checkDebugSync(uint32_t checkGameTime, GameCrcType checkCrc); ///< Dumps all syncDebug() calls from that gameTime, if the CRC doesn't match.

#endif
30 changes: 0 additions & 30 deletions lib/netplay/nettypes.cpp
Expand Up @@ -125,18 +125,6 @@ static void queue(const Q &q, uint32_t &vOrig)
}
}

template<class Q>
static void queueLarge(const Q &q, uint32_t &v)
{
uint16_t b[2] = {uint16_t(v>>16), uint16_t(v)};
queue(q, b[0]);
queue(q, b[1]);
if (Q::Direction == Q::Read)
{
v = b[0]<<16 | b[1];
}
}

template<class Q>
static void queue(const Q &q, uint64_t &v)
{
Expand Down Expand Up @@ -287,19 +275,6 @@ static void queueAuto(T &v)
}
}

template<class T>
static void queueAutoLarge(T &v)
{
if (NETgetPacketDir() == PACKET_ENCODE)
{
queueLarge(writer, v);
}
else if (NETgetPacketDir() == PACKET_DECODE)
{
queueLarge(reader, v);
}
}

// Queue selection functions

/// Gets the &NetQueuePair::send or NetQueue *, corresponding to queue.
Expand Down Expand Up @@ -577,11 +552,6 @@ void NETuint32_t(uint32_t *ip)
queueAuto(*ip);
}

void NETuint32_tLarge(uint32_t *ip)
{
queueAutoLarge(*ip);
}

void NETint64_t(int64_t *ip)
{
queueAuto(*ip);
Expand Down
1 change: 0 additions & 1 deletion lib/netplay/nettypes.h
Expand Up @@ -77,7 +77,6 @@ void NETint16_t(int16_t *ip);
void NETuint16_t(uint16_t *ip);
void NETint32_t(int32_t *ip); ///< Encodes small values (< 836 288) in at most 3 bytes, large values (≥ 22 888 448) in 5 bytes.
void NETuint32_t(uint32_t *ip); ///< Encodes small values (< 1 672 576) in at most 3 bytes, large values (≥ 45 776 896) in 5 bytes.
void NETuint32_tLarge(uint32_t *ip); ///< Encodes all values in exactly 4 bytes.
void NETint64_t(int64_t *ip);
void NETuint64_t(uint64_t *ip);
void NETbool(bool *bp);
Expand Down

0 comments on commit 0499825

Please sign in to comment.