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