api.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. enet.h - The file that should be included to use the ENet API.
  2. enet_uint8 - unsigned 8 bit integer
  3. enet_uint16 - unsigned 16 bit integer
  4. enet_uint32 - unsigned 32 bit integer
  5. typedef struct
  6. {
  7. enet_uint32 host;
  8. enet_uint16 port;
  9. } ENetAddress;
  10. Portable internet address structure. The host must be specified in network
  11. byte-order, and the port must be in host byte-order. The constant ENET_HOST_ANY
  12. may be used to specify the default server host.
  13. typedef struct
  14. {
  15. enet_uint32 flags;
  16. enet_uint8 * data;
  17. size_t dataLength;
  18. } ENetPacket;
  19. An ENet data packet that may be sent to or received from a peer. The shown fields
  20. should only be read and never modified. The data field contains the allocated data
  21. for the packet. The dataLength fields specifies the length of the allocated data.
  22. The flags field is either 0 (specifying no flags), or a bitwise-or of any
  23. combination of the following flags:
  24. ENET_PACKET_FLAG_RELIABLE -
  25. Specifies that the packet must be received by the target peer and that resend
  26. attempts should be made should delivery of the packet fail.
  27. typedef struct
  28. {
  29. ENetAddress address;
  30. void * data;
  31. size_t channelCount;
  32. enet_uint32 incomingBandwidth;
  33. enet_uint32 outgoingBandwidth;
  34. enet_uint32 roundTripTime;
  35. enet_uint32 packetLoss;
  36. } ENetPeer;
  37. An ENet peer which data packets may be sent or received from. No fields should be
  38. modified unless otherwise specified. The address field contains the internet address
  39. of the peer. The data fields may be used to associate any desired data with the peer
  40. and may be freely modified. The channelCount field tells the number of channels that
  41. have been allocated for use to communnicate with the peer. The incomingBandwidth field
  42. specifies the downstream bandwidth of the client in bytes per second. The
  43. outgoingBandwidth field specifies the upstream bandwidth of the client in bytes per
  44. second. The roundTripTime field tells the mean round trip time from the sending of
  45. a reliable packet until the receipt of its acknowledgement in milliseconds. The
  46. packetLoss field tells the mean packet loss of reliable packets as a ratio with
  47. respect to the constant ENET_PEER_PACKET_LOSS_SCALE.
  48. typedef enum
  49. {
  50. ENET_EVENT_TYPE_NONE,
  51. ENET_EVENT_TYPE_CONNECT,
  52. ENET_EVENT_TYPE_DISCONNECT,
  53. ENET_EVENT_TYPE_RECEIVE
  54. } ENetEventType;
  55. typedef struct _ENetEvent
  56. {
  57. ENetEventType type;
  58. ENetPeer * peer;
  59. enet_uint8 channelID;
  60. ENetPacket * packet;
  61. } ENetEvent;
  62. An ENet event as returned by enet_host_service. The type field contains the type
  63. of the event, which may be any one of the following:
  64. ENET_EVENT_TYPE_NONE - No event occurred within the specified time limit.
  65. ENET_EVENT_TYPE_CONNECT -
  66. A connection request initiated by enet_host_connect has completed. The peer field
  67. contains the peer which successfully connected.
  68. ENET_EVENT_TYPE_DISCONNECT -
  69. A peer has disconnected. This event is generated on successful completion of a
  70. disconnect iniated by enet_peer_disconnect, if a peer has timed out, or if a
  71. connection request initialized by enet_host_connect has timed out. The peer field
  72. contains the peer which disconnected.
  73. ENET_EVENT_TYPE_RECEIVE -
  74. A packet has been received from a peer. The peer field specifies the peer which
  75. send the packet. The channelID field specifies the channel number upon which the
  76. packet was received. The packet field contains the packet that was destroyed; this
  77. packet must be destroyed with enet_packet_destroy after use.
  78. typedef struct
  79. {
  80. ENetAddress address;
  81. enet_uint32 incomingBandwidth;
  82. enet_uint32 outgoingBandwidth;
  83. ENetPeer * peers;
  84. size_t peerCount;
  85. } ENetHost;
  86. An ENet host for communicating with peers. No fields should be modified. The address
  87. field tells the internet address of the host. The incomingBandwidth field tells the downstream
  88. bandwidth of the host. The outgoingBandwidth field specifies the upstream bandwidth of the host.
  89. The peers field contains an array of the peers that have been allocated for this host. The
  90. peerCount field specifies the number of peers that have been allocated for this host.
  91. unsigned ENET_HOST_TO_NET_8 (unsigned);
  92. unsigned ENET_HOST_TO_NET_16 (unsigned);
  93. unsigned ENET_HOST_TO_NET_32 (unsigned);
  94. Macros that convert from host byte-order to network byte-order (big
  95. endian) for unsigned integers of 8 bits, 16 bits, and 32 bits repectively.
  96. unsigned ENET_NET_TO_HOST_8 (unsigned);
  97. unsigned ENET_NET_TO_HOST_16 (unsigned);
  98. unsigned ENET_NET_TO_HOST_32 (unsigned);
  99. Macros that convert from network byte-order (big endian) to host
  100. byte-order for unsigned integers of 8 bits, 16 bits, and 32 bits repectively.
  101. enet_uint32 enet_time_get (void);
  102. Returns the wall-time in milliseconds. Its initial value is unspecified unless
  103. otherwise set.
  104. void enet_time_set (enet_uint32);
  105. Sets the current wall-time in milliseconds.
  106. int enet_initialize (void);
  107. Initialize ENet for use. Must be called prior to using any functions in ENet.
  108. Returns 0 on success and -1 on failure.
  109. void enet_deinitialize (void);
  110. Clean-up ENet after use. Should be called when a program that has initialized
  111. and used ENet exits.
  112. int enet_address_set_host (ENetAddress * address, const char * hostName);
  113. Attempts to resolve the host named by the parameter hostName and sets the host
  114. field in the address parameter if successful. Returns 0 on success and -1 on
  115. failure.
  116. int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength);
  117. Attempts to do a reverse lookup of the host field in the address parameter.
  118. If successful, the name of the host is placed in the string described by
  119. hostName and nameLength. The host name is always null-delimited and will
  120. not exceed nameLength in length. Returns 0 on success and -1 on failure.
  121. ENetPacket * enet_packet_create (const void * dataContents, size_t dataLength, enet_uint32 flags);
  122. Creates a packet that may be sent to a peer. The dataContents parameter
  123. specifies the initial contents of the packet's data; the packet's data will
  124. remain uninitialized if dataContents is NULL. The dataLength parameter specifies
  125. the size of the data that is allocated for this packet. The flags parameter
  126. specifies flags for this packet as described for the ENetPacket structure.
  127. Returns the packet on success and NULL on failure.
  128. void enet_packet_destroy (ENetPacket * packet);
  129. Destroys the packet and deallocates its data.
  130. int enet_packet_resize (ENetPacket * packet, size_t dataLength);
  131. Attempts to resize the data in the packet to the length specified in the
  132. dataLength parameter. Returns 0 on success and -1 on failure.
  133. ENetHost * enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth);
  134. Creates a host for communicating with peers. The address parameter specifies
  135. the address at which other peers may connect to this host; if the address parameter
  136. is NULL, then no peers may connect to the host. The peerCount parameter specifies
  137. the numbers of peers that should be allocated for the host; this limits the maximum
  138. number of peers that may connect to this host to peerCount. The incomingBandwidth
  139. parameter specifies the downstream bandwidth of the host in bytes per second; if
  140. the incomingBandwidth parameter is 0, ENet will assume the host has unlimited
  141. downstream bandwidth. The outgoingBandwidth parameter specifies the upstream bandwidth
  142. of the host in bytes per second; if the outgoingBandwidth parameter is 0, ENet will
  143. assume the host has unlimited upstream bandwidth. ENet will strategically drop packets
  144. on specific sides of a connection between hosts to ensure the host's bandwidth is not
  145. overwhelmed; the bandwidth parameters also determine the window size of a connection
  146. which limits the amount of reliable packets that may be in transit at any given time.
  147. Returns the host on success and NULL on failure.
  148. void enet_host_destroy (ENetHost * host);
  149. Destroys the host and all resources associated with it.
  150. ENetPeer * enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelCount);
  151. Initiates a connection from the host specified in the host parameter to a foreign
  152. host whose internet address is specified by the address parameter. The channelCount
  153. parameter specifies the number of channels that should be allocated for communicating
  154. with the foreign host. Returns a peer representing the foreign host on success and NULL
  155. on failure. The peer returned will have not completed the connection until enet_host_service
  156. notifies of an ENET_EVENT_TYPE_CONNECT event for the peer.
  157. int enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout);
  158. Waits for events on the host specified by the host parameters and shuttles packets
  159. between the host and its peers. The event parameter specifies an event structure
  160. where event details will be placed if one occurs. The timeout field specifies an
  161. amount of time in milliseconds that ENet should wait for events. Returns 1 if an
  162. event occured within the specified time limit, 0 if no event occurred within the
  163. time limit, and -1 on failure. This function must be called frequently for adequate
  164. performance.
  165. void enet_host_flush (ENetHost * host);
  166. Sends out any queued packets on the host specified in the host parameters to
  167. the designated peers. This function need only be used in circumstances where one
  168. wishes to send queued packets earlier than in a call to enet_host_service.
  169. void enet_host_broadcast (ENetHost * host, enet_uint8 channelID, ENetPacket * packet);
  170. Queues a packet to be sent to all peers on the host specified in the host parameter
  171. over the channel number identified by the channelID parameter.
  172. void enet_host_bandwidth_limit (ENetHost * host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth);
  173. Adjusts the bandwidth limits of the host specified in the host parameter. The
  174. incomingBandwidth and outgoingBandwidth parameters are as specified in a call to
  175. enet_host_create.
  176. int enet_peer_send (ENetPeer * peer, enet_uint8 channelID, ENetPacket * packet);
  177. Queues a packet to be sent to the peer specified by the peer parameter over the
  178. channel number identified by the channelID parameter. Returns 0 on success and -1
  179. on failure.
  180. ENetPacket * enet_peer_receive (ENetPeer * peer, enet_uint8 channelID);
  181. Attempts to dequeue any incoming queued packets on the peer specified by the peer
  182. parameter on the channel number identified by the channelID parameter. Returns a packet
  183. if one is available and NULL if there are no available incoming queued packets.
  184. void enet_peer_ping (ENetPeer * peer);
  185. Sends a ping request to the peer specified by the peer parameter. Ping requests factor
  186. into the mean round trip time as designated by the roundTripTime field in the ENetPeer
  187. structure. ENet automatically pings all connected peer at an interval, however, this
  188. function may be called to ensure more frequent ping requests.
  189. void enet_peer_reset (ENetPeer * peer);
  190. Forcefully disconnects the peer specified by the peer parameter. The foreign host
  191. represented by the peer is not notified of the disconnection and so will timeout on its
  192. connection to the local host.
  193. void enet_peer_disconnect (ENetPeer * peer);
  194. Request a disconnection from the peer specified by the peer parameter. An
  195. ENET_EVENT_DISCONNECT event will be generated by enet_host_service once the
  196. disconnection is complete.
  197. void enet_peer_throttle_configure (ENetPeer * peer, enet_uint32 interval, enet_uint32 acceleration, enet_uint32 deceleration);
  198. Configures throttle parameter for the peer specified by the peer parameter.
  199. Unreliable packets are dropped by ENet in response to the varying conditions of
  200. the internet connection to the peer. The throttle represents a probability that
  201. an unreliable packet should not be dropped and thus sent by ENet to the peer.
  202. The lowest mean round trip time from the sending of a reliable packet to the
  203. receipt of its acknowledgement is measured over an amount of time specified
  204. by the interval parameter in milliseconds; the constant ENET_PEER_PACKET_THROTTLE_INTERVAL
  205. is the default value for this parameter. If a measured round trip time happens
  206. to be signifigantly less than the mean round trip time measured over the interval,
  207. then the throttle probability is increased to allow more traffic by an amount
  208. specified in the acceleration parameter which is in ratio to the
  209. ENET_PEER_PACKET_THROTTLE_SCALE constant. If a measured round trip time happens
  210. to be signifigantly greater than the mean round trip time measured over the interval,
  211. then the throttle probability is decreased to limit traffic by an amount specified
  212. in the deceleration parameter which is in ratio to the ENET_PEER_PACKET_THROTTLE_SCALE
  213. constant. When the throttle has a value of ENET_PEER_PACKET_THROTTLE_SCALE, no unreliable
  214. packets are dropped by ENET, and so 100% of all unreliable packets will be sent. When the
  215. throttle has a value of 0, all unreliable packets are dropped by ENet, and so 0% of all
  216. unreliable packets will be sent. Intermediate values for the throttle represent intermediate
  217. probabilities between 0% and 100% of unreliable packets being sent. The bandwidth limits
  218. of the local and foreign host are taken into account to determine a sensible limit for
  219. the throttle probability above which it should not raise even in the best of conditions.