diff --git a/src/d_netsync.cpp b/src/d_netsync.cpp index aed0100c37..ee87850262 100644 --- a/src/d_netsync.cpp +++ b/src/d_netsync.cpp @@ -24,8 +24,58 @@ extern bool netserver, netclient; +//***************************************************************************** +// VARIABLES + +// [BB] Are we measuring outbound traffic? +static bool g_MeasuringOutboundTraffic = false; +// [BB] Number of bytes sent by NETWORK_Write* since NETWORK_StartTrafficMeasurement() was called. +static int g_OutboundBytesMeasured = 0; + IDList g_NetIDList; +//***************************************************************************** +// +void NETWORK_AdvanceByteStreamPointer( BYTESTREAM_s *pByteStream, const int NumBytes, const bool OutboundTraffic ) +{ + pByteStream->pbStream += NumBytes; + + // [BB] + if ( g_MeasuringOutboundTraffic && OutboundTraffic ) + g_OutboundBytesMeasured += NumBytes; +} + +//***************************************************************************** +// +int NETWORK_ReadByte( BYTESTREAM_s *pByteStream ) +{ + int Byte = -1; + + if (( pByteStream->pbStream + 1 ) <= pByteStream->pbStreamEnd ) + Byte = *pByteStream->pbStream; + + // Advance the pointer. + pByteStream->pbStream += 1; + + return ( Byte ); +} + +//***************************************************************************** +// +void NETWORK_WriteByte( BYTESTREAM_s *pByteStream, int Byte ) +{ + if (( pByteStream->pbStream + 1 ) > pByteStream->pbStreamEnd ) + { + Printf( "NETWORK_WriteByte: Overflow!\n" ); + return; + } + + *pByteStream->pbStream = Byte; + + // Advance the pointer. + NETWORK_AdvanceByteStreamPointer ( pByteStream, 1, true ); +} + //***************************************************************************** // void NetSyncData::AssignNetID ( AActor *pActor ) @@ -46,6 +96,44 @@ void NetSyncData::FreeNetID ( ) g_NetIDList.freeID ( NetID ); } +//***************************************************************************** +// +BYTESTREAM_s::BYTESTREAM_s() : + bitBuffer( NULL ), + bitShift( -1 ) {} + +//***************************************************************************** +// +void BYTESTREAM_s::EnsureBitSpace( int bits, bool writing ) +{ + if ( ( bitBuffer == NULL ) || ( bitShift < 0 ) || ( bitShift + bits > 8 ) ) + { + if ( writing ) + { + // Not enough bits left in our current byte, we need a new one. + NETWORK_WriteByte( this, 0 ); + bitBuffer = pbStream - 1; + } + else + { + // No room for the value in this byte, so we need a new one. + if ( NETWORK_ReadByte( this ) != -1 ) + { + bitBuffer = pbStream - 1; + } + else + { + // Argh! No bytes left! + Printf("BYTESTREAM_s::EnsureBitSpace: out of bytes to use\n"); + static uint8_t fallback = 0; + bitBuffer = &fallback; + } + } + + bitShift = 0; + } +} + //***************************************************************************** // template diff --git a/src/d_netsync.h b/src/d_netsync.h index 1ff6b09f9c..7af9def747 100644 --- a/src/d_netsync.h +++ b/src/d_netsync.h @@ -36,6 +36,23 @@ struct NetSyncData { void FreeNetID (); }; +//***************************************************************************** +struct BYTESTREAM_s +{ + BYTESTREAM_s(); + void EnsureBitSpace( int bits, bool writing ); + + // Pointer to our stream of data. + uint8_t *pbStream; + + // Pointer to the end of the stream. When pbStream > pbStreamEnd, the + // entire stream has been read. + uint8_t *pbStreamEnd; + + uint8_t *bitBuffer; + int bitShift; +}; + //========================================================================== // // IDList