diff options
Diffstat (limited to 'src/net_client.c')
| -rw-r--r-- | src/net_client.c | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/src/net_client.c b/src/net_client.c index 8d07b62..92b699b 100644 --- a/src/net_client.c +++ b/src/net_client.c @@ -9,7 +9,6 @@ #include "net.h" #include "player.h" - /** * Client side function; Send hello to server * @param[in] sock Socket to use @@ -28,7 +27,7 @@ void client_hello(int sock, const char* username) } buf[0] = msg_type_hello; - buf[1] = namelen; + buf[1] = namelen+2; memcpy(buf+2, username, namelen); send(sock, buf, namelen+2, 0); @@ -84,13 +83,13 @@ int client_connect_server(const char* host, const char* port) return sock; } -static struct player_list* client_recv_player_list(int sock) +static struct player_list* client_recv_player_list(int sock, uint8_t data_len) { - uint8_t buf[200]; + uint8_t buf[data_len]; struct player_list* players; uint32_t pos; - recv(sock, buf, 200, 0); + recv(sock, buf, data_len, 0); assert(buf[0] == msg_type_start_game); @@ -100,9 +99,9 @@ static struct player_list* client_recv_player_list(int sock) printf("client_recv_player_list: Out of memory\n"); exit(EXIT_FAILURE); } - players->count = buf[1]; + players->count = buf[2]; - pos = 2; + pos = 3; // read usernames from buffer for(int i=0; i<players->count; i++) { @@ -116,22 +115,46 @@ static struct player_list* client_recv_player_list(int sock) return players; } +static hand* client_recv_deal_cards(int sock, uint8_t data_len) +{ + uint8_t buf[data_len]; + hand* h = malloc(sizeof(hand)); + + assert(data_len == 2+MAX_HAND_CARDS); // deal_cards packet have fixed size + + recv(sock, buf, data_len, 0); + + assert(buf[0] == msg_type_deal_cards); + + for(int i=0; i<MAX_HAND_CARDS; i++) + *h[i] = (card) buf[i+2]; + + return h; +} + void* client_recv(int sock, uint8_t wanted) { void* result = NULL; - uint8_t buf[10], type; + uint8_t buf[10], type, data_len; ssize_t len = recv(sock, buf, 10, MSG_PEEK); // just peek into packet to determine type assert(len != -1); type = buf[0]; + data_len = buf[1]; if(type != wanted) + { + printf("client_recv: received type %d instead of %d", type, wanted); return NULL; + } switch(type) { case msg_type_start_game: - result = client_recv_player_list(sock); + result = client_recv_player_list(sock, data_len); + break; + case msg_type_deal_cards: + result = client_recv_deal_cards(sock, data_len); break; } |
