DEADSOFTWARE

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