This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
forked from arves100/ffloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enet.h
5143 lines (3927 loc) · 165 KB
/
enet.h
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ENet reliable UDP networking library
* Copyright (c) 2018 Lee Salzman, Vladyslav Hrytsenko, Dominik Madarász, Stanislav Denisov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ENET_H
#define ENET_H
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#define ENET_VERSION_MAJOR 2
#define ENET_VERSION_MINOR 4
#define ENET_VERSION_PATCH 7
#define ENET_VERSION_CREATE(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch))
#define ENET_VERSION_GET_MAJOR(version) (((version) >> 16) & 0xFF)
#define ENET_VERSION_GET_MINOR(version) (((version) >> 8) & 0xFF)
#define ENET_VERSION_GET_PATCH(version) ((version) & 0xFF)
#define ENET_VERSION ENET_VERSION_CREATE(ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH)
#define ENET_TIME_OVERFLOW 86400000
#define ENET_TIME_LESS(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW)
#define ENET_TIME_GREATER(a, b) ((b) - (a) >= ENET_TIME_OVERFLOW)
#define ENET_TIME_LESS_EQUAL(a, b) (!ENET_TIME_GREATER(a, b))
#define ENET_TIME_GREATER_EQUAL(a, b) (!ENET_TIME_LESS(a, b))
#define ENET_TIME_DIFFERENCE(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW ? (b) - (a) : (a) - (b))
#define ENET_MAX(x, y) ((x) > (y) ? (x) : (y))
#define ENET_MIN(x, y) ((x) < (y) ? (x) : (y))
/*
=======================================================================
System differences
=======================================================================
*/
#ifdef _WIN32
#ifndef ENET_NO_PRAGMA_LINK
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#endif
#if _MSC_VER >= 1910
/* It looks like there were changes as of Visual Studio 2017 and there are no 32/64 bit
versions of _InterlockedExchange[operation], only InterlockedExchange[operation]
(without leading underscore), so we have to distinguish between compiler versions */
#define ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mmsystem.h>
#include <intrin.h>
#if defined(_WIN32) && defined(_MSC_VER)
#if _MSC_VER < 1900
typedef struct timespec {
long tv_sec;
long tv_nsec;
};
#endif
#define CLOCK_MONOTONIC 0
#endif
typedef SOCKET ENetSocket;
#define ENET_SOCKET_NULL INVALID_SOCKET
typedef struct {
size_t dataLength;
void* data;
} ENetBuffer;
#define ENET_CALLBACK __cdecl
#ifdef ENET_DLL
#ifdef ENET_IMPLEMENTATION
#define ENET_API __declspec(dllexport)
#else
#define ENET_API __declspec(dllimport)
#endif
#else
#define ENET_API extern
#endif
#else
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <poll.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef __APPLE__
#include <mach/clock.h>
#include <mach/mach.h>
#include <Availability.h>
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#ifdef MSG_MAXIOVLEN
#define ENET_BUFFER_MAXIMUM MSG_MAXIOVLEN
#endif
typedef int ENetSocket;
#define ENET_SOCKET_NULL -1
typedef struct {
void* data;
size_t dataLength;
} ENetBuffer;
#define ENET_CALLBACK
#define ENET_API extern
#endif
#ifndef ENET_BUFFER_MAXIMUM
#define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
#endif
#define ENET_HOST_ANY in6addr_any
#define ENET_PORT_ANY 0
#define ENET_HOST_SIZE 1025
#define ENET_HOST_TO_NET_16(value) (htons(value))
#define ENET_HOST_TO_NET_32(value) (htonl(value))
#define ENET_NET_TO_HOST_16(value) (ntohs(value))
#define ENET_NET_TO_HOST_32(value) (ntohl(value))
#if __BIG_ENDIAN__
#define ENET_HOST_TO_NET_64(x) (x)
#define ENET_NET_TO_HOST_64(x) (x)
#else
#define ENET_HOST_TO_NET_64(x) (((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ENET_NET_TO_HOST_64(x) (((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
=======================================================================
Internals
=======================================================================
*/
typedef uint32_t ENetVersion;
typedef fd_set ENetSocketSet;
typedef struct _ENetCallbacks {
void* (ENET_CALLBACK* malloc)(size_t size);
void (ENET_CALLBACK* free)(void* memory);
void (ENET_CALLBACK* noMemory)(void);
} ENetCallbacks;
extern void* enet_malloc(size_t);
extern void enet_free(void*);
typedef struct _ENetListNode {
struct _ENetListNode* next;
struct _ENetListNode* previous;
} ENetListNode;
typedef ENetListNode* ENetListIterator;
typedef struct _ENetList {
ENetListNode sentinel;
} ENetList;
extern ENetListIterator enet_list_insert(ENetListIterator, void*);
extern ENetListIterator enet_list_move(ENetListIterator, void*, void*);
extern void* enet_list_remove(ENetListIterator);
extern void enet_list_clear(ENetList*);
extern size_t enet_list_size(ENetList*);
#define enet_list_begin(list) ((list)->sentinel.next)
#define enet_list_end(list) (&(list)->sentinel)
#define enet_list_empty(list) (enet_list_begin(list) == enet_list_end(list))
#define enet_list_next(iterator) ((iterator)->next)
#define enet_list_previous(iterator) ((iterator)->previous)
#define enet_list_front(list) ((void*)(list)->sentinel.next)
#define enet_list_back(list) ((void*)(list)->sentinel.previous)
#define enet_in6_equal(a, b) (memcmp(&a, &b, sizeof(struct in6_addr)) == 0)
/*
=======================================================================
Protocol
=======================================================================
*/
enum {
ENET_PROTOCOL_MINIMUM_MTU = 576,
ENET_PROTOCOL_MAXIMUM_MTU = 4096,
ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32,
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE = 4096,
ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE = 65536,
ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 1,
ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 255,
ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xFFF,
ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT = 1024 * 1024
};
typedef enum _ENetProtocolCommand {
ENET_PROTOCOL_COMMAND_NONE = 0,
ENET_PROTOCOL_COMMAND_ACKNOWLEDGE = 1,
ENET_PROTOCOL_COMMAND_CONNECT = 2,
ENET_PROTOCOL_COMMAND_VERIFY_CONNECT = 3,
ENET_PROTOCOL_COMMAND_DISCONNECT = 4,
ENET_PROTOCOL_COMMAND_PING = 5,
ENET_PROTOCOL_COMMAND_SEND_RELIABLE = 6,
ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE = 7,
ENET_PROTOCOL_COMMAND_SEND_FRAGMENT = 8,
ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED = 9,
ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT = 10,
ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE = 11,
ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT = 12,
ENET_PROTOCOL_COMMAND_COUNT = 13,
ENET_PROTOCOL_COMMAND_MASK = 0x0F
} ENetProtocolCommand;
typedef enum _ENetProtocolFlag {
ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE = (1 << 7),
ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED = (1 << 6),
ENET_PROTOCOL_HEADER_FLAG_SENT_TIME = (1 << 14),
ENET_PROTOCOL_HEADER_FLAG_MASK = ENET_PROTOCOL_HEADER_FLAG_SENT_TIME,
ENET_PROTOCOL_HEADER_SESSION_MASK = (3 << 12),
ENET_PROTOCOL_HEADER_SESSION_SHIFT = 12
} ENetProtocolFlag;
#ifdef _MSC_VER
#pragma pack(push, 1)
#define ENET_PACKED
#elif defined(__GNUC__) || defined(__clang__)
#define ENET_PACKED __attribute__ ((packed))
#else
#define ENET_PACKED
#endif
typedef struct _ENetProtocolHeader {
uint16_t peerID;
uint16_t sentTime;
} ENET_PACKED ENetProtocolHeader;
typedef struct _ENetProtocolCommandHeader {
uint8_t command;
uint8_t channelID;
uint16_t reliableSequenceNumber;
} ENET_PACKED ENetProtocolCommandHeader;
typedef struct _ENetProtocolAcknowledge {
ENetProtocolCommandHeader header;
uint16_t receivedReliableSequenceNumber;
uint16_t receivedSentTime;
} ENET_PACKED ENetProtocolAcknowledge;
typedef struct _ENetProtocolConnect {
ENetProtocolCommandHeader header;
uint16_t outgoingPeerID;
uint8_t incomingSessionID;
uint8_t outgoingSessionID;
uint32_t mtu;
uint32_t windowSize;
uint32_t channelCount;
uint32_t incomingBandwidth;
uint32_t outgoingBandwidth;
uint32_t packetThrottleInterval;
uint32_t packetThrottleAcceleration;
uint32_t packetThrottleDeceleration;
uint32_t connectID;
uint32_t data;
} ENET_PACKED ENetProtocolConnect;
typedef struct _ENetProtocolVerifyConnect {
ENetProtocolCommandHeader header;
uint16_t outgoingPeerID;
uint8_t incomingSessionID;
uint8_t outgoingSessionID;
uint32_t mtu;
uint32_t windowSize;
uint32_t channelCount;
uint32_t incomingBandwidth;
uint32_t outgoingBandwidth;
uint32_t packetThrottleInterval;
uint32_t packetThrottleAcceleration;
uint32_t packetThrottleDeceleration;
uint32_t connectID;
} ENET_PACKED ENetProtocolVerifyConnect;
typedef struct _ENetProtocolBandwidthLimit {
ENetProtocolCommandHeader header;
uint32_t incomingBandwidth;
uint32_t outgoingBandwidth;
} ENET_PACKED ENetProtocolBandwidthLimit;
typedef struct _ENetProtocolThrottleConfigure {
ENetProtocolCommandHeader header;
uint32_t packetThrottleInterval;
uint32_t packetThrottleAcceleration;
uint32_t packetThrottleDeceleration;
} ENET_PACKED ENetProtocolThrottleConfigure;
typedef struct _ENetProtocolDisconnect {
ENetProtocolCommandHeader header;
uint32_t data;
} ENET_PACKED ENetProtocolDisconnect;
typedef struct _ENetProtocolPing {
ENetProtocolCommandHeader header;
} ENET_PACKED ENetProtocolPing;
typedef struct _ENetProtocolSendReliable {
ENetProtocolCommandHeader header;
uint16_t dataLength;
} ENET_PACKED ENetProtocolSendReliable;
typedef struct _ENetProtocolSendUnreliable {
ENetProtocolCommandHeader header;
uint16_t unreliableSequenceNumber;
uint16_t dataLength;
} ENET_PACKED ENetProtocolSendUnreliable;
typedef struct _ENetProtocolSendUnsequenced {
ENetProtocolCommandHeader header;
uint16_t unsequencedGroup;
uint16_t dataLength;
} ENET_PACKED ENetProtocolSendUnsequenced;
typedef struct _ENetProtocolSendFragment {
ENetProtocolCommandHeader header;
uint16_t startSequenceNumber;
uint16_t dataLength;
uint32_t fragmentCount;
uint32_t fragmentNumber;
uint32_t totalLength;
uint32_t fragmentOffset;
} ENET_PACKED ENetProtocolSendFragment;
typedef union _ENetProtocol {
ENetProtocolCommandHeader header;
ENetProtocolAcknowledge acknowledge;
ENetProtocolConnect connect;
ENetProtocolVerifyConnect verifyConnect;
ENetProtocolDisconnect disconnect;
ENetProtocolPing ping;
ENetProtocolSendReliable sendReliable;
ENetProtocolSendUnreliable sendUnreliable;
ENetProtocolSendUnsequenced sendUnsequenced;
ENetProtocolSendFragment sendFragment;
ENetProtocolBandwidthLimit bandwidthLimit;
ENetProtocolThrottleConfigure throttleConfigure;
} ENET_PACKED ENetProtocol;
#ifdef _MSC_VER
#pragma pack(pop)
#endif
/*
=======================================================================
General structs/enums
=======================================================================
*/
typedef enum _ENetSocketType {
ENET_SOCKET_TYPE_STREAM = 1,
ENET_SOCKET_TYPE_DATAGRAM = 2
} ENetSocketType;
typedef enum _ENetSocketWait {
ENET_SOCKET_WAIT_NONE = 0,
ENET_SOCKET_WAIT_SEND = (1 << 0),
ENET_SOCKET_WAIT_RECEIVE = (1 << 1),
ENET_SOCKET_WAIT_INTERRUPT = (1 << 2)
} ENetSocketWait;
typedef enum _ENetSocketOption {
ENET_SOCKOPT_NONBLOCK = 1,
ENET_SOCKOPT_BROADCAST = 2,
ENET_SOCKOPT_RCVBUF = 3,
ENET_SOCKOPT_SNDBUF = 4,
ENET_SOCKOPT_REUSEADDR = 5,
ENET_SOCKOPT_RCVTIMEO = 6,
ENET_SOCKOPT_SNDTIMEO = 7,
ENET_SOCKOPT_ERROR = 8,
ENET_SOCKOPT_NODELAY = 9,
ENET_SOCKOPT_IPV6_V6ONLY = 10
} ENetSocketOption;
typedef enum _ENetSocketShutdown {
ENET_SOCKET_SHUTDOWN_READ = 0,
ENET_SOCKET_SHUTDOWN_WRITE = 1,
ENET_SOCKET_SHUTDOWN_READ_WRITE = 2
} ENetSocketShutdown;
typedef struct _ENetAddress {
union {
struct in6_addr ipv6;
struct {
uint8_t zeros[10];
uint16_t ffff;
struct in_addr ip;
} ipv4;
};
uint16_t port;
} ENetAddress;
typedef enum _ENetPacketFlag {
ENET_PACKET_FLAG_NONE = 0,
ENET_PACKET_FLAG_RELIABLE = (1 << 0),
ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1),
ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2),
ENET_PACKET_FLAG_UNRELIABLE_FRAGMENTED = (1 << 3),
ENET_PACKET_FLAG_INSTANT = (1 << 4),
ENET_PACKET_FLAG_UNTHROTTLED = (1 << 5),
ENET_PACKET_FLAG_SENT = (1 << 8)
} ENetPacketFlag;
typedef void (ENET_CALLBACK* ENetPacketFreeCallback)(void*);
typedef struct _ENetPacket {
uint32_t flags;
uint32_t dataLength;
uint8_t* data;
ENetPacketFreeCallback freeCallback;
uint32_t referenceCount;
void* userData;
} ENetPacket;
typedef struct _ENetAcknowledgement {
ENetListNode acknowledgementList;
uint32_t sentTime;
ENetProtocol command;
} ENetAcknowledgement;
typedef struct _ENetOutgoingCommand {
ENetListNode outgoingCommandList;
uint16_t reliableSequenceNumber;
uint16_t unreliableSequenceNumber;
uint32_t sentTime;
uint32_t roundTripTimeout;
uint32_t roundTripTimeoutLimit;
uint32_t fragmentOffset;
uint16_t fragmentLength;
uint16_t sendAttempts;
ENetProtocol command;
ENetPacket* packet;
} ENetOutgoingCommand;
typedef struct _ENetIncomingCommand {
ENetListNode incomingCommandList;
uint16_t reliableSequenceNumber;
uint16_t unreliableSequenceNumber;
ENetProtocol command;
uint32_t fragmentCount;
uint32_t fragmentsRemaining;
uint32_t* fragments;
ENetPacket* packet;
} ENetIncomingCommand;
typedef enum _ENetPeerState {
ENET_PEER_STATE_DISCONNECTED = 0,
ENET_PEER_STATE_CONNECTING = 1,
ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2,
ENET_PEER_STATE_CONNECTION_PENDING = 3,
ENET_PEER_STATE_CONNECTION_SUCCEEDED = 4,
ENET_PEER_STATE_CONNECTED = 5,
ENET_PEER_STATE_DISCONNECT_LATER = 6,
ENET_PEER_STATE_DISCONNECTING = 7,
ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 8,
ENET_PEER_STATE_ZOMBIE = 9
} ENetPeerState;
enum {
ENET_HOST_BUFFER_SIZE_MIN = 256 * 1024,
ENET_HOST_BUFFER_SIZE_MAX = 1024 * 1024,
ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000,
ENET_HOST_DEFAULT_MTU = 1280,
ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE = 32 * 1024 * 1024,
ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA = 32 * 1024 * 1024,
ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 1,
ENET_PEER_DEFAULT_PACKET_THROTTLE = 32,
ENET_PEER_PACKET_THROTTLE_THRESHOLD = 40,
ENET_PEER_PACKET_THROTTLE_SCALE = 32,
ENET_PEER_PACKET_THROTTLE_COUNTER = 7,
ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2,
ENET_PEER_PACKET_THROTTLE_DECELERATION = 2,
ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000,
ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
ENET_PEER_TIMEOUT_LIMIT = 32,
ENET_PEER_TIMEOUT_MINIMUM = 5000,
ENET_PEER_TIMEOUT_MAXIMUM = 30000,
ENET_PEER_PING_INTERVAL = 250,
ENET_PEER_UNSEQUENCED_WINDOWS = 64,
ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024,
ENET_PEER_FREE_UNSEQUENCED_WINDOWS = 32,
ENET_PEER_RELIABLE_WINDOWS = 16,
ENET_PEER_RELIABLE_WINDOW_SIZE = 0x1000,
ENET_PEER_FREE_RELIABLE_WINDOWS = 8
};
typedef struct _ENetChannel {
uint16_t outgoingReliableSequenceNumber;
uint16_t outgoingUnreliableSequenceNumber;
uint16_t usedReliableWindows;
uint16_t reliableWindows[ENET_PEER_RELIABLE_WINDOWS];
uint16_t incomingReliableSequenceNumber;
uint16_t incomingUnreliableSequenceNumber;
ENetList incomingReliableCommands;
ENetList incomingUnreliableCommands;
} ENetChannel;
typedef struct _ENetPeer {
ENetListNode dispatchList;
struct _ENetHost* host;
uint16_t outgoingPeerID;
uint16_t incomingPeerID;
uint32_t connectID;
uint8_t outgoingSessionID;
uint8_t incomingSessionID;
ENetAddress address;
void* data;
ENetPeerState state;
ENetChannel* channels;
size_t channelCount;
uint32_t incomingBandwidth;
uint32_t outgoingBandwidth;
uint32_t incomingBandwidthThrottleEpoch;
uint32_t outgoingBandwidthThrottleEpoch;
uint32_t incomingDataTotal;
uint64_t totalDataReceived;
uint32_t outgoingDataTotal;
uint64_t totalDataSent;
uint32_t lastSendTime;
uint32_t lastReceiveTime;
uint32_t nextTimeout;
uint32_t earliestTimeout;
uint64_t totalPacketsSent;
uint64_t totalPacketsLost;
uint32_t packetThrottle;
uint32_t packetThrottleThreshold;
uint32_t packetThrottleLimit;
uint32_t packetThrottleCounter;
uint32_t packetThrottleEpoch;
uint32_t packetThrottleAcceleration;
uint32_t packetThrottleDeceleration;
uint32_t packetThrottleInterval;
uint32_t pingInterval;
uint32_t timeoutLimit;
uint32_t timeoutMinimum;
uint32_t timeoutMaximum;
uint32_t lastRoundTripTime;
uint32_t lowestRoundTripTime;
uint32_t lastRoundTripTimeVariance;
uint32_t highestRoundTripTimeVariance;
uint32_t roundTripTime;
uint32_t roundTripTimeVariance;
uint32_t mtu;
uint32_t windowSize;
uint32_t reliableDataInTransit;
uint16_t outgoingReliableSequenceNumber;
ENetList acknowledgements;
ENetList sentReliableCommands;
ENetList sentUnreliableCommands;
ENetList outgoingCommands;
ENetList dispatchedCommands;
int needsDispatch;
uint16_t incomingUnsequencedGroup;
uint16_t outgoingUnsequencedGroup;
uint32_t unsequencedWindow[ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
uint32_t eventData;
size_t totalWaitingData;
} ENetPeer;
typedef enum _ENetEventType {
ENET_EVENT_TYPE_NONE = 0,
ENET_EVENT_TYPE_CONNECT = 1,
ENET_EVENT_TYPE_DISCONNECT = 2,
ENET_EVENT_TYPE_RECEIVE = 3,
ENET_EVENT_TYPE_DISCONNECT_TIMEOUT = 4
} ENetEventType;
typedef struct _ENetEvent {
ENetEventType type;
ENetPeer* peer;
uint8_t channelID;
uint32_t data;
ENetPacket* packet;
} ENetEvent;
typedef uint64_t(ENET_CALLBACK* ENetChecksumCallback)(const ENetBuffer* buffers, int bufferCount);
typedef int (ENET_CALLBACK* ENetInterceptCallback)(ENetEvent* event, ENetAddress* address, uint8_t* receivedData, int receivedDataLength);
typedef struct _ENetHost {
ENetSocket socket;
ENetAddress address;
uint32_t incomingBandwidth;
uint32_t outgoingBandwidth;
uint32_t bandwidthThrottleEpoch;
uint32_t mtu;
uint32_t randomSeed;
int recalculateBandwidthLimits;
uint8_t preventConnections;
ENetPeer* peers;
size_t peerCount;
size_t channelLimit;
uint32_t serviceTime;
ENetList dispatchQueue;
int continueSending;
size_t packetSize;
uint16_t headerFlags;
uint32_t totalSentData;
uint32_t totalSentPackets;
uint32_t totalReceivedData;
uint32_t totalReceivedPackets;
ENetProtocol commands[ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
size_t commandCount;
ENetBuffer buffers[ENET_BUFFER_MAXIMUM];
size_t bufferCount;
ENetChecksumCallback checksumCallback;
uint8_t packetData[2][ENET_PROTOCOL_MAXIMUM_MTU];
ENetAddress receivedAddress;
uint8_t* receivedData;
size_t receivedDataLength;
ENetInterceptCallback interceptCallback;
size_t connectedPeers;
size_t bandwidthLimitedPeers;
size_t duplicatePeers;
size_t maximumPacketSize;
size_t maximumWaitingData;
} ENetHost;
/*
=======================================================================
Public API
=======================================================================
*/
ENET_API int enet_initialize(void);
ENET_API int enet_initialize_with_callbacks(ENetVersion, const ENetCallbacks*);
ENET_API void enet_deinitialize(void);
ENET_API ENetVersion enet_linked_version(void);
ENET_API int enet_array_is_zeroed(const uint8_t*, int);
ENET_API uint32_t enet_time_get(void);
ENET_API uint64_t enet_crc64(const ENetBuffer*, int);
ENET_API ENetPacket* enet_packet_create(const void*, size_t, uint32_t);
ENET_API ENetPacket* enet_packet_create_offset(const void*, size_t, size_t, uint32_t);
ENET_API void enet_packet_destroy(ENetPacket*);
ENET_API int enet_peer_send(ENetPeer*, uint8_t, ENetPacket*);
ENET_API ENetPacket* enet_peer_receive(ENetPeer*, uint8_t*);
ENET_API void enet_peer_ping(ENetPeer*);
ENET_API void enet_peer_ping_interval(ENetPeer*, uint32_t);
ENET_API void enet_peer_timeout(ENetPeer*, uint32_t, uint32_t, uint32_t);
ENET_API void enet_peer_reset(ENetPeer*);
ENET_API void enet_peer_disconnect(ENetPeer*, uint32_t);
ENET_API void enet_peer_disconnect_now(ENetPeer*, uint32_t);
ENET_API void enet_peer_disconnect_later(ENetPeer*, uint32_t);
ENET_API void enet_peer_throttle_configure(ENetPeer*, uint32_t, uint32_t, uint32_t, uint32_t);
ENET_API ENetHost* enet_host_create(const ENetAddress*, size_t, size_t, uint32_t, uint32_t, int);
ENET_API void enet_host_destroy(ENetHost*);
ENET_API void enet_host_prevent_connections(ENetHost*, uint8_t);
ENET_API ENetPeer* enet_host_connect(ENetHost*, const ENetAddress*, size_t, uint32_t);
ENET_API int enet_host_check_events(ENetHost*, ENetEvent*);
ENET_API int enet_host_service(ENetHost*, ENetEvent*, uint32_t);
ENET_API void enet_host_flush(ENetHost*);
ENET_API void enet_host_broadcast(ENetHost*, uint8_t, ENetPacket*);
ENET_API void enet_host_broadcast_exclude(ENetHost*, uint8_t, ENetPacket*, ENetPeer*);
ENET_API void enet_host_broadcast_selective(ENetHost*, uint8_t, ENetPacket*, ENetPeer**, size_t);
ENET_API void enet_host_channel_limit(ENetHost*, size_t);
ENET_API void enet_host_bandwidth_limit(ENetHost*, uint32_t, uint32_t);
ENET_API int enet_address_set_ip(ENetAddress*, const char*);
ENET_API int enet_address_set_hostname(ENetAddress*, const char*);
ENET_API int enet_address_get_ip(const ENetAddress*, char*, size_t);
ENET_API int enet_address_get_hostname(const ENetAddress*, char*, size_t);
ENET_API ENetSocket enet_socket_create(ENetSocketType);
ENET_API int enet_socket_bind(ENetSocket, const ENetAddress*);
ENET_API int enet_socket_get_address(ENetSocket, ENetAddress*);
ENET_API int enet_socket_listen(ENetSocket, int);
ENET_API ENetSocket enet_socket_accept(ENetSocket, ENetAddress*);
ENET_API int enet_socket_connect(ENetSocket, const ENetAddress*);
ENET_API int enet_socket_send(ENetSocket, const ENetAddress*, const ENetBuffer*, size_t);
ENET_API int enet_socket_receive(ENetSocket, ENetAddress*, ENetBuffer*, size_t);
ENET_API int enet_socket_wait(ENetSocket, uint32_t*, uint64_t);
ENET_API int enet_socket_set_option(ENetSocket, ENetSocketOption, int);
ENET_API int enet_socket_get_option(ENetSocket, ENetSocketOption, int*);
ENET_API int enet_socket_shutdown(ENetSocket, ENetSocketShutdown);
ENET_API void enet_socket_destroy(ENetSocket);
ENET_API int enet_socket_set_select(ENetSocket, ENetSocketSet*, ENetSocketSet*, uint32_t);
/* Extended API for easier binding in other programming languages */
ENET_API void* enet_packet_get_data(const ENetPacket*);
ENET_API void* enet_packet_get_user_data(const ENetPacket*);
ENET_API void enet_packet_set_user_data(ENetPacket*, void* userData);
ENET_API int enet_packet_get_length(const ENetPacket*);
ENET_API void enet_packet_set_free_callback(ENetPacket*, ENetPacketFreeCallback);
ENET_API int enet_packet_check_references(const ENetPacket*);
ENET_API void enet_packet_dispose(ENetPacket*);
ENET_API uint32_t enet_host_get_peers_count(const ENetHost*);
ENET_API uint32_t enet_host_get_packets_sent(const ENetHost*);
ENET_API uint32_t enet_host_get_packets_received(const ENetHost*);
ENET_API uint32_t enet_host_get_bytes_sent(const ENetHost*);
ENET_API uint32_t enet_host_get_bytes_received(const ENetHost*);
ENET_API void enet_host_set_max_duplicate_peers(ENetHost*, uint16_t);
ENET_API void enet_host_set_intercept_callback(ENetHost*, ENetInterceptCallback);
ENET_API void enet_host_set_checksum_callback(ENetHost*, ENetChecksumCallback);
ENET_API uint32_t enet_peer_get_id(const ENetPeer*);
ENET_API int enet_peer_get_ip(const ENetPeer*, char*, size_t);
ENET_API uint16_t enet_peer_get_port(const ENetPeer*);
ENET_API uint32_t enet_peer_get_mtu(const ENetPeer*);
ENET_API ENetPeerState enet_peer_get_state(const ENetPeer*);
ENET_API uint32_t enet_peer_get_rtt(const ENetPeer*);
ENET_API uint32_t enet_peer_get_last_rtt(const ENetPeer* peer);
ENET_API uint32_t enet_peer_get_lastsendtime(const ENetPeer*);
ENET_API uint32_t enet_peer_get_lastreceivetime(const ENetPeer*);
ENET_API uint64_t enet_peer_get_packets_sent(const ENetPeer*);
ENET_API uint64_t enet_peer_get_packets_lost(const ENetPeer*);
ENET_API float enet_peer_get_packets_throttle(const ENetPeer*);
ENET_API uint64_t enet_peer_get_bytes_sent(const ENetPeer*);
ENET_API uint64_t enet_peer_get_bytes_received(const ENetPeer*);
ENET_API void* enet_peer_get_data(const ENetPeer*);
ENET_API void enet_peer_set_data(ENetPeer*, const void*);
/*
=======================================================================
Private API
=======================================================================
*/
extern void enet_host_bandwidth_throttle(ENetHost*);
extern uint64_t enet_host_random_seed(void);
extern int enet_peer_throttle(ENetPeer*, uint32_t);
extern void enet_peer_reset_queues(ENetPeer*);
extern void enet_peer_setup_outgoing_command(ENetPeer*, ENetOutgoingCommand*);
extern ENetOutgoingCommand* enet_peer_queue_outgoing_command(ENetPeer*, const ENetProtocol*, ENetPacket*, uint32_t, uint16_t);
extern ENetIncomingCommand* enet_peer_queue_incoming_command(ENetPeer*, const ENetProtocol*, const void*, size_t, uint32_t, uint32_t);
extern ENetAcknowledgement* enet_peer_queue_acknowledgement(ENetPeer*, const ENetProtocol*, uint16_t);
extern void enet_peer_dispatch_incoming_unreliable_commands(ENetPeer*, ENetChannel*, ENetIncomingCommand*);
extern void enet_peer_dispatch_incoming_reliable_commands(ENetPeer*, ENetChannel*, ENetIncomingCommand*);
extern void enet_peer_on_connect(ENetPeer*);
extern void enet_peer_on_disconnect(ENetPeer*);
extern size_t enet_protocol_command_size(uint8_t);
#ifdef __cplusplus
}
#endif
#if defined(ENET_IMPLEMENTATION) && !defined(ENET_IMPLEMENTATION_DONE)
#define ENET_IMPLEMENTATION_DONE 1
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4244) /* 64-bit to 32-bit integer conversion */
#pragma warning(disable: 4267) /* size_t to integer conversion */
#endif
/*
=======================================================================
Atomics
=======================================================================
*/
#ifdef _MSC_VER
#define ENET_AT_CASSERT_PRED(predicate) sizeof(char[2 * !!(predicate) - 1])
#define ENET_IS_SUPPORTED_ATOMIC(size) ENET_AT_CASSERT_PRED(size == 1 || size == 2 || size == 4 || size == 8)
#define ENET_ATOMIC_SIZEOF(variable) (ENET_IS_SUPPORTED_ATOMIC(sizeof(*(variable))), sizeof(*(variable)))
__inline int64_t enet_at_atomic_read(char* ptr, size_t size) {
switch (size) {
case 1:
return _InterlockedExchangeAdd8((volatile char*)ptr, 0);
case 2:
return _InterlockedExchangeAdd16((volatile SHORT*)ptr, 0);
case 4:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedExchangeAdd((volatile LONG*)ptr, 0);
#else
return _InterlockedExchangeAdd((volatile LONG*)ptr, 0);
#endif
case 8:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedExchangeAdd64((volatile LONGLONG*)ptr, 0);
#else
return _InterlockedExchangeAdd64((volatile LONGLONG*)ptr, 0);
#endif
default:
return 0x0;
}
}
__inline int64_t enet_at_atomic_write(char* ptr, int64_t value, size_t size) {
switch (size) {
case 1:
return _InterlockedExchange8((volatile char*)ptr, (char)value);
case 2:
return _InterlockedExchange16((volatile SHORT*)ptr, (SHORT)value);
case 4:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedExchange((volatile LONG*)ptr, (LONG)value);
#else
return _InterlockedExchange((volatile LONG*)ptr, (LONG)value);
#endif
case 8:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedExchange64((volatile LONGLONG*)ptr, (LONGLONG)value);
#else
return _InterlockedExchange64((volatile LONGLONG*)ptr, (LONGLONG)value);
#endif
default:
return 0x0;
}
}
__inline int64_t enet_at_atomic_cas(char* ptr, int64_t new_val, int64_t old_val, size_t size) {
switch (size) {
case 1:
return _InterlockedCompareExchange8((volatile char*)ptr, (char)new_val, (char)old_val);
case 2:
return _InterlockedCompareExchange16((volatile SHORT*)ptr, (SHORT)new_val, (SHORT)old_val);
case 4:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedCompareExchange((volatile LONG*)ptr, (LONG)new_val, (LONG)old_val);
#else
return _InterlockedCompareExchange((volatile LONG*)ptr, (LONG)new_val, (LONG)old_val);
#endif
case 8:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedCompareExchange64((volatile LONGLONG*)ptr, (LONGLONG)new_val, (LONGLONG)old_val);
#else
return _InterlockedCompareExchange64((volatile LONGLONG*)ptr, (LONGLONG)new_val, (LONGLONG)old_val);
#endif
default:
return 0x0;
}
}
__inline int64_t enet_at_atomic_inc(char* ptr, int64_t delta, size_t data_size) {
switch (data_size) {
case 1:
return _InterlockedExchangeAdd8((volatile char*)ptr, (char)delta);
case 2:
return _InterlockedExchangeAdd16((volatile SHORT*)ptr, (SHORT)delta);
case 4:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedExchangeAdd((volatile LONG*)ptr, (LONG)delta);
#else
return _InterlockedExchangeAdd((volatile LONG*)ptr, (LONG)delta);
#endif
case 8:
#ifdef ENET_NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
return InterlockedExchangeAdd64((volatile LONGLONG*)ptr, (LONGLONG)delta);
#else
return _InterlockedExchangeAdd64((volatile LONGLONG*)ptr, (LONGLONG)delta);
#endif
default:
return 0x0;
}
}
#define ENET_ATOMIC_READ(variable) enet_at_atomic_read((char*)(variable), ENET_ATOMIC_SIZEOF(variable))
#define ENET_ATOMIC_WRITE(variable, new_val) enet_at_atomic_write((char*)(variable), (int64_t)(new_val), ENET_ATOMIC_SIZEOF(variable))
#define ENET_ATOMIC_CAS(variable, old_value, new_val) enet_at_atomic_cas((char*)(variable), (int64_t)(new_val), (int64_t)(old_value), ENET_ATOMIC_SIZEOF(variable))
#define ENET_ATOMIC_INC(variable) enet_at_atomic_inc((char*)(variable), 1, ENET_ATOMIC_SIZEOF(variable))
#define ENET_ATOMIC_DEC(variable) enet_at_atomic_inc((char*)(variable), -1, ENET_ATOMIC_SIZEOF(variable))
#define ENET_ATOMIC_INC_BY(variable, delta) enet_at_atomic_inc((char*)(variable), (delta), ENET_ATOMIC_SIZEOF(variable))
#define ENET_ATOMIC_DEC_BY(variable, delta) enet_at_atomic_inc((char*)(variable), -(delta), ENET_ATOMIC_SIZEOF(variable))
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
#define AT_HAVE_ATOMICS
#endif
/* We want to use __atomic built-ins if possible because the __sync primitives are
deprecated, because the __atomic build-ins allow us to use ENET_ATOMIC_WRITE on
uninitialized memory without running into undefined behavior, and because the
__atomic versions generate more efficient code since we don't need to rely on
CAS when we don't actually want it.
Note that we use acquire-release memory order (like mutexes do). We could use
sequentially consistent memory order but that has lower performance and is
almost always unneeded. */
#ifdef AT_HAVE_ATOMICS
#define ENET_ATOMIC_READ(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE)
#define ENET_ATOMIC_WRITE(ptr, value) __atomic_store_n((ptr), (value), __ATOMIC_RELEASE)
#ifndef typeof
#define typeof __typeof__
#endif
/* clang_analyzer doesn't know that CAS writes to memory so it complains about
potentially lost data. Replace the code with the equivalent non-sync code. */
#ifdef __clang_analyzer__
#define ENET_ATOMIC_CAS(ptr, old_value, new_value)\
({\
typeof(*(ptr)) ENET_ATOMIC_CAS_old_actual_ = (*(ptr));\
if (ATOMIC_CAS_old_actual_ == (old_value))\
*(ptr) = new_value;\
ENET_ATOMIC_CAS_old_actual_;\
})
#else
/* Could use __auto_type instead of typeof but that shouldn't work in C++.
The ({ }) syntax is a GCC extension called statement expression. It lets
us return a value out of the macro.
TODO We should return bool here instead of the old value to avoid the ABA
problem. */
#define ENET_ATOMIC_CAS(ptr, old_value, new_value)\
({\
typeof(*(ptr)) ENET_ATOMIC_CAS_expected_ = (old_value);\
__atomic_compare_exchange_n((ptr), &ENET_ATOMIC_CAS_expected_, (new_value), false,\
__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);\
ENET_ATOMIC_CAS_expected_;\
})
#endif
#define ENET_ATOMIC_INC(ptr) __atomic_fetch_add((ptr), 1, __ATOMIC_ACQ_REL)
#define ENET_ATOMIC_DEC(ptr) __atomic_fetch_sub((ptr), 1, __ATOMIC_ACQ_REL)
#define ENET_ATOMIC_INC_BY(ptr, delta) __atomic_fetch_add((ptr), (delta), __ATOMIC_ACQ_REL)
#define ENET_ATOMIC_DEC_BY(ptr, delta) __atomic_fetch_sub((ptr), (delta), __ATOMIC_ACQ_REL)
#else
#define ENET_ATOMIC_READ(variable) __sync_fetch_and_add(variable, 0)
#define ENET_ATOMIC_WRITE(variable, new_val) (void)__sync_val_compare_and_swap((variable), *(variable), (new_val))
#define ENET_ATOMIC_CAS(variable, old_value, new_val) __sync_val_compare_and_swap((variable), (old_value), (new_val))
#define ENET_ATOMIC_INC(variable) __sync_fetch_and_add((variable), 1)
#define ENET_ATOMIC_DEC(variable) __sync_fetch_and_sub((variable), 1)
#define ENET_ATOMIC_INC_BY(variable, delta) __sync_fetch_and_add((variable), (delta), 1)
#define ENET_ATOMIC_DEC_BY(variable, delta) __sync_fetch_and_sub((variable), (delta), 1)
#endif
#undef AT_HAVE_ATOMICS
#endif