client send request and server handles and response to it
This commit is contained in:
parent
c32af03d20
commit
a87b6c1152
@ -4,9 +4,11 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <world.h>
|
||||
#include <error.h>
|
||||
#include <request.h>
|
||||
#include <world.h>
|
||||
|
||||
#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;
|
||||
|
@ -11,6 +11,8 @@ enum error_t {
|
||||
ERR_SOCKET,
|
||||
ERR_JSON_SERIALISE,
|
||||
ERR_JSON_DESERIALISE,
|
||||
ERR_INVALID_REQUEST,
|
||||
ERR_REQUEST_FAILED,
|
||||
__ERR_COUNT,
|
||||
};
|
||||
|
||||
|
@ -12,6 +12,8 @@ char const *const ERROR_STRS[] = {
|
||||
"SOCKET",
|
||||
"JSON SERIALISATION",
|
||||
"JSON DESERIALISATION",
|
||||
"INVALID REQUEST",
|
||||
"REQUEST FAILED"
|
||||
};
|
||||
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -1,3 +1,48 @@
|
||||
// request.c
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#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 };
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,13 @@
|
||||
#ifndef REQUEST_H
|
||||
#define REQUEST_H
|
||||
|
||||
#include <error.h>
|
||||
#include <request.h>
|
||||
|
||||
enum error_t handle_request(void);
|
||||
#include "data.h"
|
||||
|
||||
struct response_t handle_request(
|
||||
struct request_t const *,
|
||||
struct data_t *
|
||||
);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user