From c88a524298aa46157638a74416e8c6e968772758 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Thu, 20 Jan 2011 18:32:39 +0100 Subject: implemented functions for notifying server of selected tablestack and for broadcasting the selection results to all clients --- src/net.c | 6 ++++++ src/net.h | 15 +++++++++++---- src/net_client.c | 32 ++++++++++++++++++++++++++++++++ src/net_server.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/net.c b/src/net.c index 9897224..8540df5 100644 --- a/src/net.c +++ b/src/net.c @@ -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); diff --git a/src/net.h b/src/net.h index aca2423..bebbc6f 100644 --- a/src/net.h +++ b/src/net.h @@ -4,6 +4,7 @@ #include #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 @@ -56,6 +56,24 @@ void client_selected_card(int sock, const card c) send(sock, buf, 3, 0); } +/** + * 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 @@ -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 @@ -158,6 +158,26 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis free(buf); } +/** + * 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