1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "net.h"
#include "player.h"
/**
* Client side function; Send hello to server
* @param[in] sock Socket to use
* @param[in] username Username of player
*/
void client_hello(int sock, const char* username)
{
uint8_t* buf;
uint8_t namelen = strlen(username);
buf = malloc(namelen+2); // type + len + username
if(buf == NULL)
{
printf("client_hello: Out of memory\n");
exit(EXIT_FAILURE);
}
buf[INDEX_TYPE] = msg_type_hello;
buf[INDEX_LEN] = namelen+2;
memcpy(buf+INDEX_PAYLOAD, username, namelen);
send(sock, buf, namelen+2, 0);
free(buf);
}
/**
* Client side function; connects to specified host:port
* @param[in] host Hostname of server
* @param[in] port Port of server
* @return Socket with open connection to server
*/
int client_connect_server(const char* host, const char* port)
{
int status;
int sock;
struct addrinfo hints, *result, *tmp;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP socket
status = getaddrinfo(host, port, &hints, &result);
if(status != 0)
{
printf("getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
// connect to first result in linked addrinfo list
for(tmp = result; tmp != NULL; tmp = tmp->ai_next)
{
// create socket
sock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol);
if(sock == -1)
continue;
// connect!
if(connect(sock, tmp->ai_addr, tmp->ai_addrlen) != -1)
break; // Success!
close(sock);
}
if(tmp == NULL)
{
printf("failed to connect\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result);
return sock;
}
static player_list* client_recv_player_list(int sock, uint8_t data_len)
{
uint8_t buf[data_len];
player_list* players;
uint32_t pos;
recv(sock, buf, data_len, 0);
assert(buf[INDEX_TYPE] == msg_type_start_game);
players = malloc(sizeof(player_list));
if(players == NULL)
{
printf("client_recv_player_list: Out of memory\n");
exit(EXIT_FAILURE);
}
pos = INDEX_PAYLOAD;
players->count = buf[pos++];
// read usernames from buffer
for(int i=0; i<players->count; i++)
{
uint8_t namelen = buf[pos++];
players->names[i] = malloc(namelen+1);
memcpy(players->names[i], buf+pos, namelen);
players->names[i][namelen] = '\0';
pos += namelen;
}
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[INDEX_TYPE] == msg_type_deal_cards);
for(int i=0; i<MAX_HAND_CARDS; i++)
*h[i] = (card) buf[i+2];
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, uint8_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[INDEX_TYPE];
data_len = buf[INDEX_LEN];
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;
}
|