enet.pp 31 KB

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