simworld/client/src/main.c

44 lines
882 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[8192] = { 0 };
read(sock, ibuf, 8192);
struct world_t world = { 0 };
err = world_deserialise_str(&world, ibuf);
if (err != ERR_OK) goto handle_error_sock;
printf("CLIENT: (%zu %zu) ->", world.height++, world.width ++);
printf(" (%zu %zu)\n", world.height, world.width);
char obuf[8192] = { 0 };
err = world_serialise_buf(&world, obuf, 8192);
if (err != ERR_OK) goto handle_error_sock;
write(sock, obuf, 8192);
close(sock);
return ERR_OK;
2024-10-04 05:05:43 +00:00
handle_error_sock:
close(sock);
handle_error:
printf("CLIENT ERROR: %s\n", ERROR_STRS[err]);
return err;
2024-10-04 05:05:43 +00:00
}