Jenkins Software

Recieving Packets
Receiving a packet

When a packet comes in on the network, i.e. Receive returns non-zero, there are three steps involved in handling it:

1. Determine the packet type. This is returned by the following code
unsigned char GetPacketIdentifier(Packet *p)
{
	if ((unsigned char)p->data[0] == ID_TIMESTAMP)
		return (unsigned char) p->data[sizeof(unsigned char) + sizeof(unsigned long)];
	else
		return (unsigned char) p->data[0];
}

2. Process the data

Receiving a structure

If you originally sent a structure, you can cast it back as follows:

if (GetPacketIdentifier(packet)==/* User assigned packet identifier here */)
	DoMyPacketHandler(packet);

// Put this anywhere you want.  Inside the state class that handles the game is a good place
void DoMyPacketHandler(Packet *packet)
{
	// Cast the data to the appropriate type of struct
	MyStruct *s = (MyStruct *) packet->data;
	assert(p->length == sizeof(MyStruct)); // This is a good idea if you’re transmitting structs.
	if (p->length != sizeof(MyStruct))
		return;

	// Perform the functionality for this type of packet, with your struct,  MyStruct *s
}
Usability Comments
  • We cast the packet data to a pointer to the appropriate type of struct to avoid the copy overhead that would occur if were to actually create the struct. However, in this case if we change any of the data in the struct it will change the packet as well. This may or may not be what we want. Take care when relaying messages as a server, as this can cause inintended bugs.
  • The assert, while not necessary, is very useful for catching difficult to find bugs if we assign the wrong identifier or the wrong size when sending the packet.
  • The if statement is useful just in case someone manages to send a packet of invalid size or type in order to try to crash the client or server. This has never happened in practice, but it can't hurt to be safe.

Receiving a bitstream

If you originally sent a bitstream, we create a bitstream to unparse the data in the same order we wrote it. We create a bitstream, using the data and the length of the packet. We then use the Read functions where we formerly used the Write functions, the ReadCompressed functions where we formerly used WriteCompressed, and follow the same logical branching if we wrote any data conditionally. This is all shown in the following example which would read in the data for the mine we created in creating packets.


void DoMyPacketHandler(Packet *packet)
{
	Bitstream myBitStream(packet->data, packet->length, false); // The false is for efficiency so we don't make a copy of the passed data
	myBitStream.Read(useTimeStamp);
	myBitStream.Read(timeStamp);
	myBitStream.Read(typeId);
	bool isAtZero;
	myBitStream.Read(isAtZero);
	if (isAtZero==false)
	{
		x=0.0f;
		y=0.0f;
		z=0.0f;
	} else {
		myBitStream.Read(x);
		myBitStream.Read(y);
		myBitStream.Read(z);
	}
	
	myBitStream.Read(networkID); // In the struct this is NetworkID networkId
	myBitStream.Read(systemAddress); // In the struct this is SystemAddress systemAddress
}
3. Deallocate the packet by passing it to DeallocatePacket(Packet *packet) of the RakPeerInterface instance that you got it from.
See Also
Index
Creating Packets
Sending Packets