From 493b581dfa5a06134f524fe026a2fde5c4a83af5 Mon Sep 17 00:00:00 2001 From: Mario Kilies Date: Sun, 16 Jan 2011 02:45:16 +0100 Subject: Renamed receive buffer offset macros. Changed argument data types of client_recv and server_recv. --- src/net.h | 23 +++++++++-------------- src/net_client.c | 18 +++++++++--------- src/net_server.c | 22 +++++++++++----------- 3 files changed, 29 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/net.h b/src/net.h index 46b9de9..89b0b23 100644 --- a/src/net.h +++ b/src/net.h @@ -5,32 +5,27 @@ #include "player.h" #include "hand.h" -/* - * Packet format: - * first byte: msg_type_t - * second byte: packet length - * followed by payload - */ - -// Some fixed packet indizes (to improve readability) -#define INDEX_TYPE 0 -#define INDEX_LEN 1 -#define INDEX_PAYLOAD 2 +// Offsets within the receive buffer to easily access the data fields of the received message +#define NET_MSG_OFFSET_TYPE 0 +#define NET_MSG_OFFSET_PAYLOAD_LENGTH 1 +#define NET_MSG_OFFSET_PAYLOAD 2 typedef enum { - // Specify message type identifier here + // Specify message type identifiers here msg_type_hello = 0x0, msg_type_start_game = 0x1, msg_type_deal_cards = 0x2 } msg_type_t; +// Header format typedef struct { uint8_t type; uint8_t payload_length; } msg_header_t; +// Message format typedef struct { msg_header_t hdr; @@ -43,11 +38,11 @@ int server_start(const char* port); int* server_get_players(int serversock, const uint8_t count); void server_start_game(int* clients, const uint8_t clientcount, const player_list* players); void server_deal_cards(int sock, const hand h); -void* server_recv(int sock, uint8_t wanted); +void* server_recv(int sock, msg_type_t wanted); // Client side functions int client_connect_server(const char* host, const char* port); void client_hello(int sock, const char* username); -void* client_recv(int sock, uint8_t wanted); +void* client_recv(int sock, msg_type_t wanted); #endif // OXEN_NET_H diff --git a/src/net_client.c b/src/net_client.c index 9aff39f..5e23e7a 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[INDEX_TYPE] = msg_type_hello; - buf[INDEX_LEN] = namelen+2; - memcpy(buf+INDEX_PAYLOAD, username, namelen); + buf[NET_MSG_OFFSET_TYPE] = msg_type_hello; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = namelen+2; + memcpy(buf+NET_MSG_OFFSET_PAYLOAD, username, namelen); send(sock, buf, namelen+2, 0); @@ -91,7 +91,7 @@ static player_list* client_recv_player_list(int sock, uint8_t data_len) recv(sock, buf, data_len, 0); - assert(buf[INDEX_TYPE] == msg_type_start_game); + assert(buf[NET_MSG_OFFSET_TYPE] == msg_type_start_game); players = malloc(sizeof(player_list)); if(players == NULL) @@ -100,7 +100,7 @@ static player_list* client_recv_player_list(int sock, uint8_t data_len) exit(EXIT_FAILURE); } - pos = INDEX_PAYLOAD; + pos = NET_MSG_OFFSET_PAYLOAD; players->count = buf[pos++]; // read usernames from buffer @@ -125,7 +125,7 @@ static hand* client_recv_deal_cards(int sock, uint8_t data_len) recv(sock, buf, data_len, 0); - assert(buf[INDEX_TYPE] == msg_type_deal_cards); + assert(buf[NET_MSG_OFFSET_TYPE] == msg_type_deal_cards); for(int i=0; icount; // copy usernames with length to buffer for(int i=0; i