diff --git a/client/src/main.c b/client/src/main.c index 5334883..72d0107 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -15,8 +15,8 @@ int main(void) { err = sock_init(&sock); if (err) goto handle_error; - char ibuf[1024] = { 0 }; - read(sock, ibuf, 1024); + char ibuf[8192] = { 0 }; + read(sock, ibuf, 8192); struct world_t world = { 0 }; err = world_deserialise(&world, ibuf); @@ -25,11 +25,11 @@ int main(void) { printf("CLIENT: (%zu %zu) ->", world.height++, world.width ++); printf(" (%zu %zu)\n", world.height, world.width); - char obuf[1024] = { 0 }; - err = world_serialise_buf(&world, obuf, 1024); + char obuf[8192] = { 0 }; + err = world_serialise_buf(&world, obuf, 8192); if (err != ERR_OK) goto handle_error_sock; - write(sock, obuf, 1024); + write(sock, obuf, 8192); close(sock); return ERR_OK; diff --git a/common/include/entity/entity.h b/common/include/entity/entity.h index d3042d2..5c78d3d 100644 --- a/common/include/entity/entity.h +++ b/common/include/entity/entity.h @@ -3,6 +3,10 @@ #include +#include + +#include "error.h" + struct entity_t { size_t id; size_t x; @@ -11,4 +15,7 @@ struct entity_t { void entity_init(struct entity_t *, size_t, size_t, size_t); +enum error_t entity_serialise(struct entity_t const *, struct json_t **); +enum error_t entity_deserialise(struct entity_t *, struct json_t *); + #endif diff --git a/common/src/entity/entity.c b/common/src/entity/entity.c index 53a4d4f..b3aff8f 100644 --- a/common/src/entity/entity.c +++ b/common/src/entity/entity.c @@ -4,6 +4,10 @@ #include "entity/entity.h" + +static char const *const ENTITY_JSON_FMT = "{sI, sI, sI}"; + + void entity_init(struct entity_t *self, size_t id, size_t x, size_t y) { assert(self != NULL); @@ -11,3 +15,37 @@ void entity_init(struct entity_t *self, size_t id, size_t x, size_t y) { self->x = x; self->y = y; } + + +enum error_t entity_serialise( + struct entity_t const *self, + struct json_t **json +) { + assert(self != NULL); + assert(json != NULL); + + struct json_t *maybe_json = json_pack(ENTITY_JSON_FMT, + "id", self->id, + "x", self->x, + "y", self->y + ); + if (maybe_json == NULL) return ERR_JSON_SERIALISE; + + *json = maybe_json; + return ERR_OK; +} + + +enum error_t entity_deserialise(struct entity_t *self, struct json_t *json) { + assert(self != NULL); + assert(json != NULL); + + int json_err = json_unpack(json, ENTITY_JSON_FMT, + "id", &self->id, + "x", &self->x, + "y", &self->y + ); + if (json_err < 0) return ERR_JSON_DESERIALISE; + + return ERR_OK; +} diff --git a/common/src/entity/registry.c b/common/src/entity/registry.c index fc31e11..fc0fe9b 100644 --- a/common/src/entity/registry.c +++ b/common/src/entity/registry.c @@ -116,7 +116,10 @@ enum error_t entity_registry_serialise( if (err != ERR_OK) goto error_cleanup_array; int json_err = json_array_append_new(entities_json, maybe_entity_json); - if (json_err < 0) goto error_cleanup_array; + if (json_err < 0) { + err = ERR_JSON_SERIALISE; + goto error_cleanup_array; + } } assert(json_array_size(entities_json) == self->size); diff --git a/common/src/world.c b/common/src/world.c index ee702d6..dd35f07 100644 --- a/common/src/world.c +++ b/common/src/world.c @@ -5,7 +5,7 @@ #include "world.h" -static char const *const JSON_FMT = "{so, sI, sI}"; +static char const *const JSON_FMT = "{so, so, sI, sI}"; enum error_t world_init(struct world_t *self, size_t height, size_t width) { assert(self != NULL); @@ -15,8 +15,8 @@ enum error_t world_init(struct world_t *self, size_t height, size_t width) { for (size_t i = 0; i < MAX_ENTITIES; i++) { self->entities[i].id = 0; - self->entities[i].x = self->width; - self->entities[i].y = self->height; + self->entities[i].x = 0; + self->entities[i].y = 0; } enum error_t err = entity_registry_init(&self->registered_entities); @@ -47,16 +47,32 @@ static enum error_t serialise(struct world_t *self, struct json_t **json) { assert(json != NULL); // TODO: (de)serialise the following: // struct entity_t entities[MAX_ENTITIES]; - // struct entity_registry_t registered_entities; + + enum error_t err = ERR_OK; + + struct json_t *entities_json = json_array(); + for (size_t i = 0; i < MAX_ENTITIES; i++) { + struct json_t *maybe_entity_json; + err = entity_serialise(&self->entities[i], &maybe_entity_json); + if (err != ERR_OK) goto error_cleanup_array; + + int json_err = json_array_append_new(entities_json, maybe_entity_json); + if (json_err < 0) { + err = ERR_JSON_SERIALISE; + goto error_cleanup_array; + } + } + assert(json_array_size(entities_json) == MAX_ENTITIES); struct json_t *registered_entities_json; - enum error_t err = entity_registry_serialise( + err = entity_registry_serialise( &self->registered_entities, ®istered_entities_json ); if (err != ERR_OK) return err; struct json_t *maybe_json = json_pack(JSON_FMT, + "entities", entities_json, "registered_entities", registered_entities_json, "height", self->height, "width", self->width @@ -65,6 +81,10 @@ static enum error_t serialise(struct world_t *self, struct json_t **json) { *json = maybe_json; return ERR_OK; + +error_cleanup_array: + json_array_clear(entities_json); + return err; } @@ -72,14 +92,24 @@ static enum error_t deserialise(struct world_t *self, struct json_t *json) { assert(self != NULL); assert(json != NULL); + struct json_t *entities_json; struct json_t *registered_entities_json; int json_err = json_unpack(json, JSON_FMT, + "entities", &entities_json, "registered_entities", ®istered_entities_json, "height", &self->height, "width", &self->width ); if (json_err < 0) return ERR_JSON_DESERIALISE; + for (size_t i = 0; i < MAX_ENTITIES; i++) { + struct json_t *entity_json = json_array_get(entities_json, i); + if (entity_json == NULL) return ERR_JSON_DESERIALISE; + + enum error_t err = entity_deserialise(&self->entities[i], entity_json); + if (err != ERR_OK) return err; + } + enum error_t err = entity_registry_deserialise( &self->registered_entities, registered_entities_json diff --git a/server/src/main.c b/server/src/main.c index 66138e5..1d7f75f 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -25,14 +25,14 @@ static void handle_signal(int signal_no) { enum error_t game_loop(struct data_t *data) { enum error_t err = ERR_OK; - char obuf[1024] = { 0 }; - err = world_serialise_buf(&data->world, obuf, 1024); + char obuf[8192] = { 0 }; + err = world_serialise_buf(&data->world, obuf, 8192); if (err != ERR_OK) return err; - write(data->socket_accept, obuf, 1024); + write(data->socket_accept, obuf, 8192); - char ibuf[1024] = { 0 }; - read(data->socket_accept, ibuf, 1024); + char ibuf[8192] = { 0 }; + read(data->socket_accept, ibuf, 8192); err = world_deserialise(&data->world, ibuf); if (err != ERR_OK) return err;