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 }
  • 相关阅读:
    百度AI开放平台 情感倾向分析实例以及gbk编码解决
    根据cid获取哔哩哔哩弹幕
    python3 doc2vec文本聚类实现
    python3 LDA主题模型以及TFIDF实现
    偶得李春芬先生书信一函
    STATA一小步 我的一大步
    2013-2015南京大学历史学系若干考试题目汇编
    ArcPy批量计算Mean Center的两个实例
    解决Gephi导入csv文件时提示“边表格需要一个包含节点标号‘源’和‘目标’列” 问题的两个方案
    Vertx 实现webapi实战项目(二)
  • 原文地址:https://www.cnblogs.com/coolbear/p/6214294.html
Copyright © 2011-2022 走看看