Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added packed long write/read routines. Note that packed longs can con…
…tain an arbitrary number of bytes. However, only 28 bits fit into 4 bytes.
  • Loading branch information
skyjake committed Jun 19, 2006
1 parent eed4a59 commit 8ca8bab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doomsday/engine/portable/include/net_msg.h
Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions doomsday/engine/portable/src/net_msg.c
Expand Up @@ -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 ------------------------------------------------------------
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8ca8bab

Please sign in to comment.