-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiplayer.c
391 lines (336 loc) · 10.7 KB
/
multiplayer.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "multiplayer.h"
#include "globals.h"
#include "string.h"
#include <ifaddrs.h>
#include "net.h"
#define SIZE_PING_PACKET 16
char _receiveMessage(TCPsocket socket, s_TCPpacket *packet);
void _removeDisconnectedSockets(s_SocketConnection *socketWrapper);
char _computePacket(s_TCPpacket packet, char *message);
void _parsePacket(s_TCPpacket *packet, char *message);
void _checkForClientPing(s_SocketConnection *socketWrapper);
char _ping_server(s_SocketConnection *socketWrapper);
char multiplayer_create_connection(s_SocketConnection *socketWrapper, const char* ip, E_ConnectionType type) {
int success = SDLNet_ResolveHost(&socketWrapper->ipAddress, ip, MULTIPLAYER_PORT);
if (success == -1) {
printf("Failed to open port: %d\n", MULTIPLAYER_PORT);
return 0;
}
if (type == TCP) {
if (ip == 0) {
printf("Open UDP socket server\n");
socketWrapper->pingSocket = SDLNet_UDP_Open(MULTIPLAYER_PORT_SERVER);
}
// listen for new connections on 'port'
socketWrapper->socket = SDLNet_TCP_Open(&socketWrapper->ipAddress);
if (socketWrapper->socket == 0) {
printf("Failed to open port for listening: %d\n", MULTIPLAYER_PORT);
printf("Error: %s\n", SDLNet_GetError());
return 0;
}
}
else if (type == PING) {
if (socketWrapper->pingSocket == NULL) {
printf("Opening port\n");
// Sets our socket with our local port
socketWrapper->pingSocket = SDLNet_UDP_Open(MULTIPLAYER_PORT_CLIENT);
}
if (socketWrapper->pingSocket == NULL) {
printf("Error opening socket client\n");
return 0;
}
printf("Set remote IP %s and port %d\n", ip, MULTIPLAYER_PORT_SERVER);
if (SDLNet_ResolveHost(&socketWrapper->ipAddress, ip, MULTIPLAYER_PORT_SERVER) == -1) {
printf("SDLNet_ResolveHost failed\n");
}
_ping_server(socketWrapper);
}
return 1;
}
char _ping_server(s_SocketConnection *socketWrapper) {
// Allocate memory for the packet
UDPpacket* packet = SDLNet_AllocPacket(SIZE_PING_PACKET);
if (packet == 0) {
printf("SDLNet_AllocPacket failed : %s\n", SDLNet_GetError());
return 0;
}
packet->address = socketWrapper->ipAddress;
struct ifaddrs *ifap;
net_getIPs(&ifap);
char *interface, *address;
net_getNextIP(&ifap, &interface, &address);
memcpy(packet->data, address, SIZE_PING_PACKET);
packet->len = SIZE_PING_PACKET;
// SDLNet_UDP_Send returns number of packets sent. 0 means error
if (SDLNet_UDP_Send(socketWrapper->pingSocket, -1, packet) == 0) {
printf("SDLNet_UDP_Send failed : %s\n", SDLNet_GetError());
SDLNet_FreePacket(packet);
return 0;
}
SDLNet_FreePacket(packet);
return 1;
}
int multiplayer_check_server_pong(s_SocketConnection *socketWrapper) {
UDPpacket* packet = SDLNet_AllocPacket(SIZE_PING_PACKET);
if (SDLNet_UDP_Recv(socketWrapper->pingSocket, packet)) {
printf("Received response from server\n");
return 1;
}
else {
_ping_server(socketWrapper);
return 0;
}
}
void multiplayer_initClient(s_SocketConnection *socketWrapper) {
socketWrapper->nbConnectedSockets = 0;
socketWrapper->socketSet = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(socketWrapper->socketSet, socketWrapper->socket);
socketWrapper->type = CLIENT;
}
void multiplayer_initHost(s_SocketConnection *socketWrapper, int playersNumber) {
socketWrapper->socketSet = SDLNet_AllocSocketSet(playersNumber);
socketWrapper->nbMaxSockets = playersNumber - 1;
socketWrapper->nbConnectedSockets = 0;
long unsigned nmemb = (long unsigned) playersNumber;
socketWrapper->connectedSockets = (TCPsocket *) malloc(nmemb * sizeof(TCPsocket));
socketWrapper->type = SERVER;
}
void multiplayer_accept_client(s_SocketConnection *socketWrapper) {
if (socketWrapper->nbConnectedSockets >= socketWrapper->nbMaxSockets) {
return;
}
_checkForClientPing(socketWrapper);
TCPsocket socket = SDLNet_TCP_Accept(socketWrapper->socket);
if (socket != 0) {
SDLNet_TCP_AddSocket(socketWrapper->socketSet, socket);
socketWrapper->connectedSockets[socketWrapper->nbConnectedSockets] = socket;
socketWrapper->nbConnectedSockets++;
}
}
void _checkForClientPing(s_SocketConnection *socketWrapper) {
UDPpacket *packet = SDLNet_AllocPacket(SIZE_PING_PACKET);
int res = SDLNet_UDP_Recv(socketWrapper->pingSocket, packet);
if (res < 0) {
printf("Error received when trying to receive message\n");
printf("Error: %s\n", SDLNet_GetError());
return;
}
char ip[SIZE_PING_PACKET];
memcpy(ip, packet->data, SIZE_PING_PACKET);
IPaddress ipAddress;
if (SDLNet_ResolveHost(&ipAddress, ip, MULTIPLAYER_PORT_CLIENT) == -1) {
return;
}
UDPpacket *pongPacket = SDLNet_AllocPacket(3);
if (pongPacket == 0) {
printf("SDLNet_AllocPacket failed : %s\n", SDLNet_GetError());
return;
}
pongPacket->address = ipAddress;
memcpy(packet->data, "ok", 3);
packet->len = 3;
if (SDLNet_UDP_Send(socketWrapper->pingSocket, -1, pongPacket) == 0) {
printf("Packet received but could not be sent back\n");
}
}
void multiplayer_reject_clients(s_SocketConnection *socketWrapper, int messageType) {
// Accept the client connection to clear it from the incoming connections list
TCPsocket socket = SDLNet_TCP_Accept(socketWrapper->socket);
if (socket == 0) {
return;
}
// Send a message to the client saying the server is full and to tell the
// client to go away
s_TCPpacket packet;
packet.type = (unsigned char) messageType;
packet.size = 0;
char message[TCP_PACKET_MAX_SIZE];
_computePacket(packet, message);
SDLNet_TCP_Send(socket, message, TCP_PACKET_MAX_SIZE);
// Shutdown, disconnect, and close the socket to the client
SDLNet_TCP_Close(socket);
}
char multiplayer_check_clients(
s_SocketConnection *socketWrapper,
s_TCPpacket *packet,
int *fromIndex,
char removeDisconnected
) {
int numSockets = SDLNet_CheckSockets(socketWrapper->socketSet, 0);
if (numSockets == -1) {
printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
}
else if (numSockets > 0) {
int socket = 0;
while (socket < socketWrapper->nbConnectedSockets) {
// if this socket has nothing to say, skip it
if (!SDLNet_SocketReady(socketWrapper->connectedSockets[socket])) {
++socket;
continue;
}
char responseCode = _receiveMessage(
socketWrapper->connectedSockets[socket],
packet
);
if (responseCode == CONNECTION_LOST) {
multiplayer_close_client(socketWrapper, socket);
if (removeDisconnected) {
--socketWrapper->nbConnectedSockets;
socketWrapper->connectedSockets[socket] = socketWrapper->connectedSockets[
socketWrapper->nbConnectedSockets
];
socketWrapper->connectedSockets[socketWrapper->nbConnectedSockets] = 0;
}
}
else if (responseCode != ERROR) {
*fromIndex = socket + 1;
++socket;
return responseCode;
}
}
}
return OK;
}
void multiplayer_close_client(s_SocketConnection *socketWrapper, int socket) {
SDLNet_TCP_DelSocket(
socketWrapper->socketSet,
socketWrapper->connectedSockets[socket]
);
SDLNet_TCP_Close(socketWrapper->connectedSockets[socket]);
socketWrapper->connectedSockets[socket] = 0;
}
char multiplayer_check_server(s_SocketConnection *socketWrapper, s_TCPpacket *packet) {
int serverActive = SDLNet_CheckSockets(socketWrapper->socketSet, 0);
if (serverActive == -1) {
// an error occured, it can be read in SDLNet_GetError()
return ERROR_CHECK_SERVER;
}
if (serverActive > 0) {
return _receiveMessage(socketWrapper->socket, packet);
}
return OK;
}
void multiplayer_clean(s_SocketConnection *socketWrapper) {
SDLNet_TCP_Close(socketWrapper->socket);
socketWrapper->socket = NULL;
if (socketWrapper->pingSocket) {
SDLNet_UDP_Close(socketWrapper->pingSocket);
socketWrapper->pingSocket = NULL;
}
socketWrapper->socket = 0;
while (socketWrapper->nbConnectedSockets--) {
multiplayer_close_client(
socketWrapper,
socketWrapper->nbConnectedSockets
);
}
socketWrapper->nbConnectedSockets = 0;
if (socketWrapper->connectedSockets != 0) {
free(socketWrapper->connectedSockets);
socketWrapper->connectedSockets = 0;
}
SDLNet_FreeSocketSet(socketWrapper->socketSet);
socketWrapper->socketSet = 0;
}
char multiplayer_is_room_full(s_SocketConnection *socketWrapper) {
return socketWrapper->nbConnectedSockets == socketWrapper->nbMaxSockets;
}
void multiplayer_broadcast(s_SocketConnection *socketWrapper, s_TCPpacket packet) {
int s;
for (s = 0; s < socketWrapper->nbConnectedSockets; ++s) {
multiplayer_send_message(socketWrapper, s, packet);
}
}
void multiplayer_send_message(s_SocketConnection *socketWrapper, int socketIndex, s_TCPpacket packet) {
char message[TCP_PACKET_MAX_SIZE];
if (_computePacket(packet, message) != 0) {
printf("Packet size too large\n");
}
else {
TCPsocket socket;
if (socketIndex == -1) {
socket = socketWrapper->socket;
}
else {
socket = socketWrapper->connectedSockets[socketIndex];
}
if (socket != 0) {
SDLNet_TCP_Send(socket, message, TCP_PACKET_MAX_SIZE);
}
}
}
int multiplayer_get_number_clients(s_SocketConnection *socketWrapper) {
int nb = 0, client;
for (client = 0; client < socketWrapper->nbConnectedSockets; ++client) {
if (socketWrapper->connectedSockets[client]) {
++nb;
}
}
return nb;
}
int multiplayer_get_next_connected_socket_index(s_SocketConnection *socketWrapper, int currentIndex) {
int next = -1, s = currentIndex + 1;
while (next == -1 && s < socketWrapper->nbConnectedSockets) {
if (socketWrapper->connectedSockets[s]) {
next = s;
break;
}
++s;
}
return next;
}
char multiplayer_is_client_connected(s_SocketConnection *socketWrapper, int clientIndex) {
return socketWrapper->connectedSockets[clientIndex] != 0;
}
void _parsePacket(s_TCPpacket *packet, char *message) {
packet->type = (unsigned char) message[0];
packet->size = (unsigned char) message[1];
int current;
for (current = 0; current < TCP_PACKET_DATA_MAX_SIZE; ++current) {
if (current < packet->size) {
packet->data[current] = message[current + 2];
}
else {
packet->data[current] = '\0';
}
}
}
char _computePacket(s_TCPpacket packet, char *message) {
if (packet.size > TCP_PACKET_DATA_MAX_SIZE) {
return -1;
}
// packet.size does not include the header (1 char for the type + 1 char
// for the size)
message[0] = (signed char) packet.type;
message[1] = (signed char) packet.size;
int current;
for (current = 0; current < TCP_PACKET_DATA_MAX_SIZE; ++current) {
if (current < packet.size) {
message[current + 2] = packet.data[current];
}
else {
message[current + 2] = 0;
}
}
return 0;
}
char _receiveMessage(TCPsocket socket, s_TCPpacket *packet) {
char message[TCP_PACKET_MAX_SIZE];
int byteCount = SDLNet_TCP_Recv(socket, message, TCP_PACKET_MAX_SIZE);
if (byteCount < 0) {
// an error occured, it can be read in SDLNet_GetError()
return ERROR;
}
else if (byteCount == 0) {
return CONNECTION_LOST;
}
else {
if (byteCount > TCP_PACKET_MAX_SIZE) {
return TOO_MUCH_DATA_TO_RECEIVE;
}
else {
_parsePacket(packet, message);
return MESSAGE_RECEIVED;
}
}
}