zoukankan      html  css  js  c++  java
  • RakNet发送与接收数据

    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
    RakNet::Time timeStamp; // Put the system time in here returned by RakNet::GetTime()
    MessageID typeId; // This will 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()); // In the struct this is SystemAddress systemAddress

    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 }
  • 相关阅读:
    【HNOI 2002】 营业额统计
    【BZOJ 3224】 普通平衡树
    【NOIP2014】 联合权值
    【NOIP2016】 组合数问题
    BZOJ2212 POI2011Tree Rotations(线段树合并)
    LOJ114 k大(xiao)异或和(线性基)
    LOJ121 动态图连通性(LCT)
    BZOJ3569 DZY Loves Chinese II(随机化+树上差分+线性基)
    BZOJ3237 AHOI2013连通图(线段树分治+并查集)
    BZOJ2208 JSOI2010连通数(floyd+bitset)
  • 原文地址:https://www.cnblogs.com/coolbear/p/6214294.html
Copyright © 2011-2022 走看看