summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/net.h5
-rw-r--r--src/net_client.c19
-rw-r--r--src/net_server.c21
3 files changed, 26 insertions, 19 deletions
diff --git a/src/net.h b/src/net.h
index 018a8e5..82ba119 100644
--- a/src/net.h
+++ b/src/net.h
@@ -12,6 +12,11 @@
* followed by payload
*/
+// Some fixed packet indizes (to improve readability)
+#define INDEX_TYPE 0
+#define INDEX_LEN 1
+#define INDEX_PAYLOAD 2
+
typedef enum
{
// Specify message type identifier here
diff --git a/src/net_client.c b/src/net_client.c
index 531bbd1..d0149b4 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -26,9 +26,9 @@ void client_hello(int sock, const char* username)
exit(EXIT_FAILURE);
}
- buf[0] = msg_type_hello;
- buf[1] = namelen+2;
- memcpy(buf+2, username, namelen);
+ buf[INDEX_TYPE] = msg_type_hello;
+ buf[INDEX_LEN] = namelen+2;
+ memcpy(buf+INDEX_PAYLOAD, username, namelen);
send(sock, buf, namelen+2, 0);
@@ -91,7 +91,7 @@ static struct player_list* client_recv_player_list(int sock, uint8_t data_len)
recv(sock, buf, data_len, 0);
- assert(buf[0] == msg_type_start_game);
+ assert(buf[INDEX_TYPE] == msg_type_start_game);
players = malloc(sizeof(struct player_list));
if(players == NULL)
@@ -99,9 +99,10 @@ static struct player_list* client_recv_player_list(int sock, uint8_t data_len)
printf("client_recv_player_list: Out of memory\n");
exit(EXIT_FAILURE);
}
- players->count = buf[2];
- pos = 3;
+ pos = INDEX_PAYLOAD;
+ players->count = buf[pos++];
+
// read usernames from buffer
for(int i=0; i<players->count; i++)
{
@@ -124,7 +125,7 @@ static hand* client_recv_deal_cards(int sock, uint8_t data_len)
recv(sock, buf, data_len, 0);
- assert(buf[0] == msg_type_deal_cards);
+ assert(buf[INDEX_TYPE] == msg_type_deal_cards);
for(int i=0; i<MAX_HAND_CARDS; i++)
*h[i] = (card) buf[i+2];
@@ -146,8 +147,8 @@ void* client_recv(int sock, uint8_t wanted)
assert(len != -1);
- type = buf[0];
- data_len = buf[1];
+ type = buf[INDEX_TYPE];
+ data_len = buf[INDEX_LEN];
if(type != wanted)
{
printf("client_recv: received type %d instead of %d", type, wanted);
diff --git a/src/net_server.c b/src/net_server.c
index edd572b..bc64024 100644
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -121,7 +121,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 pos;
uint32_t buflen = 3 + usercount; // type + packetlen + usercount + (usercount * len)
for(int i=0; i<usercount; i++)
@@ -133,8 +133,9 @@ void server_start_game(int* clients, const uint8_t clientcount, const struct pla
printf("server_start_game: Out of memory\n");
exit(EXIT_FAILURE);
}
- buf[pos++] = msg_type_start_game;
- buf[pos++] = buflen;
+ buf[INDEX_TYPE] = msg_type_start_game;
+ buf[INDEX_LEN] = buflen;
+ pos = INDEX_PAYLOAD;
buf[pos++] = players->count;
// copy usernames with length to buffer
for(int i=0; i<usercount; i++)
@@ -164,9 +165,9 @@ static char* server_recv_hello(int sock, uint8_t data_len)
recv(sock, buf, data_len, 0);
- assert(buf[0] == msg_type_hello);
+ assert(buf[INDEX_TYPE] == msg_type_hello);
- namelen = buf[1] - 2;
+ namelen = buf[INDEX_LEN] - 2;
name = malloc(namelen+1);
if(name == NULL)
{
@@ -194,8 +195,8 @@ void* server_recv(int sock, uint8_t wanted)
assert(len != -1);
- type = buf[0];
- data_len = buf[1];
+ type = buf[INDEX_TYPE];
+ data_len = buf[INDEX_LEN];
if(type != wanted)
return NULL;
@@ -218,10 +219,10 @@ 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;
+ buf[INDEX_TYPE] = msg_type_deal_cards;
+ buf[INDEX_LEN] = 2+MAX_HAND_CARDS;
for(int i=0; i<MAX_HAND_CARDS; i++)
- buf[2+i] = h[i];
+ buf[INDEX_PAYLOAD+i] = h[i];
send(sock, buf, 2+MAX_HAND_CARDS, 0);