serialization of messages

This commit is contained in:
tkarrass 2013-11-02 17:10:31 +01:00
parent 6ae3c81b9a
commit f2b472d2c4

32
msg.c
View File

@ -15,15 +15,38 @@ struct Buffer {
};
struct message {
long timestamp;
int width; // varies
int height; // normally 80
char **image; // dimension is width x height
uint32_t timestamp;
uint32_t width; // varies
uint32_t height; // normally 80
//char **image; // dimension is width x height
char *image; // dimension is width x height
};
#endif
int getBufferSize(struct message *msg) {
int ret;
ret = 4 + 4 + 4 + msg->width * msg->height;
return ret;
}
void serialize (char *buf, struct message *msg) {
memcpy(&buf[0], &msg->timestamp, 4);
memcpy(&buf[4], &msg->width, 4);
memcpy(&buf[8], &msg->height, 4);
memcpy(&buf[12], msg->image, msg->width*msg->height);
}
void deserialize (struct message *msg, const char *buf) {
memcpy(&msg->timestamp, &buf[0], 4);
memcpy(&msg->width, &buf[4], 4);
memcpy(&msg->height, &buf[8], 4);
msg->image = (char *)malloc(msg->width*msg->height);
memcpy(msg->image, &buf[12], msg->width*msg->height);
}
struct Buffer *new_buffer() {
struct Buffer *b = malloc(sizeof(Buffer));
@ -84,3 +107,4 @@ int main() {
}