diff --git a/doomsday/engine/portable/include/net_msg.h b/doomsday/engine/portable/include/net_msg.h index a6659300ab..0e618d6554 100644 --- a/doomsday/engine/portable/include/net_msg.h +++ b/doomsday/engine/portable/include/net_msg.h @@ -27,11 +27,13 @@ void Msg_WriteByte(byte b); void Msg_WriteShort(short w); void Msg_WritePackedShort(short w); void Msg_WriteLong(int l); +void Msg_WritePackedLong(unsigned int l); void Msg_Write(const void *src, int len); byte Msg_ReadByte(void); short Msg_ReadShort(void); short Msg_ReadPackedShort(void); int Msg_ReadLong(void); +unsigned int Msg_ReadPackedLong(void); void Msg_Read(void *dest, int len); int Msg_Offset(void); void Msg_SetOffset(int offset); diff --git a/doomsday/engine/portable/src/net_msg.c b/doomsday/engine/portable/src/net_msg.c index 9fd1a59f3d..9d587e3393 100644 --- a/doomsday/engine/portable/src/net_msg.c +++ b/doomsday/engine/portable/src/net_msg.c @@ -21,6 +21,9 @@ * Buffer overflow checks *ARE NOT* made. * The caller must know what it's doing. * The data is stored using little-endian ordering. + * + * Note that negative values are not good for the packed write/read routines, + * as they always have the high bits set. */ // HEADER FILES ------------------------------------------------------------ @@ -88,6 +91,20 @@ void Msg_WriteLong(int l) netBuffer.cursor += 4; } +void Msg_WritePackedLong(unsigned int l) +{ + while(l >= 0x80) + { + // Write the lowest 7 bits, and set the high bit to indicate that + // at least one more byte will follow. + Msg_WriteByte(0x80 | (l & 0x7f)); + + l >>= 7; + } + // Write the last byte, with the high bit clear. + Msg_WriteByte(l); +} + void Msg_Write(const void *src, int len) { memcpy(netBuffer.cursor, src, len); @@ -139,6 +156,28 @@ int Msg_ReadLong(void) return LONG( *(int *) (netBuffer.cursor - 4) ); } +unsigned int Msg_ReadPackedLong(void) +{ + byte pack = 0; + int pos = 0; + unsigned int value = 0; + + do + { +#ifdef _DEBUG + if(Msg_Offset() >= netBuffer.length) + Con_Error("Packet read overflow!\n"); +#endif + + pack = *netBuffer.cursor++; + value |= ((pack & 0x7f) << pos); + pos += 7; + } + while(pack & 0x80); + + return value; +} + void Msg_Read(void *dest, int len) { #ifdef _DEBUG