DEADSOFTWARE

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