simworld/client/src/main.c

45 lines
881 B
C
Raw Normal View History

2024-10-04 05:05:43 +00:00
// main.c
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include "world.h"
#include "error.h"
#include "sock.h"
2024-10-04 05:05:43 +00:00
int main(void) {
enum error_t err = ERR_OK;
int sock;
err = sock_init(&sock);
if (err) goto handle_error;
char ibuf[1024] = { 0 };
char obuf[1024] = { 0 };
// TODO: I skipped out on most error handling here
read(sock, ibuf, 1024);
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);
close(sock);
return ERR_OK;
2024-10-04 05:05:43 +00:00
handle_error:
printf("ERROR: %s\n", ERROR_STRS[err]);
return err;
2024-10-04 05:05:43 +00:00
}