diff options
| -rw-r--r-- | src/game.c | 8 | ||||
| -rw-r--r-- | src/main.c | 1 | ||||
| -rw-r--r-- | src/net.h | 74 | ||||
| -rw-r--r-- | src/net/client.c (renamed from src/net_client.c) | 8 | ||||
| -rw-r--r-- | src/net/client.h | 15 | ||||
| -rw-r--r-- | src/net/comm.c (renamed from src/net.c) | 4 | ||||
| -rw-r--r-- | src/net/comm.h | 43 | ||||
| -rw-r--r-- | src/net/server.c (renamed from src/net_server.c) | 5 | ||||
| -rw-r--r-- | src/net/server.h | 25 |
9 files changed, 97 insertions, 86 deletions
@@ -11,7 +11,9 @@ #include <sys/types.h> #include "ui.h" #include "global.h" -#include "net.h" +#include "net/comm.h" +#include "net/client.h" +#include "net/server.h" static void init_mainstack(card *stack, const uint32_t size) { @@ -35,7 +37,7 @@ static void init_mainstack(card *stack, const uint32_t size) static void main_loop_client(int sock) { bool running = true; - gamestate state = STATE_CLIENT_WAIT_CARDS; + //gamestate state = STATE_CLIENT_WAIT_CARDS; while(running) { @@ -117,7 +119,7 @@ static void main_loop_client(int sock) static void main_loop_server(socket_list_t* sock) { bool running = true; - gamestate state = STATE_SERVER_DEAL_CARDS; + //gamestate state = STATE_SERVER_DEAL_CARDS; while(running) { @@ -4,7 +4,6 @@ #include <unistd.h> #include "ui.h" #include "game.h" -#include "net.h" #include "player.h" #include "hand.h" diff --git a/src/net.h b/src/net.h deleted file mode 100644 index 4f140dc..0000000 --- a/src/net.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef OXEN_NET_H -#define OXEN_NET_H - -#include <stdint.h> -#include <stdbool.h> -#include "player.h" -#include "hand.h" -#include "table_stacks.h" - -// Offsets within the receive buffer to easily access the data fields of the received message -#define NET_MSG_OFFSET_TYPE 0 -#define NET_MSG_OFFSET_PAYLOAD_LENGTH 1 -#define NET_MSG_OFFSET_PAYLOAD 2 - -typedef enum -{ - // Specify message type identifiers here - msg_type_hello = 0x0, - msg_type_start_game = 0x1, - msg_type_deal_cards = 0x2, - msg_type_init_stacks = 0x3, - msg_type_selected_card = 0x4, - msg_type_selected_stack_c = 0x5, - msg_type_selected_stack_s = 0x6, - msg_type_initial_stack = 0x7 -} msg_type_t; - -// Header format -typedef struct -{ - uint8_t type; - uint8_t payload_length; -} msg_header_t; - -// Message format -typedef struct -{ - msg_header_t hdr; - uint8_t *payload; -} msg_t; - -// Socket list -typedef struct -{ - uint8_t count; - int sockets[MAX_PLAYERS]; -} socket_list_t; - - -// generic receive function -bool net_recv(int sock, msg_type_t wanted); -bool net_send(int sock, const msg_type_t type, void* param); - -// Server side functions -int server_start(const char* port); -void server_get_players(int serversock, socket_list_t* client_socks, const uint8_t count); -bool server_recv_hello(const uint8_t* payload, const uint8_t payload_len); -card* server_recv_selected_card(const uint8_t* payload, const uint8_t payload_len); -uint8_t* server_recv_selected_stack(const uint8_t* payload, const uint8_t payload_len); -uint8_t server_send_start_game(uint8_t* payload, const uint8_t payload_len); -uint8_t server_send_deal_cards(uint8_t* payload, const uint8_t payload_len, const void* param); -uint8_t server_send_selected_stack(uint8_t* payload, const uint8_t payload_len); -uint8_t server_send_initial_stacks(uint8_t* payload, const uint8_t payload_len); - -// Client side functions -int client_connect_server(const char* host, const char* port); -uint8_t client_send_hello(uint8_t* payload, const uint8_t payload_len); -uint8_t client_send_selected_card(uint8_t* payload, const uint8_t payload_len); -uint8_t client_send_selected_stack(uint8_t* payload, const uint8_t payload_len); -bool client_recv_player_list(const uint8_t* payload, const uint8_t data_len); -bool client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len); -bool client_recv_selected_stack(const uint8_t* payload, const uint8_t payload_len); - -#endif // OXEN_NET_H diff --git a/src/net_client.c b/src/net/client.c index e742e7a..2606bd4 100644 --- a/src/net_client.c +++ b/src/net/client.c @@ -6,10 +6,10 @@ #include <stdio.h> #include <unistd.h> #include <assert.h> -#include "net.h" -#include "global.h" -#include "player.h" -#include "game.h" +#include "client.h" +#include "../global.h" +#include "../player.h" +#include "../game.h" /** * Client side function; connects to specified host:port diff --git a/src/net/client.h b/src/net/client.h new file mode 100644 index 0000000..31b3d30 --- /dev/null +++ b/src/net/client.h @@ -0,0 +1,15 @@ +#ifndef OXEN_CLIENT_H +#define OXEN_CLIENT_H + +#include <stdint.h> +#include <stdbool.h> + +int client_connect_server(const char* host, const char* port); +uint8_t client_send_hello(uint8_t* payload, const uint8_t payload_len); +uint8_t client_send_selected_card(uint8_t* payload, const uint8_t payload_len); +uint8_t client_send_selected_stack(uint8_t* payload, const uint8_t payload_len); +bool client_recv_player_list(const uint8_t* payload, const uint8_t data_len); +bool client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len); +bool client_recv_selected_stack(const uint8_t* payload, const uint8_t payload_len); + +#endif // OXEN_CLIENT_H diff --git a/src/net.c b/src/net/comm.c index 94dfe80..5a283db 100644 --- a/src/net.c +++ b/src/net/comm.c @@ -3,7 +3,9 @@ #include <stdlib.h> #include <stdio.h> #include <assert.h> -#include "net.h" +#include "comm.h" +#include "client.h" +#include "server.h" bool net_recv(int sock, const msg_type_t type) { diff --git a/src/net/comm.h b/src/net/comm.h new file mode 100644 index 0000000..03816c8 --- /dev/null +++ b/src/net/comm.h @@ -0,0 +1,43 @@ +#ifndef OXEN_NET_H +#define OXEN_NET_H + +#include <stdint.h> +#include <stdbool.h> + +// Offsets within the receive buffer to easily access the data fields of the received message +#define NET_MSG_OFFSET_TYPE 0 +#define NET_MSG_OFFSET_PAYLOAD_LENGTH 1 +#define NET_MSG_OFFSET_PAYLOAD 2 + +typedef enum +{ + // Specify message type identifiers here + msg_type_hello = 0x0, + msg_type_start_game = 0x1, + msg_type_deal_cards = 0x2, + msg_type_init_stacks = 0x3, + msg_type_selected_card = 0x4, + msg_type_selected_stack_c = 0x5, + msg_type_selected_stack_s = 0x6, + msg_type_initial_stack = 0x7 +} msg_type_t; + +// Header format +typedef struct +{ + uint8_t type; + uint8_t payload_length; +} msg_header_t; + +// Message format +typedef struct +{ + msg_header_t hdr; + uint8_t *payload; +} msg_t; + +// generic network functions +bool net_recv(int sock, msg_type_t wanted); +bool net_send(int sock, const msg_type_t type, void* param); + +#endif // OXEN_NET_H diff --git a/src/net_server.c b/src/net/server.c index 327a695..10d5d11 100644 --- a/src/net_server.c +++ b/src/net/server.c @@ -6,9 +6,8 @@ #include <stdio.h> #include <unistd.h> #include <assert.h> -#include "net.h" -#include "player.h" -#include "global.h" +#include "server.h" +#include "../global.h" /** * Server side function; start server on specified port diff --git a/src/net/server.h b/src/net/server.h new file mode 100644 index 0000000..66bce9b --- /dev/null +++ b/src/net/server.h @@ -0,0 +1,25 @@ +#ifndef OXEN_SERVER_H +#define OXEN_SERVER_H + +#include <stdint.h> +#include <stdbool.h> +#include "../player.h" + +// Socket list +typedef struct +{ + uint8_t count; + int sockets[MAX_PLAYERS]; +} socket_list_t; + +int server_start(const char* port); +void server_get_players(int serversock, socket_list_t* client_socks, const uint8_t count); +bool server_recv_hello(const uint8_t* payload, const uint8_t payload_len); +card* server_recv_selected_card(const uint8_t* payload, const uint8_t payload_len); +uint8_t* server_recv_selected_stack(const uint8_t* payload, const uint8_t payload_len); +uint8_t server_send_start_game(uint8_t* payload, const uint8_t payload_len); +uint8_t server_send_deal_cards(uint8_t* payload, const uint8_t payload_len, const void* param); +uint8_t server_send_selected_stack(uint8_t* payload, const uint8_t payload_len); +uint8_t server_send_initial_stacks(uint8_t* payload, const uint8_t payload_len); + +#endif // OXEN_SERVER_H |
