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