summaryrefslogtreecommitdiff
path: root/src/net_server.c
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2011-01-16 03:36:43 +0100
committerReiner Herrmann <reiner@reiner-h.de>2011-01-16 03:36:43 +0100
commit08a3c0b9979857e708865d2756ba057bb1c664ee (patch)
tree9afc39c05d3fce4080e425902c0c71a3af413e3c /src/net_server.c
parent3a85f876ca899d2ea523dafc79e6d3735ed52611 (diff)
moved generic recv function to new file; use payload length inside packets instead of packet length
Diffstat (limited to 'src/net_server.c')
-rw-r--r--src/net_server.c50
1 files changed, 7 insertions, 43 deletions
diff --git a/src/net_server.c b/src/net_server.c
index f55c558..da9d33d 100644
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -134,7 +134,7 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis
exit(EXIT_FAILURE);
}
buf[NET_MSG_OFFSET_TYPE] = msg_type_start_game;
- buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = buflen;
+ buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = buflen - 2;
pos = NET_MSG_OFFSET_PAYLOAD;
buf[pos++] = players->count;
// copy usernames with length to buffer
@@ -158,59 +158,24 @@ 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
* @param[in] h Hand to send
@@ -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<MAX_HAND_CARDS; i++)
buf[NET_MSG_OFFSET_PAYLOAD+i] = h[i];
-
send(sock, buf, 2+MAX_HAND_CARDS, 0);
}