-
-
Notifications
You must be signed in to change notification settings - Fork 516
/
nettypes.cpp
677 lines (577 loc) · 14.8 KB
/
nettypes.cpp
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
This file is part of Warzone 2100.
Copyright (C) 2007-2012 Warzone 2100 Project
Warzone 2100 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.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file nettypes.c
*
* Contains the 'new' Network API functions for sending and receiving both
* low-level primitives and higher-level types.
*/
#include "lib/framework/wzglobal.h"
#include "lib/framework/string_ext.h"
#include <string.h>
#ifndef WZ_OS_WIN
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif
#include "../framework/frame.h"
#include "netplay.h"
#include "nettypes.h"
#include "netqueue.h"
#include "netlog.h"
#include "src/order.h"
#include <cstring>
/// There is a game queue representing each player. The game queues are synchronised among all players, so that all players process the same game queue
/// messages at the same game time. The game queues should be used, even in single-player. Players should write to their own queue, not to other player's
/// queues, and should read messages from all queues including their own.
static NetQueue *gameQueues[MAX_PLAYERS] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
/// There is a bidirectional net queue for communicating with each client or host. Each queue corresponds either to a real socket, or a virtual socket
/// which routes via the host.
static NetQueuePair *netQueues[MAX_CONNECTED_PLAYERS] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
/// These queues are for clients which just connected, but haven't yet been assigned a player number.
static NetQueuePair *tmpQueues[MAX_TMP_SOCKETS] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
/// Sending a message to the broadcast queue is equivalent to sending the message to the net queues of all other players.
static NetQueue *broadcastQueue = NULL;
// Only used between NETbegin{Encode,Decode} and NETend calls.
static MessageWriter writer; ///< Used when serialising a message.
static MessageReader reader; ///< Used when deserialising a message.
static NetMessage message; ///< A message which is being serialised or deserialised.
static NETQUEUE queueInfo; ///< Indicates which queue is currently being (de)serialised.
static PACKETDIR NetDir; ///< Indicates whether a message is being serialised (PACKET_ENCODE) or deserialised (PACKET_DECODE), or not doing anything (PACKET_INVALID).
static void NETsetPacketDir(PACKETDIR dir)
{
NetDir = dir;
// Can't put STATIC_ASSERT in global scope, arbitrarily putting it here.
STATIC_ASSERT(MAX_PLAYERS == MAX_CONNECTED_PLAYERS); // Things might break if each connected player doesn't correspond to a player of the same index.
}
PACKETDIR NETgetPacketDir()
{
return NetDir;
}
// The queue(q, v) functions (de)serialise the object v to/from q, depending on whether q is a MessageWriter or MessageReader.
template<class Q>
static void queue(const Q &q, uint8_t &v)
{
q.byte(v);
}
template<class Q>
static void queue(const Q &q, uint16_t &v)
{
uint8_t b[2] = {uint8_t(v>>8), uint8_t(v)};
queue(q, b[0]);
queue(q, b[1]);
if (Q::Direction == Q::Read)
{
v = b[0]<<8 | b[1];
}
}
template<class Q>
static void queue(const Q &q, uint32_t &vOrig)
{
if (Q::Direction == Q::Write)
{
uint32_t v = vOrig;
bool moreBytes = true;
for (int n = 0; moreBytes; ++n)
{
uint8_t b;
moreBytes = encode_uint32_t(b, v, n);
queue(q, b);
}
}
else if (Q::Direction == Q::Read)
{
uint32_t v = 0;
bool moreBytes = true;
for (int n = 0; moreBytes; ++n)
{
uint8_t b = 0;
queue(q, b);
moreBytes = decode_uint32_t(b, v, n);
}
vOrig = v;
}
}
template<class Q>
static void queueLarge(const Q &q, uint32_t &v)
{
uint16_t b[2] = {uint16_t(v>>16), uint16_t(v)};
queue(q, b[0]);
queue(q, b[1]);
if (Q::Direction == Q::Read)
{
v = b[0]<<16 | b[1];
}
}
template<class Q>
static void queue(const Q &q, uint64_t &v)
{
uint32_t b[2] = {uint32_t(v>>32), uint32_t(v)};
queue(q, b[0]);
queue(q, b[1]);
if (Q::Direction == Q::Read)
{
v = uint64_t(b[0])<<32 | b[1];
}
}
template<class Q>
static void queue(const Q &q, char &v)
{
uint8_t b = v;
queue(q, b);
if (Q::Direction == Q::Read)
{
v = b;
}
STATIC_ASSERT(sizeof(b) == sizeof(v));
}
template<class Q>
static void queue(const Q &q, int8_t &v)
{
uint8_t b = v;
queue(q, b);
if (Q::Direction == Q::Read)
{
v = b;
}
STATIC_ASSERT(sizeof(b) == sizeof(v));
}
template<class Q>
static void queue(const Q &q, int16_t &v)
{
uint16_t b = v;
queue(q, b);
if (Q::Direction == Q::Read)
{
v = b;
}
STATIC_ASSERT(sizeof(b) == sizeof(v));
}
template<class Q>
static void queue(const Q &q, int32_t &v)
{
// Non-negative values: value*2
// Negative values: -value*2 - 1
// Example: int32_t -5 -4 -3 -2 -1 0 1 2 3 4 5
// becomes uint32_t 9 7 5 3 1 0 2 4 6 8 10
uint32_t b = v<<1 ^ (-((uint32_t)v>>31));
queue(q, b);
if (Q::Direction == Q::Read)
{
v = b>>1 ^ -(b&1);
}
STATIC_ASSERT(sizeof(b) == sizeof(v));
}
template<class Q>
static void queue(const Q &q, int64_t &v)
{
uint64_t b = v;
queue(q, b);
if (Q::Direction == Q::Read)
{
v = b;
}
STATIC_ASSERT(sizeof(b) == sizeof(v));
}
template<class Q>
static void queue(const Q &q, Position &v)
{
queue(q, v.x);
queue(q, v.y);
queue(q, v.z);
}
template<class Q>
static void queue(const Q &q, Rotation &v)
{
queue(q, v.direction);
queue(q, v.pitch);
queue(q, v.roll);
}
template<class Q>
static void queue(const Q &q, Vector2i &v)
{
queue(q, v.x);
queue(q, v.y);
}
template<class Q, class T>
static void queue(const Q &q, std::vector<T> &v)
{
uint32_t len = v.size();
queue(q, len);
switch (Q::Direction)
{
case Q::Write:
for (unsigned i = 0; i != len; ++i)
{
queue(q, v[i]);
}
break;
case Q::Read:
v.clear();
for (unsigned i = 0; i != len && q.valid(); ++i)
{
T tmp;
queue(q, tmp);
v.push_back(tmp);
}
break;
}
}
template<class Q>
static void queue(const Q &q, NetMessage &v)
{
queue(q, v.type);
queue(q, v.data);
}
template<class T>
static void queueAuto(T &v)
{
if (NETgetPacketDir() == PACKET_ENCODE)
{
queue(writer, v);
}
else if (NETgetPacketDir() == PACKET_DECODE)
{
queue(reader, v);
}
}
template<class T>
static void queueAutoLarge(T &v)
{
if (NETgetPacketDir() == PACKET_ENCODE)
{
queueLarge(writer, v);
}
else if (NETgetPacketDir() == PACKET_DECODE)
{
queueLarge(reader, v);
}
}
// Queue selection functions
/// Gets the &NetQueuePair::send or NetQueue *, corresponding to queue.
static NetQueue *sendQueue(NETQUEUE queue)
{
return queue.isPair ? &(*static_cast<NetQueuePair **>(queue.queue))->send : static_cast<NetQueue *>(queue.queue);
}
/// Gets the &NetQueuePair::receive or NetQueue *, corresponding to queue.
static NetQueue *receiveQueue(NETQUEUE queue)
{
return queue.isPair ? &(*static_cast<NetQueuePair **>(queue.queue))->receive : static_cast<NetQueue *>(queue.queue);
}
/// Gets the &NetQueuePair, corresponding to queue.
static NetQueuePair *&pairQueue(NETQUEUE queue)
{
ASSERT(queue.isPair, "Huh?");
return *static_cast<NetQueuePair **>(queue.queue);
}
NETQUEUE NETnetTmpQueue(unsigned tmpPlayer)
{
NETQUEUE ret;
ASSERT(tmpPlayer < MAX_TMP_SOCKETS, "Huh?");
NetQueuePair **queue = &tmpQueues[tmpPlayer];
ret.queue = queue;
ret.isPair = true;
ret.index = tmpPlayer;
ret.queueType = QUEUE_TMP;
return ret;
}
NETQUEUE NETnetQueue(unsigned player)
{
NETQUEUE ret;
if (player == NET_ALL_PLAYERS)
{
return NETbroadcastQueue();
}
ASSERT(player < MAX_CONNECTED_PLAYERS, "Huh?");
NetQueuePair **queue = &netQueues[player];
ret.queue = queue;
ret.isPair = true;
ret.index = player;
ret.queueType = QUEUE_NET;
return ret;
}
NETQUEUE NETgameQueue(unsigned player)
{
NETQUEUE ret;
ASSERT(player < MAX_PLAYERS, "Huh?");
NetQueue *queue = gameQueues[player];
ret.queue = queue;
ret.isPair = false;
ret.index = player;
ret.queueType = QUEUE_GAME;
return ret;
}
NETQUEUE NETbroadcastQueue()
{
NETQUEUE ret;
NetQueue *queue = broadcastQueue;
ret.queue = queue;
ret.isPair = false;
ret.index = NET_ALL_PLAYERS;
ret.queueType = QUEUE_BROADCAST;
return ret;
}
void NETinsertRawData(NETQUEUE queue, uint8_t *data, size_t dataLen)
{
receiveQueue(queue)->writeRawData(data, dataLen);
}
void NETinsertMessageFromNet(NETQUEUE queue, NetMessage const *message)
{
receiveQueue(queue)->pushMessage(*message);
}
bool NETisMessageReady(NETQUEUE queue)
{
return receiveQueue(queue)->haveMessage();
}
NetMessage const *NETgetMessage(NETQUEUE queue)
{
return &receiveQueue(queue)->getMessage();
}
/*
* Begin & End functions
*/
void NETinitQueue(NETQUEUE queue)
{
if (queue.queueType == QUEUE_BROADCAST)
{
delete broadcastQueue;
broadcastQueue = new NetQueue;
broadcastQueue->setWillNeverGetMessages();
return;
}
else if (queue.queueType == QUEUE_GAME)
{
delete gameQueues[queue.index];
gameQueues[queue.index] = new NetQueue;
return;
}
else
{
delete pairQueue(queue);
pairQueue(queue) = new NetQueuePair;
}
}
void NETsetNoSendOverNetwork(NETQUEUE queue)
{
sendQueue(queue)->setWillNeverGetMessagesForNet(); // Will not be sending over net.
}
void NETmoveQueue(NETQUEUE src, NETQUEUE dst)
{
ASSERT(src.isPair, "Huh?");
ASSERT(dst.isPair, "Huh?");
delete pairQueue(dst);
pairQueue(dst) = NULL;
std::swap(pairQueue(src), pairQueue(dst));
}
void NETbeginEncode(NETQUEUE queue, uint8_t type)
{
NETsetPacketDir(PACKET_ENCODE);
queueInfo = queue;
message = type;
writer = MessageWriter(message);
}
void NETbeginDecode(NETQUEUE queue, uint8_t type)
{
NETsetPacketDir(PACKET_DECODE);
queueInfo = queue;
message = receiveQueue(queueInfo)->getMessage();
reader = MessageReader(message);
assert(type == message.type);
}
bool NETend()
{
// If we are encoding just return true
if (NETgetPacketDir() == PACKET_ENCODE)
{
// Push the message onto the list.
NetQueue *queue = sendQueue(queueInfo);
queue->pushMessage(message);
NETlogPacket(message.type, message.data.size(), false);
if (queueInfo.queueType == QUEUE_GAME)
{
ASSERT(message.type > GAME_MIN_TYPE && message.type < GAME_MAX_TYPE, "Inserting %s into game queue.", messageTypeToString(message.type));
}
else
{
ASSERT(message.type > NET_MIN_TYPE && message.type < NET_MAX_TYPE, "Inserting %s into net queue.", messageTypeToString(message.type));
}
if (queueInfo.queueType == QUEUE_NET || queueInfo.queueType == QUEUE_BROADCAST || queueInfo.queueType == QUEUE_TMP)
{
NETsend(queueInfo, &queue->getMessageForNet());
queue->popMessageForNet();
}
// We have ended the serialisation, so mark the direction invalid
NETsetPacketDir(PACKET_INVALID);
return true; // Serialising never fails.
}
if (NETgetPacketDir() == PACKET_DECODE)
{
bool ret = reader.valid();
// We have ended the deserialisation, so mark the direction invalid
NETsetPacketDir(PACKET_INVALID);
return ret;
}
assert(false && false && false);
return false;
}
void NETflushGameQueues()
{
for (uint8_t player = 0; player < MAX_PLAYERS; ++player)
{
NetQueue *queue = gameQueues[player];
if (queue == NULL)
{
continue; // Can't send for this player.
}
uint32_t num = queue->numMessagesForNet();
if (num <= 0)
{
continue; // Nothing to send for this player.
}
// Decoded in NETprocessSystemMessage in netplay.c.
NETbeginEncode(NETbroadcastQueue(), NET_SHARE_GAME_QUEUE);
NETuint8_t(&player);
NETuint32_t(&num);
for (uint32_t n = 0; n < num; ++n)
{
queueAuto(const_cast<NetMessage &>(queue->getMessageForNet())); // const_cast is safe since we are encoding, not decoding.
queue->popMessageForNet();
}
NETend();
}
}
void NETpop(NETQUEUE queue)
{
receiveQueue(queue)->popMessage();
}
void NETint8_t(int8_t *ip)
{
queueAuto(*ip);
}
void NETuint8_t(uint8_t *ip)
{
queueAuto(*ip);
}
void NETint16_t(int16_t *ip)
{
queueAuto(*ip);
}
void NETuint16_t(uint16_t *ip)
{
queueAuto(*ip);
}
void NETint32_t(int32_t *ip)
{
queueAuto(*ip);
}
void NETuint32_t(uint32_t *ip)
{
queueAuto(*ip);
}
void NETuint32_tLarge(uint32_t *ip)
{
queueAutoLarge(*ip);
}
void NETint64_t(int64_t *ip)
{
queueAuto(*ip);
}
void NETuint64_t(uint64_t *ip)
{
queueAuto(*ip);
}
void NETbool(bool *bp)
{
uint8_t i = !!*bp;
queueAuto(i);
*bp = !!i;
}
/** Sends or receives a string to or from the current network package.
* \param str When encoding a packet this is the (NUL-terminated string to
* be sent in the current network package. When decoding this
* is the buffer to decode the string from the network package
* into. When decoding this string is guaranteed to be
* NUL-terminated provided that this buffer is at least 1 byte
* large.
* \param maxlen The buffer size of \c str. For static buffers this means
* sizeof(\c str), for dynamically allocated buffers this is
* whatever number you passed to malloc().
* \note If while decoding \c maxlen is smaller than the actual length of the
* string being decoded, the resulting string (in \c str) will be
* truncated.
*/
void NETstring(char *str, uint16_t maxlen)
{
/*
* Strings sent over the network are prefixed with their length, sent as an
* unsigned 16-bit integer, not including \0 termination.
*/
uint16_t len = NETgetPacketDir() == PACKET_ENCODE ? strnlen1(str, maxlen) - 1 : 0;
queueAuto(len);
// Truncate length if necessary
if (len > maxlen - 1)
{
debug(LOG_ERROR, "NETstring: %s packet, length %u truncated at %u", NETgetPacketDir() == PACKET_ENCODE ? "Encoding" : "Decoding", len, maxlen);
len = maxlen - 1;
}
for (unsigned i = 0; i < len; ++i)
{
queueAuto(str[i]);
}
if (NETgetPacketDir() == PACKET_DECODE)
{
// NUL-terminate
str[len] = '\0';
}
}
void NETstring(char const *str, uint16_t maxlen)
{
ASSERT(NETgetPacketDir() == PACKET_ENCODE, "Writing to const!");
NETstring(const_cast<char *>(str), maxlen);
}
void NETbin(uint8_t *str, uint32_t len)
{
for (unsigned i = 0; i < len; ++i)
{
queueAuto(str[i]);
}
}
void NETPosition(Position *vp)
{
queueAuto(*vp);
}
void NETRotation(Rotation *vp)
{
queueAuto(*vp);
}
void NETVector2i(Vector2i *vp)
{
queueAuto(*vp);
}
void NETnetMessage(NetMessage const **message)
{
if (NETgetPacketDir() == PACKET_ENCODE)
{
queueAuto(*const_cast<NetMessage *>(*message)); // Const cast safe when encoding.
}
if (NETgetPacketDir() == PACKET_DECODE)
{
NetMessage *m = new NetMessage;
queueAuto(*m);
*message = m;
return;
}
}