Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions callbacks.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
@file callbacks.c
@brief ENet callback functions
*/
Expand All @@ -21,7 +21,7 @@ enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits
callbacks.malloc = inits -> malloc;
callbacks.free = inits -> free;
}

if (inits -> no_memory != NULL)
callbacks.no_memory = inits -> no_memory;

Expand All @@ -33,7 +33,7 @@ enet_linked_version (void)
{
return ENET_VERSION;
}

void *
enet_malloc (size_t size)
{
Expand All @@ -50,4 +50,3 @@ enet_free (void * memory)
{
callbacks.free (memory);
}

56 changes: 27 additions & 29 deletions compress.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
@file compress.c
@brief An adaptive order-2 PPM range coder
*/
Expand All @@ -18,7 +18,7 @@ typedef struct _ENetSymbol
enet_uint16 symbols;
enet_uint16 escapes;
enet_uint16 total;
enet_uint16 parent;
enet_uint16 parent;
} ENetSymbol;

/* adaptation constants tuned aggressively for small packet sizes rather than large file compression */
Expand Down Expand Up @@ -100,7 +100,7 @@ enet_symbol_rescale (ENetSymbol * symbol)
total += symbol -> under;
if (! symbol -> right) break;
symbol += symbol -> right;
}
}
return total;
}

Expand Down Expand Up @@ -282,11 +282,11 @@ enet_range_coder_compress (void * context, const ENetBuffer * inBuffers, size_t
inBufferCount --;
}
value = * inData ++;
for (subcontext = & rangeCoder -> symbols [predicted];
subcontext != root;

for (subcontext = & rangeCoder -> symbols [predicted];
subcontext != root;
#ifdef ENET_CONTEXT_EXCLUSION
childContext = subcontext,
childContext = subcontext,
#endif
subcontext = & rangeCoder -> symbols [subcontext -> parent])
{
Expand All @@ -304,8 +304,8 @@ enet_range_coder_compress (void * context, const ENetBuffer * inBuffers, size_t
}
else
{
if (subcontext -> escapes > 0 && subcontext -> escapes < total)
ENET_RANGE_CODER_ENCODE (0, subcontext -> escapes, total);
if (subcontext -> escapes > 0 && subcontext -> escapes < total)
ENET_RANGE_CODER_ENCODE (0, subcontext -> escapes, total);
subcontext -> escapes += ENET_SUBCONTEXT_ESCAPE_DELTA;
subcontext -> total += ENET_SUBCONTEXT_ESCAPE_DELTA;
}
Expand All @@ -321,17 +321,17 @@ enet_range_coder_compress (void * context, const ENetBuffer * inBuffers, size_t
total = root -> total;
#ifdef ENET_CONTEXT_EXCLUSION
if (childContext -> total > ENET_SUBCONTEXT_SYMBOL_DELTA + ENET_SUBCONTEXT_ESCAPE_DELTA)
ENET_CONTEXT_ENCODE_EXCLUDE (childContext, value, under, total, ENET_CONTEXT_SYMBOL_MINIMUM);
ENET_CONTEXT_ENCODE_EXCLUDE (childContext, value, under, total, ENET_CONTEXT_SYMBOL_MINIMUM);
#endif
ENET_RANGE_CODER_ENCODE (root -> escapes + under, count, total);
root -> total += ENET_CONTEXT_SYMBOL_DELTA;
root -> total += ENET_CONTEXT_SYMBOL_DELTA;
if (count > 0xFF - 2*ENET_CONTEXT_SYMBOL_DELTA + ENET_CONTEXT_SYMBOL_MINIMUM || root -> total > ENET_RANGE_CODER_BOTTOM - 0x100)
ENET_CONTEXT_RESCALE (root, ENET_CONTEXT_SYMBOL_MINIMUM);

nextInput:
if (order >= ENET_SUBCONTEXT_ORDER)
if (order >= ENET_SUBCONTEXT_ORDER)
predicted = rangeCoder -> symbols [predicted].parent;
else
else
order ++;
ENET_RANGE_CODER_FREE_SYMBOLS;
}
Expand Down Expand Up @@ -509,7 +509,7 @@ enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t i
ENetExclude excludes [256];
ENetExclude * nextExclude = excludes;
#endif

if (rangeCoder == NULL || inLimit <= 0)
return 0;

Expand All @@ -529,35 +529,35 @@ enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t i
for (subcontext = & rangeCoder -> symbols [predicted];
subcontext != root;
#ifdef ENET_CONTEXT_EXCLUSION
childContext = subcontext,
childContext = subcontext,
#endif
subcontext = & rangeCoder -> symbols [subcontext -> parent])
{
if (subcontext -> escapes <= 0)
continue;
total = subcontext -> total;
#ifdef ENET_CONTEXT_EXCLUSION
if (childContext -> total > 0)
ENET_CONTEXT_DECODE_EXCLUDE (childContext, total, 0);
if (childContext -> total > 0)
ENET_CONTEXT_DECODE_EXCLUDE (childContext, total, 0);
#endif
if (subcontext -> escapes >= total)
continue;
code = ENET_RANGE_CODER_READ (total);
if (code < subcontext -> escapes)
if (code < subcontext -> escapes)
{
ENET_RANGE_CODER_DECODE (0, subcontext -> escapes, total);
ENET_RANGE_CODER_DECODE (0, subcontext -> escapes, total);
continue;
}
code -= subcontext -> escapes;
#ifdef ENET_CONTEXT_EXCLUSION
if (childContext -> total > 0)
{
ENET_CONTEXT_TRY_DECODE (subcontext, symbol, code, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0, ENET_CONTEXT_EXCLUDED);
ENET_CONTEXT_TRY_DECODE (subcontext, symbol, code, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0, ENET_CONTEXT_EXCLUDED);
}
else
#endif
{
ENET_CONTEXT_TRY_DECODE (subcontext, symbol, code, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0, ENET_CONTEXT_NOT_EXCLUDED);
ENET_CONTEXT_TRY_DECODE (subcontext, symbol, code, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0, ENET_CONTEXT_NOT_EXCLUDED);
}
bottom = symbol - rangeCoder -> symbols;
ENET_RANGE_CODER_DECODE (subcontext -> escapes + under, count, total);
Expand All @@ -570,7 +570,7 @@ enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t i
total = root -> total;
#ifdef ENET_CONTEXT_EXCLUSION
if (childContext -> total > 0)
ENET_CONTEXT_DECODE_EXCLUDE (childContext, total, ENET_CONTEXT_SYMBOL_MINIMUM);
ENET_CONTEXT_DECODE_EXCLUDE (childContext, total, ENET_CONTEXT_SYMBOL_MINIMUM);
#endif
code = ENET_RANGE_CODER_READ (total);
if (code < root -> escapes)
Expand All @@ -582,12 +582,12 @@ enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t i
#ifdef ENET_CONTEXT_EXCLUSION
if (childContext -> total > 0)
{
ENET_CONTEXT_ROOT_DECODE (root, symbol, code, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM, ENET_CONTEXT_EXCLUDED);
ENET_CONTEXT_ROOT_DECODE (root, symbol, code, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM, ENET_CONTEXT_EXCLUDED);
}
else
#endif
{
ENET_CONTEXT_ROOT_DECODE (root, symbol, code, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM, ENET_CONTEXT_NOT_EXCLUDED);
ENET_CONTEXT_ROOT_DECODE (root, symbol, code, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM, ENET_CONTEXT_NOT_EXCLUDED);
}
bottom = symbol - rangeCoder -> symbols;
ENET_RANGE_CODER_DECODE (root -> escapes + under, count, total);
Expand All @@ -608,7 +608,7 @@ enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t i
patch -> escapes += ENET_SUBCONTEXT_ESCAPE_DELTA;
patch -> total += ENET_SUBCONTEXT_ESCAPE_DELTA;
}
patch -> total += ENET_SUBCONTEXT_SYMBOL_DELTA;
patch -> total += ENET_SUBCONTEXT_SYMBOL_DELTA;
if (count > 0xFF - 2*ENET_SUBCONTEXT_SYMBOL_DELTA || patch -> total > ENET_RANGE_CODER_BOTTOM - 0x100)
ENET_CONTEXT_RESCALE (patch, 0);
}
Expand All @@ -622,7 +622,7 @@ enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t i
order ++;
ENET_RANGE_CODER_FREE_SYMBOLS;
}

return (size_t) (outData - outStart);
}

Expand All @@ -648,7 +648,5 @@ enet_host_compress_with_range_coder (ENetHost * host)
enet_host_compress (host, & compressor);
return 0;
}

/** @} */


37 changes: 19 additions & 18 deletions host.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
@file host.c
@brief ENet host management functions
*/
Expand All @@ -11,7 +11,7 @@
@{
*/

/** Creates a host for communicating to peers.
/** Creates a host for communicating to peers.

@param address the address at which other peers may connect to this host. If NULL, then no peers may connect to the host.
@param peerCount the maximum number of peers that should be allocated for the host.
Expand Down Expand Up @@ -66,7 +66,7 @@ enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL
enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);

if (address != NULL && enet_socket_get_address (host -> socket, & host -> address) < 0)
if (address != NULL && enet_socket_get_address (host -> socket, & host -> address) < 0)
host -> address = * address;

if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
Expand All @@ -92,11 +92,12 @@ enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL
host -> commandCount = 0;
host -> bufferCount = 0;
host -> checksum = NULL;
host -> receivedAddress.host = ENET_HOST_ANY;
host -> receivedAddress.family = AF_INET; // IPv4
host -> receivedAddress.port = 0;
host -> receivedAddress.ip.v4.host = ENET_HOST_ANY;
host -> receivedData = NULL;
host -> receivedDataLength = 0;

host -> totalSentData = 0;
host -> totalSentPackets = 0;
host -> totalReceivedData = 0;
Expand Down Expand Up @@ -167,7 +168,7 @@ enet_host_destroy (ENetHost * host)
@param host host seeking the connection
@param address destination for the connection
@param channelCount number of channels to allocate
@param data user data supplied to the receiving host
@param data user data supplied to the receiving host
@returns a peer representing the foreign host on success, NULL on failure
@remarks The peer returned will have not completed the connection until enet_host_service()
notifies of an ENET_EVENT_TYPE_CONNECT event for the peer.
Expand Down Expand Up @@ -208,15 +209,15 @@ enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelC
currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
else
currentPeer -> windowSize = (host -> outgoingBandwidth /
ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;

if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
else
if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;

for (channel = currentPeer -> channels;
channel < & currentPeer -> channels [channelCount];
++ channel)
Expand All @@ -232,7 +233,7 @@ enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelC
channel -> usedReliableWindows = 0;
memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows));
}

command.header.command = ENET_PROTOCOL_COMMAND_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
command.header.channelID = 0xFF;
command.connect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
Expand All @@ -248,7 +249,7 @@ enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelC
command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
command.connect.connectID = currentPeer -> connectID;
command.connect.data = ENET_HOST_TO_NET_32 (data);

enet_peer_queue_outgoing_command (currentPeer, & command, NULL, 0, 0);

return currentPeer;
Expand Down Expand Up @@ -367,7 +368,7 @@ enet_host_bandwidth_throttle (ENetHost * host)
while (peersRemaining > 0 && needsAdjustment != 0)
{
needsAdjustment = 0;

if (dataTotal <= bandwidth)
throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
else
Expand All @@ -378,7 +379,7 @@ enet_host_bandwidth_throttle (ENetHost * host)
++ peer)
{
enet_uint32 peerBandwidth;

if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
peer -> incomingBandwidth == 0 ||
peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
Expand All @@ -388,12 +389,12 @@ enet_host_bandwidth_throttle (ENetHost * host)
if ((throttle * peer -> outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE <= peerBandwidth)
continue;

peer -> packetThrottleLimit = (peerBandwidth *
peer -> packetThrottleLimit = (peerBandwidth *
ENET_PEER_PACKET_THROTTLE_SCALE) / peer -> outgoingDataTotal;

if (peer -> packetThrottleLimit == 0)
peer -> packetThrottleLimit = 1;

if (peer -> packetThrottle > peer -> packetThrottleLimit)
peer -> packetThrottle = peer -> packetThrottleLimit;

Expand Down Expand Up @@ -463,7 +464,7 @@ enet_host_bandwidth_throttle (ENetHost * host)
continue;

peer -> incomingBandwidthThrottleEpoch = timeCurrent;

needsAdjustment = 1;
-- peersRemaining;
bandwidth -= peer -> outgoingBandwidth;
Expand All @@ -487,8 +488,8 @@ enet_host_bandwidth_throttle (ENetHost * host)
command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (bandwidthLimit);

enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
}
}
}
}

/** @} */
Loading