Http://www.jenkinssoftware.com/raknet/manual/creatingpackets.html
Creating Packets with Bitstreams |
Write less data with Bitstreams
Lets Take we 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 Raknet::time TIMESTAMP;//Put the SYS TEM time in here returned by Raknet::gettime () MessageID typeId;//This would be assigned to a type I ' ve added after ID _user_packet_enum, lets say id_set_timed_mine Usetimestamp = id_timestamp; TimeStamp = Raknet::gettime (); Typeid=id_set_timed_mine; Bitstream Mybitstream; Mybitstream.write (Usetimestamp); Mybitstream.write (TimeStamp); Mybitstream.write (typeId); //Assume we have a mine* Mine object Mybitstream.write (Mine->getposition (). x); Mybitstream.write (Mine->getposition (). y); Mybitstream.write (Mine->getposition (). z); Mybitstream.write (Mine->getnetworkid ());//In the struct this is Networkid networkid Mybitstream.write ( Mine->getowner ()); The struct is systemaddress systemaddress |
Common mistake!
When writing the first byte to a bitstream, is sure to cast it to (MessageID) or (unsigned char). If you just write the enumeration directly, you'll 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 would see the first byte is 0, which are reserved internally to id_internal_ping, and you'll NE Ver 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 Datamybitstream.read (Usetimestamp); Mybitstream.read (timeStamp); MyB Itstream.read (typeId); Mybitstream.read (x); Mybitstream.read (y); Mybitstream.read (z); Mybitstream.read (NetworkID); In the struct this is Networkid networkidmybitstream.read (systemaddress); In the struct this is systemaddress systemaddress}
Raknet Sending and receiving data