added structs for serializing
This commit is contained in:
parent
b4d175c882
commit
2d1cd95f90
52
msg.c
Normal file
52
msg.c
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include "msg.h"
|
||||||
|
|
||||||
|
#if INTERFACE
|
||||||
|
|
||||||
|
#define INITIAL_SIZE 32
|
||||||
|
|
||||||
|
struct Buffer {
|
||||||
|
int size;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct message {
|
||||||
|
long timestamp;
|
||||||
|
int width; // varies
|
||||||
|
int height; // normally 80
|
||||||
|
char **image; // dimension is width x height
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct Buffer *new_buffer() {
|
||||||
|
struct Buffer *b = malloc(sizeof(Buffer));
|
||||||
|
|
||||||
|
b->data = malloc(INITIAL_SIZE);
|
||||||
|
b->size = 0;
|
||||||
|
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_space(Buffer * b, int bytes) {
|
||||||
|
b->size += bytes;
|
||||||
|
b->data = realloc(b->data, b->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialize_int(int x, Buffer * b) {
|
||||||
|
// htonl :: uint32_t -> uint32_t
|
||||||
|
x = htonl(x);
|
||||||
|
|
||||||
|
append_space(b, sizeof(int));
|
||||||
|
|
||||||
|
memcpy(((char *)b->data) + b->next, &x, sizeof(int));
|
||||||
|
b->next += sizeof(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
//serialize_string
|
||||||
|
//serialize_message
|
||||||
|
|
||||||
|
|
18
msg.h
Normal file
18
msg.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/* This file was automatically generated. Do not edit! */
|
||||||
|
typedef struct Buffer Buffer;
|
||||||
|
void serialize_int(int x,Buffer *b);
|
||||||
|
void append_space(Buffer *b,int bytes);
|
||||||
|
struct Buffer *new_buffer();
|
||||||
|
typedef struct message message;
|
||||||
|
struct message {
|
||||||
|
long timestamp;
|
||||||
|
int width; // varies
|
||||||
|
int height; // normally 80
|
||||||
|
char **image; // dimension is width x height
|
||||||
|
};
|
||||||
|
struct Buffer {
|
||||||
|
int size;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
#define INITIAL_SIZE 32
|
||||||
|
#define INTERFACE 0
|
Loading…
Reference in New Issue
Block a user