2024-10-04 05:05:43 +00:00
|
|
|
// main.c
|
|
|
|
|
2024-10-05 08:58:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2024-10-10 05:58:13 +00:00
|
|
|
#include "world.h"
|
2024-10-05 08:58:16 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "sock.h"
|
2024-10-04 05:05:43 +00:00
|
|
|
|
|
|
|
int main(void) {
|
2024-10-05 08:58:16 +00:00
|
|
|
enum error_t err = ERR_OK;
|
|
|
|
|
|
|
|
int sock;
|
|
|
|
err = sock_init(&sock);
|
|
|
|
if (err) goto handle_error;
|
|
|
|
|
2024-10-10 05:58:13 +00:00
|
|
|
char ibuf[1024] = { 0 };
|
|
|
|
char obuf[1024] = { 0 };
|
2024-10-05 08:58:16 +00:00
|
|
|
|
2024-10-10 05:58:13 +00:00
|
|
|
// TODO: I skipped out on most error handling here
|
|
|
|
read(sock, ibuf, 1024);
|
2024-10-05 08:58:16 +00:00
|
|
|
|
2024-10-10 05:58:13 +00:00
|
|
|
struct json_error_t json_err;
|
|
|
|
struct json_t *json = json_loads(ibuf, 0, &json_err);
|
|
|
|
|
|
|
|
struct world_t world = { 0 };
|
|
|
|
world_deserialise(&world, json);
|
|
|
|
|
|
|
|
printf("CLIENT: (%zu %zu)\n", world.height, world.width);
|
|
|
|
|
|
|
|
struct json_t *new_json;
|
|
|
|
world_serialise(&world, &new_json);
|
|
|
|
|
|
|
|
json_dumpb(new_json, obuf, 1024, 0);
|
|
|
|
write(sock, obuf, 1024);
|
2024-10-05 08:58:16 +00:00
|
|
|
|
|
|
|
close(sock);
|
|
|
|
return ERR_OK;
|
2024-10-04 05:05:43 +00:00
|
|
|
|
2024-10-05 08:58:16 +00:00
|
|
|
handle_error:
|
|
|
|
printf("ERROR: %s\n", ERROR_STRS[err]);
|
|
|
|
return err;
|
2024-10-04 05:05:43 +00:00
|
|
|
}
|