DEADSOFTWARE

fix building for osx
[d2df-sdl.git] / src / lib / enet / enet.pp
1 {.$DEFINE LIBENET_WINDOZE_STATIC}
3 {$MODE OBJFPC}
4 {$PACKRECORDS C}
6 {$IFDEF WIN32}
7 {$DEFINE MSWINDOWS}
8 {$ENDIF}
10 {$LONGSTRINGS ON}
11 {$MACRO ON}
13 {$Z4} // Force four-byte enums
16 {$IFDEF MSWINDOWS}
17 {$IFDEF LIBENET_WINDOZE_STATIC}
18 {$LINKLIB libenet.a}
19 {$LINKLIB libwinmm.a}
20 {$LINKLIB libws2_32.a}
21 {$LINKLIB libkernel32.a}
22 {$LINKLIB libm.a}
23 {$LINKLIB libmingwex.a}
24 {$LINKLIB libmingw32.a}
25 {$LINKLIB libmsvcrt.a}
26 {$LINKLIB libgcc.a}
27 {$DEFINE libraryLibENetDecl := cdecl}
28 {$DEFINE libraryLibENetImp := cdecl; external}
29 {$DEFINE libraryLibENetVar := cvar; external}
30 {$ELSE}
31 {$DEFINE libraryLibENetDecl := cdecl}
32 {$DEFINE libraryLibENetImp := cdecl; external 'enet.dll'}
33 {.$DEFINE libraryLibENetVar := cvar; external}
34 {$DEFINE libraryLibENetVar := external 'enet.dll'}
35 // external LIBNAME name 'var_name' would've been more correct here
36 // because just external is case insensitive, but fuck it
37 {$ENDIF}
38 {$ELSE}
39 {$IFDEF DARWIN}
40 {$LINKLIB libenet.a}
41 {$DEFINE libraryLibENetDecl := cdecl}
42 {$DEFINE libraryLibENetImp := cdecl; external}
43 {$DEFINE libraryLibENetVar := cvar; external}
44 {$ELSE}
45 {$DEFINE libraryLibENetDecl := cdecl}
46 {$DEFINE libraryLibENetImp := cdecl; external 'enet'}
47 {$DEFINE libraryLibENetVar := cvar; external 'enet'}
48 {$ENDIF}
49 {$ENDIF}
52 unit ENet;
54 {
55 ENet - Reliable UDP networking library
56 Copyright (c) 2002-2015 Lee Salzman
58 DLL header for Free Pascal
59 Version 3 for 1.3.13: 2016-08-24
60 Copyright (c) 2015-2016 Dmitry D. Chernov aka Black Doomer
62 Permission is hereby granted, free of charge, to any person obtaining a
63 copy of this software and associated documentation files (the "Software"),
64 to deal in the Software without restriction, including without limitation
65 the rights to use, copy, modify, merge, publish, distribute, sublicense,
66 and/or sell copies of the Software, and to permit persons to whom the
67 Software is furnished to do so, subject to the following conditions:
69 The above copyright notice and this permission notice shall be included in
70 all copies or substantial portions of the Software.
72 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
73 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
74 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
75 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
76 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
77 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
78 DEALINGS IN THE SOFTWARE.
79 }
81 interface
83 uses
84 ctypes,
85 {$IFDEF WINDOWS}
86 WinSock2;
87 {$ELSE}
88 BaseUnix, Sockets;
89 {$ENDIF}
91 ////////////////////////////////////////////////////////////////////////////////
92 // types.h
93 ////////////////////////////////////////////////////////////////////////////////
95 type
96 enet_uint8 = cuchar;
97 penet_uint8 = ^enet_uint8;
99 enet_uint16 = cushort;
100 penet_uint16 = ^enet_uint16;
102 enet_uint32 = cuint; // TODO: why 'int' instead of 'long'?
103 penet_uint32 = ^enet_uint32;
105 enet_size_t = NativeUInt;
106 penet_size_t = ^enet_size_t;
108 ////////////////////////////////////////////////////////////////////////////////
109 // callbacks.h
110 ////////////////////////////////////////////////////////////////////////////////
112 type
113 pENetCallbacks = ^TENetCallbacks;
114 TENetCallbacks = record
115 malloc : function( size: csize_t ): Pointer; cdecl;
116 free : procedure( memory: Pointer ); cdecl;
117 no_memory : procedure(); cdecl;
118 end;
120 ////////////////////////////////////////////////////////////////////////////////
121 // protocol.h
122 ////////////////////////////////////////////////////////////////////////////////
124 const
125 { unnamed enums }
126 ENET_PROTOCOL_MINIMUM_MTU = 576;
127 ENET_PROTOCOL_MAXIMUM_MTU = 4096;
128 ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32;
129 ENET_PROTOCOL_MINIMUM_WINDOW_SIZE = 4096;
130 ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE = 65536;
131 ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 1;
132 ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 255;
133 ENET_PROTOCOL_MAXIMUM_PEER_ID = $FFF;
134 ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT = 1024 * 1024;
136 { enum ENetProtocolFlag }
137 ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE = 1 shl 7;
138 ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED = 1 shl 6;
139 ENET_PROTOCOL_HEADER_FLAG_COMPRESSED = 1 shl 14;
140 ENET_PROTOCOL_HEADER_FLAG_SENT_TIME = 1 shl 15;
141 ENET_PROTOCOL_HEADER_FLAG_MASK = ENET_PROTOCOL_HEADER_FLAG_COMPRESSED or
142 ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
143 ENET_PROTOCOL_HEADER_SESSION_MASK = 3 shl 12;
144 ENET_PROTOCOL_HEADER_SESSION_SHIFT = 12;
146 type
147 { enums }
148 ENetProtocolCommand = ( ENET_PROTOCOL_COMMAND_NONE,
149 ENET_PROTOCOL_COMMAND_ACKNOWLEDGE,
150 ENET_PROTOCOL_COMMAND_CONNECT,
151 ENET_PROTOCOL_COMMAND_VERIFY_CONNECT,
152 ENET_PROTOCOL_COMMAND_DISCONNECT,
153 ENET_PROTOCOL_COMMAND_PING,
154 ENET_PROTOCOL_COMMAND_SEND_RELIABLE,
155 ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE,
156 ENET_PROTOCOL_COMMAND_SEND_FRAGMENT,
157 ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED,
158 ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT,
159 ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE,
160 ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT,
161 ENET_PROTOCOL_COMMAND_COUNT,
162 ENET_PROTOCOL_COMMAND_MASK = $0F );
164 { structs / unions }
165 pENetProtocolHeader = ^ENetProtocolHeader;
166 ENetProtocolHeader = packed record
167 peerID : enet_uint16;
168 sentTime : enet_uint16;
169 end;
171 pENetProtocolCommandHeader = ^ENetProtocolCommandHeader;
172 ENetProtocolCommandHeader = packed record
173 command : enet_uint8;
174 channelID : enet_uint8;
175 reliableSequenceNumber : enet_uint16;
176 end;
178 pENetProtocolAcknowledge = ^ENetProtocolAcknowledge;
179 ENetProtocolAcknowledge = packed record
180 header : ENetProtocolCommandHeader;
181 receivedReliableSequenceNumber : enet_uint16;
182 receivedSentTime : enet_uint16;
183 end;
185 pENetProtocolConnect = ^ENetProtocolConnect;
186 ENetProtocolConnect = packed record
187 header : ENetProtocolCommandHeader;
188 outgoingPeerID : enet_uint16;
189 incomingSessionID : enet_uint8;
190 outgoingSessionID : enet_uint8;
191 mtu : enet_uint32;
192 windowSize : enet_uint32;
193 channelCount : enet_uint32;
194 incomingBandwidth : enet_uint32;
195 outgoingBandwidth : enet_uint32;
196 packetThrottleInterval : enet_uint32;
197 packetThrottleAcceleration : enet_uint32;
198 packetThrottleDeceleration : enet_uint32;
199 connectID : enet_uint32;
200 data : enet_uint32;
201 end;
203 pENetProtocolVerifyConnect = ^ENetProtocolVerifyConnect;
204 ENetProtocolVerifyConnect = packed record
205 header : ENetProtocolCommandHeader;
206 outgoingPeerID : enet_uint16;
207 incomingSessionID : enet_uint8;
208 outgoingSessionID : enet_uint8;
209 mtu : enet_uint32;
210 windowSize : enet_uint32;
211 channelCount : enet_uint32;
212 incomingBandwidth : enet_uint32;
213 outgoingBandwidth : enet_uint32;
214 packetThrottleInterval : enet_uint32;
215 packetThrottleAcceleration : enet_uint32;
216 packetThrottleDeceleration : enet_uint32;
217 connectID : enet_uint32;
218 end;
220 pENetProtocolBandwidthLimit = ^ENetProtocolBandwidthLimit;
221 ENetProtocolBandwidthLimit = packed record
222 header : ENetProtocolCommandHeader;
223 incomingBandwidth : enet_uint32;
224 outgoingBandwidth : enet_uint32;
225 end;
227 pENetProtocolThrottleConfigure = ^ENetProtocolThrottleConfigure;
228 ENetProtocolThrottleConfigure = packed record
229 header : ENetProtocolCommandHeader;
230 packetThrottleInterval : enet_uint32;
231 packetThrottleAcceleration : enet_uint32;
232 packetThrottleDeceleration : enet_uint32;
233 end;
235 pENetProtocolDisconnect = ^ENetProtocolDisconnect;
236 ENetProtocolDisconnect = packed record
237 header : ENetProtocolCommandHeader;
238 data : enet_uint32;
239 end;
241 pENetProtocolPing = ^ENetProtocolPing;
242 ENetProtocolPing = packed record
243 header : ENetProtocolCommandHeader;
244 end;
246 pENetProtocolSendReliable = ^ENetProtocolSendReliable;
247 ENetProtocolSendReliable = packed record
248 header : ENetProtocolCommandHeader;
249 dataLength : enet_uint16;
250 end;
252 pENetProtocolSendUnreliable = ^ENetProtocolSendUnreliable;
253 ENetProtocolSendUnreliable = packed record
254 header : ENetProtocolCommandHeader;
255 unreliableSequenceNumber : enet_uint16;
256 dataLength : enet_uint16;
257 end;
259 pENetProtocolSendUnsequenced = ^ENetProtocolSendUnsequenced;
260 ENetProtocolSendUnsequenced = packed record
261 header : ENetProtocolCommandHeader;
262 unsequencedGroup : enet_uint16;
263 dataLength : enet_uint16;
264 end;
266 pENetProtocolSendFragment = ^ENetProtocolSendFragment;
267 ENetProtocolSendFragment = packed record
268 header : ENetProtocolCommandHeader;
269 startSequenceNumber : enet_uint16;
270 dataLength : enet_uint16;
271 fragmentCount : enet_uint32;
272 fragmentNumber : enet_uint32;
273 totalLength : enet_uint32;
274 fragmentOffset : enet_uint32;
275 end;
277 pENetProtocol = ^TENetProtocol;
278 TENetProtocol = packed record //union
279 case Byte of
280 0 : (header : ENetProtocolCommandHeader);
281 1 : (acknowledge : ENetProtocolAcknowledge);
282 2 : (connect : ENetProtocolConnect);
283 3 : (verifyConnect : ENetProtocolVerifyConnect);
284 4 : (disconnect : ENetProtocolDisconnect);
285 5 : (ping : ENetProtocolPing);
286 6 : (sendReliable : ENetProtocolSendReliable);
287 7 : (sendUnreliable : ENetProtocolSendUnreliable);
288 8 : (sendUnsequenced : ENetProtocolSendUnsequenced);
289 9 : (sendFragment : ENetProtocolSendFragment);
290 10: (bandwidthLimit : ENetProtocolBandwidthLimit);
291 11: (throttleConfigure : ENetProtocolThrottleConfigure);
292 end;
294 ////////////////////////////////////////////////////////////////////////////////
295 // win32.h / unix.h
296 ////////////////////////////////////////////////////////////////////////////////
298 const
299 {$IFDEF WINDOWS}
300 ENET_SOCKET_NULL = INVALID_SOCKET;
301 {$ELSE}
302 ENET_SOCKET_NULL = -1;
303 {$ENDIF}
305 type
306 {$IFDEF WINDOWS}
307 ENetSocket = TSocket;
308 {$ELSE}
309 ENetSocket = cint;
310 {$ENDIF}
312 ENetSocketSet = TFDSet;
313 pENetSocketSet = ^ENetSocketSet;
315 pENetBuffer = ^ENetBuffer;
316 ENetBuffer = record
317 {$IFDEF WINDOWS}
318 dataLength : csize_t;
319 data : Pointer;
320 {$ELSE}
321 data : Pointer;
322 dataLength : csize_t;
323 {$ENDIF}
324 end;
326 { inline macros }
328 function ENET_HOST_TO_NET_16( value: cuint16 ): cuint16; inline;
329 function ENET_HOST_TO_NET_32( value: cuint32 ): cuint32; inline;
331 function ENET_NET_TO_HOST_16( value: cuint16 ): cuint16; inline;
332 function ENET_NET_TO_HOST_32( value: cuint32 ): cuint32; inline;
334 procedure ENET_SOCKETSET_EMPTY( var sockset: ENetSocketSet ); inline;
335 procedure ENET_SOCKETSET_ADD( var sockset: ENetSocketSet; socket: ENetSocket ); inline;
336 procedure ENET_SOCKETSET_REMOVE( var sockset: ENetSocketSet; socket: ENetSocket ); inline;
337 function ENET_SOCKETSET_CHECK( var sockset: ENetSocketSet; socket: ENetSocket ): cbool; inline;
339 ////////////////////////////////////////////////////////////////////////////////
340 // list.h
341 ////////////////////////////////////////////////////////////////////////////////
343 type
344 pENetListNode = ^ENetListNode;
345 ENetListNode = record
346 next : pENetListNode;
347 previous : pENetListNode;
348 end;
350 pENetList = ^TENetList;
351 TENetList = record
352 sentinel : ENetListNode;
353 end;
355 ENetListIterator = pENetListNode;
357 { inline macros }
358 function enet_list_begin( list: pENetList ): ENetListIterator; inline;
359 function enet_list_end( list: pENetList ): ENetListIterator; inline;
361 function enet_list_empty( list: pENetList ): Boolean; inline;
363 function enet_list_next( iterator: ENetListIterator ): ENetListIterator; inline;
364 function enet_list_previous( iterator: ENetListIterator ): ENetListIterator; inline;
366 function enet_list_front( list: pENetList ): Pointer; inline;
367 function enet_list_back( list: pENetList ): Pointer; inline;
369 ////////////////////////////////////////////////////////////////////////////////
370 // time.h
371 ////////////////////////////////////////////////////////////////////////////////
373 const
374 ENET_TIME_OVERFLOW = 86400000;
376 { inline macros }
377 function ENET_TIME_LESS( const a, b: cint ): cbool; inline;
378 function ENET_TIME_GREATER( const a, b: cint ): cbool; inline;
380 function ENET_TIME_LESS_EQUAL( const a, b: cint ): cbool; inline;
381 function ENET_TIME_GREATER_EQUAL( const a, b: cint ): cbool; inline;
383 function ENET_TIME_DIFFERENCE( const a, b: cint ): cint; inline;
385 ////////////////////////////////////////////////////////////////////////////////
386 // enet.h
387 ////////////////////////////////////////////////////////////////////////////////
389 const
390 { defines }
391 ENET_VERSION_MAJOR = 1;
392 ENET_VERSION_MINOR = 3;
393 ENET_VERSION_PATCH = 13;
395 ENET_HOST_ANY = 0;
396 ENET_HOST_BROADCAST_ = $FFFFFFFF; // "_" due to name conflict
397 ENET_PORT_ANY = 0;
399 ENET_BUFFER_MAXIMUM = 1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS;
401 { unnamed enums }
402 ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024;
403 ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024;
404 ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000;
405 ENET_HOST_DEFAULT_MTU = 1400;
406 ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE = 32 * 1024 * 1024;
407 ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA = 32 * 1024 * 1024;
409 ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500;
410 ENET_PEER_DEFAULT_PACKET_THROTTLE = 32;
411 ENET_PEER_PACKET_THROTTLE_SCALE = 32;
412 ENET_PEER_PACKET_THROTTLE_COUNTER = 7;
413 ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2;
414 ENET_PEER_PACKET_THROTTLE_DECELERATION = 2;
415 ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000;
416 ENET_PEER_PACKET_LOSS_SCALE = 1 shl 16;
417 ENET_PEER_PACKET_LOSS_INTERVAL = 10000;
418 ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024;
419 ENET_PEER_TIMEOUT_LIMIT = 32;
420 ENET_PEER_TIMEOUT_MINIMUM = 5000;
421 ENET_PEER_TIMEOUT_MAXIMUM = 30000;
422 ENET_PEER_PING_INTERVAL_ = 500; // "_" due to name conflict
423 ENET_PEER_UNSEQUENCED_WINDOWS = 64;
424 ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024;
425 ENET_PEER_FREE_UNSEQUENCED_WINDOWS = 32;
426 ENET_PEER_RELIABLE_WINDOWS = 16;
427 ENET_PEER_RELIABLE_WINDOW_SIZE = $1000;
428 ENET_PEER_FREE_RELIABLE_WINDOWS = 8;
430 { enum ENetSocketWait }
431 ENET_SOCKET_WAIT_NONE = 0;
432 ENET_SOCKET_WAIT_SEND = 1 shl 0;
433 ENET_SOCKET_WAIT_RECEIVE = 1 shl 1;
434 ENET_SOCKET_WAIT_INTERRUPT = 1 shl 2;
436 { enum ENetPacketFlag }
437 ENET_PACKET_FLAG_RELIABLE = 1 shl 0;
438 ENET_PACKET_FLAG_UNSEQUENCED = 1 shl 1;
439 ENET_PACKET_FLAG_NO_ALLOCATE = 1 shl 2;
440 ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT = 1 shl 3;
441 ENET_PACKET_FLAG_SENT = 1 shl 8;
443 type
444 { enums }
445 ENetSocketType = ( ENET_SOCKET_TYPE_STREAM = 1,
446 ENET_SOCKET_TYPE_DATAGRAM = 2 );
448 ENetSocketOption = ( ENET_SOCKOPT_NONBLOCK = 1,
449 ENET_SOCKOPT_BROADCAST = 2,
450 ENET_SOCKOPT_RCVBUF = 3,
451 ENET_SOCKOPT_SNDBUF = 4,
452 ENET_SOCKOPT_REUSEADDR = 5,
453 ENET_SOCKOPT_RCVTIMEO = 6,
454 ENET_SOCKOPT_SNDTIMEO = 7,
455 ENET_SOCKOPT_ERROR = 8,
456 ENET_SOCKOPT_NODELAY = 9 );
458 ENetSocketShutdown = ( ENET_SOCKET_SHUTDOWN_READ,
459 ENET_SOCKET_SHUTDOWN_WRITE,
460 ENET_SOCKET_SHUTDOWN_READ_WRITE );
462 ENetPeerState = ( ENET_PEER_STATE_DISCONNECTED,
463 ENET_PEER_STATE_CONNECTING,
464 ENET_PEER_STATE_ACKNOWLEDGING_CONNECT,
465 ENET_PEER_STATE_CONNECTION_PENDING,
466 ENET_PEER_STATE_CONNECTION_SUCCEEDED,
467 ENET_PEER_STATE_CONNECTED,
468 ENET_PEER_STATE_DISCONNECT_LATER,
469 ENET_PEER_STATE_DISCONNECTING,
470 ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT,
471 ENET_PEER_STATE_ZOMBIE );
473 ENetEventType = ( ENET_EVENT_TYPE_NONE,
474 ENET_EVENT_TYPE_CONNECT,
475 ENET_EVENT_TYPE_DISCONNECT,
476 ENET_EVENT_TYPE_RECEIVE );
478 { typedefs }
479 ENetVersion = enet_uint32;
481 { pointers to structs }
482 pENetAddress = ^ENetAddress;
483 pENetPacket = ^ENetPacket;
484 pENetChannel = ^ENetChannel;
485 pENetPeer = ^ENetPeer;
486 pENetCompressor = ^ENetCompressor;
487 pENetHost = ^ENetHost;
488 pENetEvent = ^ENetEvent;
490 { callbacks }
491 ENetPacketFreeCallback = procedure( packet: pENetPacket ); cdecl;
492 ENetChecksumCallback = function( const buffers: pENetBuffer;
493 bufferCount: csize_t ): enet_uint32; cdecl;
494 ENetInterceptCallback = function( host: pENetHost;
495 event: pENetEvent ): cint; cdecl;
497 { structs }
498 ENetAddress = record
499 host : enet_uint32;
500 port : enet_uint16;
501 end;
502 ENetPacket = record
503 referenceCount : csize_t;
504 flags : enet_uint32;
505 data : penet_uint8;
506 dataLength : csize_t;
507 freeCallback : ENetPacketFreeCallback;
508 userData : Pointer;
509 end;
510 ENetChannel = record
511 outgoingReliableSequenceNumber : enet_uint16;
512 outgoingUnreliableSequenceNumber : enet_uint16;
513 usedReliableWindows : enet_uint16;
514 reliableWindows : array[ 0..ENET_PEER_RELIABLE_WINDOWS-1 ] of enet_uint16;
515 incomingReliableSequenceNumber : enet_uint16;
516 incomingUnreliableSequenceNumber : enet_uint16;
517 incomingReliableCommands : TENetList;
518 incomingUnreliableCommands : TENetList;
519 end;
520 ENetPeer = record
521 dispatchList : ENetListNode;
522 host : pENetHost;
523 outgoingPeerID : enet_uint16;
524 incomingPeerID : enet_uint16;
525 connectID : enet_uint32;
526 outgoingSessionID : enet_uint8;
527 incomingSessionID : enet_uint8;
528 address : ENetAddress;
529 data : Pointer;
530 state : ENetPeerState;
531 channels : pENetChannel;
532 channelCount : csize_t;
533 incomingBandwidth : enet_uint32;
534 outgoingBandwidth : enet_uint32;
535 incomingBandwidthThrottleEpoch : enet_uint32;
536 outgoingBandwidthThrottleEpoch : enet_uint32;
537 incomingDataTotal : enet_uint32;
538 outgoingDataTotal : enet_uint32;
539 lastSendTime : enet_uint32;
540 lastReceiveTime : enet_uint32;
541 nextTimeout : enet_uint32;
542 earliestTimeout : enet_uint32;
543 packetLossEpoch : enet_uint32;
544 packetsSent : enet_uint32;
545 packetsLost : enet_uint32;
546 packetLoss : enet_uint32;
547 packetLossVariance : enet_uint32;
548 packetThrottle : enet_uint32;
549 packetThrottleLimit : enet_uint32;
550 packetThrottleCounter : enet_uint32;
551 packetThrottleEpoch : enet_uint32;
552 packetThrottleAcceleration : enet_uint32;
553 packetThrottleDeceleration : enet_uint32;
554 packetThrottleInterval : enet_uint32;
555 pingInterval : enet_uint32;
556 timeoutLimit : enet_uint32;
557 timeoutMinimum : enet_uint32;
558 timeoutMaximum : enet_uint32;
559 lastRoundTripTime : enet_uint32;
560 lowestRoundTripTime : enet_uint32;
561 lastRoundTripTimeVariance : enet_uint32;
562 highestRoundTripTimeVariance : enet_uint32;
563 roundTripTime : enet_uint32;
564 roundTripTimeVariance : enet_uint32;
565 mtu : enet_uint32;
566 windowSize : enet_uint32;
567 reliableDataInTransit : enet_uint32;
568 outgoingReliableSequenceNumber : enet_uint16;
569 acknowledgements : TENetList;
570 sentReliableCommands : TENetList;
571 sentUnreliableCommands : TENetList;
572 outgoingReliableCommands : TENetList;
573 outgoingUnreliableCommands : TENetList;
574 dispatchedCommands : TENetList;
575 needsDispatch : cint;
576 incomingUnsequencedGroup : enet_uint16;
577 outgoingUnsequencedGroup : enet_uint16;
578 unsequencedWindow : array[ 0..(ENET_PEER_UNSEQUENCED_WINDOW_SIZE div 32)-1 ] of enet_uint32;
579 eventData : enet_uint32;
580 totalWaitingData : csize_t;
581 end;
582 ENetCompressor = record
583 context : Pointer;
584 compress : function( context: Pointer; const inBuffers: pENetBuffer; inBufferCount, inLimit: csize_t; outData: penet_uint8; outLimit: csize_t ): csize_t; cdecl;
585 decompress : function( context: Pointer; const inData: penet_uint8; inLimit: csize_t; outData: penet_uint8; outLimit: csize_t ): csize_t; cdecl;
586 destroy : procedure( context: Pointer ); cdecl;
587 end;
588 ENetHost = record
589 socket : ENetSocket;
590 address : ENetAddress;
591 incomingBandwidth : enet_uint32;
592 outgoingBandwidth : enet_uint32;
593 bandwidthThrottleEpoch : enet_uint32;
594 mtu : enet_uint32;
595 randomSeed : enet_uint32;
596 recalculateBandwidthLimits : cint;
597 peers : pENetPeer;
598 peerCount : csize_t;
599 channelLimit : csize_t;
600 serviceTime : enet_uint32;
601 dispatchQueue : TENetList;
602 continueSending : cint;
603 packetSize : csize_t;
604 headerFlags : enet_uint16;
605 commands : array[ 0..ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS-1 ] of TENetProtocol;
606 commandCount : csize_t;
607 buffers : array[ 0..ENET_BUFFER_MAXIMUM-1 ] of ENetBuffer;
608 bufferCount : csize_t;
609 checksum : ENetChecksumCallback;
610 compressor : ENetCompressor;
611 packetData : array[ 0..1, 0..ENET_PROTOCOL_MAXIMUM_MTU-1 ] of enet_uint8;
612 receivedAddress : ENetAddress;
613 receivedData : penet_uint8;
614 receivedDataLength : csize_t;
615 totalSentData : enet_uint32;
616 totalSentPackets : enet_uint32;
617 totalReceivedData : enet_uint32;
618 totalReceivedPackets : enet_uint32;
619 intercept : ENetInterceptCallback;
620 connectedPeers : csize_t;
621 bandwidthLimitedPeers : csize_t;
622 duplicatePeers : csize_t;
623 maximumPacketSize : csize_t;
624 maximumWaitingData : csize_t;
625 end;
626 ENetEvent = record
627 kind : ENetEventType; //originally "type", which conflicts
628 peer : pENetPeer;
629 channelID : enet_uint8;
630 data : enet_uint32;
631 packet : pENetPacket;
632 end;
634 { inline macros }
635 function ENET_VERSION_CREATE( const major, minor, patch: cint ): ENetVersion; inline;
636 function ENET_VERSION_GET_MAJOR( const version: ENetVersion ): cint; inline;
637 function ENET_VERSION_GET_MINOR( const version: ENetVersion ): cint; inline;
638 function ENET_VERSION_GET_PATCH( const version: ENetVersion ): cint; inline;
639 function ENET_VERSION(): ENetVersion; inline;
641 { library functions }
642 function enet_initialize(): cint; libraryLibENetImp;
643 function enet_initialize_with_callbacks( version: ENetVersion; const inits: pENetCallbacks ): cint; libraryLibENetImp;
644 procedure enet_deinitialize(); libraryLibENetImp;
645 function enet_linked_version(): ENetVersion; libraryLibENetImp;
647 function enet_time_get(): enet_uint32; libraryLibENetImp;
648 procedure enet_time_set( newTimeBase: enet_uint32 ); libraryLibENetImp;
650 function enet_socket_create( kind: ENetSocketType ): ENetSocket; libraryLibENetImp;
651 function enet_socket_bind( socket: ENetSocket; const address: pENetAddress ): cint; libraryLibENetImp;
652 function enet_socket_get_address( socket: ENetSocket; address: pENetAddress ): cint; libraryLibENetImp;
653 function enet_socket_listen( socket: ENetSocket; backlog: cint ): cint; libraryLibENetImp;
654 function enet_socket_accept( socket: ENetSocket; address: pENetAddress ): ENetSocket; libraryLibENetImp;
655 function enet_socket_connect( socket: ENetSocket; const address: pENetAddress ): cint; libraryLibENetImp;
656 function enet_socket_send( socket: ENetSocket; const address: pENetAddress; const buffers: pENetBuffer; bufferCount: csize_t ): cint; libraryLibENetImp;
657 function enet_socket_receive( socket: ENetSocket; address: pENetAddress; buffers: pENetBuffer; bufferCount: csize_t ): cint; libraryLibENetImp;
658 function enet_socket_wait( socket: ENetSocket; condition: penet_uint32; timeout: enet_uint32 ): cint; libraryLibENetImp;
659 function enet_socket_set_option( socket: ENetSocket; option: ENetSocketOption; value: cint ): cint; libraryLibENetImp;
660 function enet_socket_get_option( socket: ENetSocket; option: ENetSocketOption; value: pcint ): cint; libraryLibENetImp;
661 function enet_socket_shutdown( socket: ENetSocket; how: ENetSocketShutdown ): cint; libraryLibENetImp;
662 procedure enet_socket_destroy( socket: ENetSocket ); libraryLibENetImp;
663 function enet_socketset_select( maxSocket: ENetSocket; readSet: pENetSocketSet; writeSet: pENetSocketSet; timeout: enet_uint32 ): cint; libraryLibENetImp;
665 function enet_address_set_host( address: pENetAddress; const hostName: PChar ): cint; libraryLibENetImp;
666 function enet_address_get_host_ip( const address: pENetAddress; hostName: PChar; nameLength: csize_t ): cint; libraryLibENetImp;
667 function enet_address_get_host( const address: pENetAddress; hostName: PChar; nameLength: csize_t ): cint; libraryLibENetImp;
669 function enet_packet_create( const data: Pointer; dataLength: csize_t; flags: enet_uint32 ): pENetPacket; libraryLibENetImp;
670 procedure enet_packet_destroy( packet: pENetPacket ); libraryLibENetImp;
671 function enet_packet_resize( packet: pENetPacket; dataLength: csize_t ): cint; libraryLibENetImp;
672 function enet_crc32( const buffers: pENetBuffer; bufferCount: csize_t ): enet_uint32; libraryLibENetImp;
674 function enet_host_create( const address: pENetAddress; peerCount, channelLimit: csize_t; incomingBandwidth, outgoingBandwidth: enet_uint32 ): pENetHost; libraryLibENetImp;
675 procedure enet_host_destroy( host: pENetHost ); libraryLibENetImp;
676 function enet_host_connect( host: pENetHost; const address: pENetAddress; channelCount: csize_t; data: enet_uint32 ): pENetPeer; libraryLibENetImp;
677 function enet_host_check_events( host: pENetHost; event: pENetEvent ): cint; libraryLibENetImp;
678 function enet_host_service( host: pENetHost; event: pENetEvent; timeout: enet_uint32 ): cint; libraryLibENetImp;
679 procedure enet_host_flush( host: pENetHost ); libraryLibENetImp;
680 procedure enet_host_broadcast( host: pENetHost; channelID: enet_uint8; packet: pENetPacket ); libraryLibENetImp;
681 procedure enet_host_compress( host: pENetHost; const compressor: pENetCompressor ); libraryLibENetImp;
682 function enet_host_compress_with_range_coder( host: pENetHost ): cint; libraryLibENetImp;
683 procedure enet_host_channel_limit( host: pENetHost; channelLimit: csize_t ); libraryLibENetImp;
684 procedure enet_host_bandwidth_limit( host: pENetHost; incomingBandwidth, outgoingBandwidth: enet_uint32 ); libraryLibENetImp;
686 function enet_peer_send( peer: pENetPeer; channelID: enet_uint8; packet: pENetPacket ): cint; libraryLibENetImp;
687 function enet_peer_receive( peer: pENetPeer; channelID: penet_uint8 ): pENetPacket; libraryLibENetImp;
688 procedure enet_peer_ping( peer: pENetPeer ); libraryLibENetImp;
689 procedure enet_peer_ping_interval( peer: pENetPeer; pingInterval: enet_uint32 ); libraryLibENetImp;
690 procedure enet_peer_timeout( peer: pENetPeer; timeoutLimit, timeoutMinimum, timeoutMaximum: enet_uint32 ); libraryLibENetImp;
691 procedure enet_peer_reset( peer: pENetPeer ); libraryLibENetImp;
692 procedure enet_peer_disconnect( peer: pENetPeer; data: enet_uint32 ); libraryLibENetImp;
693 procedure enet_peer_disconnect_now( peer: pENetPeer; data: enet_uint32 ); libraryLibENetImp;
694 procedure enet_peer_disconnect_later( peer: pENetPeer; data: enet_uint32 ); libraryLibENetImp;
695 procedure enet_peer_throttle_configure( peer: pENetPeer; interval, acceleration, deceleration: enet_uint32 ); libraryLibENetImp;
697 function enet_range_coder_create(): Pointer; libraryLibENetImp;
698 procedure enet_range_coder_destroy( context: Pointer ); libraryLibENetImp;
699 function enet_range_coder_compress( context: Pointer; const inBuffers: pENetBuffer; inBufferCount, inLiit: csize_t; outData: penet_uint8; outLimit: csize_t ): csize_t; libraryLibENetImp;
700 function enet_range_coder_decompress( context: Pointer; const inData: penet_uint8; inLimit: csize_t; outData: penet_uint8; outLimit: csize_t ): csize_t; libraryLibENetImp;
702 implementation
704 ////////////////////////////////////////////////////////////////////////////////
705 // win32.h / unix.h
706 ////////////////////////////////////////////////////////////////////////////////
708 function ENET_HOST_TO_NET_16( value: cuint16 ): cuint16;
709 begin
710 Result := htons(value);
711 end;
713 function ENET_HOST_TO_NET_32( value: cuint32 ): cuint32;
714 begin
715 Result := htonl(value);
716 end;
718 function ENET_NET_TO_HOST_16( value: cuint16 ): cuint16;
719 begin
720 Result := ntohs(value);
721 end;
723 function ENET_NET_TO_HOST_32( value: cuint32 ): cuint32;
724 begin
725 Result := ntohl(value);
726 end;
728 procedure ENET_SOCKETSET_EMPTY( var sockset: ENetSocketSet );
729 begin
730 {$IFDEF WINDOWS}
731 FD_ZERO( sockset );
732 {$ELSE}
733 fpFD_ZERO( sockset );
734 {$ENDIF}
735 end;
737 procedure ENET_SOCKETSET_ADD( var sockset: ENetSocketSet; socket: ENetSocket );
738 begin
739 {$IFDEF WINDOWS}
740 FD_SET( socket, sockset );
741 {$ELSE}
742 fpFD_SET( socket, sockset );
743 {$ENDIF}
744 end;
746 procedure ENET_SOCKETSET_REMOVE( var sockset: ENetSocketSet; socket: ENetSocket );
747 begin
748 {$IFDEF WINDOWS}
749 FD_CLR( socket, sockset );
750 {$ELSE}
751 fpFD_CLR( socket, sockset );
752 {$ENDIF}
753 end;
755 function ENET_SOCKETSET_CHECK( var sockset: ENetSocketSet; socket: ENetSocket ): cbool;
756 begin
757 {$IFDEF WINDOWS}
758 Result := FD_ISSET( socket, sockset );
759 {$ELSE}
760 Result := fpFD_ISSET( socket, sockset ) = 1;
761 {$ENDIF}
762 end;
764 ////////////////////////////////////////////////////////////////////////////////
765 // list.h
766 ////////////////////////////////////////////////////////////////////////////////
768 function enet_list_begin( list: pENetList ): ENetListIterator;
769 begin
770 Result := list^.sentinel.next;
771 end;
773 function enet_list_end( list: pENetList ): ENetListIterator;
774 begin
775 Result := @( list^.sentinel );
776 end;
778 function enet_list_empty( list: pENetList ): Boolean;
779 begin
780 Result := enet_list_begin(list) = enet_list_end(list);
781 end;
783 function enet_list_next( iterator: ENetListIterator ): ENetListIterator;
784 begin
785 Result := iterator^.next;
786 end;
788 function enet_list_previous( iterator: ENetListIterator ): ENetListIterator;
789 begin
790 Result := iterator^.previous;
791 end;
793 function enet_list_front( list: pENetList ): Pointer;
794 begin
795 Result := Pointer( list^.sentinel.next );
796 end;
798 function enet_list_back( list: pENetList ): Pointer;
799 begin
800 Result := Pointer( list^.sentinel.previous );
801 end;
803 ////////////////////////////////////////////////////////////////////////////////
804 // time.h
805 ////////////////////////////////////////////////////////////////////////////////
807 function ENET_TIME_LESS( const a, b: cint ): cbool;
808 begin
809 Result := (a - b) >= ENET_TIME_OVERFLOW;
810 end;
812 function ENET_TIME_GREATER( const a, b: cint ): cbool;
813 begin
814 Result := (b - a) >= ENET_TIME_OVERFLOW;
815 end;
817 function ENET_TIME_LESS_EQUAL( const a, b: cint ): cbool;
818 begin
819 Result := not ENET_TIME_GREATER(a, b);
820 end;
822 function ENET_TIME_GREATER_EQUAL( const a, b: cint ): cbool;
823 begin
824 Result := not ENET_TIME_LESS(a, b);
825 end;
827 function ENET_TIME_DIFFERENCE( const a, b: cint ): cint;
828 begin
829 if (a - b) >= ENET_TIME_OVERFLOW then
830 Result := b - a
831 else
832 Result := a - b;
833 end;
835 ////////////////////////////////////////////////////////////////////////////////
836 // enet.h
837 ////////////////////////////////////////////////////////////////////////////////
839 function ENET_VERSION_CREATE( const major, minor, patch: cint ): ENetVersion;
840 begin
841 Result := (major shl 16) or (minor shl 8) or patch;
842 end;
844 function ENET_VERSION_GET_MAJOR( const version: ENetVersion ): cint;
845 begin
846 Result := (version shr 16) and $FF;
847 end;
849 function ENET_VERSION_GET_MINOR( const version: ENetVersion ): cint;
850 begin
851 Result := (version shr 8) and $FF;
852 end;
854 function ENET_VERSION_GET_PATCH( const version: ENetVersion ): cint;
855 begin
856 Result := version and $FF;
857 end;
859 function ENET_VERSION(): ENetVersion;
860 begin
861 Result := ENET_VERSION_CREATE( ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH );
862 end;
864 end.