2

很长一段时间以来,我一直试图找到一种通过 tcp 套接字序列化或发送状态对象的好方法。我的问题是我无法使用任何 3. 方库,如 boost。

我的状态对象包含多个对象。最重要的是它有一些对象和一个对象向量,但没有指针(例如,可能没有深度复制,如果向量不需要这个)。

对于我的问题:由于我不能使用 boost 或任何其他库,那么通过套接字发送带有对象的对象的最佳方法是什么?我一直在想我可能可以创建一个复制构造函数并将其发送到流中,但我不太确定这样做的后果。

4

1 回答 1

2

为您的数据类型定义(反)序列化函数。

例如,如果你有类似的东西:

class MyClass
{
public: 
    int field_a;
    int field_b;
    std::string string;
    ...
};
typedef std::vector<MyClass> MyVector;

您可以定义以下内容:

void write(int fd, const MyClass &arg)
{
    // either convert arg to byte array and write it, or write field by field
    // here we write it field by field
    write_int(fd, arg.field_a);
    write_int(fd, arg.field_b);
    write_string(fd, arg.string);
}

void write(int fd const MyVector &arg)
{
    size_t size = arg.size();
    ::write(fd, &size, sizeof(size)); // beware: machine-dependent code
    for (MyVector::const_iterator it = arg.begin(); it != arg.end(); it++)
    {
        write(*it);
    }
}

辅助功能:

void write_int(int fd, int arg)
{
    write(fd, &arg, sizeof(int));
}

void write_string(int fd, const std::string &str)
{
    size_t len = str.length();

    write(fd, &len, sizeof(len)); // string length go first
    write(fd, str.data(), len); // write string data
}

并阅读:

MyClass readMC(int fd)
{
    // read MyClass data from stream, parse it
    int f1, f2;
    std::string str;

    read_int(fd, f1);
    read_int(fd, f2);

    read_string(fd, str)

    return MyClass(f1, f2, str);
}

void read(int fd, MyVector &arg)
{
    size_t size;
    size_t i;

    read(fd, &size, sizeof(size)); // read number of elements;
    arg.reserve(size);
    for (i = 0; i < size; i++)
    {
        arg.push_back(readMC(fd));
    }
}

辅助功能:

void read_int(int fd, int &res);
{
    read(fd, &res, sizeof(res));
}

void read_string(int fd, std::string &string)
{
    size_t len;
    char *buf;

    read(fd, &len, sizeof(len));
    buf = new char[len];
    read(fd, buf, len);
    string.asssign(buf, len);
    delete []buf;
}
于 2013-04-21T10:11:32.357 回答