From 535e2a60f6986faf34ae9dcc6677b26a4a133fdf Mon Sep 17 00:00:00 2001 From: Warwick Date: Thu, 17 Aug 2023 17:42:54 +0100 Subject: [PATCH] Almost at the point of issueing move commands just have some bugs --- src/client/command_cli.c | 17 +++++++++++++++ src/client/command_cli.h | 13 +++++++++++ src/client/main.c | 13 ++++++++--- src/server/command.c | 3 ++- src/server/command.h | 2 +- src/server/main.c | 8 +++++++ urchin-util/include/message.h | 7 ++++-- urchin-util/include/message_internal.h | 3 +++ urchin-util/src/message.c | 30 +++++++++++++++++++++++--- 9 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 src/client/command_cli.c create mode 100644 src/client/command_cli.h diff --git a/src/client/command_cli.c b/src/client/command_cli.c new file mode 100644 index 0000000..5e00367 --- /dev/null +++ b/src/client/command_cli.c @@ -0,0 +1,17 @@ +#include "command_cli.h" +#include +#include + +Command *cmd__create() { + Command initialCommand = COMMAND__INIT; + Command *command = malloc(sizeof(initialCommand)); + memcpy(&initialCommand, command, sizeof(initialCommand)); + return command; +} + +void cmd__add_move(Command *command, move_direction direction) { +// command->has_move_direction = 1; + command->move_direction = direction; +} + +void cmd__destroy(Command *command) { free(command); } diff --git a/src/client/command_cli.h b/src/client/command_cli.h new file mode 100644 index 0000000..9dbb476 --- /dev/null +++ b/src/client/command_cli.h @@ -0,0 +1,13 @@ +#pragma once + +#include "world_structures.h" +#include <../src/protobuf_gen/commands.pb-c.h> + +//FIX: Create malloced command struct to be edited before being sent +Command *cmd__create(); + +// Add movement direction to command struct +void cmd__add_move(Command* command, move_direction direction); + +// Free command struct when we're done with it +void cmd__destroy(Command* command); diff --git a/src/client/main.c b/src/client/main.c index ec5b139..88cb9cc 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -1,9 +1,9 @@ // Sorry in advance for all the comments. This project is also a way for me to // learn C. -#include "error.h" -#include "message.h" -#include +#include "command_cli.h" +#include +#include #include // Contains host structure #include //sockaddr_in #include @@ -12,6 +12,8 @@ #include #include // sockaddr #include +#include +#include // void error_crash(const char *msg) { // fprintf(stderr, "error crash: %s\n", msg); @@ -60,6 +62,11 @@ int main(int argc, char *argv[]) { msg__req_login(&sockfd, "username", "passwd"); + Command *cmd = cmd__create(); + cmd__add_move(cmd, WEST); + msg__req_command(&sockfd, cmd); + cmd__destroy(cmd); + while (1) { bzero(msgbuffer, sizeof msgbuffer); fgets(msgbuffer, sizeof msgbuffer, stdin); diff --git a/src/server/command.c b/src/server/command.c index ef53ac0..b9aff86 100644 --- a/src/server/command.c +++ b/src/server/command.c @@ -28,9 +28,10 @@ void cmd__move_direction(move_direction direction, Room *current_room) { } } -void cmd__recv_proc_cmd(Player *player, Command *command) { +void cmd__process_cmd(Player *player, Command *command) { if (command->has_move_direction) { move_direction dir = {command->move_direction}; cmd__move_direction(dir, player->current_location); } + command__free_unpacked(command, NULL); } diff --git a/src/server/command.h b/src/server/command.h index 8ef9e4d..e1f5488 100644 --- a/src/server/command.h +++ b/src/server/command.h @@ -7,7 +7,7 @@ #include // Will process a command given a player to execute it as -void cmd__recv_proc_cmd(Player *player, Command *command); +void cmd__process_cmd(Player *player, Command *command); // Update room pointer based on move direction void cmd__move_direction(move_direction direction, Room *current_room); diff --git a/src/server/main.c b/src/server/main.c index 032f37e..c5032bb 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -12,6 +12,7 @@ #include #include #include "world.h" +#include "command.h" int main(int argc, char *argv[]) { // Set up containers for file descriptor id's @@ -31,6 +32,8 @@ int main(int argc, char *argv[]) { // Create world Room* world = wrld__create(); + Player player; + player.current_location = &world[0]; // Create socket serv_sockfd = msg__create_serv_connection(portno); @@ -41,6 +44,11 @@ int main(int argc, char *argv[]) { // Require login as first request msg__recv_login(&cli_sockfd); + //HACK: test recieve of movement command + err__log(player.current_location->name); + cmd__process_cmd(&player, msg__recv_command(&cli_sockfd)); + err__log(player.current_location->name); + while (1) { // Empty message buffer bzero(msgbuffer, sizeof msgbuffer); diff --git a/urchin-util/include/message.h b/urchin-util/include/message.h index 1543d44..15caca1 100644 --- a/urchin-util/include/message.h +++ b/urchin-util/include/message.h @@ -28,6 +28,9 @@ int msg__accept_cli_connection(int server_sockfd); void msg__recv_login(int *con_sockfd); void msg__req_login(int *con_sockfd, char *usernm, char *passwd); -// send and recive commands +// Recive commands, This creates a Command object that must be freed using +// command__free_unpacked() once processed. Command *msg__recv_command(int *con_sockfd); -void msg__req_command(int *con_sockfd, char *usernm, char *passwd); + +// Sends a preconfigured command to the server +void msg__req_command(int *con_sockfd, Command* command); diff --git a/urchin-util/include/message_internal.h b/urchin-util/include/message_internal.h index de28d90..454d678 100644 --- a/urchin-util/include/message_internal.h +++ b/urchin-util/include/message_internal.h @@ -7,3 +7,6 @@ void read_from_sock(uint8_t *recvbuf, unsigned bufsize, int *sockfd); // One method to see how much data is in a buffer to unpack size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out); + +// Send msg buffer to the server, crash on fail. +void msg__send_msgbuf(int *con_sockfd, uint8_t *msgbuf, size_t msgbuf_size); diff --git a/urchin-util/src/message.c b/urchin-util/src/message.c index 30771fb..cfcbdbd 100644 --- a/urchin-util/src/message.c +++ b/urchin-util/src/message.c @@ -1,12 +1,25 @@ #include "message.h" #include "error.h" +#include "protobuf_gen/commands.pb-c.h" #include "world_structures.h" #include -#define MAX_MSG_SIZE 1024 +#define MAX_MSG_SIZE 4096 + +void msg__send_msgbuf(int *con_sockfd, uint8_t *msgbuf, size_t msgbuf_size) { + if (msgbuf_size > MAX_MSG_SIZE) { + err__crash("Msg buffer too big to send"); + free(msgbuf); + } + + if (write(*con_sockfd, msgbuf, msgbuf_size) < 0) { + err__crash("Couldn't write message buffer to server"); + free(msgbuf); + } +} void msg__amsg_test(int a) { - AMessage msg = AMESSAGE__INIT; // The massage format + AMessage msg = AMESSAGE__INIT; // The message format void *buf; // Buffer to store serialized data unsigned len; // length of serialized data @@ -161,7 +174,18 @@ Command *msg__recv_command(int *con_sockfd) { return cmd; } -void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {} + +void msg__req_command(int *con_sockfd, Command *command) { + void *msgbuf; + unsigned len; + + // FIX: Figure out why this malloced structure won't work here + len = command__get_packed_size(command); + msgbuf = malloc(len); + command__pack(command, msgbuf); + + msg__send_msgbuf(con_sockfd, msgbuf, len); +} int msg__accept_cli_connection(int server_sockfd) { struct sockaddr_in cli_addr;