enet.h 18 KB

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