如何将向量序列化为字符数组

How to serialize a vector into an array of chars

我有一个向量定义为:

std::vector<message *>

消息是:

struct message{
    static unsigned int last_id;
    unsigned int id;
    std::string msg;
    std::string timestamp;
}

我的 objective 是使用 Winsock 发送此信息(从服务器到客户端),但这只允许发送出现在 WinSock2.h 中的字符。考虑到这一点,我想序列化一个字符数组中的所有信息(id、msg 和时间戳),以便将它们一起发送,并且在客户端中,具有反序列化的功能,以便具有相同的向量 I在服务器中。

我该如何实施?

感谢任何帮助。

你可以使用Boost serialization library来save/load你的结构到一个char数组。 boost 库在 C++ 中被广泛使用,如果您不熟悉它,我建议您看一看。

除了使用 winsock,您还可以学习使用 Boost 套接字并使您的 C++ 代码在几乎任何平台上工作,而不仅仅是 Windows,但这是另一个话题。

下面是一个如何序列化向量并从套接字的另一端恢复它的示例:

#include <vector>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

struct message {
    static unsigned int last_id;
    unsigned int id;
    std::string msg;
    std::string timestamp;

    template <class ArchiveT>
    void serialize(ArchiveT& ar, const unsigned int /*version*/) // function used to serialize (save/load) data from the boost serialization library
    {
        ar & boost::serialization::make_nvp("LastId", last_id);
        ar & boost::serialization::make_nvp("Id", id);
        ar & boost::serialization::make_nvp("Msg", msg);
        ar & boost::serialization::make_nvp("Timestamp", timestamp);
    }
};

unsigned int message::last_id;

template <class T>
void serialize_save(const T& obj, std::string& outString)
{
    std::stringstream binaryOut;
    boost::archive::binary_oarchive outArchive(binaryOut);
    outArchive << obj;

    outString = binaryOut.str();
}

template <class T>
void serialize_load(T& dataOut, const void* data, const size_t dataSize)
{
    const char* dataPtr = reinterpret_cast<const char*>(data);
    std::string dataString(dataPtr, dataPtr + dataSize);
    std::stringstream dataStream(dataString);
    boost::archive::binary_iarchive binArchive(dataStream);
    binArchive >> dataOut;
}


void init_vector(std::vector<message*>& vect) {
    const size_t vectorSize = 2;

    vect.resize(vectorSize);
    for (size_t i = 0; i < vectorSize; i++) {
        vect[i] = new message();
        vect[i]->last_id = 0;
        vect[i]->id = 1;
        vect[i]->msg = "This is a message";
        vect[i]->timestamp = "12:02pm";
    }
}

int main() {
    std::vector<message*> messages;
    init_vector(messages); // initialize the vector. set it to any data

    std::string outputBuffer;
    serialize_save(messages, outputBuffer); // save the vector to a string (array of char)

    socket_write(outputBuffer.c_str(), outputBuffer.size()); // write the serialized data to the socket

    // on the reception side
    std::string receiveBuffer;
    socket_read(receiveBuffer); // receive socket data

    std::vector<message*> receivedMessages;
    serialize_load(receivedMessages, receiveBuffer.c_str(), receiveBuffer.size()); // from the array of character recover the vector
    // here the vector receivedMessages contains the same values saved in init_vector()
}

如果您愿意,可以通过更改 boost::archive::binary_iarchive 对象来更改导出格式。例如,将其替换为 boost::archive::xml_iarchive 以将对象序列化为 XML。图书馆还提供其他格式。另一个优点是它支持版本控制。

下面提供了一种解决序列化问题的简单方法。

但是,请注意它不可移植。它假定两侧的环境条件相同 (client/server),即字节序和 sizeof int 和 size_t。在编写 server/client 程序时,这种假设可能不能令人满意,您的代码也应该处理这方面的问题。

例如,如果您可以说 32 位足以满足 id 值和字符串长度的要求,则可以在反序列化时使用 htonl when serializing, and ntohl

序列化器:

class MessageSerializer
{
public:
    MessageSerializer(const message& messageStruct)
    : m_msgRef(messageStruct)
    , m_msgLength(m_msgRef.msg.length())
    , m_timeLength(m_msgRef.timestamp.length())
    {}

    size_t RequiredBufferSize() const
    {
        return sizeof(int) + sizeof(size_t)*2 + m_msgLength + m_timeLength;
    }

    void Serialize(void* buffer) const
    {
        PushNum     (buffer, m_msgRef.id);
        PushString  (buffer, m_msgRef.msg.c_str(), m_msgLength);
        PushString  (buffer, m_msgRef.timestamp.c_str(), m_timeLength);
    }
private:
    const message&  m_msgRef;
    const size_t    m_msgLength;
    const size_t    m_timeLength;

    template<typename INTEGER>
    void PushNum(void*& buffer, INTEGER num) const
    {
        INTEGER* ptr = static_cast<INTEGER*>(buffer);
        //copying content
        *ptr = num;
        //updating the buffer pointer to point the next position to copy
        buffer = ++ptr;
    }
    void PushString(void*& buffer, const char* cstr, size_t length) const
    {
        PushNum(buffer, length);
        //copying string content
        memcpy(buffer, cstr, length);
        //updating the buffer pointer to point the next position to copy
        char* ptr = static_cast<char*>(buffer);
        ptr += length;
        buffer = ptr;
    }
};

解串器:

class MessageDeserializer
{
public:
    MessageDeserializer(const char* messageBuffer)
    : m_msgBuffer(messageBuffer)
    {}

    void Deserialize(message& messageOut)
    {
        messageOut.id           = PopNum<int>(m_msgBuffer);
        messageOut.msg          = PopString(m_msgBuffer);
        messageOut.timestamp    = PopString(m_msgBuffer);
    }

private:

    const void* m_msgBuffer;

    template<typename INTEGER>
    INTEGER PopNum(const void*& buffer) const
    {
        const INTEGER* ptr = static_cast<const INTEGER*>(buffer);
        //copying content
        INTEGER retVal = *ptr;
        //updating the buffer pointer to point the next position to copy
        buffer = ++ptr;

        return retVal;
    }

    std::string PopString(const void*& buffer) const
    {
        size_t length = PopNum<size_t>(buffer);
        const char* ptr = static_cast<const char*>(buffer);
        //copying content
        std::string retVal(ptr, length);
        //updating the buffer pointer to point the next position to copy
        ptr += length;
        buffer = ptr;

        return retVal;
    }
};

那么您的使用代码可能类似于:

//...
MessageSerializer serializer(*myVector[i]);
char* buffer = new char[serializer.RequiredBufferSize()];
serializer.Serialize(buffer);

和:

//...
message myMsg;
MessageDeserializer(input).Deserialize(myMsg);