diff options
| -rw-r--r-- | src/net.h | 23 | ||||
| -rw-r--r-- | src/net_client.c | 18 | ||||
| -rw-r--r-- | src/net_server.c | 22 |
3 files changed, 29 insertions, 34 deletions
@@ -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 b86508c..031e6af 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 = create_playerlist(); 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; i<MAX_HAND_CARDS; i++) *h[i] = (card) buf[i+2]; @@ -139,7 +139,7 @@ static hand* client_recv_deal_cards(int sock, uint8_t data_len) * @param[in] wanted Desired packet type * @return Pointer to desired data */ -void* client_recv(int sock, uint8_t wanted) +void* client_recv(int sock, msg_type_t wanted) { void* result = NULL; uint8_t buf[10], type, data_len; @@ -147,8 +147,8 @@ void* client_recv(int sock, uint8_t wanted) assert(len != -1); - type = buf[INDEX_TYPE]; - data_len = buf[INDEX_LEN]; + 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); diff --git a/src/net_server.c b/src/net_server.c index edafed2..f55c558 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -133,9 +133,9 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis printf("server_start_game: Out of memory\n"); exit(EXIT_FAILURE); } - buf[INDEX_TYPE] = msg_type_start_game; - buf[INDEX_LEN] = buflen; - pos = INDEX_PAYLOAD; + buf[NET_MSG_OFFSET_TYPE] = msg_type_start_game; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = buflen; + pos = NET_MSG_OFFSET_PAYLOAD; buf[pos++] = players->count; // copy usernames with length to buffer for(int i=0; i<usercount; i++) @@ -165,9 +165,9 @@ static char* server_recv_hello(int sock, uint8_t data_len) recv(sock, buf, data_len, 0); - assert(buf[INDEX_TYPE] == msg_type_hello); + assert(buf[NET_MSG_OFFSET_TYPE] == msg_type_hello); - namelen = buf[INDEX_LEN] - 2; + namelen = buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] - 2; name = malloc(namelen+1); if(name == NULL) { @@ -187,7 +187,7 @@ static char* server_recv_hello(int sock, uint8_t data_len) * @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, uint8_t wanted) +void* server_recv(int sock, msg_type_t wanted) { void* result = NULL; uint8_t buf[10], type, data_len; @@ -195,8 +195,8 @@ void* server_recv(int sock, uint8_t wanted) assert(len != -1); - type = buf[INDEX_TYPE]; - data_len = buf[INDEX_LEN]; + type = buf[NET_MSG_OFFSET_TYPE]; + data_len = buf[NET_MSG_OFFSET_PAYLOAD_LENGTH]; if(type != wanted) return NULL; @@ -219,10 +219,10 @@ void server_deal_cards(int sock, const hand h) { uint8_t buf[2+MAX_HAND_CARDS]; - buf[INDEX_TYPE] = msg_type_deal_cards; - buf[INDEX_LEN] = 2+MAX_HAND_CARDS; + buf[NET_MSG_OFFSET_TYPE] = msg_type_deal_cards; + buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = 2+MAX_HAND_CARDS; for(int i=0; i<MAX_HAND_CARDS; i++) - buf[INDEX_PAYLOAD+i] = h[i]; + buf[NET_MSG_OFFSET_PAYLOAD+i] = h[i]; send(sock, buf, 2+MAX_HAND_CARDS, 0); |
