manual header for msg
This commit is contained in:
parent
01fe9c6bf7
commit
0b46e9fb96
37
src/msg.c
37
src/msg.c
|
@ -5,52 +5,29 @@
|
||||||
#include <arpa/inet.h> // htonl
|
#include <arpa/inet.h> // htonl
|
||||||
#include "msg.h"
|
#include "msg.h"
|
||||||
|
|
||||||
#if INTERFACE
|
|
||||||
|
|
||||||
#define INITIAL_SIZE 32
|
|
||||||
|
|
||||||
struct Buffer {
|
|
||||||
int size;
|
|
||||||
void *data;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct message {
|
|
||||||
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 getBufferSize(struct message *msg) {
|
||||||
int ret;
|
return 3*sizeof(uint32_t) + msg->width * msg->height;
|
||||||
ret = 4 + 4 + 4 + msg->width * msg->height;
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void serialize (char *buf, struct message *msg) {
|
void serialize (char *buf, struct message *msg) {
|
||||||
memcpy(&buf[0], &msg->timestamp, 4);
|
memcpy(&buf[0], &msg->timestamp, 4);
|
||||||
memcpy(&buf[4], &msg->width, 4);
|
memcpy(&buf[4], &msg->width, 4);
|
||||||
memcpy(&buf[8], &msg->height, 4);
|
memcpy(&buf[8], &msg->height, 4);
|
||||||
memcpy(&buf[12], msg->image, msg->width*msg->height);
|
memcpy(&buf[12], msg->image, msg->width * msg->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deserialize (struct message *msg, const char *buf) {
|
void deserialize (struct message *msg, const char *buf) {
|
||||||
memcpy(&msg->timestamp, &buf[0], 4);
|
memcpy(&msg->timestamp, &buf[0], 4);
|
||||||
memcpy(&msg->width, &buf[4], 4);
|
memcpy(&msg->width, &buf[4], 4);
|
||||||
memcpy(&msg->height, &buf[8], 4);
|
memcpy(&msg->height, &buf[8], 4);
|
||||||
msg->image = (char *)malloc(msg->width*msg->height);
|
msg->image = (char*) malloc(msg->width * msg->height);
|
||||||
memcpy(msg->image, &buf[12], msg->width*msg->height);
|
memcpy(msg->image, &buf[12], msg->width * msg->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Buffer *new_buffer() {
|
struct Buffer *new_buffer() {
|
||||||
struct Buffer *b = malloc(sizeof(Buffer));
|
struct Buffer *b = malloc(sizeof(Buffer));
|
||||||
|
|
||||||
b->data = malloc(INITIAL_SIZE);
|
b->data = malloc(0);
|
||||||
b->size = 0;
|
b->size = 0;
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
|
@ -60,7 +37,7 @@ void append_space(Buffer * b, int n) {
|
||||||
b->size += n;
|
b->size += n;
|
||||||
b->data = realloc(b->data, b->size);
|
b->data = realloc(b->data, b->size);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
void serialize_int(int x, Buffer * b) {
|
void serialize_int(int x, Buffer * b) {
|
||||||
// htonl :: uint32_t -> uint32_t -- converts the parameter from host byte order to network byte order
|
// htonl :: uint32_t -> uint32_t -- converts the parameter from host byte order to network byte order
|
||||||
//x = htonl(x);
|
//x = htonl(x);
|
||||||
|
@ -96,7 +73,7 @@ void serialize_message(struct message *msg, Buffer *b) {
|
||||||
serialize_string(msg->image[i], b);
|
serialize_string(msg->image[i], b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
Buffer *buf = new_buffer();
|
Buffer *buf = new_buffer();
|
||||||
|
|
37
src/msg.h
37
src/msg.h
|
@ -1,21 +1,24 @@
|
||||||
/* This file was automatically generated. Do not edit! */
|
|
||||||
typedef struct Buffer Buffer;
|
|
||||||
void append_space(Buffer *b,int n);
|
|
||||||
struct Buffer *new_buffer();
|
|
||||||
typedef struct message message;
|
|
||||||
void deserialize(struct message *msg,const char *buf);
|
|
||||||
void serialize(char *buf,struct message *msg);
|
|
||||||
int getBufferSize(struct message *msg);
|
|
||||||
struct message {
|
|
||||||
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
|
|
||||||
};
|
|
||||||
struct Buffer {
|
struct Buffer {
|
||||||
int size;
|
int size;
|
||||||
void *data;
|
void *data;
|
||||||
};
|
};
|
||||||
#define INITIAL_SIZE 32
|
|
||||||
#define INTERFACE 0
|
struct message {
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
int getBufferSize(struct message *msg);
|
||||||
|
void serialize(char *buf,struct message *msg);
|
||||||
|
void deserialize(struct message *msg,const char *buf);
|
||||||
|
|
||||||
|
struct Buffer *new_buffer();
|
||||||
|
void append_space(Buffer *b,int n);
|
||||||
|
void serialize_int(int x,Buffer *b);
|
||||||
|
void serialize_string(char *str,Buffer *b);
|
||||||
|
void serialize_message(struct message *msg,Buffer *b);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user