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-17 20:14:16 +00:00
|
|
|
#include <error.h>
|
2024-10-23 19:35:07 +00:00
|
|
|
#include <request.h>
|
|
|
|
#include <world.h>
|
2024-10-17 20:14:16 +00:00
|
|
|
|
2024-10-23 19:35:07 +00:00
|
|
|
#include "render/render.h"
|
2024-10-05 08:58:16 +00:00
|
|
|
#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);
|
2024-10-17 20:14:16 +00:00
|
|
|
if (err) goto error;
|
2024-10-05 08:58:16 +00:00
|
|
|
|
2024-10-23 19:35:07 +00:00
|
|
|
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 };
|
2024-10-05 08:58:16 +00:00
|
|
|
|
2024-10-23 19:35:07 +00:00
|
|
|
err = request_serialise_buf(&request, output_buffer, 8192);
|
2024-10-17 20:14:16 +00:00
|
|
|
if (err != ERR_OK) goto error_socket;
|
2024-10-10 05:58:13 +00:00
|
|
|
|
2024-10-23 19:35:07 +00:00
|
|
|
write(sock, output_buffer, 8192);
|
2024-10-10 05:58:13 +00:00
|
|
|
|
2024-10-23 19:35:07 +00:00
|
|
|
// Get response from server
|
|
|
|
read(sock, input_buffer, 8192);
|
|
|
|
|
|
|
|
struct response_t response = { 0 };
|
|
|
|
err = response_deserialise_str(&response, input_buffer);
|
2024-10-17 20:14:16 +00:00
|
|
|
if (err != ERR_OK) goto error_socket;
|
2024-10-10 05:58:13 +00:00
|
|
|
|
2024-10-23 19:35:07 +00:00
|
|
|
// 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);
|
2024-10-05 08:58:16 +00:00
|
|
|
|
|
|
|
close(sock);
|
|
|
|
return ERR_OK;
|
2024-10-04 05:05:43 +00:00
|
|
|
|
2024-10-17 20:14:16 +00:00
|
|
|
error_socket:
|
2024-10-10 22:31:07 +00:00
|
|
|
close(sock);
|
|
|
|
|
2024-10-17 20:14:16 +00:00
|
|
|
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
|
|
|
}
|