summaryrefslogtreecommitdiff
path: root/src/net_client.c
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2011-01-16 03:36:43 +0100
committerReiner Herrmann <reiner@reiner-h.de>2011-01-16 03:36:43 +0100
commit08a3c0b9979857e708865d2756ba057bb1c664ee (patch)
tree9afc39c05d3fce4080e425902c0c71a3af413e3c /src/net_client.c
parent3a85f876ca899d2ea523dafc79e6d3735ed52611 (diff)
moved generic recv function to new file; use payload length inside packets instead of packet length
Diffstat (limited to 'src/net_client.c')
-rw-r--r--src/net_client.c64
1 files changed, 9 insertions, 55 deletions
diff --git a/src/net_client.c b/src/net_client.c
index 031e6af..9134cf7 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -27,7 +27,7 @@ void client_hello(int sock, const char* username)
}
buf[NET_MSG_OFFSET_TYPE] = msg_type_hello;
- buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = namelen+2;
+ buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = namelen;
memcpy(buf+NET_MSG_OFFSET_PAYLOAD, username, namelen);
send(sock, buf, namelen+2, 0);
@@ -83,15 +83,10 @@ int client_connect_server(const char* host, const char* port)
return sock;
}
-static player_list* client_recv_player_list(int sock, uint8_t data_len)
+player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len)
{
- uint8_t buf[data_len];
player_list* players;
- uint32_t pos;
-
- recv(sock, buf, data_len, 0);
-
- assert(buf[NET_MSG_OFFSET_TYPE] == msg_type_start_game);
+ uint32_t pos = 0;
players = create_playerlist();
if(players == NULL)
@@ -100,15 +95,14 @@ static player_list* client_recv_player_list(int sock, uint8_t data_len)
exit(EXIT_FAILURE);
}
- pos = NET_MSG_OFFSET_PAYLOAD;
- players->count = buf[pos++];
+ players->count = payload[pos++];
// read usernames from buffer
for(int i=0; i<players->count; i++)
{
- uint8_t namelen = buf[pos++];
+ uint8_t namelen = payload[pos++];
players->names[i] = malloc(namelen+1);
- memcpy(players->names[i], buf+pos, namelen);
+ memcpy(players->names[i], payload+pos, namelen);
players->names[i][namelen] = '\0';
pos += namelen;
}
@@ -116,55 +110,15 @@ static player_list* client_recv_player_list(int sock, uint8_t data_len)
return players;
}
-static hand* client_recv_deal_cards(int sock, uint8_t data_len)
+hand* client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_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[NET_MSG_OFFSET_TYPE] == msg_type_deal_cards);
+ assert(payload_len == MAX_HAND_CARDS); // deal_cards packet have fixed size
for(int i=0; i<MAX_HAND_CARDS; i++)
- *h[i] = (card) buf[i+2];
+ *h[i] = payload[i];
return h;
}
-/**
- * Client side function; generic function for receiving packets
- * @param[in] sock Socket on which to receive
- * @param[in] wanted Desired packet type
- * @return Pointer to desired data
- */
-void* client_recv(int sock, msg_type_t wanted)
-{
- void* result = NULL;
- 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[NET_MSG_OFFSET_TYPE];
- data_len = buf[NET_MSG_OFFSET_PAYLOAD_LENGTH];
- 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, data_len);
- break;
- case msg_type_deal_cards:
- result = client_recv_deal_cards(sock, data_len);
- break;
- }
-
- return result;
-}
-