diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/net.c | 6 | ||||
| -rw-r--r-- | src/net.h | 15 | ||||
| -rw-r--r-- | src/net_client.c | 32 | ||||
| -rw-r--r-- | src/net_server.c | 31 |
4 files changed, 80 insertions, 4 deletions
@@ -37,6 +37,12 @@ void *net_recv(int sock, msg_type_t type) case msg_type_selected_card: result = server_recv_selected_card(m.payload, m.hdr.payload_length); break; + case msg_type_selected_stack_c: + result = server_recv_selected_stack(m.payload, m.hdr.payload_length); + break; + case msg_type_selected_stack_s: + result = client_recv_selected_stack(m.payload, m.hdr.payload_length); + break; default: printf("net_recv: Unknown message type %d received!\n", type); exit(EXIT_FAILURE); @@ -4,6 +4,7 @@ #include <stdint.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 @@ -13,10 +14,12 @@ typedef enum { // Specify message type identifiers here - msg_type_hello = 0x0, - msg_type_start_game = 0x1, - msg_type_deal_cards = 0x2, - msg_type_selected_card = 0x3 + msg_type_hello = 0x0, + msg_type_start_game = 0x1, + msg_type_deal_cards = 0x2, + msg_type_selected_card = 0x3, + msg_type_selected_stack_c = 0x4, + msg_type_selected_stack_s = 0x5 } msg_type_t; // Header format @@ -43,12 +46,16 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis void server_deal_cards(int sock, const hand_t *h); char* 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); +void server_send_selected_stack(int* clients, const uint8_t clientcount, const uint8_t stackindex); // Client side functions int client_connect_server(const char* host, const char* port); void client_hello(int sock, const char* username); void client_selected_card(int sock, const card c); +void client_send_selected_stack(int sock, const uint8_t stackindex); player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len); hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len); +uint8_t* 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 b51c7a0..9454649 100644 --- a/src/net_client.c +++ b/src/net_client.c @@ -57,6 +57,24 @@ void client_selected_card(int sock, const card c) } /** + * Client side function; Send selected table stack to server + * @param[in] sock Socket to use + * @param[in] stackindex Index of selected stack + */ +void client_send_selected_stack(int sock, const uint8_t stackindex) +{ + assert(stackindex <= NUM_TABLE_STACKS); + + uint8_t buf[3]; + + buf[NET_MSG_OFFSET_TYPE] = msg_type_selected_stack_c; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = 1; + buf[NET_MSG_OFFSET_PAYLOAD] = stackindex; + + send(sock, buf, 3, 0); +} + +/** * Client side function; connects to specified host:port * @param[in] host Hostname of server * @param[in] port Port of server @@ -151,3 +169,17 @@ hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len return h; } + +uint8_t* client_recv_selected_stack(const uint8_t* payload, const uint8_t payload_len) +{ + assert(payload != NULL); + assert(payload_len == 1); + + uint8_t* stackindex = malloc(sizeof(uint8_t)); + *stackindex = payload[0]; + + assert(*stackindex <= NUM_TABLE_STACKS); + + return stackindex; +} + diff --git a/src/net_server.c b/src/net_server.c index 0cba08c..731e232 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -159,6 +159,26 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis } /** + * Server side function; broadcast a selected table stack to all clients + * @param[in] clients Array of sockets with connections to clients + * @param[in] clientcount Number of sockets/clients + * @param[in] stackindex Selected stack to broadcast + */ +void server_send_selected_stack(int* clients, const uint8_t clientcount, const uint8_t stackindex) +{ + assert(clients != NULL); + assert(stackindex <= NUM_TABLE_STACKS); + + uint8_t buf[3]; + buf[NET_MSG_OFFSET_TYPE] = msg_type_selected_stack_s; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = 1; + buf[NET_MSG_OFFSET_PAYLOAD] = stackindex; + + for(int i=0; i<clientcount; i++) + send(clients[i], buf, 3, 0); +} + +/** * Server side function; receive hello message from client and read username * @param[in] sock Socket to use * @return Username of client @@ -193,6 +213,17 @@ card* server_recv_selected_card(const uint8_t* payload, const uint8_t payload_le return c; } +uint8_t* server_recv_selected_stack(const uint8_t* payload, const uint8_t payload_len) +{ + assert(payload != NULL && payload_len == 1); + + uint8_t* index = malloc(sizeof(uint8_t)); + assert(index != NULL); + *index = payload[0]; + + return index; +} + /** * Server side function; deal cards to a client (send hand) * @param[in] sock Socket to use |
