split each request/response type into its own file

This commit is contained in:
snit 2024-10-29 13:14:08 -05:00
parent c892acab80
commit b03e194a34
7 changed files with 127 additions and 88 deletions

View File

@ -1,11 +1,14 @@
// render/render.c
#include <assert.h>
#include <stdio.h>
#include "render.h"
void render_world(struct world_t const *world) {
assert(world != NULL);
printf("%zu %zu\n", world->height, world->width);
for (size_t i = 0; i < MAX_ENTITIES; i++) {

View File

@ -2,11 +2,10 @@
#define COMMON_REQUEST_H
#include <stdbool.h>
#include <stdlib.h>
#include "error.h"
#include "world.h"
// ### REQUEST TYPES ### //
#include "request/get_world_data.h"
enum request_type_t {
REQUEST_NONE,
@ -14,11 +13,6 @@ enum request_type_t {
};
struct request_body_get_world_data_t {
size_t world_id;
};
struct request_t {
enum request_type_t type;
void *body;
@ -32,13 +26,6 @@ enum error_t request_deserialise_str(struct request_t *, char const *);
enum error_t request_serialise_buf(struct request_t const *, char *, size_t);
// ### RESPONSE TYPES ### //
struct response_body_get_world_data_t {
struct world_t world;
};
struct response_t {
enum request_type_t type;
bool success;

View File

@ -0,0 +1,41 @@
#ifndef COMMON_REQUEST_GET_WORLD_DATA_H
#define COMMON_REQUEST_GET_WORLD_DATA_H
#include <stdlib.h>
#include "../world.h"
struct request_body_get_world_data_t {
size_t world_id;
};
enum error_t request_body_get_world_data_serialise(
struct request_body_get_world_data_t const *,
struct json_t **
);
enum error_t request_body_get_world_data_deserialise(
struct request_body_get_world_data_t *,
struct json_t *
);
struct response_body_get_world_data_t {
struct world_t world;
};
enum error_t response_body_get_world_data_serialise(
struct response_body_get_world_data_t const *,
struct json_t **
);
enum error_t response_body_get_world_data_deserialise(
struct response_body_get_world_data_t *,
struct json_t *
);
#endif

View File

@ -4,34 +4,12 @@
#include <jansson.h>
#include "request.h"
#include "request/get_world_data.h"
// Request Format Strings
static char const *REQUEST_JSON_FMT = "{si, so}";
static char const *REQUEST_BODY_GET_WORLD_DATA_JSON_FMT = "{sI}";
// Response Format Strings
static char const *RESPONSE_JSON_FMT = "{sb, si, so}";
static enum error_t request_serialise_body_get_world_data(
struct request_body_get_world_data_t const *self,
struct json_t **jsonptr
) {
assert(self != NULL);
assert(jsonptr != NULL);
struct json_t *json = json_pack(REQUEST_BODY_GET_WORLD_DATA_JSON_FMT,
"world-id", self->world_id
);
if (json == NULL) return ERR_JSON_SERIALISE;
*jsonptr = json;
return ERR_OK;
}
static enum error_t request_serialise_body(
struct request_t const *self,
struct json_t **jsonptr
@ -41,7 +19,7 @@ static enum error_t request_serialise_body(
switch (self->type) {
case REQUEST_GET_WORLD_DATA:
return request_serialise_body_get_world_data(self->body, jsonptr);
return request_body_get_world_data_serialise(self->body, jsonptr);
default: return ERR_JSON_SERIALISE;
}
@ -160,22 +138,6 @@ static enum error_t request_deserialise_parts(
}
static enum error_t request_deserialise_body_get_world_data(
struct request_body_get_world_data_t *self,
struct json_t *json
) {
assert(self != NULL);
assert(json != NULL);
int err = json_unpack(json, REQUEST_BODY_GET_WORLD_DATA_JSON_FMT,
"world-id", &self->world_id
);
if (err < 0) return ERR_JSON_DESERIALISE;
return ERR_OK;
}
static enum error_t request_deserialise_body(
struct request_t *self,
struct json_t *json
@ -188,7 +150,7 @@ static enum error_t request_deserialise_body(
self->body = malloc(sizeof(struct request_body_get_world_data_t));
if (self->body == NULL) return ERR_ALLOC;
return request_deserialise_body_get_world_data(self->body, json);
return request_body_get_world_data_deserialise(self->body, json);
default: return ERR_JSON_SERIALISE;
}
@ -239,22 +201,6 @@ enum error_t request_deserialise_str(struct request_t *self, char const *str) {
}
static enum error_t response_serialise_body_get_world_data(
struct response_body_get_world_data_t const *self,
struct json_t **jsonptr
) {
assert(self != NULL);
assert(jsonptr != NULL);
struct json_t *json = NULL;
enum error_t err = world_serialise(&self->world, &json);
if (err != ERR_OK) return err;
*jsonptr = json;
return ERR_OK;
}
static enum error_t response_serialise_body(
struct response_t const *self,
struct json_t **jsonptr
@ -264,7 +210,7 @@ static enum error_t response_serialise_body(
switch (self->type) {
case REQUEST_GET_WORLD_DATA:
return response_serialise_body_get_world_data(self->body, jsonptr);
return response_body_get_world_data_serialise(self->body, jsonptr);
default: return ERR_JSON_SERIALISE;
}
@ -391,20 +337,6 @@ static enum error_t response_deserialise_parts(
}
static enum error_t response_deserialise_body_get_world_data(
struct response_body_get_world_data_t *self,
struct json_t *json
) {
assert(self != NULL);
assert(json != NULL);
enum error_t err = world_deserialise(&self->world, json);
if (err != ERR_OK) return err;
return ERR_OK;
}
static enum error_t response_deserialise_body(
struct response_t *self,
struct json_t *json
@ -417,7 +349,7 @@ static enum error_t response_deserialise_body(
self->body = malloc(sizeof(struct response_body_get_world_data_t));
if (self->body == NULL) return ERR_ALLOC;
return response_deserialise_body_get_world_data(self->body, json);
return response_body_get_world_data_deserialise(self->body, json);
default: return ERR_JSON_SERIALISE;
}

View File

@ -0,0 +1,71 @@
// request/get_world_data.c
#include <assert.h>
#include "request/get_world_data.h"
static char const *REQUEST_BODY_GET_WORLD_DATA_JSON_FMT = "{sI}";
enum error_t request_body_get_world_data_serialise(
struct request_body_get_world_data_t const *self,
struct json_t **jsonptr
) {
assert(self != NULL);
assert(jsonptr != NULL);
struct json_t *json = json_pack(REQUEST_BODY_GET_WORLD_DATA_JSON_FMT,
"world-id", self->world_id
);
if (json == NULL) return ERR_JSON_SERIALISE;
*jsonptr = json;
return ERR_OK;
}
enum error_t request_body_get_world_data_deserialise(
struct request_body_get_world_data_t *self,
struct json_t *json
) {
assert(self != NULL);
assert(json != NULL);
int err = json_unpack(json, REQUEST_BODY_GET_WORLD_DATA_JSON_FMT,
"world-id", &self->world_id
);
if (err < 0) return ERR_JSON_DESERIALISE;
return ERR_OK;
}
enum error_t response_body_get_world_data_serialise(
struct response_body_get_world_data_t const *self,
struct json_t **jsonptr
) {
assert(self != NULL);
assert(jsonptr != NULL);
struct json_t *json = NULL;
enum error_t err = world_serialise(&self->world, &json);
if (err != ERR_OK) return err;
*jsonptr = json;
return ERR_OK;
}
enum error_t response_body_get_world_data_deserialise(
struct response_body_get_world_data_t *self,
struct json_t *json
) {
assert(self != NULL);
assert(json != NULL);
enum error_t err = world_deserialise(&self->world, json);
if (err != ERR_OK) return err;
return ERR_OK;
}

View File

@ -20,6 +20,7 @@ A list of things I'd like to accomplish
* Write Makefile to automate compilation
## Planned
* Requests/responses for more than just the complete set of world data all at once
* Create and load worlds
* Display environment and pan camera
* Time controls (play/pause/speed up)

View File

@ -43,6 +43,10 @@ void socket_free(int const *sockptr) {
}
// TODO: Out of curiosity, what's the consensus on struct declarations outside
// of header files? Its a private struct that the end-user should never need to
// use, so I don't want to include it in socket.h, but it feels off putting it
// in socket.c as well just because I'm used to them all being in .h
struct socket_data_t {
struct game_data_t *game_data;
int socket;