RakNet发送与接收数据
http://www.jenkinssoftware.com/raknet/manual/creatingpackets.html
Creating Packets with Bitstreams |
Write less data with bitstreams |
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.html
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); 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 }