2

是否可以在不序列化类型信息的情况下序列化数据?我问是因为我正在使用 Cereal 将数据序列化为数据包并通过网络发送它们,并且希望尽可能减少数据包大小,因此不包括类型信息会有所帮助。

我认为这是可能的唯一原因是,在发送端和接收端,应用程序已经知道类型信息以及序列化的顺序。

更容易准确地理解我如何从我的实际实现中序列化和反序列化数据:

网络包.h

#pragma once

#include <cereal\archives\portable_binary.hpp>
#include <cereal\types\polymorphic.hpp>
#include <cereal\types\memory.hpp>
#include <cereal\types\vector.hpp>
#include <cereal\types\string.hpp>
#include <sstream>

class NetVar_ {
protected:
    virtual void MakeMePolymorphic() {}
public:
    template <class Archive> void serialize(Archive & archive) {}
};

class NetPacket;

template <class VARTYPE> class NetVar : public NetVar_
{
public:
    NetVar(void) {}

    NetVar(VARTYPE Value)
    {
        Var = Value;
    }

    template <class Archive> void serialize(Archive & archive)
    {
        archive(Var);
    }

private:
    friend class NetPacket;
    VARTYPE Var;
};


class NetPacket
{
private:
    ENetHost* Host;
    enet_uint8 ChannelNumber;
    unsigned int ThisMessageID;
    std::vector<std::shared_ptr<NetVar_>> PacketData;

    std::string SerializePacket();

public:
    NetPacket(ENetHost* iHost, const unsigned int MessageID, enet_uint8 ChannelNum);

    NetPacket(const char* Data, size_t Size);

    template <class DATATYPE> void WriteData(DATATYPE AddData)
    {
        PacketData.push_back(std::make_shared<NetVar<DATATYPE>>(AddData));
    }

    template <class DATATYPE> DATATYPE ReadData(const unsigned int VarNum) const
    {
        std::shared_ptr<NetVar_> Content = PacketData[VarNum];
        std::shared_ptr<NetVar<DATATYPE>> Temp = std::static_pointer_cast<NetVar<DATATYPE>>(Content);
        return Temp->Var;
    }

    void Send(const bool Reliable);

    void Send(NetClient* Client, const bool Reliable);

    unsigned int GetMessageID(void) const;

    enet_uint8 GetChannelNumber(void) const;

};

网络包.cpp

#include <enet\enet.h>
#include "NetClient.h"

#include "NetPacket.h"
// Register Types here that you want to
// read and write to packets

CEREAL_REGISTER_TYPE(NetVar<bool>);

CEREAL_REGISTER_TYPE(NetVar<unsigned int>);
CEREAL_REGISTER_TYPE(NetVar<int>);

CEREAL_REGISTER_TYPE(NetVar<float>);

CEREAL_REGISTER_TYPE(NetVar<std::string>);


std::string NetPacket::SerializePacket()
{
    std::ostringstream SData;
    {
        cereal::PortableBinaryOutputArchive Archive(SData);
        Archive(ThisMessageID, PacketData);
    }
    return SData.str();
}

NetPacket::NetPacket(ENetHost* iHost, const unsigned int MessageID, enet_uint8 ChannelNum)
{
    Host = iHost;
    ThisMessageID = MessageID;
    ChannelNumber = ChannelNum;
}

NetPacket::NetPacket(const char* Data, size_t Size)
{
    std::istringstream SData(std::string(Data, Size));
    {
        cereal::PortableBinaryInputArchive Archive(SData);
        Archive(ThisMessageID, PacketData);
    }
}

void NetPacket::Send(const bool Reliable)
{
    auto Out = SerializePacket();
    ENetPacket* Packet;
    if (Reliable)
    {
        Packet = enet_packet_create(Out.data(), Out.size(), ENET_PACKET_FLAG_RELIABLE);
    }
    else {
        Packet = enet_packet_create(Out.data(), Out.size(), ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT);
    }
    enet_host_broadcast(Host, ChannelNumber, Packet);
}

void NetPacket::Send(NetClient* Client, const bool Reliable)
{
    auto Out = SerializePacket();
    ENetPacket* Packet;
    if (Reliable)
    {
        Packet = enet_packet_create(Out.data(), Out.size(), ENET_PACKET_FLAG_RELIABLE);
    }
    else {
        Packet = enet_packet_create(Out.data(), Out.size(), ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT);
    }
    enet_peer_send(Client->GetPeer(), ChannelNumber, Packet);
}

unsigned int NetPacket::GetMessageID(void) const
{
    return ThisMessageID;
}

enet_uint8 NetPacket::GetChannelNumber(void) const
{
    return ChannelNumber;
}

从 NetPacket.h 中定义的 ReadData 和 WriteData 可以看出,要读取和写入数据,您需要在模板函数中指定类型信息。由于这个要求,类型信息在阅读器和作者端以及它的写入顺序是已知的。序列化数据时有什么方法可以省略类型信息?

对于这个大问题,我深表歉意,非常感谢您在这件事上花费的时间。

4

0 回答 0