summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c6
-rw-r--r--src/net.c45
-rw-r--r--src/net.h7
-rw-r--r--src/net_client.c64
-rw-r--r--src/net_server.c50
5 files changed, 69 insertions, 103 deletions
diff --git a/src/main.c b/src/main.c
index 8acf5ec..6e68d35 100644
--- a/src/main.c
+++ b/src/main.c
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
for(int i=0; i<opponents; i++)
{
// wait for greeting from client i and read nick
- char* name = server_recv(csocks[i], msg_type_hello);
+ char* name = net_recv(csocks[i], msg_type_hello);
players.names[i+1] = name;
printf("player connected: len:%d, name:%s\n", strlen(name), name);
}
@@ -75,12 +75,12 @@ int main(int argc, char **argv)
client_hello(sock, nickname);
// retrieve list of all players from server
- players = client_recv(sock, msg_type_start_game);
+ players = net_recv(sock, msg_type_start_game);
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);
+ testhand = net_recv(sock, msg_type_deal_cards);
printf("received cards: ");
for(int i=0; i<MAX_HAND_CARDS; i++)
printf("%d, ", *testhand[i]);
diff --git a/src/net.c b/src/net.c
new file mode 100644
index 0000000..d7222e1
--- /dev/null
+++ b/src/net.c
@@ -0,0 +1,45 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include "net.h"
+
+void* net_recv(int sock, msg_type_t wanted)
+{
+ void* result = NULL;
+ uint8_t peekbuf[2], type, payload_len, *packet, *payload;
+ ssize_t len = recv(sock, peekbuf, 2, MSG_PEEK); // just peek into packet to determine type
+
+ assert(len != -1);
+
+ type = peekbuf[NET_MSG_OFFSET_TYPE];
+ payload_len = peekbuf[NET_MSG_OFFSET_PAYLOAD_LENGTH];
+
+ if(type != wanted)
+ {
+ printf("client_recv: received type %d instead of %d", type, wanted);
+ return NULL;
+ }
+
+ packet = malloc(payload_len+NET_MSG_OFFSET_PAYLOAD);
+ recv(sock, packet, payload_len+NET_MSG_OFFSET_PAYLOAD, 0);
+ payload = &packet[NET_MSG_OFFSET_PAYLOAD];
+
+ switch(type)
+ {
+ case msg_type_hello:
+ result = server_recv_hello(payload, payload_len);
+ break;
+ case msg_type_start_game:
+ result = client_recv_player_list(payload, payload_len);
+ break;
+ case msg_type_deal_cards:
+ result = client_recv_deal_cards(payload, payload_len);
+ break;
+ }
+ free(packet);
+
+ return result;
+}
+
diff --git a/src/net.h b/src/net.h
index 89b0b23..8679d77 100644
--- a/src/net.h
+++ b/src/net.h
@@ -32,17 +32,20 @@ typedef struct
uint8_t payload[];
} msg_t;
+// generic receive function
+void* net_recv(int sock, msg_type_t wanted);
// 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 player_list* players);
void server_deal_cards(int sock, const hand h);
-void* server_recv(int sock, msg_type_t wanted);
+char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len);
// 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, msg_type_t wanted);
+player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len);
+hand* client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len);
#endif // OXEN_NET_H
diff --git a/src/net_client.c b/src/net_client.c
index 031e6af..9134cf7 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -27,7 +27,7 @@ void client_hello(int sock, const char* username)
}
buf[NET_MSG_OFFSET_TYPE] = msg_type_hello;
- buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = namelen+2;
+ buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = namelen;
memcpy(buf+NET_MSG_OFFSET_PAYLOAD, username, namelen);
send(sock, buf, namelen+2, 0);
@@ -83,15 +83,10 @@ int client_connect_server(const char* host, const char* port)
return sock;
}
-static player_list* client_recv_player_list(int sock, uint8_t data_len)
+player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len)
{
- uint8_t buf[data_len];
player_list* players;
- uint32_t pos;
-
- recv(sock, buf, data_len, 0);
-
- assert(buf[NET_MSG_OFFSET_TYPE] == msg_type_start_game);
+ uint32_t pos = 0;
players = create_playerlist();
if(players == NULL)
@@ -100,15 +95,14 @@ static player_list* client_recv_player_list(int sock, uint8_t data_len)
exit(EXIT_FAILURE);
}
- pos = NET_MSG_OFFSET_PAYLOAD;
- players->count = buf[pos++];
+ players->count = payload[pos++];
// read usernames from buffer
for(int i=0; i<players->count; i++)
{
- uint8_t namelen = buf[pos++];
+ uint8_t namelen = payload[pos++];
players->names[i] = malloc(namelen+1);
- memcpy(players->names[i], buf+pos, namelen);
+ memcpy(players->names[i], payload+pos, namelen);
players->names[i][namelen] = '\0';
pos += namelen;
}
@@ -116,55 +110,15 @@ static player_list* client_recv_player_list(int sock, uint8_t data_len)
return players;
}
-static hand* client_recv_deal_cards(int sock, uint8_t data_len)
+hand* client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_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[NET_MSG_OFFSET_TYPE] == msg_type_deal_cards);
+ assert(payload_len == MAX_HAND_CARDS); // deal_cards packet have fixed size
for(int i=0; i<MAX_HAND_CARDS; i++)
- *h[i] = (card) buf[i+2];
+ *h[i] = payload[i];
return h;
}
-/**
- * Client side function; generic function for receiving packets
- * @param[in] sock Socket on which to receive
- * @param[in] wanted Desired packet type
- * @return Pointer to desired data
- */
-void* client_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)
- {
- 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, data_len);
- break;
- case msg_type_deal_cards:
- result = client_recv_deal_cards(sock, data_len);
- break;
- }
-
- return result;
-}
-
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);
}