2004-10-20 10:17:51 -07:00
|
|
|
/**
|
|
|
|
@file peer.c
|
|
|
|
@brief ENet peer management functions
|
|
|
|
*/
|
2006-03-14 16:21:50 -07:00
|
|
|
#include <string.h>
|
2004-10-20 10:17:51 -07:00
|
|
|
#define ENET_BUILDING_LIB 1
|
|
|
|
#include "enet/enet.h"
|
|
|
|
|
|
|
|
/** @defgroup peer ENet peer functions
|
|
|
|
@{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Configures throttle parameter for a peer.
|
|
|
|
|
|
|
|
Unreliable packets are dropped by ENet in response to the varying conditions
|
|
|
|
of the Internet connection to the peer. The throttle represents a probability
|
|
|
|
that an unreliable packet should not be dropped and thus sent by ENet to the peer.
|
|
|
|
The lowest mean round trip time from the sending of a reliable packet to the
|
|
|
|
receipt of its acknowledgement is measured over an amount of time specified by
|
|
|
|
the interval parameter in milliseconds. If a measured round trip time happens to
|
|
|
|
be significantly less than the mean round trip time measured over the interval,
|
|
|
|
then the throttle probability is increased to allow more traffic by an amount
|
|
|
|
specified in the acceleration parameter, which is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE
|
|
|
|
constant. If a measured round trip time happens to be significantly greater than
|
|
|
|
the mean round trip time measured over the interval, then the throttle probability
|
|
|
|
is decreased to limit traffic by an amount specified in the deceleration parameter, which
|
|
|
|
is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE constant. When the throttle has
|
2012-09-18 06:38:10 -07:00
|
|
|
a value of ENET_PEER_PACKET_THROTTLE_SCALE, no unreliable packets are dropped by
|
2004-10-20 10:17:51 -07:00
|
|
|
ENet, and so 100% of all unreliable packets will be sent. When the throttle has a
|
|
|
|
value of 0, all unreliable packets are dropped by ENet, and so 0% of all unreliable
|
|
|
|
packets will be sent. Intermediate values for the throttle represent intermediate
|
|
|
|
probabilities between 0% and 100% of unreliable packets being sent. The bandwidth
|
|
|
|
limits of the local and foreign hosts are taken into account to determine a
|
|
|
|
sensible limit for the throttle probability above which it should not raise even in
|
|
|
|
the best of conditions.
|
|
|
|
|
|
|
|
@param peer peer to configure
|
|
|
|
@param interval interval, in milliseconds, over which to measure lowest mean RTT; the default value is ENET_PEER_PACKET_THROTTLE_INTERVAL.
|
|
|
|
@param acceleration rate at which to increase the throttle probability as mean RTT declines
|
|
|
|
@param deceleration rate at which to decrease the throttle probability as mean RTT increases
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
enet_peer_throttle_configure (ENetPeer * peer, enet_uint32 interval, enet_uint32 acceleration, enet_uint32 deceleration)
|
|
|
|
{
|
|
|
|
ENetProtocol command;
|
|
|
|
|
|
|
|
peer -> packetThrottleInterval = interval;
|
|
|
|
peer -> packetThrottleAcceleration = acceleration;
|
|
|
|
peer -> packetThrottleDeceleration = deceleration;
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
|
2004-10-20 10:17:51 -07:00
|
|
|
command.header.channelID = 0xFF;
|
|
|
|
|
|
|
|
command.throttleConfigure.packetThrottleInterval = ENET_HOST_TO_NET_32 (interval);
|
|
|
|
command.throttleConfigure.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (acceleration);
|
|
|
|
command.throttleConfigure.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (deceleration);
|
|
|
|
|
|
|
|
enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_peer_throttle (ENetPeer * peer, enet_uint32 rtt)
|
|
|
|
{
|
|
|
|
if (peer -> lastRoundTripTime <= peer -> lastRoundTripTimeVariance)
|
|
|
|
{
|
|
|
|
peer -> packetThrottle = peer -> packetThrottleLimit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (rtt < peer -> lastRoundTripTime)
|
|
|
|
{
|
|
|
|
peer -> packetThrottle += peer -> packetThrottleAcceleration;
|
|
|
|
|
|
|
|
if (peer -> packetThrottle > peer -> packetThrottleLimit)
|
|
|
|
peer -> packetThrottle = peer -> packetThrottleLimit;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (rtt > peer -> lastRoundTripTime + 2 * peer -> lastRoundTripTimeVariance)
|
|
|
|
{
|
|
|
|
if (peer -> packetThrottle > peer -> packetThrottleDeceleration)
|
|
|
|
peer -> packetThrottle -= peer -> packetThrottleDeceleration;
|
|
|
|
else
|
|
|
|
peer -> packetThrottle = 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Queues a packet to be sent.
|
|
|
|
@param peer destination for the packet
|
|
|
|
@param channelID channel on which to send
|
|
|
|
@param packet packet to send
|
|
|
|
@retval 0 on success
|
|
|
|
@retval < 0 on failure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
enet_peer_send (ENetPeer * peer, enet_uint8 channelID, ENetPacket * packet)
|
|
|
|
{
|
|
|
|
ENetChannel * channel = & peer -> channels [channelID];
|
|
|
|
ENetProtocol command;
|
|
|
|
size_t fragmentLength;
|
|
|
|
|
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTED ||
|
2012-01-06 08:11:04 -07:00
|
|
|
channelID >= peer -> channelCount ||
|
|
|
|
packet -> dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE)
|
2004-10-20 10:17:51 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
fragmentLength = peer -> mtu - sizeof (ENetProtocolHeader) - sizeof (ENetProtocolSendFragment);
|
2010-12-23 14:05:37 -07:00
|
|
|
if (peer -> host -> checksum != NULL)
|
|
|
|
fragmentLength -= sizeof(enet_uint32);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (packet -> dataLength > fragmentLength)
|
|
|
|
{
|
2012-01-06 08:11:04 -07:00
|
|
|
enet_uint32 fragmentCount = (packet -> dataLength + fragmentLength - 1) / fragmentLength,
|
2004-10-20 10:17:51 -07:00
|
|
|
fragmentNumber,
|
|
|
|
fragmentOffset;
|
2011-05-31 00:48:27 -07:00
|
|
|
enet_uint8 commandNumber;
|
|
|
|
enet_uint16 startSequenceNumber;
|
2010-05-06 06:44:47 -07:00
|
|
|
ENetList fragments;
|
|
|
|
ENetOutgoingCommand * fragment;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2012-01-06 08:11:04 -07:00
|
|
|
if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT)
|
|
|
|
return -1;
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
if ((packet -> flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT)) == ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT &&
|
|
|
|
channel -> outgoingUnreliableSequenceNumber < 0xFFFF)
|
|
|
|
{
|
|
|
|
commandNumber = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT;
|
|
|
|
startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingUnreliableSequenceNumber + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandNumber = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
|
|
|
|
startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingReliableSequenceNumber + 1);
|
|
|
|
}
|
|
|
|
|
2010-05-06 06:44:47 -07:00
|
|
|
enet_list_clear (& fragments);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
for (fragmentNumber = 0,
|
|
|
|
fragmentOffset = 0;
|
|
|
|
fragmentOffset < packet -> dataLength;
|
|
|
|
++ fragmentNumber,
|
|
|
|
fragmentOffset += fragmentLength)
|
|
|
|
{
|
2006-07-06 14:22:41 -07:00
|
|
|
if (packet -> dataLength - fragmentOffset < fragmentLength)
|
|
|
|
fragmentLength = packet -> dataLength - fragmentOffset;
|
|
|
|
|
2010-05-06 06:44:47 -07:00
|
|
|
fragment = (ENetOutgoingCommand *) enet_malloc (sizeof (ENetOutgoingCommand));
|
|
|
|
if (fragment == NULL)
|
|
|
|
{
|
|
|
|
while (! enet_list_empty (& fragments))
|
|
|
|
{
|
|
|
|
fragment = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (& fragments));
|
|
|
|
|
|
|
|
enet_free (fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-06 07:30:55 -07:00
|
|
|
|
|
|
|
fragment -> fragmentOffset = fragmentOffset;
|
|
|
|
fragment -> fragmentLength = fragmentLength;
|
|
|
|
fragment -> packet = packet;
|
2011-05-31 00:48:27 -07:00
|
|
|
fragment -> command.header.command = commandNumber;
|
2010-05-06 06:44:47 -07:00
|
|
|
fragment -> command.header.channelID = channelID;
|
|
|
|
fragment -> command.sendFragment.startSequenceNumber = startSequenceNumber;
|
|
|
|
fragment -> command.sendFragment.dataLength = ENET_HOST_TO_NET_16 (fragmentLength);
|
2012-01-06 08:11:04 -07:00
|
|
|
fragment -> command.sendFragment.fragmentCount = ENET_HOST_TO_NET_32 (fragmentCount);
|
2010-05-06 06:44:47 -07:00
|
|
|
fragment -> command.sendFragment.fragmentNumber = ENET_HOST_TO_NET_32 (fragmentNumber);
|
|
|
|
fragment -> command.sendFragment.totalLength = ENET_HOST_TO_NET_32 (packet -> dataLength);
|
|
|
|
fragment -> command.sendFragment.fragmentOffset = ENET_NET_TO_HOST_32 (fragmentOffset);
|
|
|
|
|
|
|
|
enet_list_insert (enet_list_end (& fragments), fragment);
|
|
|
|
}
|
|
|
|
|
2010-05-06 07:30:55 -07:00
|
|
|
packet -> referenceCount += fragmentNumber;
|
|
|
|
|
2010-05-06 06:44:47 -07:00
|
|
|
while (! enet_list_empty (& fragments))
|
|
|
|
{
|
|
|
|
fragment = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (& fragments));
|
|
|
|
|
2010-05-06 07:30:55 -07:00
|
|
|
enet_peer_setup_outgoing_command (peer, fragment);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
command.header.channelID = channelID;
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
if ((packet -> flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED)) == ENET_PACKET_FLAG_UNSEQUENCED)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2006-07-06 14:22:41 -07:00
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
|
|
|
|
command.sendUnsequenced.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
2010-05-06 06:44:47 -07:00
|
|
|
else
|
2011-05-31 00:48:27 -07:00
|
|
|
if (packet -> flags & ENET_PACKET_FLAG_RELIABLE || channel -> outgoingUnreliableSequenceNumber >= 0xFFFF)
|
2010-05-06 06:44:47 -07:00
|
|
|
{
|
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_SEND_RELIABLE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
|
|
|
|
command.sendReliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE;
|
2006-07-06 14:22:41 -07:00
|
|
|
command.sendUnreliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 16:37:06 -07:00
|
|
|
if (enet_peer_queue_outgoing_command (peer, & command, packet, 0, packet -> dataLength) == NULL)
|
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Attempts to dequeue any incoming queued packet.
|
|
|
|
@param peer peer to dequeue packets from
|
2010-05-13 09:55:59 -07:00
|
|
|
@param channelID holds the channel ID of the channel the packet was received on success
|
2004-10-20 10:17:51 -07:00
|
|
|
@returns a pointer to the packet, or NULL if there are no available incoming queued packets
|
|
|
|
*/
|
|
|
|
ENetPacket *
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_peer_receive (ENetPeer * peer, enet_uint8 * channelID)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2010-05-13 09:55:59 -07:00
|
|
|
ENetIncomingCommand * incomingCommand;
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetPacket * packet;
|
2010-05-13 09:55:59 -07:00
|
|
|
|
|
|
|
if (enet_list_empty (& peer -> dispatchedCommands))
|
2004-10-20 10:17:51 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
incomingCommand = (ENetIncomingCommand *) enet_list_remove (enet_list_begin (& peer -> dispatchedCommands));
|
|
|
|
|
2010-05-14 15:28:38 -07:00
|
|
|
if (channelID != NULL)
|
|
|
|
* channelID = incomingCommand -> command.header.channelID;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
packet = incomingCommand -> packet;
|
|
|
|
|
|
|
|
-- packet -> referenceCount;
|
|
|
|
|
|
|
|
if (incomingCommand -> fragments != NULL)
|
|
|
|
enet_free (incomingCommand -> fragments);
|
|
|
|
|
|
|
|
enet_free (incomingCommand);
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
enet_peer_reset_outgoing_commands (ENetList * queue)
|
|
|
|
{
|
|
|
|
ENetOutgoingCommand * outgoingCommand;
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
while (! enet_list_empty (queue))
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
outgoingCommand = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (queue));
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet != NULL)
|
|
|
|
{
|
|
|
|
-- outgoingCommand -> packet -> referenceCount;
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet -> referenceCount == 0)
|
|
|
|
enet_packet_destroy (outgoingCommand -> packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_free (outgoingCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-31 02:44:09 -07:00
|
|
|
enet_peer_remove_incoming_commands (ENetList * queue, ENetListIterator startCommand, ENetListIterator endCommand)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2011-05-31 02:44:09 -07:00
|
|
|
ENetListIterator currentCommand;
|
|
|
|
|
|
|
|
for (currentCommand = startCommand; currentCommand != endCommand; )
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2011-05-31 02:44:09 -07:00
|
|
|
ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2011-05-31 02:44:09 -07:00
|
|
|
currentCommand = enet_list_next (currentCommand);
|
|
|
|
|
|
|
|
enet_list_remove (& incomingCommand -> incomingCommandList);
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
if (incomingCommand -> packet != NULL)
|
|
|
|
{
|
|
|
|
-- incomingCommand -> packet -> referenceCount;
|
|
|
|
|
|
|
|
if (incomingCommand -> packet -> referenceCount == 0)
|
|
|
|
enet_packet_destroy (incomingCommand -> packet);
|
|
|
|
}
|
|
|
|
|
2005-07-18 22:23:43 -07:00
|
|
|
if (incomingCommand -> fragments != NULL)
|
|
|
|
enet_free (incomingCommand -> fragments);
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
enet_free (incomingCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 02:44:09 -07:00
|
|
|
static void
|
|
|
|
enet_peer_reset_incoming_commands (ENetList * queue)
|
|
|
|
{
|
2012-07-31 06:10:40 -07:00
|
|
|
enet_peer_remove_incoming_commands(queue, enet_list_begin (queue), enet_list_end (queue));
|
2011-05-31 02:44:09 -07:00
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
void
|
|
|
|
enet_peer_reset_queues (ENetPeer * peer)
|
|
|
|
{
|
|
|
|
ENetChannel * channel;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
if (peer -> needsDispatch)
|
|
|
|
{
|
|
|
|
enet_list_remove (& peer -> dispatchList);
|
|
|
|
|
|
|
|
peer -> needsDispatch = 0;
|
|
|
|
}
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
while (! enet_list_empty (& peer -> acknowledgements))
|
2004-10-20 10:17:51 -07:00
|
|
|
enet_free (enet_list_remove (enet_list_begin (& peer -> acknowledgements)));
|
|
|
|
|
|
|
|
enet_peer_reset_outgoing_commands (& peer -> sentReliableCommands);
|
|
|
|
enet_peer_reset_outgoing_commands (& peer -> sentUnreliableCommands);
|
|
|
|
enet_peer_reset_outgoing_commands (& peer -> outgoingReliableCommands);
|
|
|
|
enet_peer_reset_outgoing_commands (& peer -> outgoingUnreliableCommands);
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_peer_reset_incoming_commands (& peer -> dispatchedCommands);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (peer -> channels != NULL && peer -> channelCount > 0)
|
|
|
|
{
|
|
|
|
for (channel = peer -> channels;
|
|
|
|
channel < & peer -> channels [peer -> channelCount];
|
|
|
|
++ channel)
|
|
|
|
{
|
|
|
|
enet_peer_reset_incoming_commands (& channel -> incomingReliableCommands);
|
|
|
|
enet_peer_reset_incoming_commands (& channel -> incomingUnreliableCommands);
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_free (peer -> channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
peer -> channels = NULL;
|
|
|
|
peer -> channelCount = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-11 11:53:55 -07:00
|
|
|
void
|
|
|
|
enet_peer_on_connect (ENetPeer * peer)
|
|
|
|
{
|
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
{
|
|
|
|
if (peer -> incomingBandwidth != 0)
|
|
|
|
++ peer -> host -> bandwidthLimitedPeers;
|
|
|
|
|
|
|
|
++ peer -> host -> connectedPeers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
enet_peer_on_disconnect (ENetPeer * peer)
|
|
|
|
{
|
|
|
|
if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
{
|
|
|
|
if (peer -> incomingBandwidth != 0)
|
|
|
|
-- peer -> host -> bandwidthLimitedPeers;
|
|
|
|
|
|
|
|
-- peer -> host -> connectedPeers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
/** Forcefully disconnects a peer.
|
|
|
|
@param peer peer to forcefully disconnect
|
|
|
|
@remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
|
|
|
|
on its connection to the local host.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
enet_peer_reset (ENetPeer * peer)
|
|
|
|
{
|
2013-05-11 11:53:55 -07:00
|
|
|
enet_peer_on_disconnect (peer);
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
peer -> outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID;
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> connectID = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
peer -> state = ENET_PEER_STATE_DISCONNECTED;
|
|
|
|
|
|
|
|
peer -> incomingBandwidth = 0;
|
|
|
|
peer -> outgoingBandwidth = 0;
|
|
|
|
peer -> incomingBandwidthThrottleEpoch = 0;
|
|
|
|
peer -> outgoingBandwidthThrottleEpoch = 0;
|
|
|
|
peer -> incomingDataTotal = 0;
|
|
|
|
peer -> outgoingDataTotal = 0;
|
|
|
|
peer -> lastSendTime = 0;
|
|
|
|
peer -> lastReceiveTime = 0;
|
|
|
|
peer -> nextTimeout = 0;
|
2005-05-23 23:08:30 -07:00
|
|
|
peer -> earliestTimeout = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
peer -> packetLossEpoch = 0;
|
|
|
|
peer -> packetsSent = 0;
|
|
|
|
peer -> packetsLost = 0;
|
|
|
|
peer -> packetLoss = 0;
|
|
|
|
peer -> packetLossVariance = 0;
|
|
|
|
peer -> packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
|
|
|
|
peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
|
|
|
|
peer -> packetThrottleCounter = 0;
|
|
|
|
peer -> packetThrottleEpoch = 0;
|
|
|
|
peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
|
|
|
|
peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
|
|
|
|
peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
|
2012-05-29 19:05:34 -07:00
|
|
|
peer -> pingInterval = ENET_PEER_PING_INTERVAL;
|
|
|
|
peer -> timeoutLimit = ENET_PEER_TIMEOUT_LIMIT;
|
|
|
|
peer -> timeoutMinimum = ENET_PEER_TIMEOUT_MINIMUM;
|
|
|
|
peer -> timeoutMaximum = ENET_PEER_TIMEOUT_MAXIMUM;
|
2004-10-20 10:17:51 -07:00
|
|
|
peer -> lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
|
|
|
|
peer -> lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
|
|
|
|
peer -> lastRoundTripTimeVariance = 0;
|
|
|
|
peer -> highestRoundTripTimeVariance = 0;
|
|
|
|
peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
|
|
|
|
peer -> roundTripTimeVariance = 0;
|
|
|
|
peer -> mtu = peer -> host -> mtu;
|
|
|
|
peer -> reliableDataInTransit = 0;
|
|
|
|
peer -> outgoingReliableSequenceNumber = 0;
|
|
|
|
peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
|
|
|
|
peer -> incomingUnsequencedGroup = 0;
|
|
|
|
peer -> outgoingUnsequencedGroup = 0;
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> eventData = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
|
|
|
|
|
|
|
|
enet_peer_reset_queues (peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sends a ping request to a peer.
|
|
|
|
@param peer destination for the ping request
|
|
|
|
@remarks ping requests factor into the mean round trip time as designated by the
|
|
|
|
roundTripTime field in the ENetPeer structure. Enet automatically pings all connected
|
|
|
|
peers at regular intervals, however, this function may be called to ensure more
|
|
|
|
frequent ping requests.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
enet_peer_ping (ENetPeer * peer)
|
|
|
|
{
|
|
|
|
ENetProtocol command;
|
|
|
|
|
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTED)
|
|
|
|
return;
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_PING | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
|
2004-10-20 10:17:51 -07:00
|
|
|
command.header.channelID = 0xFF;
|
|
|
|
|
|
|
|
enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-05-29 19:05:34 -07:00
|
|
|
/** Sets the interval at which pings will be sent to a peer.
|
|
|
|
|
|
|
|
Pings are used both to monitor the liveness of the connection and also to dynamically
|
|
|
|
adjust the throttle during periods of low traffic so that the throttle has reasonable
|
|
|
|
responsiveness during traffic spikes.
|
|
|
|
|
|
|
|
@param peer the peer to adjust
|
|
|
|
@param pingInterval the interval at which to send pings; defaults to ENET_PEER_PING_INTERVAL if 0
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
enet_peer_ping_interval (ENetPeer * peer, enet_uint32 pingInterval)
|
|
|
|
{
|
|
|
|
peer -> pingInterval = pingInterval ? pingInterval : ENET_PEER_PING_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the timeout parameters for a peer.
|
|
|
|
|
|
|
|
The timeout parameter control how and when a peer will timeout from a failure to acknowledge
|
|
|
|
reliable traffic. Timeout values use an exponential backoff mechanism, where if a reliable
|
|
|
|
packet is not acknowledge within some multiple of the average RTT plus a variance tolerance,
|
|
|
|
the timeout will be doubled until it reaches a set limit. If the timeout is thus at this
|
|
|
|
limit and reliable packets have been sent but not acknowledged within a certain minimum time
|
|
|
|
period, the peer will be disconnected. Alternatively, if reliable packets have been sent
|
|
|
|
but not acknowledged for a certain maximum time period, the peer will be disconnected regardless
|
|
|
|
of the current timeout limit value.
|
|
|
|
|
|
|
|
@param peer the peer to adjust
|
|
|
|
@param timeoutLimit the timeout limit; defaults to ENET_PEER_TIMEOUT_LIMIT if 0
|
|
|
|
@param timeoutMinimum the timeout minimum; defaults to ENET_PEER_TIMEOUT_MINIMUM if 0
|
|
|
|
@param timeoutMaximum the timeout maximum; defaults to ENET_PEER_TIMEOUT_MAXIMUM if 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
enet_peer_timeout (ENetPeer * peer, enet_uint32 timeoutLimit, enet_uint32 timeoutMinimum, enet_uint32 timeoutMaximum)
|
|
|
|
{
|
|
|
|
peer -> timeoutLimit = timeoutLimit ? timeoutLimit : ENET_PEER_TIMEOUT_LIMIT;
|
|
|
|
peer -> timeoutMinimum = timeoutMinimum ? timeoutMinimum : ENET_PEER_TIMEOUT_MINIMUM;
|
|
|
|
peer -> timeoutMaximum = timeoutMaximum ? timeoutMaximum : ENET_PEER_TIMEOUT_MAXIMUM;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
/** Force an immediate disconnection from a peer.
|
|
|
|
@param peer peer to disconnect
|
2006-03-14 16:21:50 -07:00
|
|
|
@param data data describing the disconnection
|
2004-10-20 10:17:51 -07:00
|
|
|
@remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
|
|
|
|
guarenteed to receive the disconnect notification, and is reset immediately upon
|
|
|
|
return from this function.
|
|
|
|
*/
|
|
|
|
void
|
2006-03-14 16:21:50 -07:00
|
|
|
enet_peer_disconnect_now (ENetPeer * peer, enet_uint32 data)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
ENetProtocol command;
|
|
|
|
|
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECTED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (peer -> state != ENET_PEER_STATE_ZOMBIE &&
|
|
|
|
peer -> state != ENET_PEER_STATE_DISCONNECTING)
|
|
|
|
{
|
|
|
|
enet_peer_reset_queues (peer);
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
|
2004-10-20 10:17:51 -07:00
|
|
|
command.header.channelID = 0xFF;
|
2006-07-06 14:22:41 -07:00
|
|
|
command.disconnect.data = ENET_HOST_TO_NET_32 (data);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
|
|
|
|
|
|
|
|
enet_host_flush (peer -> host);
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_peer_reset (peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Request a disconnection from a peer.
|
|
|
|
@param peer peer to request a disconnection
|
2006-03-14 16:21:50 -07:00
|
|
|
@param data data describing the disconnection
|
2004-10-20 10:17:51 -07:00
|
|
|
@remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
|
|
|
|
once the disconnection is complete.
|
|
|
|
*/
|
|
|
|
void
|
2006-03-14 16:21:50 -07:00
|
|
|
enet_peer_disconnect (ENetPeer * peer, enet_uint32 data)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
ENetProtocol command;
|
|
|
|
|
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
|
|
|
|
peer -> state == ENET_PEER_STATE_DISCONNECTED ||
|
2008-07-30 02:39:09 -07:00
|
|
|
peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT ||
|
2004-10-20 10:17:51 -07:00
|
|
|
peer -> state == ENET_PEER_STATE_ZOMBIE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
enet_peer_reset_queues (peer);
|
|
|
|
|
|
|
|
command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
|
|
|
|
command.header.channelID = 0xFF;
|
2006-07-06 14:22:41 -07:00
|
|
|
command.disconnect.data = ENET_HOST_TO_NET_32 (data);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
|
|
|
|
else
|
|
|
|
command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
|
2013-05-11 11:53:55 -07:00
|
|
|
{
|
2013-05-12 08:29:22 -07:00
|
|
|
enet_peer_on_disconnect (peer);
|
2013-05-11 11:53:55 -07:00
|
|
|
|
|
|
|
peer -> state = ENET_PEER_STATE_DISCONNECTING;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
enet_host_flush (peer -> host);
|
|
|
|
enet_peer_reset (peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
/** Request a disconnection from a peer, but only after all queued outgoing packets are sent.
|
|
|
|
@param peer peer to request a disconnection
|
|
|
|
@param data data describing the disconnection
|
|
|
|
@remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
|
|
|
|
once the disconnection is complete.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
enet_peer_disconnect_later (ENetPeer * peer, enet_uint32 data)
|
|
|
|
{
|
|
|
|
if ((peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) &&
|
|
|
|
! (enet_list_empty (& peer -> outgoingReliableCommands) &&
|
|
|
|
enet_list_empty (& peer -> outgoingUnreliableCommands) &&
|
|
|
|
enet_list_empty (& peer -> sentReliableCommands)))
|
|
|
|
{
|
|
|
|
peer -> state = ENET_PEER_STATE_DISCONNECT_LATER;
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> eventData = data;
|
2006-07-06 14:22:41 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
enet_peer_disconnect (peer, data);
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetAcknowledgement *
|
2006-07-06 14:22:41 -07:00
|
|
|
enet_peer_queue_acknowledgement (ENetPeer * peer, const ENetProtocol * command, enet_uint16 sentTime)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
ENetAcknowledgement * acknowledgement;
|
|
|
|
|
2007-10-12 10:37:31 -07:00
|
|
|
if (command -> header.channelID < peer -> channelCount)
|
|
|
|
{
|
|
|
|
ENetChannel * channel = & peer -> channels [command -> header.channelID];
|
|
|
|
enet_uint16 reliableWindow = command -> header.reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE,
|
|
|
|
currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
|
2007-10-12 15:28:04 -07:00
|
|
|
if (command -> header.reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
|
2007-10-12 10:37:31 -07:00
|
|
|
|
2007-10-21 12:14:54 -07:00
|
|
|
if (reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1 && reliableWindow <= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS)
|
2007-10-12 10:37:31 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
acknowledgement = (ENetAcknowledgement *) enet_malloc (sizeof (ENetAcknowledgement));
|
2010-05-05 16:37:06 -07:00
|
|
|
if (acknowledgement == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
peer -> outgoingDataTotal += sizeof (ENetProtocolAcknowledge);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
acknowledgement -> sentTime = sentTime;
|
|
|
|
acknowledgement -> command = * command;
|
|
|
|
|
|
|
|
enet_list_insert (enet_list_end (& peer -> acknowledgements), acknowledgement);
|
|
|
|
|
|
|
|
return acknowledgement;
|
|
|
|
}
|
|
|
|
|
2010-05-06 06:44:47 -07:00
|
|
|
void
|
2010-05-06 07:30:55 -07:00
|
|
|
enet_peer_setup_outgoing_command (ENetPeer * peer, ENetOutgoingCommand * outgoingCommand)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2010-05-06 06:44:47 -07:00
|
|
|
ENetChannel * channel = & peer -> channels [outgoingCommand -> command.header.channelID];
|
2013-05-11 11:53:55 -07:00
|
|
|
|
2010-05-06 07:30:55 -07:00
|
|
|
peer -> outgoingDataTotal += enet_protocol_command_size (outgoingCommand -> command.header.command) + outgoingCommand -> fragmentLength;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2010-05-06 06:44:47 -07:00
|
|
|
if (outgoingCommand -> command.header.channelID == 0xFF)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
++ peer -> outgoingReliableSequenceNumber;
|
|
|
|
|
|
|
|
outgoingCommand -> reliableSequenceNumber = peer -> outgoingReliableSequenceNumber;
|
|
|
|
outgoingCommand -> unreliableSequenceNumber = 0;
|
|
|
|
}
|
|
|
|
else
|
2010-05-06 06:44:47 -07:00
|
|
|
if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
++ channel -> outgoingReliableSequenceNumber;
|
2007-10-11 22:18:46 -07:00
|
|
|
channel -> outgoingUnreliableSequenceNumber = 0;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
|
|
|
|
outgoingCommand -> unreliableSequenceNumber = 0;
|
|
|
|
}
|
|
|
|
else
|
2010-05-06 06:44:47 -07:00
|
|
|
if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2004-12-23 01:20:50 -07:00
|
|
|
++ peer -> outgoingUnsequencedGroup;
|
2004-12-23 01:19:42 -07:00
|
|
|
|
|
|
|
outgoingCommand -> reliableSequenceNumber = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
outgoingCommand -> unreliableSequenceNumber = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-31 00:48:27 -07:00
|
|
|
if (outgoingCommand -> fragmentOffset == 0)
|
|
|
|
++ channel -> outgoingUnreliableSequenceNumber;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
|
|
|
|
outgoingCommand -> unreliableSequenceNumber = channel -> outgoingUnreliableSequenceNumber;
|
|
|
|
}
|
|
|
|
|
2007-10-11 19:27:45 -07:00
|
|
|
outgoingCommand -> sendAttempts = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
outgoingCommand -> sentTime = 0;
|
|
|
|
outgoingCommand -> roundTripTimeout = 0;
|
|
|
|
outgoingCommand -> roundTripTimeoutLimit = 0;
|
2006-07-06 14:22:41 -07:00
|
|
|
outgoingCommand -> command.header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (outgoingCommand -> reliableSequenceNumber);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
switch (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK)
|
|
|
|
{
|
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
|
|
|
|
outgoingCommand -> command.sendUnreliable.unreliableSequenceNumber = ENET_HOST_TO_NET_16 (outgoingCommand -> unreliableSequenceNumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
|
|
|
|
outgoingCommand -> command.sendUnsequenced.unsequencedGroup = ENET_HOST_TO_NET_16 (peer -> outgoingUnsequencedGroup);
|
|
|
|
break;
|
2011-05-31 02:44:09 -07:00
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-06 06:44:47 -07:00
|
|
|
if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
|
2004-10-20 10:17:51 -07:00
|
|
|
enet_list_insert (enet_list_end (& peer -> outgoingReliableCommands), outgoingCommand);
|
|
|
|
else
|
|
|
|
enet_list_insert (enet_list_end (& peer -> outgoingUnreliableCommands), outgoingCommand);
|
2010-05-06 06:44:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ENetOutgoingCommand *
|
|
|
|
enet_peer_queue_outgoing_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 offset, enet_uint16 length)
|
|
|
|
{
|
|
|
|
ENetOutgoingCommand * outgoingCommand = (ENetOutgoingCommand *) enet_malloc (sizeof (ENetOutgoingCommand));
|
|
|
|
if (outgoingCommand == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
outgoingCommand -> command = * command;
|
2010-05-06 07:30:55 -07:00
|
|
|
outgoingCommand -> fragmentOffset = offset;
|
|
|
|
outgoingCommand -> fragmentLength = length;
|
|
|
|
outgoingCommand -> packet = packet;
|
|
|
|
if (packet != NULL)
|
|
|
|
++ packet -> referenceCount;
|
2010-05-06 06:44:47 -07:00
|
|
|
|
2010-05-06 07:30:55 -07:00
|
|
|
enet_peer_setup_outgoing_command (peer, outgoingCommand);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return outgoingCommand;
|
|
|
|
}
|
|
|
|
|
2010-06-05 13:31:31 -07:00
|
|
|
void
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_peer_dispatch_incoming_unreliable_commands (ENetPeer * peer, ENetChannel * channel)
|
|
|
|
{
|
2011-05-31 02:44:09 -07:00
|
|
|
ENetListIterator droppedCommand, startCommand, currentCommand;
|
2010-05-13 09:55:59 -07:00
|
|
|
|
2011-05-31 02:44:09 -07:00
|
|
|
for (droppedCommand = startCommand = currentCommand = enet_list_begin (& channel -> incomingUnreliableCommands);
|
2010-05-13 09:55:59 -07:00
|
|
|
currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
|
|
|
|
currentCommand = enet_list_next (currentCommand))
|
|
|
|
{
|
|
|
|
ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
|
|
|
|
continue;
|
|
|
|
|
2012-07-31 06:10:40 -07:00
|
|
|
if (incomingCommand -> reliableSequenceNumber == channel -> incomingReliableSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> fragmentsRemaining <= 0)
|
|
|
|
{
|
|
|
|
channel -> incomingUnreliableSequenceNumber = incomingCommand -> unreliableSequenceNumber;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startCommand != currentCommand)
|
|
|
|
{
|
|
|
|
enet_list_move (enet_list_end (& peer -> dispatchedCommands), startCommand, enet_list_previous (currentCommand));
|
|
|
|
|
|
|
|
if (! peer -> needsDispatch)
|
|
|
|
{
|
2011-05-31 02:44:09 -07:00
|
|
|
enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
|
2011-05-31 00:48:27 -07:00
|
|
|
|
2011-05-31 02:44:09 -07:00
|
|
|
peer -> needsDispatch = 1;
|
2012-07-31 06:10:40 -07:00
|
|
|
}
|
2011-05-31 02:44:09 -07:00
|
|
|
|
2012-07-31 06:10:40 -07:00
|
|
|
droppedCommand = currentCommand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (droppedCommand != currentCommand)
|
|
|
|
droppedCommand = enet_list_previous (currentCommand);
|
2011-05-31 00:48:27 -07:00
|
|
|
}
|
2012-07-31 06:10:40 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
enet_uint16 reliableWindow = incomingCommand -> reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE,
|
|
|
|
currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
|
|
|
|
if (reliableWindow >= currentWindow && reliableWindow < currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
droppedCommand = enet_list_next (currentCommand);
|
|
|
|
|
|
|
|
if (startCommand != currentCommand)
|
|
|
|
{
|
|
|
|
enet_list_move (enet_list_end (& peer -> dispatchedCommands), startCommand, enet_list_previous (currentCommand));
|
|
|
|
|
|
|
|
if (! peer -> needsDispatch)
|
|
|
|
{
|
|
|
|
enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
|
|
|
|
|
|
|
|
peer -> needsDispatch = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startCommand = enet_list_next (currentCommand);
|
2010-05-13 09:55:59 -07:00
|
|
|
}
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
if (startCommand != currentCommand)
|
|
|
|
{
|
2012-07-31 06:10:40 -07:00
|
|
|
enet_list_move (enet_list_end (& peer -> dispatchedCommands), startCommand, enet_list_previous (currentCommand));
|
2010-05-13 09:55:59 -07:00
|
|
|
|
2012-07-31 06:10:40 -07:00
|
|
|
if (! peer -> needsDispatch)
|
|
|
|
{
|
|
|
|
enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
|
2010-05-13 09:55:59 -07:00
|
|
|
|
2012-07-31 06:10:40 -07:00
|
|
|
peer -> needsDispatch = 1;
|
|
|
|
}
|
2011-05-31 00:48:27 -07:00
|
|
|
|
2012-07-31 06:10:40 -07:00
|
|
|
droppedCommand = currentCommand;
|
2010-05-13 09:55:59 -07:00
|
|
|
}
|
2011-05-31 02:44:09 -07:00
|
|
|
|
|
|
|
enet_peer_remove_incoming_commands (& channel -> incomingUnreliableCommands, enet_list_begin (& channel -> incomingUnreliableCommands), droppedCommand);
|
2010-05-13 09:55:59 -07:00
|
|
|
}
|
|
|
|
|
2010-06-05 13:31:31 -07:00
|
|
|
void
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_peer_dispatch_incoming_reliable_commands (ENetPeer * peer, ENetChannel * channel)
|
|
|
|
{
|
|
|
|
ENetListIterator currentCommand;
|
|
|
|
|
|
|
|
for (currentCommand = enet_list_begin (& channel -> incomingReliableCommands);
|
|
|
|
currentCommand != enet_list_end (& channel -> incomingReliableCommands);
|
|
|
|
currentCommand = enet_list_next (currentCommand))
|
|
|
|
{
|
|
|
|
ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
|
|
|
|
|
|
|
|
if (incomingCommand -> fragmentsRemaining > 0 ||
|
|
|
|
incomingCommand -> reliableSequenceNumber != (enet_uint16) (channel -> incomingReliableSequenceNumber + 1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
channel -> incomingReliableSequenceNumber = incomingCommand -> reliableSequenceNumber;
|
|
|
|
|
|
|
|
if (incomingCommand -> fragmentCount > 0)
|
|
|
|
channel -> incomingReliableSequenceNumber += incomingCommand -> fragmentCount - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentCommand == enet_list_begin (& channel -> incomingReliableCommands))
|
|
|
|
return;
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
channel -> incomingUnreliableSequenceNumber = 0;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_list_move (enet_list_end (& peer -> dispatchedCommands), enet_list_begin (& channel -> incomingReliableCommands), enet_list_previous (currentCommand));
|
|
|
|
|
|
|
|
if (! peer -> needsDispatch)
|
|
|
|
{
|
|
|
|
enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
|
|
|
|
|
|
|
|
peer -> needsDispatch = 1;
|
|
|
|
}
|
|
|
|
|
2012-07-31 06:10:40 -07:00
|
|
|
if (! enet_list_empty (& channel -> incomingUnreliableCommands))
|
|
|
|
enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
|
2010-05-13 09:55:59 -07:00
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetIncomingCommand *
|
|
|
|
enet_peer_queue_incoming_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 fragmentCount)
|
|
|
|
{
|
2010-05-06 06:44:47 -07:00
|
|
|
static ENetIncomingCommand dummyCommand;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetChannel * channel = & peer -> channels [command -> header.channelID];
|
2011-12-31 05:37:26 -07:00
|
|
|
enet_uint32 unreliableSequenceNumber = 0, reliableSequenceNumber = 0;
|
2007-10-11 22:18:46 -07:00
|
|
|
enet_uint16 reliableWindow, currentWindow;
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetIncomingCommand * incomingCommand;
|
|
|
|
ENetListIterator currentCommand;
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
goto freePacket;
|
|
|
|
|
|
|
|
if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
|
|
|
|
{
|
|
|
|
reliableSequenceNumber = command -> header.reliableSequenceNumber;
|
2007-10-11 22:18:46 -07:00
|
|
|
reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2007-10-11 22:18:46 -07:00
|
|
|
if (reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
|
|
|
|
|
2007-10-12 10:54:11 -07:00
|
|
|
if (reliableWindow < currentWindow || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
|
2006-07-06 14:22:41 -07:00
|
|
|
goto freePacket;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2004-12-09 11:41:02 -07:00
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
|
2004-10-20 10:17:51 -07:00
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
|
2006-07-06 14:22:41 -07:00
|
|
|
if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber)
|
2011-05-31 00:48:27 -07:00
|
|
|
goto freePacket;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
|
|
|
|
currentCommand != enet_list_end (& channel -> incomingReliableCommands);
|
|
|
|
currentCommand = enet_list_previous (currentCommand))
|
|
|
|
{
|
|
|
|
incomingCommand = (ENetIncomingCommand *) currentCommand;
|
|
|
|
|
2007-10-12 15:28:04 -07:00
|
|
|
if (reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
break;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
|
|
|
if (incomingCommand -> reliableSequenceNumber <= reliableSequenceNumber)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2006-07-06 14:22:41 -07:00
|
|
|
if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
|
2004-10-20 10:17:51 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
goto freePacket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
|
2011-05-31 00:48:27 -07:00
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
|
2006-07-06 14:22:41 -07:00
|
|
|
unreliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendUnreliable.unreliableSequenceNumber);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber &&
|
|
|
|
unreliableSequenceNumber <= channel -> incomingUnreliableSequenceNumber)
|
|
|
|
goto freePacket;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
|
|
|
|
currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
|
|
|
|
currentCommand = enet_list_previous (currentCommand))
|
|
|
|
{
|
|
|
|
incomingCommand = (ENetIncomingCommand *) currentCommand;
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
|
2006-07-06 14:22:41 -07:00
|
|
|
continue;
|
|
|
|
|
2007-10-12 15:28:04 -07:00
|
|
|
if (reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
break;
|
2007-10-11 22:18:46 -07:00
|
|
|
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
|
|
|
|
break;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2007-10-12 16:58:37 -07:00
|
|
|
if (incomingCommand -> reliableSequenceNumber > reliableSequenceNumber)
|
|
|
|
continue;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
if (incomingCommand -> unreliableSequenceNumber <= unreliableSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> unreliableSequenceNumber < unreliableSequenceNumber)
|
|
|
|
break;
|
|
|
|
|
|
|
|
goto freePacket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
|
|
|
|
currentCommand = enet_list_end (& channel -> incomingUnreliableCommands);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto freePacket;
|
|
|
|
}
|
|
|
|
|
|
|
|
incomingCommand = (ENetIncomingCommand *) enet_malloc (sizeof (ENetIncomingCommand));
|
2010-05-05 16:37:06 -07:00
|
|
|
if (incomingCommand == NULL)
|
2010-05-06 06:44:47 -07:00
|
|
|
goto notifyError;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
incomingCommand -> reliableSequenceNumber = command -> header.reliableSequenceNumber;
|
2006-07-06 14:22:41 -07:00
|
|
|
incomingCommand -> unreliableSequenceNumber = unreliableSequenceNumber & 0xFFFF;
|
2004-10-20 10:17:51 -07:00
|
|
|
incomingCommand -> command = * command;
|
|
|
|
incomingCommand -> fragmentCount = fragmentCount;
|
|
|
|
incomingCommand -> fragmentsRemaining = fragmentCount;
|
|
|
|
incomingCommand -> packet = packet;
|
|
|
|
incomingCommand -> fragments = NULL;
|
|
|
|
|
|
|
|
if (fragmentCount > 0)
|
|
|
|
{
|
2012-01-06 08:11:04 -07:00
|
|
|
if (fragmentCount <= ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT)
|
|
|
|
incomingCommand -> fragments = (enet_uint32 *) enet_malloc ((fragmentCount + 31) / 32 * sizeof (enet_uint32));
|
2010-05-05 16:37:06 -07:00
|
|
|
if (incomingCommand -> fragments == NULL)
|
|
|
|
{
|
|
|
|
enet_free (incomingCommand);
|
2010-05-06 06:44:47 -07:00
|
|
|
|
|
|
|
goto notifyError;
|
2010-05-05 16:37:06 -07:00
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
memset (incomingCommand -> fragments, 0, (fragmentCount + 31) / 32 * sizeof (enet_uint32));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet != NULL)
|
|
|
|
++ packet -> referenceCount;
|
|
|
|
|
|
|
|
enet_list_insert (enet_list_next (currentCommand), incomingCommand);
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
|
|
|
|
{
|
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
|
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
|
2010-05-13 11:30:17 -07:00
|
|
|
enet_peer_dispatch_incoming_reliable_commands (peer, channel);
|
2010-05-13 09:55:59 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-05-13 11:30:17 -07:00
|
|
|
enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
|
2010-05-13 09:55:59 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
return incomingCommand;
|
|
|
|
|
|
|
|
freePacket:
|
2010-05-06 06:44:47 -07:00
|
|
|
if (fragmentCount > 0)
|
|
|
|
goto notifyError;
|
|
|
|
|
|
|
|
if (packet != NULL && packet -> referenceCount == 0)
|
|
|
|
enet_packet_destroy (packet);
|
|
|
|
|
|
|
|
return & dummyCommand;
|
|
|
|
|
|
|
|
notifyError:
|
|
|
|
if (packet != NULL && packet -> referenceCount == 0)
|
|
|
|
enet_packet_destroy (packet);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|