simworld/client/src/main.c

59 lines
1.3 KiB
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 <error.h>
#include <request.h>
#include <world.h>
#include "render/render.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 error;
char input_buffer[8192] = { 0 };
char output_buffer[8192] = { 0 };
// 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;
write(sock, output_buffer, 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;
// 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;
2024-10-04 05:05:43 +00:00
error_socket:
close(sock);
error:
printf("CLIENT ERROR: %s\n", ERROR_STRS[err]);
return err;
2024-10-04 05:05:43 +00:00
}