http://www.jenkinssoftware.com/raknet/manual/creatingpackets.html
Creating Packets with Bitstreams |
Write less data with bitstreams Lets take our mine example above and use a bitstream to write it out instead. We have all the same data as before. MessageID useTimeStamp; // Assign this to ID_TIMESTAMP |
Common mistake!
When writing the first byte to a bitstream, be sure to cast it to (MessageID) or (unsigned char). If you just write the enumeration directly you will be writing a full integer (4 bytes).
Right:
bitStream->Write((MessageID)ID_SET_TIMED_MINE);
Wrong:
bitStream->Write(ID_SET_TIMED_MINE);
In the second case, RakNet will see the first byte is 0, which is reserved internally to ID_INTERNAL_PING, and you will never get it.
http://www.jenkinssoftware.com/raknet/manual/receivingpackets.htmlvoid 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); 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 }