2013-11-02 14:44:17 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-11-02 15:08:19 +01:00
|
|
|
#include <string.h>
|
2013-11-02 14:44:17 +01:00
|
|
|
#include <sys/types.h>
|
2013-11-02 16:09:38 +01:00
|
|
|
#include <arpa/inet.h> // htonl
|
2013-11-02 14:44:17 +01:00
|
|
|
#include "msg.h"
|
|
|
|
|
|
|
|
#if INTERFACE
|
|
|
|
|
|
|
|
#define INITIAL_SIZE 32
|
|
|
|
|
|
|
|
struct Buffer {
|
|
|
|
int size;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct message {
|
2013-11-02 17:10:31 +01:00
|
|
|
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
|
2013-11-02 14:44:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-11-02 17:10:31 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-11-02 14:44:17 +01:00
|
|
|
struct Buffer *new_buffer() {
|
|
|
|
struct Buffer *b = malloc(sizeof(Buffer));
|
|
|
|
|
|
|
|
b->data = malloc(INITIAL_SIZE);
|
|
|
|
b->size = 0;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2013-11-02 15:08:19 +01:00
|
|
|
void append_space(Buffer * b, int n) {
|
|
|
|
b->size += n;
|
2013-11-02 14:44:17 +01:00
|
|
|
b->data = realloc(b->data, b->size);
|
|
|
|
}
|
2013-11-02 17:25:42 +01:00
|
|
|
/*
|
2013-11-02 14:44:17 +01:00
|
|
|
void serialize_int(int x, Buffer * b) {
|
2013-11-02 15:08:19 +01:00
|
|
|
// htonl :: uint32_t -> uint32_t -- converts the parameter from host byte order to network byte order
|
2013-11-02 16:09:38 +01:00
|
|
|
//x = htonl(x);
|
2013-11-02 14:44:17 +01:00
|
|
|
|
|
|
|
append_space(b, sizeof(int));
|
|
|
|
|
2013-11-02 15:08:19 +01:00
|
|
|
memcpy( ((char*)b->data) + b->size, &x, sizeof(int));
|
|
|
|
b->size += sizeof(int);
|
2013-11-02 14:44:17 +01:00
|
|
|
}
|
|
|
|
|
2013-11-02 15:08:19 +01:00
|
|
|
void serialize_string(char *str, Buffer *b) {
|
|
|
|
|
|
|
|
int newlen = strlen(str);
|
|
|
|
|
|
|
|
append_space(b, newlen);
|
|
|
|
|
2013-11-02 16:09:38 +01:00
|
|
|
int i = 0;
|
|
|
|
while (i < newlen) {
|
|
|
|
memcpy( ((char*)b->data) + b->size + i, str, sizeof(char));
|
|
|
|
str++;
|
|
|
|
i++;
|
2013-11-02 15:08:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
b->size += newlen;
|
|
|
|
|
|
|
|
}
|
2013-11-02 15:01:40 +01:00
|
|
|
|
2013-11-02 15:34:39 +01:00
|
|
|
void serialize_message(struct message *msg, Buffer *b) {
|
|
|
|
serialize_int(msg->timestamp, b); // does this also work for long?
|
|
|
|
serialize_int(msg->width, b);
|
2013-11-02 16:09:38 +01:00
|
|
|
serialize_int(msg->height, b);
|
|
|
|
for (int i=0; i < msg->width; i++)
|
|
|
|
serialize_string(msg->image[i], b);
|
2013-11-02 15:34:39 +01:00
|
|
|
}
|
2013-11-02 14:44:17 +01:00
|
|
|
|
2013-11-02 16:09:38 +01:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
Buffer *buf = new_buffer();
|
|
|
|
//serialize_string("lol", buf);
|
|
|
|
serialize_int(42, buf);
|
|
|
|
printf("buf: size:%d data:%p\n", buf->size, buf->data);
|
|
|
|
|
|
|
|
}
|
2013-11-02 17:25:42 +01:00
|
|
|
*/
|
2013-11-02 16:09:38 +01:00
|
|
|
|
2013-11-02 17:10:31 +01:00
|
|
|
|