-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.c
521 lines (365 loc) · 11.6 KB
/
send.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
**
** $Id $
**
** The Lattice (lattice) by remorse@paddylee.com; scalable MMO solutions
** Copyright (C) 2010-2013 Ralph Covelli
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#ifdef __linux__
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#ifdef __MINGW32__
#include <getopt.h>
#include <unistd.h>
#endif
#endif
#include "lattice_config.h"
#include "serversocket.h"
#include "struct.h"
#include "globals.h"
#include "socket.h"
#include "macros.h"
//#include "numerics.h"
#include "lattice_packet.h"
#define IsWritable(fd) ( \
( \
( FD_ISSET((fd), &rtest_set) && !FD_ISSET((fd), &wtest_set) ) || \
( FD_ISSET((fd), &rtest_set) && FD_ISSET((fd), &wtest_set) && FD_ISSET((fd), &wready_set) ) \
) \
)
// -1 on error
int flush_write(server_socket *s, int closing) {
int ret;
if (!s) return(-1);
if (s->socket < 0) return(-1);
if (!FD_ISSET(s->socket, &rtest_set) && !FD_ISSET(s->socket, &wtest_set)) return(-1);
if (s->wlen <= 0) return(-1);
if (!IsWritable(s->socket)) return(0);
//ret = write(s->socket, s->writebuf, s->wlen);
ret = send(s->socket, s->writebuf, s->wlen, 0);
if (ret < 0) {
if (!sock_ignoreError(sock_getError()) && !closing) {
SetFlagClosed(s);
closesock(s);
return(-1);
}
ret=0;
}
if (!ret) return(0);
if (ret < s->wlen) { // partial write! slide the rest over!
memmove(s->writebuf, s->writebuf + ret, s->wlen - ret);
if (!FD_ISSET(s->socket, &wtest_set))
FD_SET(s->socket, &wtest_set);
} else {
if (!s->sendq_head) {
if (FD_ISSET(s->socket, &wtest_set))
FD_CLR(s->socket, &wtest_set);
}
}
s->wlen -= ret;
return(ret);
}
void flushall_write(void) {
int fd;
for (fd = 0; fd <= maxfd; fd++)
if ( IsWritable(fd) )
flush_write(socket_table+fd, 0);
return;
}
// 0 on fail
int sendq_add(server_socket *s, void *data, int length) {
size_t sendq_move_length;
size_t sendq_read_index;
sendq_link *new_sendq_link;
if (!s || !data) return(0);
if (length < 0) return(0);
if (!length) return(1);
sendq_read_index = 0;
// sendq overflow!
if (s->sendq_length + length > s->sendq_max) return(0);
if (!s->sendq_head) {
// no sendq exists!
new_sendq_link = malloc(sizeof(sendq_link));
if (!new_sendq_link) return(0);
new_sendq_link->length = 0;
new_sendq_link->offset = 0;
new_sendq_link->next = NULL;
s->sendq_head = s->sendq_tail = new_sendq_link;
s->sendq_length = 0;
}
while (sendq_read_index < length) {
// check length of input remaining VS length left in this sendq block
sendq_move_length = my_min(length - sendq_read_index,
SENDQ_BLOCK - s->sendq_tail->length);
if (sendq_move_length > 0) {
memcpy(s->sendq_tail->sendq_block + s->sendq_tail->length,
(char *) data + sendq_read_index,
sendq_move_length);
sendq_read_index += sendq_move_length;
s->sendq_tail->length += sendq_move_length;
s->sendq_length += sendq_move_length;
}
if (sendq_read_index < length) {
// we still have more to add in the sendq...we need a new block
new_sendq_link = malloc(sizeof(sendq_link));
if (!new_sendq_link) return(0);
new_sendq_link->length = 0;
new_sendq_link->offset = 0;
new_sendq_link->next = NULL;
s->sendq_tail->next = new_sendq_link;
s->sendq_tail = new_sendq_link;
}
} // End while (sendq_read_index < length)
return(1);
}
// 0 on fail
int sendq_pop(server_socket *s, int length) {
int sendq_pop_index=0;
int sendq_pop_willing;
int sendq_pop_move;
sendq_link *sendq_next;
if (!s) return(0);
// trunc length to sendq length
sendq_pop_willing = my_min(length, s->sendq_length);
while (sendq_pop_index < sendq_pop_willing) {
// length of remaining head sendq block VS how much remains to remove
sendq_pop_move = my_min (
s->sendq_head->length - s->sendq_head->offset,
sendq_pop_willing - sendq_pop_index);
if (sendq_pop_move > 0) {
s->sendq_head->offset += sendq_pop_move;
sendq_pop_index += sendq_pop_move;
s->sendq_length -= sendq_pop_move;
}
if (s->sendq_head->length == s->sendq_head->offset) {
// we pulled everything out of this block... remove it.
sendq_next = s->sendq_head->next;
free(s->sendq_head);
s->sendq_head = sendq_next;
if (!s->sendq_head) s->sendq_tail=NULL;
}
}
return(1);
}
// 0 on fail
//int sendq_popall(server_socket *s)
//
// this can be found in macro.h
// 0 on fail (theres more in the sendq)
// 1 sendq is now empty
int sendq_flush(server_socket *s) {
int sendq_flush_move;
int sendq_flush_index=0;
int sendq_flush_willing;
sendq_link *sendq_next;
if (!s->sendq_head) return(1);
//if (!TestSocketLive(s)) return(1);
//if (TestSocketClosed(s)) return(1);
// trunc write block remaining length to sendq length
sendq_flush_willing = my_min(
WRITE_LENGTH - s->wlen,
s->sendq_length);
while (sendq_flush_index < sendq_flush_willing) {
// length of remaining head sendq block VS how much remains to remove
sendq_flush_move = my_min (
s->sendq_head->length - s->sendq_head->offset,
sendq_flush_willing - sendq_flush_index);
if (sendq_flush_move > 0) {
memcpy(s->writebuf + s->wlen,
s->sendq_head->sendq_block + s->sendq_head->offset,
sendq_flush_move);
s->sendq_head->offset += sendq_flush_move;
s->wlen += sendq_flush_move;
sendq_flush_index += sendq_flush_move;
s->sendq_length -= sendq_flush_move;
}
if (s->sendq_head->length == s->sendq_head->offset) {
// we pulled everything out of this block... remove it.
sendq_next = s->sendq_head->next;
free(s->sendq_head);
s->sendq_head = sendq_next;
if (!s->sendq_head) s->sendq_tail=NULL;
}
}
if (s->sendq_length > 0)
return(0);
else
return(1);
}
// 1 if we closed the socket
int writeto(server_socket *s, const void *buf, size_t count) {
int writeto_move;
if (!s || !buf) return 0;
if (count <= 0) return 0;
// if theres already a sendq we must append...
if (s->sendq_head) {
if (sendq_add(s, (char *) buf, count)) {
if (!FD_ISSET(s->socket, &wtest_set))
FD_SET(s->socket, &wtest_set);
} else {
SetFlagClosed(s);
closesock(s);
return 1;
}
return 0;
}
// length we are attempting to write VS length left in sockets write_block
writeto_move = my_min(count, WRITE_LENGTH - s->wlen);
if (writeto_move > 0) {
memcpy(s->writebuf + s->wlen,
buf,
writeto_move);
s->wlen += writeto_move;
}
if (writeto_move < count) {
if (sendq_add(s, (char *) buf + writeto_move, count - writeto_move)) {
if (!FD_ISSET(s->socket, &wtest_set))
FD_SET(s->socket, &wtest_set);
} else {
SetFlagClosed(s);
closesock(s);
return 1;
}
}
return 0;
}
// -------- sendto stuff
int sendto_one(server_socket *entry, char *format, ...) {
char buf[8192];
int i;
int ret=0;
va_list args;
va_start(args, format);
i=vsnprintf((char *)buf, 8192, format, args);
if (i > 0)
ret=writeto(entry, (char *) buf, i);
va_end(args);
return ret;
}
int sendto_allservers(char *format, ...) {
char buf[8192];
server_socket *s;
int x;
int y;
int z;
int i;
va_list args;
if (!format) return 0;
va_start(args, format);
i=vsnprintf((char *)buf, 8192, format, args);
if (i > 0)
for (x=0;x<3;x++)
for (y=0;y<3;y++)
for (z=0;z<3;z++) {
if ((s=neighbor_table[x][y][z])) {
writeto(s, (char *) buf, i);
}
}
va_end(args);
return(i);
}
int sendto_allservers_butone(server_socket *butme, char *format, ...) {
char buf[8192];
server_socket *s;
int x;
int y;
int z;
int i;
va_list args;
if (!butme || !format) return 0;
va_start(args, format);
i=vsnprintf((char *)buf, 8192, format, args);
if (i > 0)
for (x=0;x<3;x++)
for (y=0;y<3;y++)
for (z=0;z<3;z++) {
if ((s=neighbor_table[x][y][z])) {
if (s != butme)
writeto(s, (char *) buf, i);
}
}
va_end(args);
return(i);
}
// packet header is passed in HOST order
int sendpacket(server_socket *entry, lt_packet *packet) {
uint32_t packet_length;
int ret=0;
if (!entry || !packet) return 0;
packet_length = packet->header.payload_length + sizeof(lt_packet_h);
packet_hton(&packet->header);
ret=writeto(entry, packet, packet_length);
packet_ntoh(&packet->header);
return ret;
}
int sendpacketto_allservers(lt_packet *packet) {
uint32_t packet_length;
server_socket *s;
int x;
int y;
int z;
if (!packet) return 0;
packet_length = packet->header.payload_length + sizeof(lt_packet_h);
packet_hton(&packet->header);
for (x=0;x<3;x++)
for (y=0;y<3;y++)
for (z=0;z<3;z++) {
if ((s=neighbor_table[x][y][z])) {
writeto(s, packet, packet_length);
}
}
packet_ntoh(&packet->header);
return(packet_length);
}
int sendpacketto_allservers_butone(server_socket *butme, lt_packet *packet) {
uint32_t packet_length;
server_socket *s;
int x;
int y;
int z;
if (!butme || !packet) return 0;
packet_length = packet->header.payload_length + sizeof(lt_packet_h);
packet_hton(&packet->header);
for (x=0;x<3;x++)
for (y=0;y<3;y++)
for (z=0;z<3;z++) {
if ((s=neighbor_table[x][y][z])) {
if (s != butme)
writeto(s, packet, packet_length);
}
}
packet_ntoh(&packet->header);
return(packet_length);
}