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/net_server.c | |
| 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/net_server.c')
| -rw-r--r-- | src/net_server.c | 29 |
1 files changed, 22 insertions, 7 deletions
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); +} + |
