diff --git a/client/src/main.c b/client/src/main.c index 9bbb7bd..a361f70 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -4,9 +4,11 @@ #include #include -#include #include +#include +#include +#include "render/render.h" #include "sock.h" int main(void) { @@ -16,21 +18,33 @@ int main(void) { err = sock_init(&sock); if (err) goto error; - char ibuf[8192] = { 0 }; - read(sock, ibuf, 8192); + char input_buffer[8192] = { 0 }; + char output_buffer[8192] = { 0 }; - struct world_t world = { 0 }; - err = world_deserialise_str(&world, ibuf); + // Send request for world data to server + struct request_body_get_world_data_t request_body = { 420 }; + struct request_t request = { REQUEST_GET_WORLD_DATA, &request_body }; + + err = request_serialise_buf(&request, output_buffer, 8192); if (err != ERR_OK) goto error_socket; - printf("CLIENT: (%zu %zu) ->", world.height++, world.width ++); - printf(" (%zu %zu)\n", world.height, world.width); + write(sock, output_buffer, 8192); - char obuf[8192] = { 0 }; - err = world_serialise_buf(&world, obuf, 8192); + // Get response from server + read(sock, input_buffer, 8192); + + struct response_t response = { 0 }; + err = response_deserialise_str(&response, input_buffer); if (err != ERR_OK) goto error_socket; - write(sock, obuf, 8192); + // Render data + if (response.success != true) { + err = ERR_REQUEST_FAILED; + goto error_socket; + } + + struct response_body_get_world_data_t *response_body = response.body; + render_world(&response_body->world); close(sock); return ERR_OK; diff --git a/common/include/error.h b/common/include/error.h index 534a4eb..2a1a66f 100644 --- a/common/include/error.h +++ b/common/include/error.h @@ -11,6 +11,8 @@ enum error_t { ERR_SOCKET, ERR_JSON_SERIALISE, ERR_JSON_DESERIALISE, + ERR_INVALID_REQUEST, + ERR_REQUEST_FAILED, __ERR_COUNT, }; diff --git a/common/src/error.c b/common/src/error.c index e033003..3e3b028 100644 --- a/common/src/error.c +++ b/common/src/error.c @@ -12,6 +12,8 @@ char const *const ERROR_STRS[] = { "SOCKET", "JSON SERIALISATION", "JSON DESERIALISATION", + "INVALID REQUEST", + "REQUEST FAILED" }; diff --git a/common/src/request.c b/common/src/request.c index 5e6af9e..e5d1708 100644 --- a/common/src/request.c +++ b/common/src/request.c @@ -13,8 +13,6 @@ static char const *REQUEST_BODY_GET_WORLD_DATA_JSON_FMT = "{sI}"; // Response Format Strings static char const *RESPONSE_JSON_FMT = "{sb, si, so}"; -static char const *RESPONSE_BODY_GET_WORLD_DATA_JSON_FMT = "{so}"; - static enum error_t request_serialise_body_get_world_data( struct request_body_get_world_data_t const *self, diff --git a/doc/TODO.gmi b/doc/TODO.gmi index 7817741..4d7c48b 100644 --- a/doc/TODO.gmi +++ b/doc/TODO.gmi @@ -2,12 +2,13 @@ A list of things I'd like to accomplish ## In Progress +* Split game ticking and socket connecting logic (can only tick on connection atm) + +## Completed * Client send request * Server handle request and send response * Client handle response * Have client render world data (return to pre-daemonised equivalent state) - -## Completed * Serialise data to JSON for socket data transmission * Send world data to client * Initialise a world again @@ -19,7 +20,6 @@ A list of things I'd like to accomplish * Write Makefile to automate compilation ## Planned -* Split game ticking and socket connecting logic (can only tick on connection atm) * Create and load worlds * Display environment and pan camera * Time controls (play/pause/speed up) diff --git a/server/src/main.c b/server/src/main.c index 3f0d3f9..7360d44 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -14,6 +14,7 @@ #include "opts.h" #include "sock.h" #include "data.h" +#include "request.h" static void handle_signal(int signal_no) { (void)signal_no; @@ -23,50 +24,25 @@ static void handle_signal(int signal_no) { } -enum error_t game_loop(struct data_t *data) { +static enum error_t game_loop(struct data_t *data) { enum error_t err = ERR_OK; - // TEMP - struct response_body_get_world_data_t response_body = { data->world }; - struct response_t response = { true, REQUEST_GET_WORLD_DATA, &response_body }; + char output_buffer[8192] = { 0 }; + char input_buffer[8192] = { 0 }; - char const *response_json_str = NULL; - err = response_serialise_str(&response, &response_json_str); - if (err) return err; + // Receive request from client + read(data->socket_accept, input_buffer, 8192); - printf("serialised response: %s\n", response_json_str); - - struct response_t response2 = { 0 }; - err = response_deserialise_str(&response2, response_json_str); - if (err) return err; - - struct response_body_get_world_data_t *response2_body = response2.body; - - char const *world_json_str = NULL; - err = world_serialise_str(&response2_body->world, &world_json_str); - if (err) return err; - printf("deserialised response: (%d, %d, %s\n", - response2.success, - response2.type, - world_json_str - ); - free((void *)response_json_str); - - // TEMP - - char obuf[8192] = { 0 }; - err = world_serialise_buf(&data->world, obuf, 8192); + struct request_t request = { 0 }; + err = request_deserialise_str(&request, input_buffer); if (err != ERR_OK) return err; - write(data->socket_accept, obuf, 8192); - - char ibuf[8192] = { 0 }; - read(data->socket_accept, ibuf, 8192); - - err = world_deserialise_str(&data->world, ibuf); + // Send response to client + struct response_t response = handle_request(&request, data); + err = response_serialise_buf(&response, output_buffer, 8192); if (err != ERR_OK) return err; - printf("SERVER: \"%s\" -> \"%s\"\n", obuf, ibuf); + write(data->socket_accept, output_buffer, 8192); return ERR_OK; } diff --git a/server/src/request.c b/server/src/request.c index f30b9cd..9b8dda2 100644 --- a/server/src/request.c +++ b/server/src/request.c @@ -1,3 +1,48 @@ // request.c +#include + #include "request.h" + +static void handle_request_get_world_data( + struct response_t *response, + struct request_t const *request, + struct data_t const *ctx +) { + assert(response != NULL); + assert(request != NULL); + assert(ctx != NULL); + + // TODO: World creation isn't set up yet, so the world-id parameter isn't + // useful right now, but make sure to use it later when it is set up + + struct response_body_get_world_data_t *body = response->body; + body->world = ctx->world; + + response->type = request->type; + response->success = true; +} + + +struct response_t handle_request( + struct request_t const *request, + struct data_t *ctx +) { + assert(request != NULL); + assert(ctx != NULL); + + struct response_t response = { 0 }; + + switch (request->type) { + case REQUEST_GET_WORLD_DATA: + response.body = malloc(sizeof(struct response_body_get_world_data_t)); + if (response.body == NULL) + return (struct response_t){ request->type, false, NULL }; + + handle_request_get_world_data(&response, request, ctx); + return response; + + default: + return (struct response_t){ 0 }; + } +} diff --git a/server/src/request.h b/server/src/request.h index 537d983..e55c1c6 100644 --- a/server/src/request.h +++ b/server/src/request.h @@ -1,8 +1,13 @@ #ifndef REQUEST_H #define REQUEST_H -#include +#include -enum error_t handle_request(void); +#include "data.h" + +struct response_t handle_request( + struct request_t const *, + struct data_t * +); #endif