enet.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**
  2. @file enet.h
  3. @brief ENet public header file
  4. */
  5. #ifndef __ENET_ENET_H__
  6. #define __ENET_ENET_H__
  7. #ifdef __cplusplus
  8. extern "C"
  9. {
  10. #endif
  11. #include <stdlib.h>
  12. #ifdef WIN32
  13. #include "enet/win32.h"
  14. #else
  15. #include "enet/unix.h"
  16. #endif
  17. #include "enet/types.h"
  18. #include "enet/protocol.h"
  19. #include "enet/list.h"
  20. #include "enet/callbacks.h"
  21. typedef enum
  22. {
  23. ENET_VERSION = 1
  24. } ENetVersion;
  25. typedef enum
  26. {
  27. ENET_SOCKET_TYPE_STREAM = 1,
  28. ENET_SOCKET_TYPE_DATAGRAM = 2
  29. } ENetSocketType;
  30. typedef enum
  31. {
  32. ENET_SOCKET_WAIT_NONE = 0,
  33. ENET_SOCKET_WAIT_SEND = (1 << 0),
  34. ENET_SOCKET_WAIT_RECEIVE = (1 << 1)
  35. } ENetSocketWait;
  36. enum
  37. {
  38. ENET_HOST_ANY = 0, /**< specifies the default server host */
  39. ENET_HOST_BROADCAST = 0xFFFFFFFF /**< specifies a subnet-wide broadcast */
  40. };
  41. /**
  42. * Portable internet address structure.
  43. *
  44. * The host must be specified in network byte-order, and the port must be in host
  45. * byte-order. The constant ENET_HOST_ANY may be used to specify the default
  46. * server host. The constant ENET_HOST_BROADCAST may be used to specify the
  47. * broadcast address (255.255.255.255). This makes sense for enet_host_connect,
  48. * but not for enet_host_create. Once a server responds to a broadcast, the
  49. * address is updated from ENET_HOST_BROADCAST to the server's actual IP address.
  50. */
  51. typedef struct _ENetAddress
  52. {
  53. enet_uint32 host;
  54. enet_uint16 port;
  55. } ENetAddress;
  56. /**
  57. * Packet flag bit constants.
  58. *
  59. * The host must be specified in network byte-order, and the port must be in
  60. * host byte-order. The constant ENET_HOST_ANY may be used to specify the
  61. * default server host.
  62. @sa ENetPacket
  63. */
  64. typedef enum
  65. {
  66. /** packet must be received by the target peer and resend attempts should be
  67. * made until the packet is delivered */
  68. ENET_PACKET_FLAG_RELIABLE = (1 << 0),
  69. /** packet will not be sequenced with other packets
  70. * not supported for reliable packets
  71. */
  72. ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1)
  73. } ENetPacketFlag;
  74. /**
  75. * ENet packet structure.
  76. *
  77. * An ENet data packet that may be sent to or received from a peer. The shown
  78. * fields should only be read and never modified. The data field contains the
  79. * allocated data for the packet. The dataLength fields specifies the length
  80. * of the allocated data. The flags field is either 0 (specifying no flags),
  81. * or a bitwise-or of any combination of the following flags:
  82. *
  83. * ENET_PACKET_FLAG_RELIABLE - packet must be received by the target peer
  84. * and resend attempts should be made until the packet is delivered
  85. @sa ENetPacketFlag
  86. */
  87. typedef struct _ENetPacket
  88. {
  89. size_t referenceCount; /**< internal use only */
  90. enet_uint32 flags; /**< bitwise or of ENetPacketFlag constants */
  91. enet_uint8 * data; /**< allocated data for packet */
  92. size_t dataLength; /**< length of data */
  93. } ENetPacket;
  94. typedef struct _ENetAcknowledgement
  95. {
  96. ENetListNode acknowledgementList;
  97. enet_uint32 sentTime;
  98. ENetProtocol command;
  99. } ENetAcknowledgement;
  100. typedef struct _ENetOutgoingCommand
  101. {
  102. ENetListNode outgoingCommandList;
  103. enet_uint32 reliableSequenceNumber;
  104. enet_uint32 unreliableSequenceNumber;
  105. enet_uint32 sentTime;
  106. enet_uint32 roundTripTimeout;
  107. enet_uint32 roundTripTimeoutLimit;
  108. enet_uint32 fragmentOffset;
  109. enet_uint16 fragmentLength;
  110. ENetProtocol command;
  111. ENetPacket * packet;
  112. } ENetOutgoingCommand;
  113. typedef struct _ENetIncomingCommand
  114. {
  115. ENetListNode incomingCommandList;
  116. enet_uint32 reliableSequenceNumber;
  117. enet_uint32 unreliableSequenceNumber;
  118. ENetProtocol command;
  119. enet_uint32 fragmentCount;
  120. enet_uint32 fragmentsRemaining;
  121. enet_uint32 * fragments;
  122. ENetPacket * packet;
  123. } ENetIncomingCommand;
  124. typedef enum
  125. {
  126. ENET_PEER_STATE_DISCONNECTED = 0,
  127. ENET_PEER_STATE_CONNECTING = 1,
  128. ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2,
  129. ENET_PEER_STATE_CONNECTED = 3,
  130. ENET_PEER_STATE_DISCONNECTING = 4,
  131. ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 5,
  132. ENET_PEER_STATE_ZOMBIE = 6
  133. } ENetPeerState;
  134. #ifndef ENET_BUFFER_MAXIMUM
  135. #define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
  136. #endif
  137. enum
  138. {
  139. ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024,
  140. ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000,
  141. ENET_HOST_DEFAULT_MTU = 1400,
  142. ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500,
  143. ENET_PEER_DEFAULT_PACKET_THROTTLE = 32,
  144. ENET_PEER_PACKET_THROTTLE_SCALE = 32,
  145. ENET_PEER_PACKET_THROTTLE_COUNTER = 7,
  146. ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2,
  147. ENET_PEER_PACKET_THROTTLE_DECELERATION = 2,
  148. ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000,
  149. ENET_PEER_PACKET_LOSS_SCALE = (1 << 16),
  150. ENET_PEER_PACKET_LOSS_INTERVAL = 10000,
  151. ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
  152. ENET_PEER_TIMEOUT_LIMIT = 32,
  153. ENET_PEER_TIMEOUT_MINIMUM = 3000,
  154. ENET_PEER_TIMEOUT_MAXIMUM = 30000,
  155. ENET_PEER_PING_INTERVAL = 500,
  156. ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 4 * 32,
  157. };
  158. typedef struct _ENetChannel
  159. {
  160. enet_uint32 outgoingReliableSequenceNumber;
  161. enet_uint32 outgoingUnreliableSequenceNumber;
  162. enet_uint32 incomingReliableSequenceNumber;
  163. enet_uint32 incomingUnreliableSequenceNumber;
  164. ENetList incomingReliableCommands;
  165. ENetList incomingUnreliableCommands;
  166. } ENetChannel;
  167. /**
  168. * An ENet peer which data packets may be sent or received from.
  169. *
  170. * No fields should be modified unless otherwise specified.
  171. */
  172. typedef struct _ENetPeer
  173. {
  174. struct _ENetHost * host;
  175. enet_uint16 outgoingPeerID;
  176. enet_uint16 incomingPeerID;
  177. enet_uint32 challenge;
  178. ENetAddress address; /**< Internet address of the peer */
  179. void * data; /**< Application private data, may be freely modified */
  180. ENetPeerState state;
  181. ENetChannel * channels;
  182. size_t channelCount; /**< Number of channels allocated for communication with peer */
  183. enet_uint32 incomingBandwidth; /**< Downstream bandwidth of the client in bytes/second */
  184. enet_uint32 outgoingBandwidth; /**< Upstream bandwidth of the client in bytes/second */
  185. enet_uint32 incomingBandwidthThrottleEpoch;
  186. enet_uint32 outgoingBandwidthThrottleEpoch;
  187. enet_uint32 incomingDataTotal;
  188. enet_uint32 outgoingDataTotal;
  189. enet_uint32 lastSendTime;
  190. enet_uint32 lastReceiveTime;
  191. enet_uint32 nextTimeout;
  192. enet_uint32 earliestTimeout;
  193. enet_uint32 packetLossEpoch;
  194. enet_uint32 packetsSent;
  195. enet_uint32 packetsLost;
  196. enet_uint32 packetLoss; /**< mean packet loss of reliable packets as a ratio with respect to the constant ENET_PEER_PACKET_LOSS_SCALE */
  197. enet_uint32 packetLossVariance;
  198. enet_uint32 packetThrottle;
  199. enet_uint32 packetThrottleLimit;
  200. enet_uint32 packetThrottleCounter;
  201. enet_uint32 packetThrottleEpoch;
  202. enet_uint32 packetThrottleAcceleration;
  203. enet_uint32 packetThrottleDeceleration;
  204. enet_uint32 packetThrottleInterval;
  205. enet_uint32 lastRoundTripTime;
  206. enet_uint32 lowestRoundTripTime;
  207. enet_uint32 lastRoundTripTimeVariance;
  208. enet_uint32 highestRoundTripTimeVariance;
  209. enet_uint32 roundTripTime; /**< mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgement */
  210. enet_uint32 roundTripTimeVariance;
  211. enet_uint16 mtu;
  212. enet_uint32 windowSize;
  213. enet_uint32 reliableDataInTransit;
  214. enet_uint32 outgoingReliableSequenceNumber;
  215. ENetList acknowledgements;
  216. ENetList sentReliableCommands;
  217. ENetList sentUnreliableCommands;
  218. ENetList outgoingReliableCommands;
  219. ENetList outgoingUnreliableCommands;
  220. enet_uint32 incomingUnsequencedGroup;
  221. enet_uint32 outgoingUnsequencedGroup;
  222. enet_uint32 unsequencedWindow [ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
  223. } ENetPeer;
  224. /** An ENet host for communicating with peers.
  225. *
  226. * No fields should be modified.
  227. @sa enet_host_create()
  228. @sa enet_host_destroy()
  229. @sa enet_host_connect()
  230. @sa enet_host_service()
  231. @sa enet_host_flush()
  232. @sa enet_host_broadcast()
  233. @sa enet_host_bandwidth_limit()
  234. @sa enet_host_bandwidth_throttle()
  235. */
  236. typedef struct _ENetHost
  237. {
  238. ENetSocket socket;
  239. ENetAddress address; /**< Internet address of the host */
  240. enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
  241. enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */
  242. enet_uint32 bandwidthThrottleEpoch;
  243. enet_uint32 mtu;
  244. int recalculateBandwidthLimits;
  245. ENetPeer * peers; /**< array of peers allocated for this host */
  246. size_t peerCount; /**< number of peers allocated for this host */
  247. ENetPeer * lastServicedPeer;
  248. size_t packetSize;
  249. ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
  250. size_t commandCount;
  251. ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
  252. size_t bufferCount;
  253. ENetAddress receivedAddress;
  254. enet_uint8 receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
  255. size_t receivedDataLength;
  256. } ENetHost;
  257. /**
  258. * An ENet event type, as specified in @ref ENetEvent.
  259. */
  260. typedef enum
  261. {
  262. /** no event occurred within the specified time limit */
  263. ENET_EVENT_TYPE_NONE = 0,
  264. /** a connection request initiated by enet_host_connect has completed.
  265. * The peer field contains the peer which successfully connected.
  266. */
  267. ENET_EVENT_TYPE_CONNECT = 1,
  268. /** a peer has disconnected. This event is generated on a successful
  269. * completion of a disconnect initiated by enet_pper_disconnect, if
  270. * a peer has timed out, or if a connection request intialized by
  271. * enet_host_connect has timed out. The peer field contains the peer
  272. * which disconnected.
  273. */
  274. ENET_EVENT_TYPE_DISCONNECT = 2,
  275. /** a packet has been received from a peer. The peer field specifies the
  276. * peer which sent the packet. The channelID field specifies the channel
  277. * number upon which the packet was received. The packet field contains
  278. * the packet that was received; this packet must be destroyed with
  279. * enet_packet_destroy after use.
  280. */
  281. ENET_EVENT_TYPE_RECEIVE = 3
  282. } ENetEventType;
  283. /**
  284. * An ENet event as returned by enet_host_service().
  285. @sa enet_host_service
  286. */
  287. typedef struct _ENetEvent
  288. {
  289. ENetEventType type; /**< type of the event */
  290. ENetPeer * peer; /**< peer that generated a connect, disconnect or receive event */
  291. enet_uint8 channelID;
  292. ENetPacket * packet;
  293. } ENetEvent;
  294. /** @defgroup global ENet global functions
  295. @{
  296. */
  297. /**
  298. Initializes ENet globally. Must be called prior to using any functions in
  299. ENet.
  300. @returns 0 on success, < 0 on failure
  301. */
  302. ENET_API int enet_initialize (void);
  303. /**
  304. Initializes ENet and sets the callbacks provided in the ENetCallbacks structure.
  305. Callbacks that are set to NULL will simply use the default ENet functions.
  306. ENET_VERSION should be passed in as the first argument so that ENet may verify the
  307. callbacks available.
  308. @returns 0 on success, < 0 on failure
  309. */
  310. ENET_API int enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits);
  311. /**
  312. Shuts down ENet globally. Should be called when a program that has
  313. initialized ENet exits.
  314. */
  315. ENET_API void enet_deinitialize (void);
  316. /** @} */
  317. /** @defgroup private ENet private implementation functions */
  318. /**
  319. Returns the wall-time in milliseconds. Its initial value is unspecified
  320. unless otherwise set.
  321. */
  322. ENET_API enet_uint32 enet_time_get (void);
  323. /**
  324. Sets the current wall-time in milliseconds.
  325. */
  326. ENET_API void enet_time_set (enet_uint32);
  327. /** @defgroup socket ENet socket functions
  328. @{
  329. @ingroup private
  330. */
  331. extern ENetSocket enet_socket_create (ENetSocketType, const ENetAddress *);
  332. extern ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
  333. extern int enet_socket_connect (ENetSocket, const ENetAddress *);
  334. extern int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
  335. extern int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t);
  336. extern int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
  337. extern void enet_socket_destroy (ENetSocket);
  338. /** @} */
  339. /** @defgroup Address ENet address functions
  340. @{
  341. */
  342. /** Attempts to resolve the host named by the parameter hostName and sets
  343. the host field in the address parameter if successful.
  344. @param address destination to store resolved address
  345. @param hostName host name to lookup
  346. @retval 0 on success
  347. @retval < 0 on failure
  348. @returns the address of the given hostName in address on success
  349. */
  350. ENET_API int enet_address_set_host (ENetAddress *address, const char *hostName );
  351. /** Attempts to do a reserve lookup of the host field in the address parameter.
  352. @param address address used for reverse lookup
  353. @param hostName destination for name, must not be NULL
  354. @param nameLength maximum length of hostName.
  355. @returns the null-terminated name of the host in hostName on success
  356. @retval 0 on success
  357. @retval < 0 on failure
  358. */
  359. ENET_API int enet_address_get_host (const ENetAddress *address, char *hostName, size_t nameLength );
  360. /** @} */
  361. ENET_API ENetPacket * enet_packet_create (const void *dataContents, size_t dataLength, enet_uint32 flags);
  362. ENET_API void enet_packet_destroy (ENetPacket *packet );
  363. ENET_API int enet_packet_resize (ENetPacket *packet, size_t dataLength );
  364. ENET_API ENetHost * enet_host_create (const ENetAddress *address, size_t peerCount, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth );
  365. ENET_API void enet_host_destroy (ENetHost *host );
  366. ENET_API ENetPeer * enet_host_connect (ENetHost *host, const ENetAddress *address, size_t channelCount );
  367. ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32);
  368. ENET_API void enet_host_flush (ENetHost *);
  369. ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *);
  370. ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32);
  371. extern void enet_host_bandwidth_throttle (ENetHost *);
  372. ENET_API int enet_peer_send (ENetPeer *, enet_uint8, ENetPacket *);
  373. ENET_API ENetPacket * enet_peer_receive (ENetPeer *, enet_uint8);
  374. ENET_API void enet_peer_ping (ENetPeer *);
  375. ENET_API void enet_peer_reset (ENetPeer *);
  376. ENET_API void enet_peer_disconnect (ENetPeer *);
  377. ENET_API void enet_peer_disconnect_now (ENetPeer *);
  378. ENET_API void enet_peer_throttle_configure (ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
  379. extern int enet_peer_throttle (ENetPeer *, enet_uint32);
  380. extern void enet_peer_reset_queues (ENetPeer *);
  381. extern ENetOutgoingCommand * enet_peer_queue_outgoing_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32, enet_uint16);
  382. extern ENetIncomingCommand * enet_peer_queue_incoming_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32);
  383. extern ENetAcknowledgement * enet_peer_queue_acknowledgement (ENetPeer *, const ENetProtocol *, enet_uint32);
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif /* __ENET_ENET_H__ */