用Cerial和省略型信息序列化数据

时间:2014-07-18 02:37:53

标签: c++ c++11 optimization serialization cereal

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

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

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

NetPacket.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;

};

NetPacket.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可以看出,要读取和写入数据,您需要在模板函数中指定类型信息。由于这个要求,读者和作者的类型信息已知,并且编写的顺序。在序列化数据时有没有办法省略类型信息?

我为这个大问题道歉,我非常感谢你在这件事上的时间。

0 个答案:

没有答案