Advertisement
Guest User

enet

a guest
Feb 7th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 27.29 KB | None | 0 0
  1. unit enet;
  2.  
  3. interface
  4.  
  5. {$MACRO ON}
  6. {$INLINE ON}
  7. {$PACKRECORDS C}
  8.  
  9. {$DEFINE lenet := cdecl; external 'enet'}
  10.  
  11.  
  12. //===== enet/win32.h and enet/unix.h =====
  13.  
  14. {$IFDEF WINDOWS}
  15.  
  16. uses
  17.   winsock2;
  18.  
  19. type
  20.   ENetSocket = TSocket;
  21.  
  22. const
  23.   ENET_SOCKET_NULL = INVALID_SOCKET;
  24.  
  25. type
  26.   pENetBuffer = ^ENetBuffer;
  27.   ENetBuffer = record
  28.     dataLength: SizeUint;
  29.     data: pointer;
  30.   end;
  31.  
  32. {$ELSE}
  33.  
  34. uses
  35.   BaseUnix,
  36.   Sockets;
  37.  
  38. type
  39.   ENetSocket = longint;
  40.  
  41. const
  42.   ENET_SOCKET_NULL = -1;
  43.  
  44. type
  45.   pENetBuffer = ^ENetBuffer;
  46.   ENetBuffer = record
  47.     data: pointer;
  48.     dataLength: SizeUint;
  49.   end;
  50.  
  51. {$ENDIF}
  52.  
  53. type
  54.   pENetSocketSet = ^ENetSocketSet;
  55.   ENetSocketSet = TFDSet;
  56.  
  57. function ENET_HOST_TO_NET_16(const value: word): word; inline;
  58. function ENET_HOST_TO_NET_32(const value: longword): longword; inline;
  59.  
  60. function ENET_NET_TO_HOST_16(const value: word): word; inline;
  61. function ENET_NET_TO_HOST_32(const value: word): longword; inline;
  62.  
  63. procedure ENET_SOCKETSET_EMPTY(var sockset: ENetSocketSet); inline;
  64. procedure ENET_SOCKETSET_ADD(var sockset: ENetSocketSet;
  65.                              socket: ENetSocket); inline;
  66. procedure ENET_SOCKETSET_REMOVE(var sockset: ENetSocketSet;
  67.                                 socket: ENetSocket); inline;
  68. function ENET_SOCKETSET_CHECK(sockset: ENetSocketSet;
  69.                               socket: ENetSocket): boolean; inline;
  70.  
  71.  
  72. //===== enet/types.h ====
  73.  
  74. type
  75.   penet_uint8 = ^enet_uint8;
  76.   enet_uint8 = byte;
  77.  
  78.   penet_uint16 = ^enet_uint16;
  79.   enet_uint16 = word;
  80.  
  81.   penet_uint32 = ^enet_uint32;
  82.   enet_uint32 = longword;
  83.  
  84.  
  85. //===== enet/protocol.h =====
  86.  
  87. const
  88.   ENET_PROTOCOL_MINIMUM_MTU             = 576;
  89.   ENET_PROTOCOL_MAXIMUM_MTU             = 4096;
  90.   ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32;
  91.   ENET_PROTOCOL_MINIMUM_WINDOW_SIZE     = 4096;
  92.   ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE     = 65536;
  93.   ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT   = 1;
  94.   ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT   = 255;
  95.   ENET_PROTOCOL_MAXIMUM_PEER_ID         = $FFF;
  96.   ENET_PROTOCOL_MAXIMUM_PACKET_SIZE     = 1024 * 1024 * 1024;
  97.   ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT  = 1024 * 1024;
  98.  
  99. type
  100.   ENetProtocolCommand = (
  101.     ENET_PROTOCOL_COMMAND_NONE,
  102.     ENET_PROTOCOL_COMMAND_ACKNOWLEDGE,
  103.     ENET_PROTOCOL_COMMAND_CONNECT,
  104.     ENET_PROTOCOL_COMMAND_VERIFY_CONNECT,
  105.     ENET_PROTOCOL_COMMAND_DISCONNECT,
  106.     ENET_PROTOCOL_COMMAND_PING,
  107.     ENET_PROTOCOL_COMMAND_SEND_RELIABLE,
  108.     ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE,
  109.     ENET_PROTOCOL_COMMAND_SEND_FRAGMENT,
  110.     ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED,
  111.     ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT,
  112.     ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE,
  113.     ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT,
  114.     ENET_PROTOCOL_COMMAND_COUNT,
  115.  
  116.     ENET_PROTOCOL_COMMAND_MASK = $0F);
  117.  
  118. const
  119.   //ENetProtocolFlag
  120.   ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE = 1 shl 7;
  121.   ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED = 1 shl 6;
  122.   ENET_PROTOCOL_HEADER_FLAG_COMPRESSED   = 1 shl 14;
  123.   ENET_PROTOCOL_HEADER_FLAG_SENT_TIME    = 1 shl 15;
  124.   ENET_PROTOCOL_HEADER_FLAG_MASK = ENET_PROTOCOL_HEADER_FLAG_COMPRESSED or
  125.                                    ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
  126.   ENET_PROTOCOL_HEADER_SESSION_MASK      = 3 shl 12;
  127.   ENET_PROTOCOL_HEADER_SESSION_SHIFT     = 12;
  128.  
  129. type
  130.   pENetProtocolHeader = ^ENetProtocolHeader;
  131.   ENetProtocolHeader = record
  132.     peerID,
  133.     sentTime: enet_uint16;
  134.   end;
  135.  
  136.   pENetProtocolCommandHeader = ^ENetProtocolCommandHeader;
  137.   ENetProtocolCommandHeader = record
  138.     command,
  139.     channelID: enet_uint8;
  140.     reliableSequenceNumber: enet_uint16;
  141.   end;
  142.  
  143.   pENetProtocolAcknowledge = ^ENetProtocolAcknowledge;
  144.   ENetProtocolAcknowledge = record
  145.     header: ENetProtocolCommandHeader;
  146.     receivedReliableSequenceNumber,
  147.     receivedSentTime: enet_uint16;
  148.   end;
  149.  
  150.   pENetProtocolConnect = ^ENetProtocolConnect;
  151.   ENetProtocolConnect = record
  152.     header: ENetProtocolCommandHeader;
  153.     outgoingPeerID: enet_uint16;
  154.     incomingSessionID,
  155.     outgoingSessionID: enet_uint8;
  156.     mtu,
  157.     windowSize,
  158.     channelCount,
  159.     incomingBandwidth,
  160.     outgoingBandwidth,
  161.     packetThrottleInterval,
  162.     packetThrottleAcceleration,
  163.     packetThrottleDeceleration,
  164.     connectID,
  165.     data: enet_uint32;
  166.   end;
  167.  
  168.   pENetProtocolVerifyConnect = ^ENetProtocolVerifyConnect;
  169.   ENetProtocolVerifyConnect = record
  170.     header: ENetProtocolCommandHeader;
  171.     outgoingPeerID: enet_uint16;
  172.     incomingSessionID,
  173.     outgoingSessionID: enet_uint8;
  174.     mtu,
  175.     windowSize,
  176.     channelCount,
  177.     incomingBandwidth,
  178.     outgoingBandwidth,
  179.     packetThrottleInterval,
  180.     packetThrottleAcceleration,
  181.     packetThrottleDeceleration,
  182.     connectID: enet_uint32;
  183.   end;
  184.  
  185.   pENetProtocolBandwidthLimit = ^ENetProtocolBandwidthLimit;
  186.   ENetProtocolBandwidthLimit = record
  187.     header: ENetProtocolCommandHeader;
  188.     incomingBandwidth,
  189.     outgoingBandwidth: enet_uint32;
  190.   end;
  191.  
  192.   pENetProtocolThrottleConfigure = ^ENetProtocolThrottleConfigure;
  193.   ENetProtocolThrottleConfigure = record
  194.     header: ENetProtocolCommandHeader;
  195.     packetThrottleInterval,
  196.     packetThrottleAcceleration,
  197.     packetThrottleDeceleration: enet_uint32;
  198.   end;
  199.  
  200.   pENetProtocolDisconnect = ^ENetProtocolDisconnect;
  201.   ENetProtocolDisconnect = record
  202.     header: ENetProtocolCommandHeader;
  203.     data: enet_uint32;
  204.   end;
  205.  
  206.   pENetProtocolPing = ^ENetProtocolPing;
  207.   ENetProtocolPing = record
  208.     header: ENetProtocolCommandHeader;
  209.   end;
  210.  
  211.   pENetProtocolSendReliable = ^ENetProtocolSendReliable;
  212.   ENetProtocolSendReliable = record
  213.     header: ENetProtocolCommandHeader;
  214.     dataLength: enet_uint16;
  215.   end;
  216.  
  217.   pENetProtocolSendUnreliable = ^ENetProtocolSendUnreliable;
  218.   ENetProtocolSendUnreliable = record
  219.     header: ENetProtocolCommandHeader;
  220.     unreliableSequenceNumber,
  221.     dataLength: enet_uint16;
  222.   end;
  223.  
  224.   pENetProtocolSendUnsequenced = ^ENetProtocolSendUnsequenced;
  225.   ENetProtocolSendUnsequenced = record
  226.     header: ENetProtocolCommandHeader;
  227.     unsequencedGroup,
  228.     dataLength: enet_uint16;
  229.   end;
  230.  
  231.   pENetProtocolSendFragment = ^ENetProtocolSendFragment;
  232.   ENetProtocolSendFragment = record
  233.     header: ENetProtocolCommandHeader;
  234.     startSequenceNumber,
  235.     dataLength: enet_uint16;
  236.     fragmentCount,
  237.     fragmentNumber,
  238.     totalLength,
  239.     fragmentOffset: enet_uint32;
  240.   end;
  241.  
  242.   pENetProtocol = ^ENetProtocol;
  243.   ENetProtocol = record
  244.     case byte of
  245.       0:  (header            : ENetProtocolCommandHeader);
  246.       1:  (acknowledge       : ENetProtocolAcknowledge);
  247.       2:  (connect           : ENetProtocolConnect);
  248.       3:  (verifyConnect     : ENetProtocolVerifyConnect);
  249.       4:  (disconnect        : ENetProtocolDisconnect);
  250.       5:  (ping              : ENetProtocolPing);
  251.       6:  (sendReliable      : ENetProtocolSendReliable);
  252.       7:  (sendUnreliable    : ENetProtocolSendUnreliable);
  253.       8:  (sendUnsequenced   : ENetProtocolSendUnsequenced);
  254.       9:  (sendFragment      : ENetProtocolSendFragment);
  255.       10: (bandwidthLimit    : ENetProtocolBandwidthLimit);
  256.       11: (throttleConfigure : ENetProtocolThrottleConfigure);
  257.     end;
  258.  
  259.  
  260.  
  261. //===== enet/list.h =====
  262.  
  263. type
  264.   pENetListNode = ^ENetListNode;
  265.   ENetListNode = record
  266.     next,
  267.     previous: pENetListNode;
  268.   end;
  269.  
  270.   ENetListIterator = pENetListNode;
  271.  
  272.   pENetList = ^ENetList;
  273.   ENetList = record
  274.     sentinel: ENetListNode;
  275.   end;
  276.  
  277. procedure enet_list_clear(list: pENetList); lenet;
  278. function enet_list_insert(position: ENetListIterator;
  279.                           data: pointer): ENetListIterator; lenet;
  280. function enet_list_remove(position: ENetListIterator): pointer; lenet;
  281. function enet_list_move(position: ENetListIterator;
  282.                         dataFirst, dataLast: pointer): ENetListIterator; lenet;
  283. function enet_list_size(list: pENetList): SizeUint; lenet;
  284.  
  285. function enet_list_begin(list: pENetList): ENetListIterator; inline;
  286. function enet_list_end(list: pENetList): ENetListIterator; inline;
  287.  
  288. function enet_list_empty(list: pENetList): boolean; inline;
  289.  
  290. function enet_list_next(iterator: ENetListIterator): ENetListIterator; inline;
  291. function enet_list_previous(iterator: ENetListIterator): ENetListIterator; inline;
  292.  
  293. function enet_list_front(list: pENetList): pointer; inline;
  294. function enet_list_back(list: pENetList): pointer; inline;
  295.  
  296.  
  297.  
  298. //===== enet/callbacks.h =====
  299.  
  300. type
  301.   pENetCallbacks = ^ENetCallbacks;
  302.   ENetCallbacks = record
  303.     malloc: function(size: SizeUint): pointer; cdecl;
  304.     free: procedure(memory: pointer); cdecl;
  305.     no_memory: procedure; cdecl;
  306.   end;
  307.  
  308. function enet_malloc(size: SizeUint): pointer; lenet;
  309. procedure enet_free(memory: pointer); lenet;
  310.  
  311.  
  312.  
  313. //===== enet/enet.h =====
  314.  
  315. const
  316.   ENET_VERSION_MAJOR = 1;
  317.   ENET_VERSION_MINOR = 3;
  318.   ENET_VERSION_PATCH = 12;
  319.  
  320. type
  321.   ENetVersion = enet_uint32;
  322.  
  323. function ENET_VERSION_CREATE(const major, minor, patch: longint): ENetVersion; inline;
  324. function ENET_VERSION_GET_MAJOR(const version: ENetVersion): longint; inline;
  325. function ENET_VERSION_GET_MINOR(const version: ENetVersion): longint; inline;
  326. function ENET_VERSION_GET_PATCH(const version: ENetVersion): longint; inline;
  327. function ENET_VERSION: ENetVersion; inline;
  328.  
  329. type
  330.   ENetSocketType = (
  331.     ENET_SOCKET_TYPE_STREAM = 1,
  332.     ENET_SOCKET_TYPE_DATAGRAM);
  333.  
  334.   ENetSocketWait = (
  335.     ENET_SOCKET_WAIT_NONE      = 0,
  336.     ENET_SOCKET_WAIT_SEND      = 1 shl 0,
  337.     ENET_SOCKET_WAIT_RECEIVE   = 1 shl 1,
  338.     ENET_SOCKET_WAIT_INTERRUPT = 1 shl 2);
  339.  
  340.   ENetSocketOption = (
  341.     ENET_SOCKOPT_NONBLOCK = 1,
  342.     ENET_SOCKOPT_BROADCAST,
  343.     ENET_SOCKOPT_RCVBUF,
  344.     ENET_SOCKOPT_SNDBUF,
  345.     ENET_SOCKOPT_REUSEADDR,
  346.     ENET_SOCKOPT_RCVTIMEO,
  347.     ENET_SOCKOPT_SNDTIMEO,
  348.     ENET_SOCKOPT_ERROR,
  349.     ENET_SOCKOPT_NODELAY);
  350.  
  351.   ENetSocketShutdown = (
  352.     ENET_SOCKET_SHUTDOWN_READ,
  353.     ENET_SOCKET_SHUTDOWN_WRITE,
  354.     ENET_SOCKET_SHUTDOWN_READ_WRITE);
  355.  
  356. const
  357.   ENET_HOST_ANY = 0;
  358.   ENET_HOST_BROADCAST_ = $ffffffff;
  359.   ENET_PORT_ANY = 0;
  360.  
  361. type
  362.   pENetAddress = ^ENetAddress;
  363.   ENetAddress = record
  364.     host: enet_uint32;
  365.     port: enet_uint16;
  366.   end;
  367.  
  368. const
  369.   ENET_PACKET_FLAG_RELIABLE    = 1 shl 0;
  370.   ENET_PACKET_FLAG_UNSEQUENCED = 1 shl 1;
  371.   ENET_PACKET_FLAG_NO_ALLOCATE = 1 shl 2;
  372.   ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT = 1 shl 3;
  373.   ENET_PACKET_FLAG_SENT = 1 shl 8;
  374.  
  375. type
  376.   pENetPacket = ^ENetPacket;
  377.  
  378.   ENetPacketFreeCallback = procedure(packet: pENetPacket); cdecl;
  379.  
  380.   ENetPacket = record
  381.     referenceCount : SizeUint;
  382.     flags          : enet_uint32;
  383.     data           : ^enet_uint8;
  384.     dataLength     : SizeUint;
  385.     freeCallback   : ENetPacketFreeCallback;
  386.     userData       : pointer;
  387.   end;
  388.  
  389.   pENetAcknowledgement = ^ENetAcknowledgement;
  390.   ENetAcknowledgement = record
  391.     acknowledgementList : ENetListNode;
  392.     sentTime            : enet_uint32;
  393.     command             : ENetProtocol;
  394.   end;
  395.  
  396.   pENetOutgoingCommand = ^ENetOutgoingCommand;
  397.   ENetOutgoingCommand = record
  398.     outgoingCommandList      : ENetListNode;
  399.     reliableSequenceNumber,
  400.     unreliableSequenceNumber : enet_uint16;
  401.     sentTime,
  402.     roundTripTimeout,
  403.     roundTripTimeoutLimit,
  404.     fragmentOffset           : enet_uint32;
  405.     fragmentLength,
  406.     sendAttempts             : enet_uint16;
  407.     command                  : ENetProtocol;
  408.     packet                   : pENetPacket;
  409.   end;
  410.  
  411.   pENetIncomingCommand = ^ENetIncomingCommand;
  412.   ENetIncomingCommand = record
  413.     incomingCommandList      : ENetListNode;
  414.     reliableSequenceNumber,
  415.     unreliableSequenceNumber : enet_uint16;
  416.     command                  : ENetProtocol;
  417.     fragmentCount,
  418.     fragmentsRemaining,
  419.     fragments                : ^enet_uint32;
  420.     packet                   : pENetPacket;
  421.   end;
  422.  
  423.   ENetPeerState = (
  424.     ENET_PEER_STATE_DISCONNECTED,
  425.     ENET_PEER_STATE_CONNECTING,
  426.     ENET_PEER_STATE_ACKNOWLEDGING_CONNECT,
  427.     ENET_PEER_STATE_CONNECTION_PENDING,
  428.     ENET_PEER_STATE_CONNECTION_SUCCEEDED,
  429.     ENET_PEER_STATE_CONNECTED,
  430.     ENET_PEER_STATE_DISCONNECT_LATER,
  431.     ENET_PEER_STATE_DISCONNECTING,
  432.     ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT,
  433.     ENET_PEER_STATE_ZOMBIE);
  434.  
  435. const
  436.   ENET_BUFFER_MAXIMUM = 1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS;
  437.  
  438.   ENET_HOST_RECEIVE_BUFFER_SIZE          = 256 * 1024;
  439.   ENET_HOST_SEND_BUFFER_SIZE             = 256 * 1024;
  440.   ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL  = 1000;
  441.   ENET_HOST_DEFAULT_MTU                  = 1400;
  442.  
  443.   ENET_PEER_DEFAULT_ROUND_TRIP_TIME      = 500;
  444.   ENET_PEER_DEFAULT_PACKET_THROTTLE      = 32;
  445.   ENET_PEER_PACKET_THROTTLE_SCALE        = 32;
  446.   ENET_PEER_PACKET_THROTTLE_COUNTER      = 7;
  447.   ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2;
  448.   ENET_PEER_PACKET_THROTTLE_DECELERATION = 2;
  449.   ENET_PEER_PACKET_THROTTLE_INTERVAL     = 5000;
  450.   ENET_PEER_PACKET_LOSS_SCALE            = 1 shl 16;
  451.   ENET_PEER_PACKET_LOSS_INTERVAL         = 10000;
  452.   ENET_PEER_WINDOW_SIZE_SCALE            = 64 * 1024;
  453.   ENET_PEER_TIMEOUT_LIMIT                = 32;
  454.   ENET_PEER_TIMEOUT_MINIMUM              = 5000;
  455.   ENET_PEER_TIMEOUT_MAXIMUM              = 30000;
  456.   ENET_PEER_PING_INTERVAL_                = 500;
  457.   ENET_PEER_UNSEQUENCED_WINDOWS          = 64;
  458.   ENET_PEER_UNSEQUENCED_WINDOW_SIZE      = 1024;
  459.   ENET_PEER_FREE_UNSEQUENCED_WINDOWS     = 32;
  460.   ENET_PEER_RELIABLE_WINDOWS             = 16;
  461.   ENET_PEER_RELIABLE_WINDOW_SIZE         = $1000;
  462.   ENET_PEER_FREE_RELIABLE_WINDOWS        = 8;
  463.  
  464. type
  465.   pENetChannel = ^ENetChannel;
  466.   ENetChannel = record
  467.     outgoingReliableSequenceNumber,
  468.     outgoingUnreliableSequenceNumber,
  469.     usedReliableWindows              : enet_uint16;
  470.     reliableWindows                  : array [0..ENET_PEER_RELIABLE_WINDOWS-1] of enet_uint16;
  471.     incomingReliableSequenceNumber,
  472.     incomingUnreliableSequenceNumber : enet_uint16;
  473.     incomingReliableCommands,
  474.     incomingUnreliableCommands       : ENetList;
  475.   end;
  476.  
  477.   pENetHost = ^ENetHost;
  478.  
  479.   pENetPeer = ^ENetPeer;
  480.   ENetPeer = record
  481.     dispatchList                   : ENetListNode;
  482.     host                           : pENetHost;
  483.     outgoingPeerID,
  484.     incomingPeerID                 : enet_uint16;
  485.     connectID                      : enet_uint32;
  486.     outgoingSessionID,
  487.     incomingSessionID              : enet_uint8;
  488.     address                        : ENetAddress;
  489.     data                           : pointer;
  490.     state                          : ENetPeerState;
  491.     channels                       : pENetChannel;
  492.     channelCount                   : SizeUint;
  493.     incomingBandwidth,
  494.     outgoingBandwidth,
  495.     incomingBandwidthThrottleEpoch,
  496.     outgoingBandwidthThrottleEpoch,
  497.     incomingDataTotal,
  498.     outgoingDataTotal,
  499.     lastSendTime,
  500.     lastReceiveTime,
  501.     nextTimeout,
  502.     earliestTimeout,
  503.     packetLossEpoch,
  504.     packetsSent,
  505.     packetsLost,
  506.     packetLoss,
  507.     packetLossVariance,
  508.     packetThrottle,
  509.     packetThrottleLimit,
  510.     packetThrottleCounter,
  511.     packetThrottleEpoch,
  512.     packetThrottleAcceleration,
  513.     packetThrottleDeceleration,
  514.     packetThrottleInterval,
  515.     pingInterval,
  516.     timeoutLimit,
  517.     timeoutMinimum,
  518.     timeoutMaximum,
  519.     lastRoundTripTime,
  520.     lowestRoundTripTime,
  521.     lastRoundTripTimeVariance,
  522.     highestRoundTripTimeVariance,
  523.     roundTripTime,
  524.     roundTripTimeVariance,
  525.     mtu,
  526.     windowSize,
  527.     reliableDataInTransit          : enet_uint32;
  528.     outgoingReliableSequenceNumber : enet_uint16;
  529.     acknowledgements,
  530.     sentReliableCommands,
  531.     sentUnreliableCommands,
  532.     outgoingReliableCommands,
  533.     outgoingUnreliableCommands,
  534.     dispatchedCommands             : ENetList;
  535.     needsDispatch                  : longint;
  536.     incomingUnsequencedGroup,
  537.     outgoingUnsequencedGroup       : enet_uint16;
  538.     unsequencedWindow              : array [0..(ENET_PEER_UNSEQUENCED_WINDOW_SIZE div 32) - 1] of enet_uint32;
  539.     eventData                      : enet_uint32;
  540.     totalWaitingData               : SizeUint;
  541.   end;
  542.  
  543.   pENetCompressor = ^ENetCompressor;
  544.   ENetCompressor = record
  545.     context: pointer;
  546.     compress: function(context: pointer;
  547.           const inBuffers: pENetBuffer;
  548.           inBufferCount, inLimit: SizeUint;
  549.           outData: penet_uint8;
  550.           outLimit: SizeUInt): SizeUint; cdecl;
  551.     decompress: function(
  552.           context: pointer;
  553.           const inData: penet_uint8;
  554.           inLimit: SizeUint;
  555.           outData: penet_uint8;
  556.           outLimit: SizeUint): SizeUint; cdecl;
  557.     destroy: procedure(context: pointer); cdecl;
  558.   end;
  559.  
  560.   ENetEventType = (
  561.     ENET_EVENT_TYPE_NONE,
  562.     ENET_EVENT_TYPE_CONNECT,
  563.     ENET_EVENT_TYPE_DISCONNECT,
  564.     ENET_EVENT_TYPE_RECEIVE);
  565.  
  566.   pENetEvent = ^ENetEvent;
  567.   ENetEvent = record
  568.     type_     : ENetEventType;
  569.     peer      : pENetPeer;
  570.     channelID : enet_uint8;
  571.     data      : enet_uint32;
  572.     packet    : pENetPacket;
  573.   end;
  574.  
  575.   ENetChecksumCallback = function(const buffers: pENetBuffer;
  576.                                   bufferCount: SizeUint): enet_uint32; cdecl;
  577.   ENetInterceptCallback = function(host: pENetHost;
  578.                                    event: pENetEvent): longint; cdecl;
  579.  
  580.   ENetHost = record
  581.     socket                     : ENetSocket;
  582.     address                    : ENetAddress;
  583.     incomingBandwidth,
  584.     outgoingBandwidth,
  585.     bandwidthThrottleEpoch,
  586.     mtu,
  587.     randomSeed                 : enet_uint32;
  588.     recalculateBandwidthLimits : longint;
  589.     peers                      : pENetPeer;
  590.     peerCount,
  591.     channelLimit               : SizeUint;
  592.     serviceTime                : enet_uint32;
  593.     dispatchQueue              : ENetList;
  594.     continueSending            : longint;
  595.     packetSize                 : SizeUint;
  596.     headerFlags                : enet_uint16;
  597.     commands                   : array [0..ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS - 1] of ENetProtocol;
  598.     commandCount               : SizeUint;
  599.     buffers                    : array [0..ENET_BUFFER_MAXIMUM - 1] of ENetBuffer;
  600.     bufferCount                : SizeUint;
  601.     checksum                   : ENetChecksumCallback;
  602.     compressor                 : ENetCompressor;
  603.     packetData                 : array [0..1, 0..ENET_PROTOCOL_MAXIMUM_MTU - 1] of enet_uint8;
  604.     receivedAddress            : ENetAddress;
  605.     receivedData               : penet_uint8;
  606.     receivedDataLength         : SizeUint;
  607.     totalSentData,
  608.     totalSentPackets,
  609.     totalReceivedData,
  610.     totalReceivedPackets       : enet_uint32;
  611.     intercept                  : ENetInterceptCallback;
  612.     connectedPeers,
  613.     bandwidthLimitedPeers,
  614.     duplicatePeers,
  615.     maximumPacketSize,
  616.     maximumWaitingData         : SizeUint;
  617.   end;
  618.  
  619. function enet_initialize: longint; lenet;
  620. function enet_initialize_with_callbacks(version: ENetVersion;
  621.             const inits: pENetCallbacks): longint; lenet;
  622. procedure enet_deinitialize; lenet;
  623. function enet_linked_version: ENetVersion; lenet;
  624.  
  625. function enet_time_get: enet_uint32; lenet;
  626. procedure enet_time_set(newTimeBase: enet_uint32); lenet;
  627.  
  628. function enet_socket_create(kind: ENetSocketType): ENetSocket; lenet;
  629. function enet_socket_bind(socket: ENetSocket; const address: pENetAddress): longint; lenet;
  630. function enet_socket_get_address(socket: ENetSocket; address: pENetAddress): longint; lenet;
  631. function enet_socket_listen(socket: ENetSocket; backlog: longint): longint; lenet;
  632. function enet_socket_accept(socket: ENetSocket; address: pENetAddress): ENetSocket; lenet;
  633. function enet_socket_connect(socket: ENetSocket; const address: pENetAddress): longint; lenet;
  634. function enet_socket_send(socket: ENetSocket; const address: pENetAddress; const buffers: pENetBuffer; bufferCount: SizeUint): longint; lenet;
  635. function enet_socket_receive(socket: ENetSocket; address: pENetAddress; buffers: pENetBuffer; bufferCount: SizeUint): longint; lenet;
  636. function enet_socket_wait(socket: ENetSocket; condition: penet_uint32; timeout: enet_uint32): longint; lenet;
  637. function enet_socket_set_option(socket: ENetSocket; option: ENetSocketOption; value: longint): longint; lenet;
  638. function enet_socket_get_option(socket: ENetSocket; option: ENetSocketOption; value: plongint): longint; lenet;
  639. function enet_socket_shutdown(socket: ENetSocket; how: ENetSocketShutdown): longint; lenet;
  640. procedure enet_socket_destroy(socket: ENetSocket); lenet;
  641. function enet_socketset_select(maxSocket: ENetSocket; readSet: pENetSocketSet; writeSet: pENetSocketSet; timeout: enet_uint32): longint; lenet;
  642.  
  643. function enet_address_set_host(address: pENetAddress; const hostName: pAnsiChar): longint; lenet;
  644. function enet_address_get_host_ip(const address: pENetAddress; hostName: pAnsiChar; nameLength: SizeUint): longint; lenet;
  645. function enet_address_get_host(const address: pENetAddress; hostName: pAnsiChar; nameLength: SizeUint): longint; lenet;
  646.  
  647. function enet_packet_create(const data: pointer; dataLength: SizeUint; flags: enet_uint32): pENetPacket; lenet;
  648. procedure enet_packet_destroy(packet: pENetPacket); lenet;
  649. function enet_packet_resize(packet: pENetPacket; dataLength: SizeUint): longint; lenet;
  650. function enet_crc32(const buffers: pENetBuffer; bufferCount: SizeUint): enet_uint32; lenet;
  651.  
  652. function enet_host_create(const address: pENetAddress; peerCount, channelLimit: SizeUint; incomingBandwidth, outgoingBandwidth: enet_uint32): pENetHost; lenet;
  653. procedure enet_host_destroy(host: pENetHost); lenet;
  654. function enet_host_connect(host: pENetHost; const address: pENetAddress; channelCount: SizeUint; data: enet_uint32): pENetPeer; lenet;
  655. function enet_host_check_events(host: pENetHost; event: pENetEvent): longint; lenet;
  656. function enet_host_service(host: pENetHost; event: pENetEvent; timeout: enet_uint32): longint; lenet;
  657. procedure enet_host_flush(host: pENetHost); lenet;
  658. procedure enet_host_broadcast(host: pENetHost; channelID: enet_uint8; packet: pENetPacket); lenet;
  659. procedure enet_host_compress(host: pENetHost; const compressor: pENetCompressor); lenet;
  660. function enet_host_compress_with_range_coder(host: pENetHost): longint; lenet;
  661. procedure enet_host_channel_limit(host: pENetHost; channelLimit: SizeUint); lenet;
  662. procedure enet_host_bandwidth_limit(host: pENetHost; incomingBandwidth, outgoingBandwidth: enet_uint32); lenet;
  663. procedure enet_host_bandwidth_throttle(host: pENetHost); lenet;
  664. function enet_host_random_seed: enet_uint32; lenet;
  665.  
  666. function enet_peer_send(peer: pENetPeer; channelID: enet_uint8; packet: pENetPacket): longint; lenet;
  667. function enet_peer_receive(peer: pENetPeer; channelID: penet_uint8): pENetPacket; lenet;
  668. procedure enet_peer_ping(peer: pENetPeer); lenet;
  669. procedure enet_peer_ping_interval(peer: pENetPeer; pingInterval: enet_uint32); lenet;
  670. procedure enet_peer_timeout(peer: pENetPeer; timeoutLimit, timeoutMinimum, timeoutMaximum: enet_uint32); lenet;
  671. procedure enet_peer_reset(peer: pENetPeer); lenet;
  672. procedure enet_peer_disconnect(peer: pENetPeer; data: enet_uint32); lenet;
  673. procedure enet_peer_disconnect_now(peer: pENetPeer; data: enet_uint32); lenet;
  674. procedure enet_peer_disconnect_later(peer: pENetPeer; data: enet_uint32); lenet;
  675. procedure enet_peer_throttle_configure(peer: pENetPeer; interval, acceleration, deceleration: enet_uint32); lenet;
  676. function enet_peer_throttle(peer: pENetPeer; rtt: enet_uint32): longint; lenet;
  677. procedure enet_peer_reset_queues(peer: pENetPeer); lenet;
  678. procedure enet_peer_setup_outgoing_command(peer: pENetPeer; outgoingCommand: pENetOutgoingCommand); lenet;
  679. function enet_peer_queue_outgoing_command(peer: pENetPeer; const command: pENetProtocol; packet: pENetPacket; offset: enet_uint32; length: enet_uint16): pENetOutgoingCommand; lenet;
  680. function enet_peer_queue_incoming_command(peer: pENetPeer; const command: pENetProtocol; packet: pENetPacket; fragmentCount: enet_uint32): pENetIncomingCommand; lenet;
  681. function enet_peer_queue_queue_acknowledgement(peer: pENetPeer; const command: pENetProtocol; sentTime: enet_uint16): pENetAcknowledgement; lenet;
  682. procedure enet_peer_dispatch_incoming_unreliable_commands(peer: pENetPeer; channel: pENetChannel); lenet;
  683. procedure enet_peer_dispatch_incoming_reliable_commands(peer: pENetPeer; channel: pENetChannel); lenet;
  684. procedure enet_peer_on_connect(peer: pENetPeer); lenet;
  685. procedure enet_peer_on_disconnect(peer: pENetPeer); lenet;
  686.  
  687. function enet_range_coder_create: pointer; lenet;
  688. procedure enet_range_coder_destroy(context: pointer); lenet;
  689. function enet_range_coder_compress(context: pointer; const inBuffers: pENetBuffer; inBufferCount, inLiit: SizeUint; outData: penet_uint8; outLimit: SizeUint): SizeUint; lenet;
  690. function enet_range_coder_decompress(context: pointer; const inData: penet_uint8; inLimit: SizeUint; outData: penet_uint8; outLimit: SizeUint): SizeUint; lenet;
  691.  
  692. function enet_protocol_command_size(commandNumber: enet_uint8): SizeUint; lenet;
  693.  
  694.  
  695. implementation
  696.  
  697. //===== enet/win32.h and enet/unix.h =====
  698.  
  699. function ENET_HOST_TO_NET_16(const value: word): word; inline;
  700. begin
  701.   exit(htons(value));
  702. end;
  703.  
  704. function ENET_HOST_TO_NET_32(const value: longword): longword; inline;
  705. begin
  706.   exit(htonl(value));
  707. end;
  708.  
  709. function ENET_NET_TO_HOST_16(const value: word): word; inline;
  710. begin
  711.   exit(ntohs(value));
  712. end;
  713.  
  714. function ENET_NET_TO_HOST_32(const value: word): longword; inline;
  715. begin
  716.   exit(ntohl(value));
  717. end;
  718.  
  719. procedure ENET_SOCKETSET_EMPTY(var sockset: ENetSocketSet); inline;
  720. begin
  721.   {$IFNDEF WINDOWS}fpFD_ZERO{$ELSE}FD_ZERO{$ENDIF}(sockset);
  722. end;
  723.  
  724. procedure ENET_SOCKETSET_ADD(var sockset: ENetSocketSet;
  725.                              socket: ENetSocket); inline;
  726. begin
  727.   {$IFNDEF WINDOWS}fpFD_SET{$ELSE}FD_SET{$ENDIF}(socket, sockset);
  728. end;
  729.  
  730. procedure ENET_SOCKETSET_REMOVE(var sockset: ENetSocketSet;
  731.                                 socket: ENetSocket); inline;
  732. begin
  733.   {$IFNDEF WINDOWS}fpFD_CLR{$ELSE}FD_CLR{$ENDIF}(socket, sockset);
  734. end;
  735.  
  736. function ENET_SOCKETSET_CHECK(sockset: ENetSocketSet;
  737.                               socket: ENetSocket): boolean; inline;
  738. begin
  739.   {$IFNDEF WINDOWS}
  740.   exit(fpFD_ISSET(socket, sockset) <> 0);
  741.   {$ELSE}
  742.   exit(FD_ISSET(socket, sockset));
  743.   {$ENDIF}
  744. end;
  745.  
  746.  
  747.  
  748. //===== enet/list.h =====
  749.  
  750. function enet_list_begin(list: pENetList): ENetListIterator; inline;
  751. begin
  752.   exit(list^.sentinel.next);
  753. end;
  754.  
  755. function enet_list_end(list: pENetList): ENetListIterator; inline;
  756. begin
  757.   exit(@list^.sentinel);
  758. end;
  759.  
  760. function enet_list_empty(list: pENetList): boolean; inline;
  761. begin
  762.   exit(enet_list_begin(list) = enet_list_end(list));
  763. end;
  764.  
  765. function enet_list_next(iterator: ENetListIterator): ENetListIterator; inline;
  766. begin
  767.   exit(iterator^.next);
  768. end;
  769.  
  770. function enet_list_previous(iterator: ENetListIterator): ENetListIterator; inline;
  771. begin
  772.   exit(iterator^.previous);
  773. end;
  774.  
  775. function enet_list_front(list: pENetList): pointer; inline;
  776. begin
  777.   exit(pointer(list^.sentinel.next));
  778. end;
  779.  
  780. function enet_list_back(list: pENetList): pointer; inline;
  781. begin
  782.   exit(pointer(list^.sentinel.previous));
  783. end;
  784.  
  785.  
  786.  
  787. //===== enet/enet.h =====
  788.  
  789. function ENET_VERSION_CREATE(const major, minor, patch: longint): ENetVersion; inline;
  790. begin
  791.   exit((major shl 16) or (minor shl 16) or patch);
  792. end;
  793.  
  794. function ENET_VERSION_GET_MAJOR(const version: ENetVersion): longint; inline;
  795. begin
  796.   exit((version shr 16) and $ff);
  797. end;
  798.  
  799. function ENET_VERSION_GET_MINOR(const version: ENetVersion): longint; inline;
  800. begin
  801.   exit((version shr 8) and $ff);
  802. end;
  803.  
  804. function ENET_VERSION_GET_PATCH(const version: ENetVersion): longint; inline;
  805. begin
  806.   exit(version and $ff);
  807. end;
  808.  
  809. function ENET_VERSION: ENetVersion; inline;
  810. begin
  811.   exit(ENET_VERSION_CREATE(
  812.         ENET_VERSION_MAJOR,
  813.         ENET_VERSION_MINOR,
  814.         ENET_VERSION_PATCH));
  815. end;
  816.  
  817. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement