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

时间:2016-04-09 10:55:09

标签: c++ serialization vector winsock

我有一个矢量定义为:

std::vector<message *>

消息是:

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

我的目标是使用Winsock(从服务器到客户端)发送此信息,但这只允许发送在WinSock2.h中显示的字符。考虑到这一点,我想序列化一个字符数组中的所有信息(id,msg和timestamp),以便将它们一起发送,并在客户端中,有一个反序列化的函数,以便具有相同的向量I曾在服务器上。

我该如何实施?

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您可以使用Boost serialization library将结构保存/加载到char数组中。 boost库在C ++中被广泛使用,如果你不熟悉它,我建议你去看看它。

您可以学习使用Boost套接字,并使您的C ++代码几乎可以在任何平台上运行,而不仅仅是Windows,而不是使用winsock,而不仅仅是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。库提供了其他格式。另一个优点是它支持版本控制。

答案 1 :(得分:1)

以下为序列化问题提供了一种简单的方法。

但请注意不便携。它假设双方(客户端/服务器)的环境条件相同,即endianness和sizeof int和size_t。在编写服务器/客户端程序时,这种假设可能并不令人满意,您的代码也应该处理这个方面。

例如,如果您可以说32位是id值和字符串长度的足够大小,则可以在序列化时使用htonl,而在ntohl时使用here反序列化。

<强>串行器:

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);