From 08a3c0b9979857e708865d2756ba057bb1c664ee Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sun, 16 Jan 2011 03:36:43 +0100 Subject: moved generic recv function to new file; use payload length inside packets instead of packet length --- src/net_server.c | 50 +++++++------------------------------------------- 1 file changed, 7 insertions(+), 43 deletions(-) (limited to 'src/net_server.c') diff --git a/src/net_server.c b/src/net_server.c index f55c558..da9d33d 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -134,7 +134,7 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis exit(EXIT_FAILURE); } buf[NET_MSG_OFFSET_TYPE] = msg_type_start_game; - buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = buflen; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = buflen - 2; pos = NET_MSG_OFFSET_PAYLOAD; buf[pos++] = players->count; // copy usernames with length to buffer @@ -158,58 +158,23 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis * @param[in] sock Socket to use * @return Username of client */ -static char* server_recv_hello(int sock, uint8_t data_len) +char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len) { - char buf[data_len], *name; - uint8_t namelen; + char* name; - recv(sock, buf, data_len, 0); - - assert(buf[NET_MSG_OFFSET_TYPE] == msg_type_hello); - - namelen = buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] - 2; - name = malloc(namelen+1); + name = malloc(payload_len+1); if(name == NULL) { printf("sender_recv_hello: Out of memory\n"); exit(EXIT_FAILURE); } - memcpy(name, buf+2, namelen); - name[namelen] = '\0'; + memcpy(name, payload, payload_len); + name[payload_len] = '\0'; return name; } -/** - * Server side function; calls correct handler for incoming packet - * @param[in] sock Socket to use - * @param[in] wanted Packet type that should be handled - * @return Pointer to desired data or NULL if not in recv queue - */ -void* server_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) - return NULL; - - switch(type) - { - case msg_type_hello: - result = server_recv_hello(sock, data_len); - break; - } - - return result; -} - /** * Server side function; deal cards to a client (send hand) * @param[in] sock Socket to use @@ -220,11 +185,10 @@ void server_deal_cards(int sock, const hand h) uint8_t buf[2+MAX_HAND_CARDS]; buf[NET_MSG_OFFSET_TYPE] = msg_type_deal_cards; - buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = 2+MAX_HAND_CARDS; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = MAX_HAND_CARDS; for(int i=0; i