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/main.c | 6 +++--- src/net.c | 45 +++++++++++++++++++++++++++++++++++++++ src/net.h | 7 +++++-- src/net_client.c | 64 ++++++++------------------------------------------------ src/net_server.c | 50 +++++++------------------------------------ 5 files changed, 69 insertions(+), 103 deletions(-) create mode 100644 src/net.c diff --git a/src/main.c b/src/main.c index 8acf5ec..6e68d35 100644 --- a/src/main.c +++ b/src/main.c @@ -41,7 +41,7 @@ int main(int argc, char **argv) for(int i=0; icount; i++) printf("Player %d: %s\n", i, players->names[i]); // receive test hand - testhand = client_recv(sock, msg_type_deal_cards); + testhand = net_recv(sock, msg_type_deal_cards); printf("received cards: "); for(int i=0; i +#include +#include +#include +#include +#include "net.h" + +void* net_recv(int sock, msg_type_t wanted) +{ + void* result = NULL; + uint8_t peekbuf[2], type, payload_len, *packet, *payload; + ssize_t len = recv(sock, peekbuf, 2, MSG_PEEK); // just peek into packet to determine type + + assert(len != -1); + + type = peekbuf[NET_MSG_OFFSET_TYPE]; + payload_len = peekbuf[NET_MSG_OFFSET_PAYLOAD_LENGTH]; + + if(type != wanted) + { + printf("client_recv: received type %d instead of %d", type, wanted); + return NULL; + } + + packet = malloc(payload_len+NET_MSG_OFFSET_PAYLOAD); + recv(sock, packet, payload_len+NET_MSG_OFFSET_PAYLOAD, 0); + payload = &packet[NET_MSG_OFFSET_PAYLOAD]; + + switch(type) + { + case msg_type_hello: + result = server_recv_hello(payload, payload_len); + break; + case msg_type_start_game: + result = client_recv_player_list(payload, payload_len); + break; + case msg_type_deal_cards: + result = client_recv_deal_cards(payload, payload_len); + break; + } + free(packet); + + return result; +} + diff --git a/src/net.h b/src/net.h index 89b0b23..8679d77 100644 --- a/src/net.h +++ b/src/net.h @@ -32,17 +32,20 @@ typedef struct uint8_t payload[]; } msg_t; +// generic receive function +void* net_recv(int sock, msg_type_t wanted); // Server side functions 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, msg_type_t wanted); +char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len); // 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, msg_type_t wanted); +player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len); +hand* client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len); #endif // OXEN_NET_H 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; icount; 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; icount; // 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