diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2011-01-15 18:44:52 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2011-01-15 18:44:52 +0100 |
| commit | 8d1391507cbb943ecf17a8e4097f524da2505e32 (patch) | |
| tree | fa23300471c8359fed3e6bce8279f1b3830200f1 /src | |
| parent | d9b86b44ff96e7ca87af4c5dee7a61f61b55f603 (diff) | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/card.h | 2 | ||||
| -rw-r--r-- | src/main.c | 21 | ||||
| -rw-r--r-- | src/net.h | 15 | ||||
| -rw-r--r-- | src/net_client.c | 41 | ||||
| -rw-r--r-- | src/net_server.c | 29 |
5 files changed, 87 insertions, 21 deletions
@@ -3,7 +3,7 @@ #include <stdint.h> -typedef char card; +typedef uint8_t card; uint32_t card_get_points(const card 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; i<opponents; i++) + server_deal_cards(csocks[i], testhand); + // cleanup for(int i=0; i<players.count; i++) free(players.names[i]); @@ -58,6 +64,7 @@ int main(int argc, char **argv) int sock; struct player_list* players; const char* nickname = "schnippi"; + hand* testhand; addr = argv[1]; port = argv[2]; @@ -72,15 +79,23 @@ int main(int argc, char **argv) for(int i=0; i<players->count; 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; i<MAX_HAND_CARDS; i++) + printf("%d, ", *testhand[i]); + printf("\n"); + // cleanup for(int i=0; i<players->count; 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; } @@ -3,12 +3,21 @@ #include <stdint.h> #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; i<players->count; 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; i<MAX_HAND_CARDS; i++) + *h[i] = (card) buf[i+2]; + + return h; +} + void* client_recv(int sock, uint8_t wanted) { void* result = NULL; - uint8_t buf[10], type; + 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[0]; + data_len = buf[1]; if(type != wanted) + { + printf("client_recv: received type %d instead of %d", type, wanted); return NULL; + } switch(type) { case msg_type_start_game: - result = client_recv_player_list(sock); + result = client_recv_player_list(sock, data_len); + break; + case msg_type_deal_cards: + result = client_recv_deal_cards(sock, data_len); break; } diff --git a/src/net_server.c b/src/net_server.c index 39b6c51..716cc48 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -116,7 +116,7 @@ void server_start_game(int* clients, const uint8_t clientcount, const struct pla uint8_t* buf; uint8_t usercount = players->count; 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; i<usercount; i++) buflen += strlen(players->names[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<usercount; i++) @@ -150,16 +151,16 @@ void server_start_game(int* clients, const uint8_t clientcount, const struct pla * @param[in] sock Socket to use * @return Username of client */ -static char* server_recv_hello(int sock) +static char* server_recv_hello(int sock, uint8_t data_len) { - char buf[12], *name; + char buf[data_len], *name; uint8_t namelen; - recv(sock, buf, 12, 0); + recv(sock, buf, data_len, 0); assert(buf[0] == msg_type_hello); - namelen = buf[1]; + namelen = buf[1] - 2; name = malloc(namelen+1); if(name == NULL) { @@ -182,22 +183,36 @@ static char* server_recv_hello(int sock) void* server_recv(int sock, uint8_t wanted) { void* result = NULL; - uint8_t buf[10], type; + 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[0]; + data_len = buf[1]; if(type != wanted) return NULL; switch(type) { case msg_type_hello: - result = server_recv_hello(sock); + result = server_recv_hello(sock, data_len); break; } return result; } +void server_deal_cards(int sock, const hand h) +{ + uint8_t buf[2+MAX_HAND_CARDS]; + + buf[0] = msg_type_deal_cards; + buf[1] = 2+MAX_HAND_CARDS; + for(int i=0; i<MAX_HAND_CARDS; i++) + buf[2+i] = h[i]; + + + send(sock, buf, 2+MAX_HAND_CARDS, 0); +} + |
