00001
00005 #ifndef __ENET_ENET_H__
00006 #define __ENET_ENET_H__
00007
00008 #ifdef __cplusplus
00009 extern "C"
00010 {
00011 #endif
00012
00013 #include <stdlib.h>
00014
00015 #ifdef WIN32
00016 #include "enet/win32.h"
00017 #else
00018 #include "enet/unix.h"
00019 #endif
00020
00021 #include "enet/types.h"
00022 #include "enet/protocol.h"
00023 #include "enet/list.h"
00024 #include "enet/callbacks.h"
00025
00026 typedef enum
00027 {
00028 ENET_VERSION = 1
00029 } ENetVersion;
00030
00031 typedef enum
00032 {
00033 ENET_SOCKET_TYPE_STREAM = 1,
00034 ENET_SOCKET_TYPE_DATAGRAM = 2
00035 } ENetSocketType;
00036
00037 typedef enum
00038 {
00039 ENET_SOCKET_WAIT_NONE = 0,
00040 ENET_SOCKET_WAIT_SEND = (1 << 0),
00041 ENET_SOCKET_WAIT_RECEIVE = (1 << 1)
00042 } ENetSocketWait;
00043
00044 enum
00045 {
00046 ENET_HOST_ANY = 0,
00047 ENET_HOST_BROADCAST = 0xFFFFFFFF,
00049 ENET_PORT_ANY = 0
00050 };
00051
00062 typedef struct _ENetAddress
00063 {
00064 enet_uint32 host;
00065 enet_uint16 port;
00066 } ENetAddress;
00067
00077 typedef enum
00078 {
00081 ENET_PACKET_FLAG_RELIABLE = (1 << 0),
00085 ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1),
00087 ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2)
00088 } ENetPacketFlag;
00089
00090 struct _ENetPacket;
00091 typedef void (ENET_CALLBACK * ENetPacketFreeCallback) (struct _ENetPacket *);
00092
00112 typedef struct _ENetPacket
00113 {
00114 size_t referenceCount;
00115 enet_uint32 flags;
00116 enet_uint8 * data;
00117 size_t dataLength;
00118 ENetPacketFreeCallback freeCallback;
00119 } ENetPacket;
00120
00121 typedef struct _ENetAcknowledgement
00122 {
00123 ENetListNode acknowledgementList;
00124 enet_uint32 sentTime;
00125 ENetProtocol command;
00126 } ENetAcknowledgement;
00127
00128 typedef struct _ENetOutgoingCommand
00129 {
00130 ENetListNode outgoingCommandList;
00131 enet_uint16 reliableSequenceNumber;
00132 enet_uint16 unreliableSequenceNumber;
00133 enet_uint32 sentTime;
00134 enet_uint32 roundTripTimeout;
00135 enet_uint32 roundTripTimeoutLimit;
00136 enet_uint32 fragmentOffset;
00137 enet_uint16 fragmentLength;
00138 ENetProtocol command;
00139 ENetPacket * packet;
00140 } ENetOutgoingCommand;
00141
00142 typedef struct _ENetIncomingCommand
00143 {
00144 ENetListNode incomingCommandList;
00145 enet_uint16 reliableSequenceNumber;
00146 enet_uint16 unreliableSequenceNumber;
00147 ENetProtocol command;
00148 enet_uint32 fragmentCount;
00149 enet_uint32 fragmentsRemaining;
00150 enet_uint32 * fragments;
00151 ENetPacket * packet;
00152 } ENetIncomingCommand;
00153
00154 typedef enum
00155 {
00156 ENET_PEER_STATE_DISCONNECTED = 0,
00157 ENET_PEER_STATE_CONNECTING = 1,
00158 ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2,
00159 ENET_PEER_STATE_CONNECTION_PENDING = 3,
00160 ENET_PEER_STATE_CONNECTION_SUCCEEDED = 4,
00161 ENET_PEER_STATE_CONNECTED = 5,
00162 ENET_PEER_STATE_DISCONNECT_LATER = 6,
00163 ENET_PEER_STATE_DISCONNECTING = 7,
00164 ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 8,
00165 ENET_PEER_STATE_ZOMBIE = 9
00166 } ENetPeerState;
00167
00168 #ifndef ENET_BUFFER_MAXIMUM
00169 #define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
00170 #endif
00171
00172 enum
00173 {
00174 ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024,
00175 ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024,
00176 ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000,
00177 ENET_HOST_DEFAULT_MTU = 1400,
00178
00179 ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500,
00180 ENET_PEER_DEFAULT_PACKET_THROTTLE = 32,
00181 ENET_PEER_PACKET_THROTTLE_SCALE = 32,
00182 ENET_PEER_PACKET_THROTTLE_COUNTER = 7,
00183 ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2,
00184 ENET_PEER_PACKET_THROTTLE_DECELERATION = 2,
00185 ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000,
00186 ENET_PEER_PACKET_LOSS_SCALE = (1 << 16),
00187 ENET_PEER_PACKET_LOSS_INTERVAL = 10000,
00188 ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
00189 ENET_PEER_TIMEOUT_LIMIT = 32,
00190 ENET_PEER_TIMEOUT_MINIMUM = 5000,
00191 ENET_PEER_TIMEOUT_MAXIMUM = 30000,
00192 ENET_PEER_PING_INTERVAL = 500,
00193 ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 4 * 32
00194 };
00195
00196 typedef struct _ENetChannel
00197 {
00198 enet_uint16 outgoingReliableSequenceNumber;
00199 enet_uint16 outgoingUnreliableSequenceNumber;
00200 enet_uint16 incomingReliableSequenceNumber;
00201 enet_uint16 incomingUnreliableSequenceNumber;
00202 ENetList incomingReliableCommands;
00203 ENetList incomingUnreliableCommands;
00204 } ENetChannel;
00205
00211 typedef struct _ENetPeer
00212 {
00213 struct _ENetHost * host;
00214 enet_uint16 outgoingPeerID;
00215 enet_uint16 incomingPeerID;
00216 enet_uint32 sessionID;
00217 ENetAddress address;
00218 void * data;
00219 ENetPeerState state;
00220 ENetChannel * channels;
00221 size_t channelCount;
00222 enet_uint32 incomingBandwidth;
00223 enet_uint32 outgoingBandwidth;
00224 enet_uint32 incomingBandwidthThrottleEpoch;
00225 enet_uint32 outgoingBandwidthThrottleEpoch;
00226 enet_uint32 incomingDataTotal;
00227 enet_uint32 outgoingDataTotal;
00228 enet_uint32 lastSendTime;
00229 enet_uint32 lastReceiveTime;
00230 enet_uint32 nextTimeout;
00231 enet_uint32 earliestTimeout;
00232 enet_uint32 packetLossEpoch;
00233 enet_uint32 packetsSent;
00234 enet_uint32 packetsLost;
00235 enet_uint32 packetLoss;
00236 enet_uint32 packetLossVariance;
00237 enet_uint32 packetThrottle;
00238 enet_uint32 packetThrottleLimit;
00239 enet_uint32 packetThrottleCounter;
00240 enet_uint32 packetThrottleEpoch;
00241 enet_uint32 packetThrottleAcceleration;
00242 enet_uint32 packetThrottleDeceleration;
00243 enet_uint32 packetThrottleInterval;
00244 enet_uint32 lastRoundTripTime;
00245 enet_uint32 lowestRoundTripTime;
00246 enet_uint32 lastRoundTripTimeVariance;
00247 enet_uint32 highestRoundTripTimeVariance;
00248 enet_uint32 roundTripTime;
00249 enet_uint32 roundTripTimeVariance;
00250 enet_uint16 mtu;
00251 enet_uint32 windowSize;
00252 enet_uint32 reliableDataInTransit;
00253 enet_uint16 outgoingReliableSequenceNumber;
00254 ENetList acknowledgements;
00255 ENetList sentReliableCommands;
00256 ENetList sentUnreliableCommands;
00257 ENetList outgoingReliableCommands;
00258 ENetList outgoingUnreliableCommands;
00259 enet_uint16 incomingUnsequencedGroup;
00260 enet_uint16 outgoingUnsequencedGroup;
00261 enet_uint32 unsequencedWindow [ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
00262 enet_uint32 disconnectData;
00263 } ENetPeer;
00264
00278 typedef struct _ENetHost
00279 {
00280 ENetSocket socket;
00281 ENetAddress address;
00282 enet_uint32 incomingBandwidth;
00283 enet_uint32 outgoingBandwidth;
00284 enet_uint32 bandwidthThrottleEpoch;
00285 enet_uint32 mtu;
00286 int recalculateBandwidthLimits;
00287 ENetPeer * peers;
00288 size_t peerCount;
00289 ENetPeer * lastServicedPeer;
00290 int continueSending;
00291 size_t packetSize;
00292 enet_uint16 headerFlags;
00293 ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
00294 size_t commandCount;
00295 ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
00296 size_t bufferCount;
00297 ENetAddress receivedAddress;
00298 enet_uint8 receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
00299 size_t receivedDataLength;
00300 } ENetHost;
00301
00305 typedef enum
00306 {
00308 ENET_EVENT_TYPE_NONE = 0,
00309
00313 ENET_EVENT_TYPE_CONNECT = 1,
00314
00322 ENET_EVENT_TYPE_DISCONNECT = 2,
00323
00330 ENET_EVENT_TYPE_RECEIVE = 3
00331 } ENetEventType;
00332
00338 typedef struct _ENetEvent
00339 {
00340 ENetEventType type;
00341 ENetPeer * peer;
00342 enet_uint8 channelID;
00343 enet_uint32 data;
00344 ENetPacket * packet;
00345 } ENetEvent;
00346
00356 ENET_API int enet_initialize (void);
00357
00365 ENET_API int enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits);
00366
00371 ENET_API void enet_deinitialize (void);
00372
00381 ENET_API enet_uint32 enet_time_get (void);
00385 ENET_API void enet_time_set (enet_uint32);
00386
00390 ENET_API ENetSocket enet_socket_create (ENetSocketType, const ENetAddress *);
00391 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
00392 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *);
00393 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
00394 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t);
00395 ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
00396 ENET_API void enet_socket_destroy (ENetSocket);
00397
00411 ENET_API int enet_address_set_host (ENetAddress * address, const char * hostName);
00412
00421 ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostName, size_t nameLength);
00422
00431 ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength);
00432
00435 ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32);
00436 ENET_API void enet_packet_destroy (ENetPacket *);
00437 ENET_API int enet_packet_resize (ENetPacket *, size_t);
00438 extern enet_uint32 enet_crc32 (const ENetBuffer *, size_t);
00439
00440 ENET_API ENetHost * enet_host_create (const ENetAddress *, size_t, enet_uint32, enet_uint32);
00441 ENET_API void enet_host_destroy (ENetHost *);
00442 ENET_API ENetPeer * enet_host_connect (ENetHost *, const ENetAddress *, size_t);
00443 ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32);
00444 ENET_API void enet_host_flush (ENetHost *);
00445 ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *);
00446 ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32);
00447 extern void enet_host_bandwidth_throttle (ENetHost *);
00448
00449 ENET_API int enet_peer_send (ENetPeer *, enet_uint8, ENetPacket *);
00450 ENET_API ENetPacket * enet_peer_receive (ENetPeer *, enet_uint8);
00451 ENET_API void enet_peer_ping (ENetPeer *);
00452 ENET_API void enet_peer_reset (ENetPeer *);
00453 ENET_API void enet_peer_disconnect (ENetPeer *, enet_uint32);
00454 ENET_API void enet_peer_disconnect_now (ENetPeer *, enet_uint32);
00455 ENET_API void enet_peer_disconnect_later (ENetPeer *, enet_uint32);
00456 ENET_API void enet_peer_throttle_configure (ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
00457 extern int enet_peer_throttle (ENetPeer *, enet_uint32);
00458 extern void enet_peer_reset_queues (ENetPeer *);
00459 extern ENetOutgoingCommand * enet_peer_queue_outgoing_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32, enet_uint16);
00460 extern ENetIncomingCommand * enet_peer_queue_incoming_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32);
00461 extern ENetAcknowledgement * enet_peer_queue_acknowledgement (ENetPeer *, const ENetProtocol *, enet_uint16);
00462
00463 extern size_t enet_protocol_command_size (enet_uint8);
00464
00465 #ifdef __cplusplus
00466 }
00467 #endif
00468
00469 #endif
00470