Raknet Sending and receiving data

Source: Internet
Author: User


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 ();
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).
Bitstream->write ((MessageID) id_set_timed_mine);

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.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.