Ç - 序列化技术 [英] C - serialization techniques

查看:85
本文介绍了Ç - 序列化技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一些code连载一些数据通过网络发送。目前,我用这种原始的方法:

I'm writing some code to serialize some data to send it over the network. Currently, I use this primitive procedure:


  1. 创建一个无效* 缓存

  2. 将任何字节顺序操作,如 hton 家庭上的数据我想通过网络发送

  3. 使用的memcpy 来内存复制到缓冲区中

  4. 发送存储在网络上

  1. create a void* buffer
  2. apply any byte ordering operations such as the hton family on the data I want to send over the network
  3. use memcpy to copy the memory into the buffer
  4. send the memory over the network

问题是,随着各种数据结构(常含有无效*数据,所以你不知道你是否需要关心字节顺序)的code变得很臃肿系列化code这是非常的特定于每个数据结构,并且不能在所有重复使用。

The problem is that with various data structures (which often contain void* data so you don't know whether you need to care about byte ordering) the code becomes really bloated with serialization code that's very specific to each data structure and can't be reused at all.

什么是对C一些很好的序列化技术,使这更容易/少丑?

What are some good serialization techniques for C that make this easier / less ugly?

-

请注意:我绑定到特定的协议,所以我不能自由选择如何序列化我的数据

Note: I'm bound to a specific protocol so I cannot freely choose how to serialize my data.

推荐答案

有关每个数据结构,有一个serialize_X函数(其中X是结构名称),它采用指向的X和一个指向不透明缓冲结构和调用相应的序列化功能。你应该提供一些原语,如serialize_int其写入到缓冲区,并更新产出指数。
原语必须调用类似reserve_space(N),其中N是写入任何数据之前,需要的字节数。 reserve_space()将realloc的虚空*缓冲区,使之至少大,因为它的当前大小加N个字节。
为了使这成为可能,缓冲结构将需要包含一个指针的实际数据,所述索引写(输出索引),并且被分配给该数据的大小的下一个字节。
有了这个系统,所有的serialize_X功能应该是pretty简单,例如:

For each data structure, have a serialize_X function (where X is the struct name) which takes a pointer to an X and a pointer to an opaque buffer structure and calls the appropriate serializing functions. You should supply some primitives such as serialize_int which write to the buffer and update the output index. The primitives will have to call something like reserve_space(N) where N is the number of bytes that are required before writing any data. reserve_space() will realloc the void* buffer to make it at least as big as it's current size plus N bytes. To make this possible, the buffer structure will need to contain a pointer to the actual data, the index to write the next byte to (output index) and the size that is allocated for the data. With this system, all of your serialize_X functions should be pretty straightforward, for example:

struct X {
    int n, m;
    char *string;
}

void serialize_X(struct X *x, struct Buffer *output) {
    serialize_int(x->n, output);
    serialize_int(x->m, output);
    serialize_string(x->string, output);
}

和框架code将是这样的:

And the framework code will be something like:

#define INITIAL_SIZE 32

struct Buffer {
    void *data;
    int next;
    size_t size;
}

struct Buffer *new_buffer() {
    struct Buffer *b = malloc(sizeof(Buffer));

    b->data = malloc(INITIAL_SIZE);
    b->size = INITIAL_SIZE;
    b->next = 0;

    return b;
}

void reserve_space(Buffer *b, size_t bytes) {
    if((b->next + bytes) > b->size) {
        /* double size to enforce O(lg N) reallocs */
        b->data = realloc(b->data, b->size * 2);
        b->size *= 2;
    }
}

从这个,应该是pretty全部实施serialize_的(),你需要的功能简单。

From this, it should be pretty simple to implement all of the serialize_() functions you need.

编辑:
例如:

For example:

void serialize_int(int x, Buffer *b) {
    /* assume int == long; how can this be done better? */
    x = htonl(x);

    reserve_space(b, sizeof(int));

    memcpy(((char *)b->data) + b->next, &x, sizeof(int));
    b->next += sizeof(int);
}

编辑:
另外请注意,我的code有一些潜在的错误。在缓冲区数组的大小存储在一个为size_t但该指数是一个int(我不知道是否为size_t被认为是合理的类型的指数)。另外,没有规定的错误处理,并没有函数释放缓冲区大功告成后,所以你必须这样做你自己。我只是给了我将使用的基本架构的演示。

Also note that my code has some potential bugs. The size of the buffer array is stored in a size_t but the index is an int (I'm not sure if size_t is considered a reasonable type for an index). Also, there is no provision for error handling and no function to free the Buffer after you're done so you'll have to do this yourself. I was just giving a demonstration of the basic architecture that I would use.

这篇关于Ç - 序列化技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆