RakNet

k.quoter

Новичок
Автор темы
4
0
Как принимать пакеты с сервера в клиент, и осуществлять сопутствующее действие?
Код:
#include "PluginRPC.h"
#include <string>

template <typename T>
std::string readWithSize(RakNet::BitStream& bs) {
    T size;
    if (!bs.Read(size) || size <= 0)
        return {};
    std::string str(size, '\0');
    bs.Read(str.data(), size);
    return str;
}

template <typename T>
void writeWithSize(RakNet::BitStream& bs, std::string_view str) {
    T size = static_cast<T>(str.size());
    bs.Write(size);
    bs.Write(str.data(), size);
}

bool PluginRPC::onServerMessage(unsigned char& id, RakNet::BitStream* bs) {
    if (id != 93) // RPC_ClientMessage
        return true;
    unsigned long color;
    std::string   msg;

    // read
    bs->Read(color);
    msg = readWithSize<unsigned int>(*bs);

    msg = "[HK] " + msg;

    // write
    bs->Reset();
    bs->Write(color);
    writeWithSize<unsigned int>(*bs, msg);
    return true;
}
 

vmprotect

Известный
352
193
server:
new BitStream: bitstream = BS_New();

BS_WriteValue(
    bitstream,
    PR_UINT8,   228, // custom packet
    PR_UINT32,  1 // custom rpc
);

PR_SendPacket(bitstream, playerid, PR_MEDIUM_PRIORITY, PR_RELIABLE);
BS_Delete(bitstream);

client:
rakhook::on_receive_packet += [](Packet* p) -> bool
{
    RakNet::BitStream bs(p->data, p->length, false);

    uint8_t packetID;
    uint32_t rpcID;

    bs.ResetReadPointer();
    bs.Read(packetID);
    bs.Read(rpcID);

    if (packetID == 228)
    {
        if (rpcID == 1)
        {
            // ...
        }
    }
    return true;
};