From d55b1110ee447573eacda1fbbd7dfa8c37339756 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sat, 15 Jan 2011 19:20:41 +0100 Subject: use packet indizes for better readability --- src/net.h | 5 +++++ src/net_client.c | 19 ++++++++++--------- src/net_server.c | 21 +++++++++++---------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/net.h b/src/net.h index 018a8e5..82ba119 100644 --- a/src/net.h +++ b/src/net.h @@ -12,6 +12,11 @@ * followed by payload */ +// Some fixed packet indizes (to improve readability) +#define INDEX_TYPE 0 +#define INDEX_LEN 1 +#define INDEX_PAYLOAD 2 + typedef enum { // Specify message type identifier here diff --git a/src/net_client.c b/src/net_client.c index 531bbd1..d0149b4 100644 --- a/src/net_client.c +++ b/src/net_client.c @@ -26,9 +26,9 @@ void client_hello(int sock, const char* username) exit(EXIT_FAILURE); } - buf[0] = msg_type_hello; - buf[1] = namelen+2; - memcpy(buf+2, username, namelen); + buf[INDEX_TYPE] = msg_type_hello; + buf[INDEX_LEN] = namelen+2; + memcpy(buf+INDEX_PAYLOAD, username, namelen); send(sock, buf, namelen+2, 0); @@ -91,7 +91,7 @@ static struct player_list* client_recv_player_list(int sock, uint8_t data_len) recv(sock, buf, data_len, 0); - assert(buf[0] == msg_type_start_game); + assert(buf[INDEX_TYPE] == msg_type_start_game); players = malloc(sizeof(struct player_list)); if(players == NULL) @@ -99,9 +99,10 @@ static struct player_list* client_recv_player_list(int sock, uint8_t data_len) printf("client_recv_player_list: Out of memory\n"); exit(EXIT_FAILURE); } - players->count = buf[2]; - pos = 3; + pos = INDEX_PAYLOAD; + players->count = buf[pos++]; + // read usernames from buffer for(int i=0; icount; i++) { @@ -124,7 +125,7 @@ static hand* client_recv_deal_cards(int sock, uint8_t data_len) recv(sock, buf, data_len, 0); - assert(buf[0] == msg_type_deal_cards); + assert(buf[INDEX_TYPE] == msg_type_deal_cards); for(int i=0; icount; - uint32_t pos = 0; + uint32_t pos; uint32_t buflen = 3 + usercount; // type + packetlen + usercount + (usercount * len) for(int i=0; icount; // copy usernames with length to buffer for(int i=0; i