enet.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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/time.h"
  21. #include "enet/utility.h"
  22. #include "enet/callbacks.h"
  23. #define ENET_VERSION_MAJOR 1
  24. #define ENET_VERSION_MINOR 3
  25. #define ENET_VERSION_PATCH 13
  26. #define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
  27. #define ENET_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF)
  28. #define ENET_VERSION_GET_MINOR(version) (((version)>>8)&0xFF)
  29. #define ENET_VERSION_GET_PATCH(version) ((version)&0xFF)
  30. #define ENET_VERSION ENET_VERSION_CREATE(ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH)
  31. typedef enet_uint32 ENetVersion;
  32. struct _ENetHost;
  33. struct _ENetEvent;
  34. struct _ENetPacket;
  35. typedef enum _ENetSocketType
  36. {
  37. ENET_SOCKET_TYPE_STREAM = 1,
  38. ENET_SOCKET_TYPE_DATAGRAM = 2
  39. } ENetSocketType;
  40. typedef enum _ENetSocketWait
  41. {
  42. ENET_SOCKET_WAIT_NONE = 0,
  43. ENET_SOCKET_WAIT_SEND = (1 << 0),
  44. ENET_SOCKET_WAIT_RECEIVE = (1 << 1),
  45. ENET_SOCKET_WAIT_INTERRUPT = (1 << 2)
  46. } ENetSocketWait;
  47. typedef enum _ENetSocketOption
  48. {
  49. ENET_SOCKOPT_NONBLOCK = 1,
  50. ENET_SOCKOPT_BROADCAST = 2,
  51. ENET_SOCKOPT_RCVBUF = 3,
  52. ENET_SOCKOPT_SNDBUF = 4,
  53. ENET_SOCKOPT_REUSEADDR = 5,
  54. ENET_SOCKOPT_RCVTIMEO = 6,
  55. ENET_SOCKOPT_SNDTIMEO = 7,
  56. ENET_SOCKOPT_ERROR = 8,
  57. ENET_SOCKOPT_NODELAY = 9
  58. } ENetSocketOption;
  59. typedef enum _ENetSocketShutdown
  60. {
  61. ENET_SOCKET_SHUTDOWN_READ = 0,
  62. ENET_SOCKET_SHUTDOWN_WRITE = 1,
  63. ENET_SOCKET_SHUTDOWN_READ_WRITE = 2
  64. } ENetSocketShutdown;
  65. #define ENET_HOST_ANY 0
  66. #define ENET_HOST_BROADCAST 0xFFFFFFFFU
  67. #define ENET_PORT_ANY 0
  68. /**
  69. * Portable internet address structure.
  70. *
  71. * The host must be specified in network byte-order, and the port must be in host
  72. * byte-order. The constant ENET_HOST_ANY may be used to specify the default
  73. * server host. The constant ENET_HOST_BROADCAST may be used to specify the
  74. * broadcast address (255.255.255.255). This makes sense for enet_host_connect,
  75. * but not for enet_host_create. Once a server responds to a broadcast, the
  76. * address is updated from ENET_HOST_BROADCAST to the server's actual IP address.
  77. */
  78. typedef struct _ENetAddress
  79. {
  80. enet_uint32 host;
  81. enet_uint16 port;
  82. } ENetAddress;
  83. /**
  84. * Packet flag bit constants.
  85. *
  86. * The host must be specified in network byte-order, and the port must be in
  87. * host byte-order. The constant ENET_HOST_ANY may be used to specify the
  88. * default server host.
  89. @sa ENetPacket
  90. */
  91. typedef enum _ENetPacketFlag
  92. {
  93. /** packet must be received by the target peer and resend attempts should be
  94. * made until the packet is delivered */
  95. ENET_PACKET_FLAG_RELIABLE = (1 << 0),
  96. /** packet will not be sequenced with other packets
  97. * not supported for reliable packets
  98. */
  99. ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1),
  100. /** packet will not allocate data, and user must supply it instead */
  101. ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2),
  102. /** packet will be fragmented using unreliable (instead of reliable) sends
  103. * if it exceeds the MTU */
  104. ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT = (1 << 3),
  105. /** whether the packet has been sent from all queues it has been entered into */
  106. ENET_PACKET_FLAG_SENT = (1<<8)
  107. } ENetPacketFlag;
  108. typedef void (ENET_CALLBACK * ENetPacketFreeCallback) (struct _ENetPacket *);
  109. /**
  110. * ENet packet structure.
  111. *
  112. * An ENet data packet that may be sent to or received from a peer. The shown
  113. * fields should only be read and never modified. The data field contains the
  114. * allocated data for the packet. The dataLength fields specifies the length
  115. * of the allocated data. The flags field is either 0 (specifying no flags),
  116. * or a bitwise-or of any combination of the following flags:
  117. *
  118. * ENET_PACKET_FLAG_RELIABLE - packet must be received by the target peer
  119. * and resend attempts should be made until the packet is delivered
  120. *
  121. * ENET_PACKET_FLAG_UNSEQUENCED - packet will not be sequenced with other packets
  122. * (not supported for reliable packets)
  123. *
  124. * ENET_PACKET_FLAG_NO_ALLOCATE - packet will not allocate data, and user must supply it instead
  125. *
  126. * ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT - packet will be fragmented using unreliable
  127. * (instead of reliable) sends if it exceeds the MTU
  128. *
  129. * ENET_PACKET_FLAG_SENT - whether the packet has been sent from all queues it has been entered into
  130. @sa ENetPacketFlag
  131. */
  132. typedef struct _ENetPacket
  133. {
  134. size_t referenceCount; /**< internal use only */
  135. enet_uint32 flags; /**< bitwise-or of ENetPacketFlag constants */
  136. enet_uint8 * data; /**< allocated data for packet */
  137. size_t dataLength; /**< length of data */
  138. ENetPacketFreeCallback freeCallback; /**< function to be called when the packet is no longer in use */
  139. void * userData; /**< application private data, may be freely modified */
  140. } ENetPacket;
  141. typedef struct _ENetAcknowledgement
  142. {
  143. ENetListNode acknowledgementList;
  144. enet_uint32 sentTime;
  145. ENetProtocol command;
  146. } ENetAcknowledgement;
  147. typedef struct _ENetOutgoingCommand
  148. {
  149. ENetListNode outgoingCommandList;
  150. enet_uint16 reliableSequenceNumber;
  151. enet_uint16 unreliableSequenceNumber;
  152. enet_uint32 sentTime;
  153. enet_uint32 roundTripTimeout;
  154. enet_uint32 roundTripTimeoutLimit;
  155. enet_uint32 fragmentOffset;
  156. enet_uint16 fragmentLength;
  157. enet_uint16 sendAttempts;
  158. ENetProtocol command;
  159. ENetPacket * packet;
  160. } ENetOutgoingCommand;
  161. typedef struct _ENetIncomingCommand
  162. {
  163. ENetListNode incomingCommandList;
  164. enet_uint16 reliableSequenceNumber;
  165. enet_uint16 unreliableSequenceNumber;
  166. ENetProtocol command;
  167. enet_uint32 fragmentCount;
  168. enet_uint32 fragmentsRemaining;
  169. enet_uint32 * fragments;
  170. ENetPacket * packet;
  171. } ENetIncomingCommand;
  172. typedef enum _ENetPeerState
  173. {
  174. ENET_PEER_STATE_DISCONNECTED = 0,
  175. ENET_PEER_STATE_CONNECTING = 1,
  176. ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2,
  177. ENET_PEER_STATE_CONNECTION_PENDING = 3,
  178. ENET_PEER_STATE_CONNECTION_SUCCEEDED = 4,
  179. ENET_PEER_STATE_CONNECTED = 5,
  180. ENET_PEER_STATE_DISCONNECT_LATER = 6,
  181. ENET_PEER_STATE_DISCONNECTING = 7,
  182. ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 8,
  183. ENET_PEER_STATE_ZOMBIE = 9
  184. } ENetPeerState;
  185. #ifndef ENET_BUFFER_MAXIMUM
  186. #define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
  187. #endif
  188. enum
  189. {
  190. ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024,
  191. ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024,
  192. ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000,
  193. ENET_HOST_DEFAULT_MTU = 1400,
  194. ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE = 32 * 1024 * 1024,
  195. ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA = 32 * 1024 * 1024,
  196. ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500,
  197. ENET_PEER_DEFAULT_PACKET_THROTTLE = 32,
  198. ENET_PEER_PACKET_THROTTLE_SCALE = 32,
  199. ENET_PEER_PACKET_THROTTLE_COUNTER = 7,
  200. ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2,
  201. ENET_PEER_PACKET_THROTTLE_DECELERATION = 2,
  202. ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000,
  203. ENET_PEER_PACKET_LOSS_SCALE = (1 << 16),
  204. ENET_PEER_PACKET_LOSS_INTERVAL = 10000,
  205. ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
  206. ENET_PEER_TIMEOUT_LIMIT = 32,
  207. ENET_PEER_TIMEOUT_MINIMUM = 5000,
  208. ENET_PEER_TIMEOUT_MAXIMUM = 30000,
  209. ENET_PEER_PING_INTERVAL = 500,
  210. ENET_PEER_UNSEQUENCED_WINDOWS = 64,
  211. ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024,
  212. ENET_PEER_FREE_UNSEQUENCED_WINDOWS = 32,
  213. ENET_PEER_RELIABLE_WINDOWS = 16,
  214. ENET_PEER_RELIABLE_WINDOW_SIZE = 0x1000,
  215. ENET_PEER_FREE_RELIABLE_WINDOWS = 8
  216. };
  217. typedef struct _ENetChannel
  218. {
  219. enet_uint16 outgoingReliableSequenceNumber;
  220. enet_uint16 outgoingUnreliableSequenceNumber;
  221. enet_uint16 usedReliableWindows;
  222. enet_uint16 reliableWindows [ENET_PEER_RELIABLE_WINDOWS];
  223. enet_uint16 incomingReliableSequenceNumber;
  224. enet_uint16 incomingUnreliableSequenceNumber;
  225. ENetList incomingReliableCommands;
  226. ENetList incomingUnreliableCommands;
  227. } ENetChannel;
  228. /**
  229. * An ENet peer which data packets may be sent or received from.
  230. *
  231. * No fields should be modified unless otherwise specified.
  232. */
  233. typedef struct _ENetPeer
  234. {
  235. ENetListNode dispatchList;
  236. struct _ENetHost * host;
  237. enet_uint16 outgoingPeerID;
  238. enet_uint16 incomingPeerID;
  239. enet_uint32 connectID;
  240. enet_uint8 outgoingSessionID;
  241. enet_uint8 incomingSessionID;
  242. ENetAddress address; /**< Internet address of the peer */
  243. void * data; /**< Application private data, may be freely modified */
  244. ENetPeerState state;
  245. ENetChannel * channels;
  246. size_t channelCount; /**< Number of channels allocated for communication with peer */
  247. enet_uint32 incomingBandwidth; /**< Downstream bandwidth of the client in bytes/second */
  248. enet_uint32 outgoingBandwidth; /**< Upstream bandwidth of the client in bytes/second */
  249. enet_uint32 incomingBandwidthThrottleEpoch;
  250. enet_uint32 outgoingBandwidthThrottleEpoch;
  251. enet_uint32 incomingDataTotal;
  252. enet_uint32 outgoingDataTotal;
  253. enet_uint32 lastSendTime;
  254. enet_uint32 lastReceiveTime;
  255. enet_uint32 nextTimeout;
  256. enet_uint32 earliestTimeout;
  257. enet_uint32 packetLossEpoch;
  258. enet_uint32 packetsSent;
  259. enet_uint32 packetsLost;
  260. enet_uint32 packetLoss; /**< mean packet loss of reliable packets as a ratio with respect to the constant ENET_PEER_PACKET_LOSS_SCALE */
  261. enet_uint32 packetLossVariance;
  262. enet_uint32 packetThrottle;
  263. enet_uint32 packetThrottleLimit;
  264. enet_uint32 packetThrottleCounter;
  265. enet_uint32 packetThrottleEpoch;
  266. enet_uint32 packetThrottleAcceleration;
  267. enet_uint32 packetThrottleDeceleration;
  268. enet_uint32 packetThrottleInterval;
  269. enet_uint32 pingInterval;
  270. enet_uint32 timeoutLimit;
  271. enet_uint32 timeoutMinimum;
  272. enet_uint32 timeoutMaximum;
  273. enet_uint32 lastRoundTripTime;
  274. enet_uint32 lowestRoundTripTime;
  275. enet_uint32 lastRoundTripTimeVariance;
  276. enet_uint32 highestRoundTripTimeVariance;
  277. enet_uint32 roundTripTime; /**< mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgement */
  278. enet_uint32 roundTripTimeVariance;
  279. enet_uint32 mtu;
  280. enet_uint32 windowSize;
  281. enet_uint32 reliableDataInTransit;
  282. enet_uint16 outgoingReliableSequenceNumber;
  283. ENetList acknowledgements;
  284. ENetList sentReliableCommands;
  285. ENetList sentUnreliableCommands;
  286. ENetList outgoingReliableCommands;
  287. ENetList outgoingUnreliableCommands;
  288. ENetList dispatchedCommands;
  289. int needsDispatch;
  290. enet_uint16 incomingUnsequencedGroup;
  291. enet_uint16 outgoingUnsequencedGroup;
  292. enet_uint32 unsequencedWindow [ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
  293. enet_uint32 eventData;
  294. size_t totalWaitingData;
  295. } ENetPeer;
  296. /** An ENet packet compressor for compressing UDP packets before socket sends or receives.
  297. */
  298. typedef struct _ENetCompressor
  299. {
  300. /** Context data for the compressor. Must be non-NULL. */
  301. void * context;
  302. /** Compresses from inBuffers[0:inBufferCount-1], containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */
  303. size_t (ENET_CALLBACK * compress) (void * context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit);
  304. /** Decompresses from inData, containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */
  305. size_t (ENET_CALLBACK * decompress) (void * context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit);
  306. /** Destroys the context when compression is disabled or the host is destroyed. May be NULL. */
  307. void (ENET_CALLBACK * destroy) (void * context);
  308. } ENetCompressor;
  309. /** Callback that computes the checksum of the data held in buffers[0:bufferCount-1] */
  310. typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * buffers, size_t bufferCount);
  311. /** Callback for intercepting received raw UDP packets. Should return 1 to intercept, 0 to ignore, or -1 to propagate an error. */
  312. typedef int (ENET_CALLBACK * ENetInterceptCallback) (struct _ENetHost * host, struct _ENetEvent * event);
  313. /** An ENet host for communicating with peers.
  314. *
  315. * No fields should be modified unless otherwise stated.
  316. @sa enet_host_create()
  317. @sa enet_host_destroy()
  318. @sa enet_host_connect()
  319. @sa enet_host_service()
  320. @sa enet_host_flush()
  321. @sa enet_host_broadcast()
  322. @sa enet_host_compress()
  323. @sa enet_host_compress_with_range_coder()
  324. @sa enet_host_channel_limit()
  325. @sa enet_host_bandwidth_limit()
  326. @sa enet_host_bandwidth_throttle()
  327. */
  328. typedef struct _ENetHost
  329. {
  330. ENetSocket socket;
  331. ENetAddress address; /**< Internet address of the host */
  332. enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
  333. enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */
  334. enet_uint32 bandwidthThrottleEpoch;
  335. enet_uint32 mtu;
  336. enet_uint32 randomSeed;
  337. int recalculateBandwidthLimits;
  338. ENetPeer * peers; /**< array of peers allocated for this host */
  339. size_t peerCount; /**< number of peers allocated for this host */
  340. size_t channelLimit; /**< maximum number of channels allowed for connected peers */
  341. enet_uint32 serviceTime;
  342. ENetList dispatchQueue;
  343. int continueSending;
  344. size_t packetSize;
  345. enet_uint16 headerFlags;
  346. ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
  347. size_t commandCount;
  348. ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
  349. size_t bufferCount;
  350. ENetChecksumCallback checksum; /**< callback the user can set to enable packet checksums for this host */
  351. ENetCompressor compressor;
  352. enet_uint8 packetData [2][ENET_PROTOCOL_MAXIMUM_MTU];
  353. ENetAddress receivedAddress;
  354. enet_uint8 * receivedData;
  355. size_t receivedDataLength;
  356. enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */
  357. enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */
  358. enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */
  359. enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */
  360. ENetInterceptCallback intercept; /**< callback the user can set to intercept received raw UDP packets */
  361. size_t connectedPeers;
  362. size_t bandwidthLimitedPeers;
  363. size_t duplicatePeers; /**< optional number of allowed peers from duplicate IPs, defaults to ENET_PROTOCOL_MAXIMUM_PEER_ID */
  364. size_t maximumPacketSize; /**< the maximum allowable packet size that may be sent or received on a peer */
  365. size_t maximumWaitingData; /**< the maximum aggregate amount of buffer space a peer may use waiting for packets to be delivered */
  366. } ENetHost;
  367. /**
  368. * An ENet event type, as specified in @ref ENetEvent.
  369. */
  370. typedef enum _ENetEventType
  371. {
  372. /** no event occurred within the specified time limit */
  373. ENET_EVENT_TYPE_NONE = 0,
  374. /** a connection request initiated by enet_host_connect has completed.
  375. * The peer field contains the peer which successfully connected.
  376. */
  377. ENET_EVENT_TYPE_CONNECT = 1,
  378. /** a peer has disconnected. This event is generated on a successful
  379. * completion of a disconnect initiated by enet_peer_disconnect, if
  380. * a peer has timed out, or if a connection request intialized by
  381. * enet_host_connect has timed out. The peer field contains the peer
  382. * which disconnected. The data field contains user supplied data
  383. * describing the disconnection, or 0, if none is available.
  384. */
  385. ENET_EVENT_TYPE_DISCONNECT = 2,
  386. /** a packet has been received from a peer. The peer field specifies the
  387. * peer which sent the packet. The channelID field specifies the channel
  388. * number upon which the packet was received. The packet field contains
  389. * the packet that was received; this packet must be destroyed with
  390. * enet_packet_destroy after use.
  391. */
  392. ENET_EVENT_TYPE_RECEIVE = 3
  393. } ENetEventType;
  394. /**
  395. * An ENet event as returned by enet_host_service().
  396. @sa enet_host_service
  397. */
  398. typedef struct _ENetEvent
  399. {
  400. ENetEventType type; /**< type of the event */
  401. ENetPeer * peer; /**< peer that generated a connect, disconnect or receive event */
  402. enet_uint8 channelID; /**< channel on the peer that generated the event, if appropriate */
  403. enet_uint32 data; /**< data associated with the event, if appropriate */
  404. ENetPacket * packet; /**< packet associated with the event, if appropriate */
  405. } ENetEvent;
  406. /** @defgroup global ENet global functions
  407. @{
  408. */
  409. /**
  410. Initializes ENet globally. Must be called prior to using any functions in
  411. ENet.
  412. @returns 0 on success, < 0 on failure
  413. */
  414. ENET_API int enet_initialize (void);
  415. /**
  416. 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. Make sure the ENetCallbacks structure is zeroed out so that any additional callbacks added in future versions will be properly ignored.
  417. @param version the constant ENET_VERSION should be supplied so ENet knows which version of ENetCallbacks struct to use
  418. @param inits user-overridden callbacks where any NULL callbacks will use ENet's defaults
  419. @returns 0 on success, < 0 on failure
  420. */
  421. ENET_API int enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits);
  422. /**
  423. Shuts down ENet globally. Should be called when a program that has
  424. initialized ENet exits.
  425. */
  426. ENET_API void enet_deinitialize (void);
  427. /**
  428. Gives the linked version of the ENet library.
  429. @returns the version number
  430. */
  431. ENET_API ENetVersion enet_linked_version (void);
  432. /** @} */
  433. /** @defgroup private ENet private implementation functions */
  434. /**
  435. Returns the wall-time in milliseconds. Its initial value is unspecified
  436. unless otherwise set.
  437. */
  438. ENET_API enet_uint64 enet_time_get (void);
  439. /**
  440. Sets the current wall-time in milliseconds.
  441. */
  442. ENET_API void enet_time_set (enet_uint64);
  443. /** @defgroup socket ENet socket functions
  444. @{
  445. */
  446. ENET_API ENetSocket enet_socket_create (ENetSocketType);
  447. ENET_API int enet_socket_bind (ENetSocket, const ENetAddress *);
  448. ENET_API int enet_socket_get_address (ENetSocket, ENetAddress *);
  449. ENET_API int enet_socket_listen (ENetSocket, int);
  450. ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
  451. ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *);
  452. ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
  453. ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t);
  454. ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint64);
  455. ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int);
  456. ENET_API int enet_socket_get_option (ENetSocket, ENetSocketOption, int *);
  457. ENET_API int enet_socket_shutdown (ENetSocket, ENetSocketShutdown);
  458. ENET_API void enet_socket_destroy (ENetSocket);
  459. ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32);
  460. /** @} */
  461. /** @defgroup Address ENet address functions
  462. @{
  463. */
  464. /** Attempts to parse the printable form of the IP address in the parameter hostName
  465. and sets the host field in the address parameter if successful.
  466. @param address destination to store the parsed IP address
  467. @param hostName IP address to parse
  468. @retval 0 on success
  469. @retval < 0 on failure
  470. @returns the address of the given hostName in address on success
  471. */
  472. ENET_API int enet_address_set_host_ip (ENetAddress * address, const char * hostName);
  473. /** Attempts to resolve the host named by the parameter hostName and sets
  474. the host field in the address parameter if successful.
  475. @param address destination to store resolved address
  476. @param hostName host name to lookup
  477. @retval 0 on success
  478. @retval < 0 on failure
  479. @returns the address of the given hostName in address on success
  480. */
  481. ENET_API int enet_address_set_host (ENetAddress * address, const char * hostName);
  482. /** Gives the printable form of the IP address specified in the address parameter.
  483. @param address address printed
  484. @param hostName destination for name, must not be NULL
  485. @param nameLength maximum length of hostName.
  486. @returns the null-terminated name of the host in hostName on success
  487. @retval 0 on success
  488. @retval < 0 on failure
  489. */
  490. ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostName, size_t nameLength);
  491. /** Attempts to do a reverse lookup of the host field in the address parameter.
  492. @param address address used for reverse lookup
  493. @param hostName destination for name, must not be NULL
  494. @param nameLength maximum length of hostName.
  495. @returns the null-terminated name of the host in hostName on success
  496. @retval 0 on success
  497. @retval < 0 on failure
  498. */
  499. ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength);
  500. /** @} */
  501. ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32);
  502. ENET_API void enet_packet_destroy (ENetPacket *);
  503. ENET_API int enet_packet_resize (ENetPacket *, size_t);
  504. ENET_API enet_uint32 enet_crc32 (const ENetBuffer *, size_t);
  505. ENET_API ENetHost * enet_host_create (const ENetAddress *, size_t, size_t, enet_uint32, enet_uint32);
  506. ENET_API void enet_host_destroy (ENetHost *);
  507. ENET_API ENetPeer * enet_host_connect (ENetHost *, const ENetAddress *, size_t, enet_uint32);
  508. ENET_API int enet_host_check_events (ENetHost *, ENetEvent *);
  509. ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32);
  510. ENET_API void enet_host_flush (ENetHost *);
  511. ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *);
  512. ENET_API void enet_host_compress (ENetHost *, const ENetCompressor *);
  513. ENET_API int enet_host_compress_with_range_coder (ENetHost * host);
  514. ENET_API void enet_host_channel_limit (ENetHost *, size_t);
  515. ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32);
  516. extern void enet_host_bandwidth_throttle (ENetHost *);
  517. extern enet_uint64 enet_host_random_seed (void);
  518. ENET_API int enet_peer_send (ENetPeer *, enet_uint8, ENetPacket *);
  519. ENET_API ENetPacket * enet_peer_receive (ENetPeer *, enet_uint8 * channelID);
  520. ENET_API void enet_peer_ping (ENetPeer *);
  521. ENET_API void enet_peer_ping_interval (ENetPeer *, enet_uint32);
  522. ENET_API void enet_peer_timeout (ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
  523. ENET_API void enet_peer_reset (ENetPeer *);
  524. ENET_API void enet_peer_disconnect (ENetPeer *, enet_uint32);
  525. ENET_API void enet_peer_disconnect_now (ENetPeer *, enet_uint32);
  526. ENET_API void enet_peer_disconnect_later (ENetPeer *, enet_uint32);
  527. ENET_API void enet_peer_throttle_configure (ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
  528. extern int enet_peer_throttle (ENetPeer *, enet_uint32);
  529. extern void enet_peer_reset_queues (ENetPeer *);
  530. extern void enet_peer_setup_outgoing_command (ENetPeer *, ENetOutgoingCommand *);
  531. extern ENetOutgoingCommand * enet_peer_queue_outgoing_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32, enet_uint16);
  532. extern ENetIncomingCommand * enet_peer_queue_incoming_command (ENetPeer *, const ENetProtocol *, const void *, size_t, enet_uint32, enet_uint32);
  533. extern ENetAcknowledgement * enet_peer_queue_acknowledgement (ENetPeer *, const ENetProtocol *, enet_uint16);
  534. extern void enet_peer_dispatch_incoming_unreliable_commands (ENetPeer *, ENetChannel *);
  535. extern void enet_peer_dispatch_incoming_reliable_commands (ENetPeer *, ENetChannel *);
  536. extern void enet_peer_on_connect (ENetPeer *);
  537. extern void enet_peer_on_disconnect (ENetPeer *);
  538. ENET_API void * enet_range_coder_create (void);
  539. ENET_API void enet_range_coder_destroy (void *);
  540. ENET_API size_t enet_range_coder_compress (void *, const ENetBuffer *, size_t, size_t, enet_uint8 *, size_t);
  541. ENET_API size_t enet_range_coder_decompress (void *, const enet_uint8 *, size_t, enet_uint8 *, size_t);
  542. extern size_t enet_protocol_command_size (enet_uint8);
  543. #ifdef __cplusplus
  544. }
  545. #endif
  546. #endif /* __ENET_ENET_H__ */