3
@brief ENet public header file
5
#ifndef __ENET_ENET_H__
6
#define __ENET_ENET_H__
16
#include "enet/win32.h"
18
#include "enet/unix.h"
21
#include "enet/types.h"
22
#include "enet/protocol.h"
23
#include "enet/list.h"
24
#include "enet/callbacks.h"
26
#define ENET_VERSION_MAJOR 1
27
#define ENET_VERSION_MINOR 3
28
#define ENET_VERSION_PATCH 3
29
#define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
30
#define ENET_VERSION ENET_VERSION_CREATE(ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH)
32
typedef enet_uint32 ENetVersion;
34
typedef enum _ENetSocketType
36
ENET_SOCKET_TYPE_STREAM = 1,
37
ENET_SOCKET_TYPE_DATAGRAM = 2
40
typedef enum _ENetSocketWait
42
ENET_SOCKET_WAIT_NONE = 0,
43
ENET_SOCKET_WAIT_SEND = (1 << 0),
44
ENET_SOCKET_WAIT_RECEIVE = (1 << 1)
47
typedef enum _ENetSocketOption
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
60
ENET_HOST_ANY = 0, /**< specifies the default server host */
61
ENET_HOST_BROADCAST = 0xFFFFFFFF, /**< specifies a subnet-wide broadcast */
63
ENET_PORT_ANY = 0 /**< specifies that a port should be automatically chosen */
67
* Portable internet address structure.
69
* The host must be specified in network byte-order, and the port must be in host
70
* byte-order. The constant ENET_HOST_ANY may be used to specify the default
71
* server host. The constant ENET_HOST_BROADCAST may be used to specify the
72
* broadcast address (255.255.255.255). This makes sense for enet_host_connect,
73
* but not for enet_host_create. Once a server responds to a broadcast, the
74
* address is updated from ENET_HOST_BROADCAST to the server's actual IP address.
76
typedef struct _ENetAddress
83
* Packet flag bit constants.
85
* The host must be specified in network byte-order, and the port must be in
86
* host byte-order. The constant ENET_HOST_ANY may be used to specify the
87
* default server host.
91
typedef enum _ENetPacketFlag
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
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)
108
typedef void (ENET_CALLBACK * ENetPacketFreeCallback) (struct _ENetPacket *);
111
* ENet packet structure.
113
* An ENet data packet that may be sent to or received from a peer. The shown
114
* fields should only be read and never modified. The data field contains the
115
* allocated data for the packet. The dataLength fields specifies the length
116
* of the allocated data. The flags field is either 0 (specifying no flags),
117
* or a bitwise-or of any combination of the following flags:
119
* ENET_PACKET_FLAG_RELIABLE - packet must be received by the target peer
120
* and resend attempts should be made until the packet is delivered
122
* ENET_PACKET_FLAG_UNSEQUENCED - packet will not be sequenced with other packets
123
* (not supported for reliable packets)
125
* ENET_PACKET_FLAG_NO_ALLOCATE - packet will not allocate data, and user must supply it instead
129
typedef struct _ENetPacket
131
size_t referenceCount; /**< internal use only */
132
enet_uint32 flags; /**< bitwise-or of ENetPacketFlag constants */
133
enet_uint8 * data; /**< allocated data for packet */
134
size_t dataLength; /**< length of data */
135
ENetPacketFreeCallback freeCallback; /**< function to be called when the packet is no longer in use */
138
typedef struct _ENetAcknowledgement
140
ENetListNode acknowledgementList;
141
enet_uint32 sentTime;
142
ENetProtocol command;
143
} ENetAcknowledgement;
145
typedef struct _ENetOutgoingCommand
147
ENetListNode outgoingCommandList;
148
enet_uint16 reliableSequenceNumber;
149
enet_uint16 unreliableSequenceNumber;
150
enet_uint32 sentTime;
151
enet_uint32 roundTripTimeout;
152
enet_uint32 roundTripTimeoutLimit;
153
enet_uint32 fragmentOffset;
154
enet_uint16 fragmentLength;
155
enet_uint16 sendAttempts;
156
ENetProtocol command;
158
} ENetOutgoingCommand;
160
typedef struct _ENetIncomingCommand
162
ENetListNode incomingCommandList;
163
enet_uint16 reliableSequenceNumber;
164
enet_uint16 unreliableSequenceNumber;
165
ENetProtocol command;
166
enet_uint32 fragmentCount;
167
enet_uint32 fragmentsRemaining;
168
enet_uint32 * fragments;
170
} ENetIncomingCommand;
172
typedef enum _ENetPeerState
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
186
#ifndef ENET_BUFFER_MAXIMUM
187
#define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
192
ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024,
193
ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024,
194
ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000,
195
ENET_HOST_DEFAULT_MTU = 1400,
197
ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500,
198
ENET_PEER_DEFAULT_PACKET_THROTTLE = 32,
199
ENET_PEER_PACKET_THROTTLE_SCALE = 32,
200
ENET_PEER_PACKET_THROTTLE_COUNTER = 7,
201
ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2,
202
ENET_PEER_PACKET_THROTTLE_DECELERATION = 2,
203
ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000,
204
ENET_PEER_PACKET_LOSS_SCALE = (1 << 16),
205
ENET_PEER_PACKET_LOSS_INTERVAL = 10000,
206
ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
207
ENET_PEER_TIMEOUT_LIMIT = 32,
208
ENET_PEER_TIMEOUT_MINIMUM = 5000,
209
ENET_PEER_TIMEOUT_MAXIMUM = 30000,
210
ENET_PEER_PING_INTERVAL = 500,
211
ENET_PEER_UNSEQUENCED_WINDOWS = 64,
212
ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024,
213
ENET_PEER_FREE_UNSEQUENCED_WINDOWS = 32,
214
ENET_PEER_RELIABLE_WINDOWS = 16,
215
ENET_PEER_RELIABLE_WINDOW_SIZE = 0x1000,
216
ENET_PEER_FREE_RELIABLE_WINDOWS = 8
219
typedef struct _ENetChannel
221
enet_uint16 outgoingReliableSequenceNumber;
222
enet_uint16 outgoingUnreliableSequenceNumber;
223
enet_uint16 usedReliableWindows;
224
enet_uint16 reliableWindows [ENET_PEER_RELIABLE_WINDOWS];
225
enet_uint16 incomingReliableSequenceNumber;
226
enet_uint16 incomingUnreliableSequenceNumber;
227
ENetList incomingReliableCommands;
228
ENetList incomingUnreliableCommands;
232
* An ENet peer which data packets may be sent or received from.
234
* No fields should be modified unless otherwise specified.
236
typedef struct _ENetPeer
238
ENetListNode dispatchList;
239
struct _ENetHost * host;
240
enet_uint16 outgoingPeerID;
241
enet_uint16 incomingPeerID;
242
enet_uint32 connectID;
243
enet_uint8 outgoingSessionID;
244
enet_uint8 incomingSessionID;
245
ENetAddress address; /**< Internet address of the peer */
246
void * data; /**< Application private data, may be freely modified */
248
ENetChannel * channels;
249
size_t channelCount; /**< Number of channels allocated for communication with peer */
250
enet_uint32 incomingBandwidth; /**< Downstream bandwidth of the client in bytes/second */
251
enet_uint32 outgoingBandwidth; /**< Upstream bandwidth of the client in bytes/second */
252
enet_uint32 incomingBandwidthThrottleEpoch;
253
enet_uint32 outgoingBandwidthThrottleEpoch;
254
enet_uint32 incomingDataTotal;
255
enet_uint32 outgoingDataTotal;
256
enet_uint32 lastSendTime;
257
enet_uint32 lastReceiveTime;
258
enet_uint32 nextTimeout;
259
enet_uint32 earliestTimeout;
260
enet_uint32 packetLossEpoch;
261
enet_uint32 packetsSent;
262
enet_uint32 packetsLost;
263
enet_uint32 packetLoss; /**< mean packet loss of reliable packets as a ratio with respect to the constant ENET_PEER_PACKET_LOSS_SCALE */
264
enet_uint32 packetLossVariance;
265
enet_uint32 packetThrottle;
266
enet_uint32 packetThrottleLimit;
267
enet_uint32 packetThrottleCounter;
268
enet_uint32 packetThrottleEpoch;
269
enet_uint32 packetThrottleAcceleration;
270
enet_uint32 packetThrottleDeceleration;
271
enet_uint32 packetThrottleInterval;
272
enet_uint32 lastRoundTripTime;
273
enet_uint32 lowestRoundTripTime;
274
enet_uint32 lastRoundTripTimeVariance;
275
enet_uint32 highestRoundTripTimeVariance;
276
enet_uint32 roundTripTime; /**< mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgement */
277
enet_uint32 roundTripTimeVariance;
279
enet_uint32 windowSize;
280
enet_uint32 reliableDataInTransit;
281
enet_uint16 outgoingReliableSequenceNumber;
282
ENetList acknowledgements;
283
ENetList sentReliableCommands;
284
ENetList sentUnreliableCommands;
285
ENetList outgoingReliableCommands;
286
ENetList outgoingUnreliableCommands;
287
ENetList dispatchedCommands;
289
enet_uint16 incomingUnsequencedGroup;
290
enet_uint16 outgoingUnsequencedGroup;
291
enet_uint32 unsequencedWindow [ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
292
enet_uint32 eventData;
295
/** An ENet packet compressor for compressing UDP packets before socket sends or receives.
297
typedef struct _ENetCompressor
299
/** Context data for the compressor. Must be non-NULL. */
301
/** Compresses from inBuffers[0:inBufferCount-1], containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */
302
size_t (ENET_CALLBACK * compress) (void * context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit);
303
/** Decompresses from inData, containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */
304
size_t (ENET_CALLBACK * decompress) (void * context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit);
305
/** Destroys the context when compression is disabled or the host is destroyed. May be NULL. */
306
void (ENET_CALLBACK * destroy) (void * context);
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);
312
/** An ENet host for communicating with peers.
314
* 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()
328
typedef struct _ENetHost
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;
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;
345
enet_uint16 headerFlags;
346
ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
348
ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
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 */
363
* An ENet event type, as specified in @ref ENetEvent.
365
typedef enum _ENetEventType
367
/** no event occurred within the specified time limit */
368
ENET_EVENT_TYPE_NONE = 0,
370
/** a connection request initiated by enet_host_connect has completed.
371
* The peer field contains the peer which successfully connected.
373
ENET_EVENT_TYPE_CONNECT = 1,
375
/** a peer has disconnected. This event is generated on a successful
376
* completion of a disconnect initiated by enet_pper_disconnect, if
377
* a peer has timed out, or if a connection request intialized by
378
* enet_host_connect has timed out. The peer field contains the peer
379
* which disconnected. The data field contains user supplied data
380
* describing the disconnection, or 0, if none is available.
382
ENET_EVENT_TYPE_DISCONNECT = 2,
384
/** a packet has been received from a peer. The peer field specifies the
385
* peer which sent the packet. The channelID field specifies the channel
386
* number upon which the packet was received. The packet field contains
387
* the packet that was received; this packet must be destroyed with
388
* enet_packet_destroy after use.
390
ENET_EVENT_TYPE_RECEIVE = 3
394
* An ENet event as returned by enet_host_service().
396
@sa enet_host_service
398
typedef struct _ENetEvent
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 */
407
/** @defgroup global ENet global functions
412
Initializes ENet globally. Must be called prior to using any functions in
414
@returns 0 on success, < 0 on failure
416
ENET_API int enet_initialize (void);
419
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.
421
@param version the constant ENET_VERSION should be supplied so ENet knows which version of ENetCallbacks struct to use
422
@param inits user-overriden callbacks where any NULL callbacks will use ENet's defaults
423
@returns 0 on success, < 0 on failure
425
ENET_API int enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits);
428
Shuts down ENet globally. Should be called when a program that has
429
initialized ENet exits.
431
ENET_API void enet_deinitialize (void);
435
/** @defgroup private ENet private implementation functions */
438
Returns the wall-time in milliseconds. Its initial value is unspecified
439
unless otherwise set.
441
ENET_API enet_uint32 enet_time_get (void);
443
Sets the current wall-time in milliseconds.
445
ENET_API void enet_time_set (enet_uint32);
447
/** @defgroup socket ENet socket functions
450
ENET_API ENetSocket enet_socket_create (ENetSocketType);
451
ENET_API int enet_socket_bind (ENetSocket, const ENetAddress *);
452
ENET_API int enet_socket_listen (ENetSocket, int);
453
ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
454
ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *);
455
ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
456
ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t);
457
ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
458
ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int);
459
ENET_API void enet_socket_destroy (ENetSocket);
460
ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32);
464
/** @defgroup Address ENet address functions
467
/** Attempts to resolve the host named by the parameter hostName and sets
468
the host field in the address parameter if successful.
469
@param address destination to store resolved address
470
@param hostName host name to lookup
472
@retval < 0 on failure
473
@returns the address of the given hostName in address on success
475
ENET_API int enet_address_set_host (ENetAddress * address, const char * hostName);
477
/** Gives the printable form of the ip address specified in the address parameter.
478
@param address address printed
479
@param hostName destination for name, must not be NULL
480
@param nameLength maximum length of hostName.
481
@returns the null-terminated name of the host in hostName on success
483
@retval < 0 on failure
485
ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostName, size_t nameLength);
487
/** Attempts to do a reverse lookup of the host field in the address parameter.
488
@param address address used for reverse lookup
489
@param hostName destination for name, must not be NULL
490
@param nameLength maximum length of hostName.
491
@returns the null-terminated name of the host in hostName on success
493
@retval < 0 on failure
495
ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength);
499
ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32);
500
ENET_API void enet_packet_destroy (ENetPacket *);
501
ENET_API int enet_packet_resize (ENetPacket *, size_t);
502
ENET_API enet_uint32 enet_crc32 (const ENetBuffer *, size_t);
504
ENET_API ENetHost * enet_host_create (const ENetAddress *, size_t, size_t, enet_uint32, enet_uint32);
505
ENET_API void enet_host_destroy (ENetHost *);
506
ENET_API ENetPeer * enet_host_connect (ENetHost *, const ENetAddress *, size_t, enet_uint32);
507
ENET_API int enet_host_check_events (ENetHost *, ENetEvent *);
508
ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32);
509
ENET_API void enet_host_flush (ENetHost *);
510
ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *);
511
ENET_API void enet_host_compress (ENetHost *, const ENetCompressor *);
512
ENET_API int enet_host_compress_with_range_coder (ENetHost * host);
513
ENET_API void enet_host_channel_limit (ENetHost *, size_t);
514
ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32);
515
extern void enet_host_bandwidth_throttle (ENetHost *);
517
ENET_API int enet_peer_send (ENetPeer *, enet_uint8, ENetPacket *);
518
ENET_API ENetPacket * enet_peer_receive (ENetPeer *, enet_uint8 * channelID);
519
ENET_API void enet_peer_ping (ENetPeer *);
520
ENET_API void enet_peer_reset (ENetPeer *);
521
ENET_API void enet_peer_disconnect (ENetPeer *, enet_uint32);
522
ENET_API void enet_peer_disconnect_now (ENetPeer *, enet_uint32);
523
ENET_API void enet_peer_disconnect_later (ENetPeer *, enet_uint32);
524
ENET_API void enet_peer_throttle_configure (ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
525
extern int enet_peer_throttle (ENetPeer *, enet_uint32);
526
extern void enet_peer_reset_queues (ENetPeer *);
527
extern void enet_peer_setup_outgoing_command (ENetPeer *, ENetOutgoingCommand *);
528
extern ENetOutgoingCommand * enet_peer_queue_outgoing_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32, enet_uint16);
529
extern ENetIncomingCommand * enet_peer_queue_incoming_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32);
530
extern ENetAcknowledgement * enet_peer_queue_acknowledgement (ENetPeer *, const ENetProtocol *, enet_uint16);
531
extern void enet_peer_dispatch_incoming_unreliable_commands (ENetPeer *, ENetChannel *);
532
extern void enet_peer_dispatch_incoming_reliable_commands (ENetPeer *, ENetChannel *);
534
ENET_API void * enet_range_coder_create (void);
535
ENET_API void enet_range_coder_destroy (void *);
536
ENET_API size_t enet_range_coder_compress (void *, const ENetBuffer *, size_t, size_t, enet_uint8 *, size_t);
537
ENET_API size_t enet_range_coder_decompress (void *, const enet_uint8 *, size_t, enet_uint8 *, size_t);
539
extern size_t enet_protocol_command_size (enet_uint8);
545
#endif /* __ENET_ENET_H__ */