From 8d1391507cbb943ecf17a8e4097f524da2505e32 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sat, 15 Jan 2011 18:44:52 +0100 Subject: 1. network fix: send also packet length in every packet after packet type to be able to determine exact amount to recv 2. implement functions for sending/receiving dealt hands --- src/card.h | 2 +- src/main.c | 21 ++++++++++++++++++--- src/net.h | 15 ++++++++++++++- src/net_client.c | 41 ++++++++++++++++++++++++++++++++--------- src/net_server.c | 29 ++++++++++++++++++++++------- 5 files changed, 87 insertions(+), 21 deletions(-) diff --git a/src/card.h b/src/card.h index 2ec3ea4..82cd5cf 100644 --- a/src/card.h +++ b/src/card.h @@ -3,7 +3,7 @@ #include -typedef char card; +typedef uint8_t card; uint32_t card_get_points(const card c); diff --git a/src/main.c b/src/main.c index 62c3ce1..1c2946b 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "game.h" #include "net.h" #include "player.h" +#include "hand.h" int main(int argc, char **argv) { @@ -23,6 +24,7 @@ int main(int argc, char **argv) struct player_list players; int opponents = 3; const char* nickname = "deki"; + const hand testhand = { 12, 23, 35, 42, 55, 57, 70, 81, 103, 0 }; servermode = true; port = argv[1]; @@ -47,6 +49,10 @@ int main(int argc, char **argv) // start game and send player list to clients server_start_game(csocks, opponents, &players); + // send test hand + 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); + printf("received cards: "); + for(int i=0; icount; i++) free(players->names[i]); free(players); + free(testhand); close(sock); } - ui_init(); - start_game(servermode, addr, port); - ui_fini(); + //ui_init(); + //start_game(servermode, addr, port); + //ui_fini(); return EXIT_SUCCESS; } diff --git a/src/net.h b/src/net.h index 907412d..018a8e5 100644 --- a/src/net.h +++ b/src/net.h @@ -3,12 +3,21 @@ #include #include "player.h" +#include "hand.h" + +/* + * Packet format: + * first byte: msg_type_t + * second byte: packet length + * followed by payload + */ typedef enum { // Specify message type identifier here msg_type_hello = 0x0, - msg_type_start_game = 0x1 + msg_type_start_game = 0x1, + msg_type_deal_cards = 0x2 } msg_type_t; typedef struct @@ -23,11 +32,15 @@ typedef struct uint8_t payload[]; } msg_t; + +// 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 struct player_list* players); +void server_deal_cards(int sock, const hand h); void* server_recv(int sock, uint8_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); diff --git a/src/net_client.c b/src/net_client.c index 8d07b62..92b699b 100644 --- a/src/net_client.c +++ b/src/net_client.c @@ -9,7 +9,6 @@ #include "net.h" #include "player.h" - /** * Client side function; Send hello to server * @param[in] sock Socket to use @@ -28,7 +27,7 @@ void client_hello(int sock, const char* username) } buf[0] = msg_type_hello; - buf[1] = namelen; + buf[1] = namelen+2; memcpy(buf+2, username, namelen); send(sock, buf, namelen+2, 0); @@ -84,13 +83,13 @@ int client_connect_server(const char* host, const char* port) return sock; } -static struct player_list* client_recv_player_list(int sock) +static struct player_list* client_recv_player_list(int sock, uint8_t data_len) { - uint8_t buf[200]; + uint8_t buf[data_len]; struct player_list* players; uint32_t pos; - recv(sock, buf, 200, 0); + recv(sock, buf, data_len, 0); assert(buf[0] == msg_type_start_game); @@ -100,9 +99,9 @@ static struct player_list* client_recv_player_list(int sock) printf("client_recv_player_list: Out of memory\n"); exit(EXIT_FAILURE); } - players->count = buf[1]; + players->count = buf[2]; - pos = 2; + pos = 3; // read usernames from buffer for(int i=0; icount; i++) { @@ -116,22 +115,46 @@ static struct player_list* client_recv_player_list(int sock) return players; } +static hand* client_recv_deal_cards(int sock, uint8_t data_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[0] == msg_type_deal_cards); + + for(int i=0; icount; uint32_t pos = 0; - uint32_t buflen = 2 + usercount; // type + usercount + (usercount * len) + uint32_t buflen = 3 + usercount; // type + packetlen + usercount + (usercount * len) for(int i=0; inames[i]); @@ -128,6 +128,7 @@ void server_start_game(int* clients, const uint8_t clientcount, const struct pla exit(EXIT_FAILURE); } buf[pos++] = msg_type_start_game; + buf[pos++] = buflen; buf[pos++] = players->count; // copy usernames with length to buffer for(int i=0; i