|
@@ -1,698 +0,0 @@
|
1
|
|
-/*!
|
2
|
|
- * \file src/Network.cpp
|
3
|
|
- * \brief Networking Singleton class
|
4
|
|
- *
|
5
|
|
- * \author Mongoose
|
6
|
|
- */
|
7
|
|
-
|
8
|
|
-#include <unistd.h>
|
9
|
|
-#include <signal.h>
|
10
|
|
-#include <string.h>
|
11
|
|
-#include <time.h>
|
12
|
|
-#include <sys/time.h>
|
13
|
|
-#include <errno.h>
|
14
|
|
-#include <strings.h>
|
15
|
|
-#include <stdio.h>
|
16
|
|
-#include <sys/types.h>
|
17
|
|
-#include <sys/socket.h>
|
18
|
|
-#include <netinet/in.h>
|
19
|
|
-#include <netdb.h>
|
20
|
|
-#include <arpa/inet.h>
|
21
|
|
-#include <stdlib.h>
|
22
|
|
-
|
23
|
|
-#include "utils/math.h" // Random Number
|
24
|
|
-#include "Network.h"
|
25
|
|
-
|
26
|
|
-//#define LOCAL_BCAST
|
27
|
|
-#define MAX_CLIENTS 32
|
28
|
|
-
|
29
|
|
-typedef struct client_s {
|
30
|
|
- unsigned int uid;
|
31
|
|
- char active;
|
32
|
|
- unsigned int seq;
|
33
|
|
- unsigned int frameExpected;
|
34
|
|
-} client_t;
|
35
|
|
-
|
36
|
|
-#include <pthread.h>
|
37
|
|
-pthread_t gPThreadId[3];
|
38
|
|
-
|
39
|
|
-unsigned int gUID;
|
40
|
|
-client_t gClients[MAX_CLIENTS];
|
41
|
|
-unsigned int gNumClients = 0;
|
42
|
|
-network_frame_t gPiggyBack;
|
43
|
|
-
|
44
|
|
-
|
45
|
|
-////////////////////////////////////////////////////////////
|
46
|
|
-// Constructors
|
47
|
|
-////////////////////////////////////////////////////////////
|
48
|
|
-
|
49
|
|
-Network *Network::mInstance = 0x0;
|
50
|
|
-
|
51
|
|
-
|
52
|
|
-Network *Network::Instance()
|
53
|
|
-{
|
54
|
|
- if (mInstance == 0x0)
|
55
|
|
- {
|
56
|
|
- mInstance = new Network();
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- return mInstance;
|
60
|
|
-}
|
61
|
|
-
|
62
|
|
-
|
63
|
|
-void killNetworkSingleton()
|
64
|
|
-{
|
65
|
|
- printf("Shutting down Network...\n");
|
66
|
|
-
|
67
|
|
- // Requires public deconstructor
|
68
|
|
- delete Network::Instance();
|
69
|
|
-}
|
70
|
|
-
|
71
|
|
-
|
72
|
|
-Network::Network()
|
73
|
|
-{
|
74
|
|
- strncpy(mRemoteHost, "localhost", REMOTE_HOST_STR_SZ);
|
75
|
|
- memset(mBindHost, 0, BIND_HOST_STR_SZ);
|
76
|
|
- setPort(8080);
|
77
|
|
-
|
78
|
|
- mPiggyBack = true;
|
79
|
|
- mNetworkReliable = true;
|
80
|
|
- mSpawnedClient = false;
|
81
|
|
- mSpawnedServer = false;
|
82
|
|
- mKillClient = false;
|
83
|
|
- mKillServer = false;
|
84
|
|
- mDebug = false;
|
85
|
|
-
|
86
|
|
- gUID = getUID();
|
87
|
|
-
|
88
|
|
- printf("UID %u\n", gUID);
|
89
|
|
-
|
90
|
|
- for (gNumClients = MAX_CLIENTS; gNumClients > 0;)
|
91
|
|
- {
|
92
|
|
- --gNumClients;
|
93
|
|
-
|
94
|
|
- gClients[gNumClients].active = 0;
|
95
|
|
- gClients[gNumClients].uid = 0;
|
96
|
|
- gClients[gNumClients].seq = 0;
|
97
|
|
- }
|
98
|
|
-}
|
99
|
|
-
|
100
|
|
-
|
101
|
|
-Network::~Network()
|
102
|
|
-{
|
103
|
|
- killServerThread();
|
104
|
|
- killClientThread();
|
105
|
|
-}
|
106
|
|
-
|
107
|
|
-
|
108
|
|
-////////////////////////////////////////////////////////////
|
109
|
|
-// Public Accessors
|
110
|
|
-////////////////////////////////////////////////////////////
|
111
|
|
-
|
112
|
|
-network_frame_t &Network::getPiggyBack()
|
113
|
|
-{
|
114
|
|
- return gPiggyBack;
|
115
|
|
-}
|
116
|
|
-
|
117
|
|
-
|
118
|
|
-unsigned int Network::getUID()
|
119
|
|
-{
|
120
|
|
- struct timeval tv;
|
121
|
|
- struct timezone tz;
|
122
|
|
-
|
123
|
|
-
|
124
|
|
- gettimeofday(&tv, &tz);
|
125
|
|
-
|
126
|
|
- srand(tv.tv_usec);
|
127
|
|
-
|
128
|
|
- return ((unsigned int)(tv.tv_sec * helRandomNum(2.0f, 3.3f) -
|
129
|
|
- tv.tv_sec * helRandomNum(1.0f, 2.0f)) +
|
130
|
|
- (unsigned int)(tv.tv_usec * helRandomNum(2.0f, 3.3f) -
|
131
|
|
- tv.tv_usec * helRandomNum(1.0f, 2.0f)) +
|
132
|
|
- (unsigned int)helRandomNum(666.0f, 5000.0f));
|
133
|
|
-}
|
134
|
|
-
|
135
|
|
-
|
136
|
|
-int Network::getPort()
|
137
|
|
-{
|
138
|
|
- return mPort;
|
139
|
|
-}
|
140
|
|
-
|
141
|
|
-
|
142
|
|
-////////////////////////////////////////////////////////////
|
143
|
|
-// Public Mutators
|
144
|
|
-////////////////////////////////////////////////////////////
|
145
|
|
-
|
146
|
|
-void *client_thread(void *v)
|
147
|
|
-{
|
148
|
|
- Network &network = *Network::Instance();
|
149
|
|
- network.runClient();
|
150
|
|
-
|
151
|
|
- return NULL;
|
152
|
|
-}
|
153
|
|
-
|
154
|
|
-
|
155
|
|
-void *server_thread(void *v)
|
156
|
|
-{
|
157
|
|
- Network &network = *Network::Instance();
|
158
|
|
- network.runServer();
|
159
|
|
-
|
160
|
|
- return NULL;
|
161
|
|
-}
|
162
|
|
-
|
163
|
|
-
|
164
|
|
-void Network::setBindHost(char *s)
|
165
|
|
-{
|
166
|
|
- if (!s || !s[0])
|
167
|
|
- return;
|
168
|
|
-
|
169
|
|
- strncpy(mBindHost, s, BIND_HOST_STR_SZ);
|
170
|
|
-}
|
171
|
|
-
|
172
|
|
-
|
173
|
|
-void Network::setRemoteHost(char *s)
|
174
|
|
-{
|
175
|
|
- if (!s || !s[0])
|
176
|
|
- return;
|
177
|
|
-
|
178
|
|
- strncpy(mRemoteHost, s, REMOTE_HOST_STR_SZ);
|
179
|
|
-}
|
180
|
|
-
|
181
|
|
-
|
182
|
|
-void Network::setDebug(bool toggle)
|
183
|
|
-{
|
184
|
|
- mDebug = toggle;
|
185
|
|
-}
|
186
|
|
-
|
187
|
|
-
|
188
|
|
-void Network::setPort(unsigned int port)
|
189
|
|
-{
|
190
|
|
- mPort = port;
|
191
|
|
-}
|
192
|
|
-
|
193
|
|
-
|
194
|
|
-void Network::killServerThread()
|
195
|
|
-{
|
196
|
|
- mKillServer = true;
|
197
|
|
-
|
198
|
|
- // Remember for mutex
|
199
|
|
- // while (mKillServer)
|
200
|
|
- // {
|
201
|
|
- // }
|
202
|
|
-
|
203
|
|
- mSpawnedServer = false;
|
204
|
|
-}
|
205
|
|
-
|
206
|
|
-
|
207
|
|
-void Network::killClientThread()
|
208
|
|
-{
|
209
|
|
- mKillClient = true;
|
210
|
|
-
|
211
|
|
- // Remember for mutex
|
212
|
|
- // while (mKillClient)
|
213
|
|
- // {
|
214
|
|
- // }
|
215
|
|
-
|
216
|
|
- mSpawnedClient = false;
|
217
|
|
-}
|
218
|
|
-
|
219
|
|
-
|
220
|
|
-void Network::spawnServerThread()
|
221
|
|
-{
|
222
|
|
- // For now don't handle shutting down server to start client and vv
|
223
|
|
- if (!mSpawnedServer && !mSpawnedClient)
|
224
|
|
- {
|
225
|
|
- pthread_create(gPThreadId + 0, 0, server_thread, NULL);
|
226
|
|
- mSpawnedServer = true;
|
227
|
|
- }
|
228
|
|
-}
|
229
|
|
-
|
230
|
|
-
|
231
|
|
-void Network::spawnClientThread()
|
232
|
|
-{
|
233
|
|
- // For now don't handle shutting down server to start client and vv
|
234
|
|
- if (!mSpawnedServer && !mSpawnedClient)
|
235
|
|
- {
|
236
|
|
- pthread_create(gPThreadId + 1, 0, client_thread, NULL);
|
237
|
|
- mSpawnedClient = true;
|
238
|
|
- }
|
239
|
|
-}
|
240
|
|
-
|
241
|
|
-
|
242
|
|
-////////////////////////////////////////////////////////////
|
243
|
|
-// Protected Mutators
|
244
|
|
-////////////////////////////////////////////////////////////
|
245
|
|
-
|
246
|
|
-int Network::runServer()
|
247
|
|
-{
|
248
|
|
- unsigned int fsize;
|
249
|
|
- int socket_fd, cc, cip;
|
250
|
|
- struct sockaddr_in s_in, from;
|
251
|
|
- char hostid[64];
|
252
|
|
- network_frame_t f;
|
253
|
|
- unsigned int i;
|
254
|
|
- unsigned int packetsRecieved = 0;
|
255
|
|
-
|
256
|
|
-
|
257
|
|
- socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
258
|
|
-
|
259
|
|
- if (socket_fd < 0)
|
260
|
|
- {
|
261
|
|
- perror("recv_udp:socket");
|
262
|
|
- return -1;
|
263
|
|
- }
|
264
|
|
-
|
265
|
|
- if (mBindHost[0])
|
266
|
|
- {
|
267
|
|
- strncpy(hostid, mBindHost, 64);
|
268
|
|
- }
|
269
|
|
- else
|
270
|
|
- {
|
271
|
|
- if (gethostname(hostid, 64) < 0)
|
272
|
|
- {
|
273
|
|
- perror("Server: recv_udp:gethostname");
|
274
|
|
- return -1;
|
275
|
|
- }
|
276
|
|
-
|
277
|
|
- printf("Server: gethostname returned '%s'\n", hostid);
|
278
|
|
- fflush(stdout);
|
279
|
|
- }
|
280
|
|
-
|
281
|
|
- // Setup for port binding
|
282
|
|
- memset(&s_in, 0, sizeof(s_in));
|
283
|
|
- s_in.sin_family = AF_INET;
|
284
|
|
-#ifdef LOCAL_BCAST
|
285
|
|
- // Replace deprecated gethostbyname() with getaddrinfo()
|
286
|
|
- //struct hostent *hostptr;
|
287
|
|
- //if ((hostptr = gethostbyname(hostid)) == NULL)
|
288
|
|
- //{
|
289
|
|
- // fprintf(stderr, "Server: recv_udp, Invalid host name '%s'\n", hostid);
|
290
|
|
- // return -1;
|
291
|
|
- //}
|
292
|
|
- //memcpy((void *)(&s_in.sin_addr), hostptr->h_addr, hostptr->h_length);
|
293
|
|
- struct addrinfo *result, *res;
|
294
|
|
- bool found = false;
|
295
|
|
- int error = getaddrinfo(hostid, NULL, NULL, &result);
|
296
|
|
- if (error != 0) {
|
297
|
|
- fprintf(stderr, "Server: %s\n", gai_strerror(error));
|
298
|
|
- return -1;
|
299
|
|
- }
|
300
|
|
- for (res = result; res != NULL; res = res->ai_next) {
|
301
|
|
- if (res->ss_family == AF_INET) {
|
302
|
|
- found = true;
|
303
|
|
- memcpy((void *)&s_in, res->ai_addr, res->ai_addrlen);
|
304
|
|
- break; // Found something suitable
|
305
|
|
- }
|
306
|
|
- }
|
307
|
|
- freeaddrinfo(result);
|
308
|
|
- if (!found) {
|
309
|
|
- fprintf(stderr, "Server: Can't bind to %s\n", hostid);
|
310
|
|
- return -1;
|
311
|
|
- }
|
312
|
|
-#else
|
313
|
|
- s_in.sin_addr.s_addr = htonl(INADDR_ANY);
|
314
|
|
-#endif
|
315
|
|
- int port = getPort();
|
316
|
|
- s_in.sin_port = htons(port); // htons new
|
317
|
|
-
|
318
|
|
- fflush(stdout);
|
319
|
|
-
|
320
|
|
- // Bind
|
321
|
|
- while (bind(socket_fd, (struct sockaddr *)&s_in, sizeof(s_in)) < 0)
|
322
|
|
- {
|
323
|
|
- if (s_in.sin_port++ > (port + 10))
|
324
|
|
- {
|
325
|
|
- perror("Server: recv_udp:bind exhausted");
|
326
|
|
- return -1;
|
327
|
|
- }
|
328
|
|
- }
|
329
|
|
-
|
330
|
|
- cip = ntohl(s_in.sin_addr.s_addr);
|
331
|
|
-
|
332
|
|
- printf("Server: Started on ( %i.%i.%i.%i:%i )\n",
|
333
|
|
- cip >> 24, cip << 8 >> 24,
|
334
|
|
- cip << 16 >> 24, cip << 24 >> 24, s_in.sin_port);
|
335
|
|
-
|
336
|
|
- for (; !mKillClient;)
|
337
|
|
- {
|
338
|
|
- fsize = sizeof(from);
|
339
|
|
-
|
340
|
|
- // 1. Wait for event
|
341
|
|
- // 2. Get inbound frame
|
342
|
|
- cc = recvfrom(socket_fd, &f, sizeof(network_frame_t), 0,
|
343
|
|
- (struct sockaddr *)&from, &fsize);
|
344
|
|
-
|
345
|
|
- if (cc < 0)
|
346
|
|
- {
|
347
|
|
- perror("Server: recv_udp:recvfrom");
|
348
|
|
- continue;
|
349
|
|
- }
|
350
|
|
-
|
351
|
|
- ++packetsRecieved;
|
352
|
|
-
|
353
|
|
- if (mDebug)
|
354
|
|
- {
|
355
|
|
- printf("=====================================================\n");
|
356
|
|
- printf("Packet %u\n", packetsRecieved);
|
357
|
|
- printf("Server: Recieved packet from %u\n",
|
358
|
|
- f.uid);
|
359
|
|
- }
|
360
|
|
-
|
361
|
|
- // A. Look and see if this client has connected before
|
362
|
|
- for (i = 0; i < gNumClients; ++i)
|
363
|
|
- {
|
364
|
|
- if (gClients[i].uid == f.uid)
|
365
|
|
- {
|
366
|
|
- break;
|
367
|
|
- }
|
368
|
|
- }
|
369
|
|
-
|
370
|
|
- // B. Collect client data if it's a new connection
|
371
|
|
- if (!gClients[i].active)
|
372
|
|
- {
|
373
|
|
- for (i = 0; i < gNumClients+1; ++i)
|
374
|
|
- {
|
375
|
|
- if ((i + 1) < MAX_CLIENTS && !gClients[i].active)
|
376
|
|
- {
|
377
|
|
- gClients[i].uid = f.uid;
|
378
|
|
- gClients[i].active = 1;
|
379
|
|
- gClients[i].frameExpected = 0;
|
380
|
|
- ++gNumClients;
|
381
|
|
-
|
382
|
|
- printf("Server: %u made connection, as client %u\n",
|
383
|
|
- gClients[i].uid, i);
|
384
|
|
- break;
|
385
|
|
- }
|
386
|
|
- }
|
387
|
|
-
|
388
|
|
- if (i == MAX_CLIENTS || !gClients[i].active)
|
389
|
|
- {
|
390
|
|
- if (mDebug)
|
391
|
|
- {
|
392
|
|
- printf("Server: Handshake packet from %u failed?\n",
|
393
|
|
- f.uid);
|
394
|
|
- }
|
395
|
|
- continue;
|
396
|
|
- }
|
397
|
|
- }
|
398
|
|
-
|
399
|
|
- cip = ntohl(from.sin_addr.s_addr);
|
400
|
|
-
|
401
|
|
- if (mDebug)
|
402
|
|
- {
|
403
|
|
- printf("Server: Client (Famliy %d, Address %i.%i.%i.%i:%d)\n",
|
404
|
|
- ntohs(from.sin_family), cip >> 24, cip << 8 >> 24,
|
405
|
|
- cip << 16 >> 24, cip << 24 >> 24,
|
406
|
|
- ntohs(from.sin_port));
|
407
|
|
-
|
408
|
|
- printf("Server: Datalink layer recieved: packet seq %u\n",
|
409
|
|
- f.seq);
|
410
|
|
- }
|
411
|
|
-
|
412
|
|
- if (mNetworkReliable)
|
413
|
|
- {
|
414
|
|
- if (f.seq == gClients[i].seq)
|
415
|
|
- {
|
416
|
|
- if (mDebug)
|
417
|
|
- {
|
418
|
|
- printf("SERVER> Msg from %u\n", f.uid);
|
419
|
|
- }
|
420
|
|
-
|
421
|
|
- to_network_layer(f.data);
|
422
|
|
- gClients[i].seq = f.seq;
|
423
|
|
- }
|
424
|
|
- else
|
425
|
|
- {
|
426
|
|
- continue;
|
427
|
|
- }
|
428
|
|
- }
|
429
|
|
-
|
430
|
|
- //! \fixme Combine with above, duh
|
431
|
|
- // 3. Send to network layer
|
432
|
|
- if (gClients[i].frameExpected == f.header)
|
433
|
|
- {
|
434
|
|
- f.data.cid = i;
|
435
|
|
-
|
436
|
|
- to_network_layer(f.data);
|
437
|
|
- gClients[i].frameExpected = !gClients[i].frameExpected;
|
438
|
|
- }
|
439
|
|
-
|
440
|
|
- fflush(stdout);
|
441
|
|
-
|
442
|
|
-#ifdef UNIT_TEST
|
443
|
|
- if ((rand() % 10 == 0))
|
444
|
|
- {
|
445
|
|
- printf("Server: Simulating a lost ack %u\n", f.seq);
|
446
|
|
- continue;
|
447
|
|
- }
|
448
|
|
-#endif
|
449
|
|
-
|
450
|
|
- // 4. Send ACK, w/ piggyback if requested
|
451
|
|
- if (mPiggyBack)
|
452
|
|
- {
|
453
|
|
- gPiggyBack.header = 0;
|
454
|
|
- gPiggyBack.seq = f.seq;
|
455
|
|
- gPiggyBack.uid = gUID;
|
456
|
|
-
|
457
|
|
- if (mDebug)
|
458
|
|
- {
|
459
|
|
- printf("SERVER> Sending data by piggyback\n");
|
460
|
|
- }
|
461
|
|
-
|
462
|
|
- cc = sendto(socket_fd, &gPiggyBack, sizeof(gPiggyBack), 0,
|
463
|
|
- (struct sockaddr *)&from, sizeof(from));
|
464
|
|
- }
|
465
|
|
- else
|
466
|
|
- {
|
467
|
|
- f.header = 0;
|
468
|
|
- f.seq = 0;
|
469
|
|
- f.uid = gUID;
|
470
|
|
-
|
471
|
|
- cc = sendto(socket_fd, &f, sizeof(f), 0,
|
472
|
|
- (struct sockaddr *)&from, sizeof(from));
|
473
|
|
- }
|
474
|
|
-
|
475
|
|
- if (cc < 0)
|
476
|
|
- {
|
477
|
|
- perror("Server: send_udp:sendto");
|
478
|
|
- }
|
479
|
|
- else
|
480
|
|
- {
|
481
|
|
- if (mDebug)
|
482
|
|
- {
|
483
|
|
- printf("Server: Ack sent to %u\n", gClients[i].uid);
|
484
|
|
- }
|
485
|
|
- }
|
486
|
|
- }
|
487
|
|
-
|
488
|
|
- mKillClient = false;
|
489
|
|
-
|
490
|
|
- return 0;
|
491
|
|
-}
|
492
|
|
-
|
493
|
|
-
|
494
|
|
-void Network::runClient()
|
495
|
|
-{
|
496
|
|
- unsigned int fsize, last_frame_sent = 0;
|
497
|
|
- int socket_fd, cc, done;
|
498
|
|
- struct sockaddr_in dest;
|
499
|
|
- struct addrinfo *addr;
|
500
|
|
- network_frame_t f;
|
501
|
|
- struct timeval timeout;
|
502
|
|
- fd_set readfds;
|
503
|
|
- unsigned int packetsSent = 0;
|
504
|
|
- unsigned int seq = 0;
|
505
|
|
- char timedOut = 1;
|
506
|
|
-
|
507
|
|
- if (!mRemoteHost || !mRemoteHost[0])
|
508
|
|
- {
|
509
|
|
- return;
|
510
|
|
- }
|
511
|
|
-
|
512
|
|
- memset((char*) &timeout, 0, sizeof(timeout));
|
513
|
|
- timeout.tv_sec = 5;
|
514
|
|
-
|
515
|
|
- socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
516
|
|
-
|
517
|
|
- if (socket_fd == -1)
|
518
|
|
- {
|
519
|
|
- perror("Client: send_udp: socket");
|
520
|
|
- exit(0);
|
521
|
|
- }
|
522
|
|
-
|
523
|
|
- //if ((hostptr = gethostbyname(mRemoteHost)) == NULL)
|
524
|
|
- //{
|
525
|
|
- // fprintf(stderr, "Client: send_udp: invalid host name, %s\n",
|
526
|
|
- // mRemoteHost);
|
527
|
|
- // exit(0);
|
528
|
|
- //}
|
529
|
|
-
|
530
|
|
- int error = getaddrinfo(mRemoteHost, NULL, NULL, &addr);
|
531
|
|
- if (error != 0) {
|
532
|
|
- fprintf(stderr, "Client: %s\n", gai_strerror(error));
|
533
|
|
- exit(0);
|
534
|
|
- }
|
535
|
|
-
|
536
|
|
- // Setup connection
|
537
|
|
- memset(&dest, 0, sizeof(dest));
|
538
|
|
- dest.sin_family = AF_INET;
|
539
|
|
- int port = getPort();
|
540
|
|
- dest.sin_port = htons(port);
|
541
|
|
-#ifdef LOCAL_BCAST
|
542
|
|
- //memcpy(hostptr->h_addr, (char *) &dest.sin_addr, hostptr->h_length);
|
543
|
|
- bool found = false;
|
544
|
|
- for (struct addrinfo *res = addr; res != NULL; res = res->ai_next) {
|
545
|
|
- if (res->ss_family == AF_INET) {
|
546
|
|
- found = true;
|
547
|
|
- memcpy((void *)&dest.sin_addr, res->ai_addr.sin_addr, sizeof(res->ai_addr.sin_addr));
|
548
|
|
- break; // Found something suitable
|
549
|
|
- }
|
550
|
|
- }
|
551
|
|
- freeaddrinfo(addr);
|
552
|
|
- if (!found) {
|
553
|
|
- fprintf(stderr, "Client: Can't connect to %s\n", hostid);
|
554
|
|
- return;
|
555
|
|
- }
|
556
|
|
-#else
|
557
|
|
- if (inet_pton(AF_INET, mRemoteHost, &dest.sin_addr) < 0)
|
558
|
|
- {
|
559
|
|
- perror("inet_pton");
|
560
|
|
- return;
|
561
|
|
- }
|
562
|
|
-#endif
|
563
|
|
-
|
564
|
|
-
|
565
|
|
- // init
|
566
|
|
- f.data.send = 0;
|
567
|
|
- f.seq = 0;
|
568
|
|
-
|
569
|
|
- for (; !mKillServer;)
|
570
|
|
- {
|
571
|
|
- ++packetsSent;
|
572
|
|
-
|
573
|
|
- if (mDebug)
|
574
|
|
- {
|
575
|
|
- printf("=====================================================\n");
|
576
|
|
- printf("Packet %u\n", packetsSent);
|
577
|
|
- }
|
578
|
|
-
|
579
|
|
- // 1. Get packet to send over wire
|
580
|
|
- if (mNetworkReliable && timedOut && f.seq != seq)
|
581
|
|
- {
|
582
|
|
- if (mDebug)
|
583
|
|
- {
|
584
|
|
- printf("Client: Resending packet\n");
|
585
|
|
- }
|
586
|
|
- }
|
587
|
|
- else
|
588
|
|
- {
|
589
|
|
- from_network_layer(&f.data, &last_frame_sent);
|
590
|
|
-
|
591
|
|
- if (!f.data.send)
|
592
|
|
- {
|
593
|
|
- struct timespec tmp;
|
594
|
|
- tmp.tv_sec = 0;
|
595
|
|
- tmp.tv_nsec = 20000;
|
596
|
|
- nanosleep(&tmp, NULL);
|
597
|
|
- continue;
|
598
|
|
- }
|
599
|
|
- }
|
600
|
|
-
|
601
|
|
- // 2. Copy to frame
|
602
|
|
- f.seq = 0;//seq; // 0 forces all packets to check out
|
603
|
|
- f.uid = gUID;
|
604
|
|
-
|
605
|
|
- // 3. Send over the wire
|
606
|
|
- done = 0;
|
607
|
|
- timedOut = 0;
|
608
|
|
-
|
609
|
|
- while (!done)
|
610
|
|
- {
|
611
|
|
- if (mDebug)
|
612
|
|
- {
|
613
|
|
- printf("Client: Sending packet %u\n", f.seq);
|
614
|
|
- }
|
615
|
|
-
|
616
|
|
- cc = sendto(socket_fd, &f, sizeof(f), 0,
|
617
|
|
- (struct sockaddr *)&dest, sizeof(dest));
|
618
|
|
-
|
619
|
|
- if (cc < 0)
|
620
|
|
- {
|
621
|
|
- perror("Client: send_udp:sendto");
|
622
|
|
-
|
623
|
|
- if (errno == EMSGSIZE)
|
624
|
|
- {
|
625
|
|
- printf("Client: packet was too large\n");
|
626
|
|
- }
|
627
|
|
- }
|
628
|
|
- else
|
629
|
|
- {
|
630
|
|
- f.data.send = 0;
|
631
|
|
- }
|
632
|
|
-
|
633
|
|
- // Comment out this to enable more reliable service
|
634
|
|
- done = 1;
|
635
|
|
- }
|
636
|
|
-
|
637
|
|
- // 4. Wait for +ack or resend
|
638
|
|
- FD_ZERO(&readfds);
|
639
|
|
-
|
640
|
|
- // Setup socket to listen on here
|
641
|
|
- FD_SET(socket_fd, &readfds);
|
642
|
|
-
|
643
|
|
- // Set timeout in milliseconds
|
644
|
|
- timeout.tv_usec = 850;
|
645
|
|
-
|
646
|
|
- cc = select(socket_fd + 1, &readfds, NULL, NULL, &timeout);
|
647
|
|
-
|
648
|
|
- if ((cc < 0) && (errno != EINTR))
|
649
|
|
- {
|
650
|
|
- // there was an local error with select
|
651
|
|
- }
|
652
|
|
-
|
653
|
|
- if (cc == 0)
|
654
|
|
- {
|
655
|
|
- if (mDebug)
|
656
|
|
- {
|
657
|
|
- printf("Client: Timeout detected on packet %u\n", f.seq);
|
658
|
|
- }
|
659
|
|
- timedOut = 1;
|
660
|
|
- continue;
|
661
|
|
- }
|
662
|
|
-
|
663
|
|
- // Clear header for recv use
|
664
|
|
- f.header = 0;
|
665
|
|
-
|
666
|
|
- fsize = sizeof(dest);
|
667
|
|
- cc = recvfrom(socket_fd, &f, sizeof(f), 0,
|
668
|
|
- (struct sockaddr *)&dest, &fsize);
|
669
|
|
-
|
670
|
|
- if (cc < 0)
|
671
|
|
- {
|
672
|
|
- perror("Client: recv_udp:recvfrom");
|
673
|
|
- }
|
674
|
|
- else
|
675
|
|
- {
|
676
|
|
- if (mDebug)
|
677
|
|
- {
|
678
|
|
- printf("Client: Datalink layer recieved: packet seq %u\n", f.seq);
|
679
|
|
- printf("CLIENT> Msg from %u\n", f.uid);
|
680
|
|
- }
|
681
|
|
-
|
682
|
|
- to_network_layer(f.data);
|
683
|
|
- }
|
684
|
|
-
|
685
|
|
- if (seq == f.seq)
|
686
|
|
- {
|
687
|
|
- if (mDebug)
|
688
|
|
- {
|
689
|
|
- printf("Client: Recieved ack %u\n", f.seq);
|
690
|
|
- }
|
691
|
|
-
|
692
|
|
- ++seq;
|
693
|
|
- }
|
694
|
|
- }
|
695
|
|
-
|
696
|
|
- mKillServer = false;
|
697
|
|
-}
|
698
|
|
-
|