enet/protocol.c

1500 lines
52 KiB
C
Raw Normal View History

2004-10-20 10:17:51 -07:00
/**
@file protocol.c
@brief ENet protocol functions
*/
#include <stdio.h>
#include <string.h>
#define ENET_BUILDING_LIB 1
#include "enet/utility.h"
#include "enet/time.h"
#include "enet/enet.h"
2006-07-06 14:22:41 -07:00
static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
{
0,
sizeof (ENetProtocolAcknowledge),
sizeof (ENetProtocolConnect),
sizeof (ENetProtocolVerifyConnect),
sizeof (ENetProtocolDisconnect),
sizeof (ENetProtocolPing),
sizeof (ENetProtocolSendReliable),
sizeof (ENetProtocolSendUnreliable),
sizeof (ENetProtocolSendFragment),
sizeof (ENetProtocolSendUnsequenced),
sizeof (ENetProtocolBandwidthLimit),
sizeof (ENetProtocolThrottleConfigure),
};
size_t
enet_protocol_command_size (enet_uint8 commandNumber)
{
return commandSizes [commandNumber & ENET_PROTOCOL_COMMAND_MASK];
}
2004-10-20 10:17:51 -07:00
static int
enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
{
ENetPeer * currentPeer = host -> lastServicedPeer;
ENetChannel * channel;
do
{
++ currentPeer;
if (currentPeer >= & host -> peers [host -> peerCount])
currentPeer = host -> peers;
2006-03-14 16:21:50 -07:00
switch (currentPeer -> state)
2004-10-20 10:17:51 -07:00
{
2006-03-14 16:21:50 -07:00
case ENET_PEER_STATE_CONNECTION_PENDING:
2006-07-06 14:22:41 -07:00
case ENET_PEER_STATE_CONNECTION_SUCCEEDED:
2006-03-14 16:21:50 -07:00
currentPeer -> state = ENET_PEER_STATE_CONNECTED;
event -> type = ENET_EVENT_TYPE_CONNECT;
event -> peer = currentPeer;
return 1;
case ENET_PEER_STATE_ZOMBIE:
2004-10-20 10:17:51 -07:00
host -> recalculateBandwidthLimits = 1;
event -> type = ENET_EVENT_TYPE_DISCONNECT;
event -> peer = currentPeer;
2006-03-14 16:21:50 -07:00
event -> data = currentPeer -> disconnectData;
2004-10-20 10:17:51 -07:00
enet_peer_reset (currentPeer);
host -> lastServicedPeer = currentPeer;
return 1;
}
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
for (channel = currentPeer -> channels;
channel < & currentPeer -> channels [currentPeer -> channelCount];
++ channel)
{
if (enet_list_empty (& channel -> incomingReliableCommands) &&
enet_list_empty (& channel -> incomingUnreliableCommands))
continue;
event -> packet = enet_peer_receive (currentPeer, channel - currentPeer -> channels);
if (event -> packet == NULL)
continue;
event -> type = ENET_EVENT_TYPE_RECEIVE;
event -> peer = currentPeer;
event -> channelID = (enet_uint8) (channel - currentPeer -> channels);
host -> lastServicedPeer = currentPeer;
return 1;
}
} while (currentPeer != host -> lastServicedPeer);
return 0;
}
2006-03-14 16:21:50 -07:00
static void
enet_protocol_notify_connect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
{
host -> recalculateBandwidthLimits = 1;
if (event == NULL)
2006-07-06 14:22:41 -07:00
peer -> state = (peer -> state == ENET_PEER_STATE_CONNECTING ? ENET_PEER_STATE_CONNECTION_SUCCEEDED : ENET_PEER_STATE_CONNECTION_PENDING);
2006-03-14 16:21:50 -07:00
else
{
peer -> state = ENET_PEER_STATE_CONNECTED;
event -> type = ENET_EVENT_TYPE_CONNECT;
event -> peer = peer;
}
}
static void
enet_protocol_notify_disconnect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
{
if (peer -> state >= ENET_PEER_STATE_CONNECTION_PENDING)
host -> recalculateBandwidthLimits = 1;
2007-01-12 00:00:57 -07:00
if (peer -> state != ENET_PEER_STATE_CONNECTING && peer -> state < ENET_PEER_STATE_CONNECTION_SUCCEEDED)
2006-03-14 16:21:50 -07:00
enet_peer_reset (peer);
else
if (event == NULL)
peer -> state = ENET_PEER_STATE_ZOMBIE;
else
{
event -> type = ENET_EVENT_TYPE_DISCONNECT;
event -> peer = peer;
event -> data = 0;
enet_peer_reset (peer);
}
}
2004-10-20 10:17:51 -07:00
static void
enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer)
{
ENetOutgoingCommand * outgoingCommand;
2006-07-06 14:22:41 -07:00
while (! enet_list_empty (& peer -> sentUnreliableCommands))
2004-10-20 10:17:51 -07:00
{
outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
enet_list_remove (& outgoingCommand -> outgoingCommandList);
if (outgoingCommand -> packet != NULL)
{
-- outgoingCommand -> packet -> referenceCount;
if (outgoingCommand -> packet -> referenceCount == 0)
enet_packet_destroy (outgoingCommand -> packet);
}
enet_free (outgoingCommand);
}
}
static ENetProtocolCommand
2006-07-06 14:22:41 -07:00
enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint16 reliableSequenceNumber, enet_uint8 channelID)
2004-10-20 10:17:51 -07:00
{
ENetOutgoingCommand * outgoingCommand;
ENetListIterator currentCommand;
ENetProtocolCommand commandNumber;
for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
currentCommand != enet_list_end (& peer -> sentReliableCommands);
currentCommand = enet_list_next (currentCommand))
{
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
outgoingCommand -> command.header.channelID == channelID)
break;
}
if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
return ENET_PROTOCOL_COMMAND_NONE;
2006-07-06 14:22:41 -07:00
commandNumber = outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK;
2004-10-20 10:17:51 -07:00
enet_list_remove (& outgoingCommand -> outgoingCommandList);
if (outgoingCommand -> packet != NULL)
{
peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
-- outgoingCommand -> packet -> referenceCount;
if (outgoingCommand -> packet -> referenceCount == 0)
enet_packet_destroy (outgoingCommand -> packet);
}
enet_free (outgoingCommand);
if (enet_list_empty (& peer -> sentReliableCommands))
return commandNumber;
outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
return commandNumber;
}
static ENetPeer *
2006-07-06 14:22:41 -07:00
enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENetProtocol * command)
2004-10-20 10:17:51 -07:00
{
enet_uint16 mtu;
enet_uint32 windowSize;
ENetChannel * channel;
size_t channelCount;
ENetPeer * currentPeer;
ENetProtocol verifyCommand;
2006-07-06 14:22:41 -07:00
#ifdef USE_CRC32
{
enet_uint32 crc = header -> checksum;
ENetBuffer buffer;
command -> header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (command -> header.reliableSequenceNumber);
header -> checksum = command -> connect.sessionID;
buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
if (enet_crc32 (& buffer, 1) != crc)
return NULL;
command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
}
#endif
2004-10-20 10:17:51 -07:00
channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT ||
channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
return NULL;
for (currentPeer = host -> peers;
currentPeer < & host -> peers [host -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
currentPeer -> address.host == host -> receivedAddress.host &&
currentPeer -> address.port == host -> receivedAddress.port &&
2006-07-06 14:22:41 -07:00
currentPeer -> sessionID == command -> connect.sessionID)
2004-10-20 10:17:51 -07:00
return NULL;
}
for (currentPeer = host -> peers;
currentPeer < & host -> peers [host -> peerCount];
++ currentPeer)
{
if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
break;
}
if (currentPeer >= & host -> peers [host -> peerCount])
return NULL;
currentPeer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
2006-07-06 14:22:41 -07:00
currentPeer -> sessionID = command -> connect.sessionID;
2004-10-20 10:17:51 -07:00
currentPeer -> address = host -> receivedAddress;
currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
currentPeer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
currentPeer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
currentPeer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
currentPeer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
currentPeer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
currentPeer -> channelCount = channelCount;
for (channel = currentPeer -> channels;
channel < & currentPeer -> channels [channelCount];
++ channel)
{
channel -> outgoingReliableSequenceNumber = 0;
channel -> outgoingUnreliableSequenceNumber = 0;
channel -> incomingReliableSequenceNumber = 0;
channel -> incomingUnreliableSequenceNumber = 0;
enet_list_clear (& channel -> incomingReliableCommands);
enet_list_clear (& channel -> incomingUnreliableCommands);
}
mtu = ENET_NET_TO_HOST_16 (command -> connect.mtu);
if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
mtu = ENET_PROTOCOL_MINIMUM_MTU;
else
if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
mtu = ENET_PROTOCOL_MAXIMUM_MTU;
currentPeer -> mtu = mtu;
if (host -> outgoingBandwidth == 0 &&
currentPeer -> incomingBandwidth == 0)
currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
2007-07-12 20:25:16 -07:00
else
if (host -> outgoingBandwidth == 0 ||
currentPeer -> incomingBandwidth == 0)
currentPeer -> windowSize = (ENET_MAX (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
2004-10-20 10:17:51 -07:00
else
currentPeer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
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;
if (host -> incomingBandwidth == 0)
windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
else
windowSize = (host -> incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (windowSize > ENET_NET_TO_HOST_32 (command -> connect.windowSize))
windowSize = ENET_NET_TO_HOST_32 (command -> connect.windowSize);
if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
else
if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
2006-07-06 14:22:41 -07:00
verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
2004-10-20 10:17:51 -07:00
verifyCommand.header.channelID = 0xFF;
verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_16 (currentPeer -> mtu);
verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
enet_peer_queue_outgoing_command (currentPeer, & verifyCommand, NULL, 0, 0);
return currentPeer;
}
2006-07-06 14:22:41 -07:00
static int
enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
2004-10-20 10:17:51 -07:00
{
ENetPacket * packet;
2006-07-06 14:22:41 -07:00
size_t dataLength;
if (command -> header.channelID >= peer -> channelCount ||
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
return -1;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
dataLength = ENET_NET_TO_HOST_16 (command -> sendReliable.dataLength);
* currentData += dataLength;
if (* currentData > & host -> receivedData [host -> receivedDataLength])
return -1;
2004-10-20 10:17:51 -07:00
packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable),
2006-07-06 14:22:41 -07:00
dataLength,
2004-10-20 10:17:51 -07:00
ENET_PACKET_FLAG_RELIABLE);
2006-10-03 08:39:29 -07:00
if (packet == NULL)
return -1;
2004-10-20 10:17:51 -07:00
enet_peer_queue_incoming_command (peer, command, packet, 0);
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
enet_protocol_handle_send_unsequenced (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
2004-10-20 10:17:51 -07:00
{
ENetPacket * packet;
enet_uint32 unsequencedGroup, index;
2006-07-06 14:22:41 -07:00
size_t dataLength;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
if (command -> header.channelID >= peer -> channelCount ||
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
return -1;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
dataLength = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.dataLength);
* currentData += dataLength;
if (* currentData > & host -> receivedData [host -> receivedDataLength])
return -1;
unsequencedGroup = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.unsequencedGroup);
2004-10-20 10:17:51 -07:00
index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
2006-07-06 14:22:41 -07:00
if (unsequencedGroup >= peer -> incomingUnsequencedGroup + ENET_PEER_UNSEQUENCED_WINDOW_SIZE ||
peer -> incomingUnsequencedGroup >= 0xF000 && unsequencedGroup < 0x1000)
2004-10-20 10:17:51 -07:00
{
peer -> incomingUnsequencedGroup = unsequencedGroup - index;
memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
}
else
if (unsequencedGroup < peer -> incomingUnsequencedGroup ||
peer -> unsequencedWindow [index / 32] & (1 << (index % 32)))
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
peer -> unsequencedWindow [index / 32] |= 1 << (index % 32);
packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced),
2006-07-06 14:22:41 -07:00
dataLength,
2004-10-20 10:17:51 -07:00
ENET_PACKET_FLAG_UNSEQUENCED);
2006-10-03 08:39:29 -07:00
if (packet == NULL)
return -1;
2004-10-20 10:17:51 -07:00
enet_peer_queue_incoming_command (peer, command, packet, 0);
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
2004-10-20 10:17:51 -07:00
{
ENetPacket * packet;
2006-07-06 14:22:41 -07:00
size_t dataLength;
if (command -> header.channelID >= peer -> channelCount ||
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
return -1;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
dataLength = ENET_NET_TO_HOST_16 (command -> sendUnreliable.dataLength);
* currentData += dataLength;
if (* currentData > & host -> receivedData [host -> receivedDataLength])
return -1;
2004-10-20 10:17:51 -07:00
packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable),
2006-07-06 14:22:41 -07:00
dataLength,
2004-10-20 10:17:51 -07:00
0);
2006-10-03 08:39:29 -07:00
if (packet == NULL)
return -1;
2004-10-20 10:17:51 -07:00
enet_peer_queue_incoming_command (peer, command, packet, 0);
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
2004-10-20 10:17:51 -07:00
{
enet_uint32 fragmentNumber,
fragmentCount,
fragmentOffset,
fragmentLength,
startSequenceNumber,
totalLength;
ENetChannel * channel;
ENetListIterator currentCommand;
ENetIncomingCommand * startCommand;
2006-07-06 14:22:41 -07:00
if (command -> header.channelID >= peer -> channelCount ||
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
return -1;
fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
* currentData += fragmentLength;
if (* currentData > & host -> receivedData [host -> receivedDataLength])
return -1;
channel = & peer -> channels [command -> header.channelID];
startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
if (channel -> incomingReliableSequenceNumber >= 0xF000 && startSequenceNumber < 0x1000)
startSequenceNumber += 0x10000;
if (startSequenceNumber < channel -> incomingReliableSequenceNumber ||
(channel -> incomingReliableSequenceNumber < 0x1000 && (startSequenceNumber & 0xFFFF) >= 0xF000))
return 0;
2004-10-20 10:17:51 -07:00
fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
if (fragmentOffset >= totalLength ||
fragmentOffset + fragmentLength > totalLength ||
fragmentNumber >= fragmentCount)
2006-07-06 14:22:41 -07:00
return -1;
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))
{
startCommand = (ENetIncomingCommand *) currentCommand;
2006-07-06 14:22:41 -07:00
if ((startCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_FRAGMENT &&
startCommand -> command.sendFragment.startSequenceNumber == (startSequenceNumber & 0xFFFF))
2004-10-20 10:17:51 -07:00
break;
}
if (currentCommand == enet_list_end (& channel -> incomingReliableCommands))
{
ENetProtocol hostCommand = * command;
2006-10-03 08:39:29 -07:00
ENetPacket * packet = enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE);
if (packet == NULL)
return -1;
2004-10-20 10:17:51 -07:00
hostCommand.header.reliableSequenceNumber = startSequenceNumber;
hostCommand.sendFragment.startSequenceNumber = startSequenceNumber;
2006-07-06 14:22:41 -07:00
hostCommand.sendFragment.dataLength = fragmentLength;
2004-10-20 10:17:51 -07:00
hostCommand.sendFragment.fragmentNumber = fragmentNumber;
hostCommand.sendFragment.fragmentCount = fragmentCount;
hostCommand.sendFragment.fragmentOffset = fragmentOffset;
hostCommand.sendFragment.totalLength = totalLength;
2006-10-03 08:39:29 -07:00
startCommand = enet_peer_queue_incoming_command (peer, & hostCommand, packet, fragmentCount);
2004-10-20 10:17:51 -07:00
}
else
if (totalLength != startCommand -> packet -> dataLength ||
fragmentCount != startCommand -> fragmentCount)
2006-07-06 14:22:41 -07:00
return -1;
2004-10-20 10:17:51 -07:00
2005-04-07 08:01:58 -07:00
if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
2004-12-09 11:41:02 -07:00
{
-- startCommand -> fragmentsRemaining;
2004-10-20 10:17:51 -07:00
2005-04-07 08:01:58 -07:00
startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
2004-10-20 10:17:51 -07:00
2004-12-09 11:41:02 -07:00
if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
2004-10-20 10:17:51 -07:00
2004-12-09 11:41:02 -07:00
memcpy (startCommand -> packet -> data + fragmentOffset,
(enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
fragmentLength);
}
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
2004-10-20 10:17:51 -07:00
enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
{
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
2004-10-20 10:17:51 -07:00
enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
{
peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
2006-07-06 14:22:41 -07:00
if (peer -> incomingBandwidth == 0 && host -> outgoingBandwidth == 0)
2004-10-20 10:17:51 -07:00
peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
else
peer -> windowSize = (ENET_MIN (peer -> incomingBandwidth, host -> outgoingBandwidth) /
ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (peer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
peer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
else
if (peer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
2004-10-20 10:17:51 -07:00
enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
{
peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
static int
2004-10-20 10:17:51 -07:00
enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
{
enet_peer_reset_queues (peer);
2006-07-06 14:22:41 -07:00
if (peer -> state == ENET_PEER_STATE_CONNECTION_SUCCEEDED)
peer -> state = ENET_PEER_STATE_ZOMBIE;
else
if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
2006-03-14 16:21:50 -07:00
{
if (peer -> state == ENET_PEER_STATE_CONNECTION_PENDING) host -> recalculateBandwidthLimits = 1;
enet_peer_reset (peer);
}
2004-10-20 10:17:51 -07:00
else
2006-07-06 14:22:41 -07:00
if (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
2004-10-20 10:17:51 -07:00
peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
else
peer -> state = ENET_PEER_STATE_ZOMBIE;
2006-03-14 16:21:50 -07:00
2006-07-06 14:22:41 -07:00
peer -> disconnectData = ENET_NET_TO_HOST_32 (command -> disconnect.data);
return 0;
2004-10-20 10:17:51 -07:00
}
static int
enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
{
enet_uint32 roundTripTime,
receivedSentTime,
receivedReliableSequenceNumber;
ENetProtocolCommand commandNumber;
2006-07-06 14:22:41 -07:00
receivedSentTime = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedSentTime);
2007-08-31 19:42:31 -07:00
receivedSentTime |= host -> serviceTime & 0xFFFF0000;
if ((receivedSentTime & 0x8000) > (host -> serviceTime & 0x8000))
2006-07-06 14:22:41 -07:00
receivedSentTime -= 0x10000;
2004-10-20 10:17:51 -07:00
2007-08-31 19:42:31 -07:00
if (ENET_TIME_LESS (host -> serviceTime, receivedSentTime))
2004-10-20 10:17:51 -07:00
return 0;
2007-08-31 19:42:31 -07:00
peer -> lastReceiveTime = host -> serviceTime;
2005-05-23 23:08:30 -07:00
peer -> earliestTimeout = 0;
2004-10-20 10:17:51 -07:00
2007-08-31 19:42:31 -07:00
roundTripTime = ENET_TIME_DIFFERENCE (host -> serviceTime, receivedSentTime);
2004-10-20 10:17:51 -07:00
enet_peer_throttle (peer, roundTripTime);
peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
if (roundTripTime >= peer -> roundTripTime)
{
peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
}
else
{
peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
}
if (peer -> roundTripTime < peer -> lowestRoundTripTime)
peer -> lowestRoundTripTime = peer -> roundTripTime;
if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
if (peer -> packetThrottleEpoch == 0 ||
2007-08-31 19:42:31 -07:00
ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
2004-10-20 10:17:51 -07:00
{
peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
peer -> lowestRoundTripTime = peer -> roundTripTime;
peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
2007-08-31 19:42:31 -07:00
peer -> packetThrottleEpoch = host -> serviceTime;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
receivedReliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedReliableSequenceNumber);
2004-10-20 10:17:51 -07:00
commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
switch (peer -> state)
{
case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
2006-07-06 14:22:41 -07:00
return -1;
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
enet_protocol_notify_connect (host, peer, event);
2006-07-06 14:22:41 -07:00
break;
2004-10-20 10:17:51 -07:00
case ENET_PEER_STATE_DISCONNECTING:
if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
2006-07-06 14:22:41 -07:00
return -1;
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
enet_protocol_notify_disconnect (host, peer, event);
2006-07-06 14:22:41 -07:00
break;
case ENET_PEER_STATE_DISCONNECT_LATER:
if (enet_list_empty (& peer -> outgoingReliableCommands) &&
enet_list_empty (& peer -> outgoingUnreliableCommands) &&
enet_list_empty (& peer -> sentReliableCommands))
enet_peer_disconnect (peer, peer -> disconnectData);
break;
2004-10-20 10:17:51 -07:00
}
return 0;
}
2006-07-06 14:22:41 -07:00
static int
2004-10-20 10:17:51 -07:00
enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
{
enet_uint16 mtu;
enet_uint32 windowSize;
2006-07-06 14:22:41 -07:00
if (peer -> state != ENET_PEER_STATE_CONNECTING)
return -1;
2004-10-20 10:17:51 -07:00
if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
{
peer -> state = ENET_PEER_STATE_ZOMBIE;
2006-07-06 14:22:41 -07:00
return -1;
2004-10-20 10:17:51 -07:00
}
2006-07-06 14:22:41 -07:00
enet_protocol_remove_sent_reliable_command (peer, 1, 0xFF);
2004-10-20 10:17:51 -07:00
peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
mtu = ENET_PROTOCOL_MINIMUM_MTU;
else
if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
mtu = ENET_PROTOCOL_MAXIMUM_MTU;
if (mtu < peer -> mtu)
peer -> mtu = mtu;
windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
if (windowSize < peer -> windowSize)
peer -> windowSize = windowSize;
peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
2006-03-14 16:21:50 -07:00
enet_protocol_notify_connect (host, peer, event);
2006-07-06 14:22:41 -07:00
return 0;
2004-10-20 10:17:51 -07:00
}
static int
enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
{
ENetProtocolHeader * header;
ENetProtocol * command;
ENetPeer * peer;
enet_uint8 * currentData;
2006-07-06 14:22:41 -07:00
size_t headerSize;
enet_uint16 peerID, flags;
2004-10-20 10:17:51 -07:00
if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
return 0;
header = (ENetProtocolHeader *) host -> receivedData;
2006-07-06 14:22:41 -07:00
peerID = ENET_NET_TO_HOST_16 (header -> peerID);
flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
peerID &= ~ ENET_PROTOCOL_HEADER_FLAG_MASK;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
if (peerID == ENET_PROTOCOL_MAXIMUM_PEER_ID)
2004-10-20 10:17:51 -07:00
peer = NULL;
else
2006-07-06 14:22:41 -07:00
if (peerID >= host -> peerCount)
2004-10-20 10:17:51 -07:00
return 0;
else
{
2006-07-06 14:22:41 -07:00
peer = & host -> peers [peerID];
2004-10-20 10:17:51 -07:00
if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
peer -> state == ENET_PEER_STATE_ZOMBIE ||
2005-06-07 18:13:28 -07:00
(host -> receivedAddress.host != peer -> address.host &&
2006-07-06 14:22:41 -07:00
peer -> address.host != ENET_HOST_BROADCAST))
2004-10-20 10:17:51 -07:00
return 0;
2006-07-06 14:22:41 -07:00
#ifdef USE_CRC32
2005-06-07 18:13:28 -07:00
{
2006-07-06 14:22:41 -07:00
enet_uint32 crc = header -> checksum;
ENetBuffer buffer;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
header -> checksum = peer -> sessionID;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength;
if (enet_crc32 (& buffer, 1) != crc)
return 0;
}
#else
if (header -> checksum != peer -> sessionID)
return 0;
#endif
peer -> address.host = host -> receivedAddress.host;
peer -> address.port = host -> receivedAddress.port;
peer -> incomingDataTotal += host -> receivedDataLength;
}
headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : (size_t) & ((ENetProtocolHeader *) 0) -> sentTime);
currentData = host -> receivedData + headerSize;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
while (currentData < & host -> receivedData [host -> receivedDataLength])
2004-10-20 10:17:51 -07:00
{
2006-07-06 14:22:41 -07:00
enet_uint8 commandNumber;
size_t commandSize;
2004-10-20 10:17:51 -07:00
command = (ENetProtocol *) currentData;
if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
2006-03-14 16:21:50 -07:00
break;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
commandNumber = command -> header.command & ENET_PROTOCOL_COMMAND_MASK;
if (commandNumber >= ENET_PROTOCOL_COMMAND_COUNT)
break;
commandSize = commandSizes [commandNumber];
if (commandSize == 0 || currentData + commandSize > & host -> receivedData [host -> receivedDataLength])
2006-03-14 16:21:50 -07:00
break;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
currentData += commandSize;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
if (peer == NULL && commandNumber != ENET_PROTOCOL_COMMAND_CONNECT)
2006-03-14 16:21:50 -07:00
break;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
2004-10-20 10:17:51 -07:00
{
case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_acknowledge (host, event, peer, command))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_CONNECT:
peer = enet_protocol_handle_connect (host, header, command);
2006-07-06 14:22:41 -07:00
if (peer == NULL)
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_verify_connect (host, event, peer, command))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_DISCONNECT:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_disconnect (host, peer, command))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_PING:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_ping (host, peer, command))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_send_reliable (host, peer, command, & currentData))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_send_unreliable (host, peer, command, & currentData))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_send_unsequenced (host, peer, command, & currentData))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_send_fragment (host, peer, command, & currentData))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_bandwidth_limit (host, peer, command))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
2006-07-06 14:22:41 -07:00
if (enet_protocol_handle_throttle_configure (host, peer, command))
goto commandError;
2004-10-20 10:17:51 -07:00
break;
default:
2006-07-06 14:22:41 -07:00
goto commandError;
2004-10-20 10:17:51 -07:00
}
if (peer != NULL &&
2006-07-06 14:22:41 -07:00
(command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
2004-10-20 10:17:51 -07:00
{
2006-07-06 14:22:41 -07:00
enet_uint16 sentTime;
if (! (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME))
break;
sentTime = ENET_NET_TO_HOST_16 (header -> sentTime);
2004-10-20 10:17:51 -07:00
switch (peer -> state)
{
case ENET_PEER_STATE_DISCONNECTING:
2006-07-06 14:22:41 -07:00
case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
2004-10-20 10:17:51 -07:00
break;
case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
2006-07-06 14:22:41 -07:00
if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
enet_peer_queue_acknowledgement (peer, command, sentTime);
break;
2004-10-20 10:17:51 -07:00
default:
2006-07-06 14:22:41 -07:00
enet_peer_queue_acknowledgement (peer, command, sentTime);
2004-10-20 10:17:51 -07:00
break;
}
}
}
2006-07-06 14:22:41 -07:00
commandError:
2006-03-14 16:21:50 -07:00
if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
2004-10-20 10:17:51 -07:00
return 1;
return 0;
}
static int
enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
{
for (;;)
{
int receivedLength;
ENetBuffer buffer;
buffer.data = host -> receivedData;
buffer.dataLength = sizeof (host -> receivedData);
receivedLength = enet_socket_receive (host -> socket,
& host -> receivedAddress,
& buffer,
1);
if (receivedLength < 0)
return -1;
if (receivedLength == 0)
return 0;
host -> receivedDataLength = receivedLength;
switch (enet_protocol_handle_incoming_commands (host, event))
{
case 1:
return 1;
case -1:
return -1;
default:
break;
}
}
return -1;
}
static void
enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
{
ENetProtocol * command = & host -> commands [host -> commandCount];
ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
ENetAcknowledgement * acknowledgement;
ENetListIterator currentAcknowledgement;
currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
{
if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
2006-07-06 14:22:41 -07:00
{
host -> continueSending = 1;
break;
}
2004-10-20 10:17:51 -07:00
acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
currentAcknowledgement = enet_list_next (currentAcknowledgement);
buffer -> data = command;
buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
host -> packetSize += buffer -> dataLength;
command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
command -> header.channelID = acknowledgement -> command.header.channelID;
2006-07-06 14:22:41 -07:00
command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_16 (acknowledgement -> sentTime);
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
if ((acknowledgement -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
2004-10-20 10:17:51 -07:00
peer -> state = ENET_PEER_STATE_ZOMBIE;
enet_list_remove (& acknowledgement -> acknowledgementList);
enet_free (acknowledgement);
++ command;
++ buffer;
}
host -> commandCount = command - host -> commands;
host -> bufferCount = buffer - host -> buffers;
}
static void
enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
{
ENetProtocol * command = & host -> commands [host -> commandCount];
ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
ENetOutgoingCommand * outgoingCommand;
ENetListIterator currentCommand;
currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
{
2006-07-06 14:22:41 -07:00
size_t commandSize;
2004-10-20 10:17:51 -07:00
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
2006-07-06 14:22:41 -07:00
commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
2004-10-20 10:17:51 -07:00
if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
2006-07-06 14:22:41 -07:00
peer -> mtu - host -> packetSize < commandSize ||
2004-10-20 10:17:51 -07:00
(outgoingCommand -> packet != NULL &&
2006-07-06 14:22:41 -07:00
peer -> mtu - host -> packetSize < commandSize + outgoingCommand -> packet -> dataLength))
{
host -> continueSending = 1;
break;
}
2004-10-20 10:17:51 -07:00
currentCommand = enet_list_next (currentCommand);
if (outgoingCommand -> packet != NULL)
{
peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
if (peer -> packetThrottleCounter > peer -> packetThrottle)
{
-- outgoingCommand -> packet -> referenceCount;
if (outgoingCommand -> packet -> referenceCount == 0)
enet_packet_destroy (outgoingCommand -> packet);
enet_list_remove (& outgoingCommand -> outgoingCommandList);
enet_free (outgoingCommand);
continue;
}
}
buffer -> data = command;
2006-07-06 14:22:41 -07:00
buffer -> dataLength = commandSize;
2004-10-20 10:17:51 -07:00
host -> packetSize += buffer -> dataLength;
* command = outgoingCommand -> command;
enet_list_remove (& outgoingCommand -> outgoingCommandList);
if (outgoingCommand -> packet != NULL)
{
++ buffer;
buffer -> data = outgoingCommand -> packet -> data;
buffer -> dataLength = outgoingCommand -> packet -> dataLength;
host -> packetSize += buffer -> dataLength;
enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
}
else
enet_free (outgoingCommand);
++ command;
++ buffer;
}
host -> commandCount = command - host -> commands;
host -> bufferCount = buffer - host -> buffers;
2006-07-06 14:22:41 -07:00
if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
enet_list_empty (& peer -> outgoingReliableCommands) &&
enet_list_empty (& peer -> outgoingUnreliableCommands) &&
enet_list_empty (& peer -> sentReliableCommands))
enet_peer_disconnect (peer, peer -> disconnectData);
2004-10-20 10:17:51 -07:00
}
static int
enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
{
ENetOutgoingCommand * outgoingCommand;
ENetListIterator currentCommand;
currentCommand = enet_list_begin (& peer -> sentReliableCommands);
while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
{
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
currentCommand = enet_list_next (currentCommand);
2007-08-31 19:42:31 -07:00
if (ENET_TIME_DIFFERENCE (host -> serviceTime, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
2004-10-20 10:17:51 -07:00
continue;
2007-08-31 19:42:31 -07:00
if (peer -> earliestTimeout == 0 ||
ENET_TIME_LESS (outgoingCommand -> sentTime, peer -> earliestTimeout))
peer -> earliestTimeout = outgoingCommand -> sentTime;
2005-05-23 23:08:30 -07:00
if (peer -> earliestTimeout != 0 &&
2007-08-31 19:42:31 -07:00
(ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MAXIMUM ||
2005-05-23 23:08:30 -07:00
(outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit &&
2007-08-31 19:42:31 -07:00
ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MINIMUM)))
2004-10-20 10:17:51 -07:00
{
2006-03-14 16:21:50 -07:00
enet_protocol_notify_disconnect (host, peer, event);
2004-10-20 10:17:51 -07:00
return 1;
}
if (outgoingCommand -> packet != NULL)
peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
++ peer -> packetsLost;
outgoingCommand -> roundTripTimeout *= 2;
enet_list_insert (enet_list_begin (& peer -> outgoingReliableCommands),
enet_list_remove (& outgoingCommand -> outgoingCommandList));
if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
2006-07-06 14:22:41 -07:00
! enet_list_empty (& peer -> sentReliableCommands))
2004-10-20 10:17:51 -07:00
{
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
}
}
return 0;
}
static void
enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
{
ENetProtocol * command = & host -> commands [host -> commandCount];
ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
ENetOutgoingCommand * outgoingCommand;
ENetListIterator currentCommand;
currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
{
2006-07-06 14:22:41 -07:00
size_t commandSize;
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
2004-10-20 10:17:51 -07:00
if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
2006-07-06 14:22:41 -07:00
peer -> mtu - host -> packetSize < commandSize)
{
host -> continueSending = 1;
break;
}
2004-10-20 10:17:51 -07:00
currentCommand = enet_list_next (currentCommand);
if (outgoingCommand -> packet != NULL)
{
2006-07-06 14:22:41 -07:00
if (peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > peer -> windowSize)
2004-10-20 10:17:51 -07:00
break;
2006-07-06 14:22:41 -07:00
if ((enet_uint16) (peer -> mtu - host -> packetSize) < (enet_uint16) (commandSize + outgoingCommand -> fragmentLength))
{
host -> continueSending = 1;
break;
}
2004-10-20 10:17:51 -07:00
}
if (outgoingCommand -> roundTripTimeout == 0)
{
outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
outgoingCommand -> roundTripTimeoutLimit = ENET_PEER_TIMEOUT_LIMIT * outgoingCommand -> roundTripTimeout;
}
if (enet_list_empty (& peer -> sentReliableCommands))
2007-08-31 19:42:31 -07:00
peer -> nextTimeout = host -> serviceTime + outgoingCommand -> roundTripTimeout;
2004-10-20 10:17:51 -07:00
enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
enet_list_remove (& outgoingCommand -> outgoingCommandList));
2007-08-31 19:42:31 -07:00
outgoingCommand -> sentTime = host -> serviceTime;
2004-10-20 10:17:51 -07:00
buffer -> data = command;
2006-07-06 14:22:41 -07:00
buffer -> dataLength = commandSize;
2004-10-20 10:17:51 -07:00
host -> packetSize += buffer -> dataLength;
2006-07-06 14:22:41 -07:00
host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
2004-10-20 10:17:51 -07:00
* command = outgoingCommand -> command;
if (outgoingCommand -> packet != NULL)
{
++ buffer;
buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
buffer -> dataLength = outgoingCommand -> fragmentLength;
host -> packetSize += outgoingCommand -> fragmentLength;
peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
}
++ peer -> packetsSent;
++ command;
++ buffer;
}
host -> commandCount = command - host -> commands;
host -> bufferCount = buffer - host -> buffers;
}
static int
enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
{
ENetProtocolHeader header;
ENetPeer * currentPeer;
int sentLength;
2006-07-06 14:22:41 -07:00
host -> continueSending = 1;
while (host -> continueSending)
for (host -> continueSending = 0,
currentPeer = host -> peers;
2004-10-20 10:17:51 -07:00
currentPeer < & host -> peers [host -> peerCount];
++ currentPeer)
{
if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
continue;
2006-07-06 14:22:41 -07:00
host -> headerFlags = 0;
2004-10-20 10:17:51 -07:00
host -> commandCount = 0;
host -> bufferCount = 1;
host -> packetSize = sizeof (ENetProtocolHeader);
2006-07-06 14:22:41 -07:00
if (! enet_list_empty (& currentPeer -> acknowledgements))
2004-10-20 10:17:51 -07:00
enet_protocol_send_acknowledgements (host, currentPeer);
2006-07-06 14:22:41 -07:00
if (checkForTimeouts != 0 &&
! enet_list_empty (& currentPeer -> sentReliableCommands) &&
2007-08-31 19:42:31 -07:00
ENET_TIME_GREATER_EQUAL (host -> serviceTime, currentPeer -> nextTimeout) &&
2006-07-06 14:22:41 -07:00
enet_protocol_check_timeouts (host, currentPeer, event) == 1)
return 1;
if (! enet_list_empty (& currentPeer -> outgoingReliableCommands))
2004-10-20 10:17:51 -07:00
enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
else
if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
2007-08-31 19:42:31 -07:00
ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
2004-10-20 10:17:51 -07:00
currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
{
enet_peer_ping (currentPeer);
enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
}
2006-07-06 14:22:41 -07:00
if (! enet_list_empty (& currentPeer -> outgoingUnreliableCommands))
2004-10-20 10:17:51 -07:00
enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
if (host -> commandCount == 0)
continue;
if (currentPeer -> packetLossEpoch == 0)
2007-08-31 19:42:31 -07:00
currentPeer -> packetLossEpoch = host -> serviceTime;
2004-10-20 10:17:51 -07:00
else
2007-08-31 19:42:31 -07:00
if (ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
2004-10-20 10:17:51 -07:00
currentPeer -> packetsSent > 0)
{
enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
#ifdef ENET_DEBUG
#ifdef WIN32
2005-04-07 08:01:58 -07:00
printf (
2004-10-20 10:17:51 -07:00
#else
2005-04-07 08:01:58 -07:00
fprintf (stderr,
2004-10-20 10:17:51 -07:00
#endif
2006-10-03 08:39:29 -07:00
"peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands) : 0);
2004-10-20 10:17:51 -07:00
#endif
currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
if (packetLoss >= currentPeer -> packetLoss)
{
currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
}
else
{
currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
}
2007-08-31 19:42:31 -07:00
currentPeer -> packetLossEpoch = host -> serviceTime;
2004-10-20 10:17:51 -07:00
currentPeer -> packetsSent = 0;
currentPeer -> packetsLost = 0;
}
2006-07-06 14:22:41 -07:00
header.checksum = currentPeer -> sessionID;
header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID | host -> headerFlags);
2004-10-20 10:17:51 -07:00
host -> buffers -> data = & header;
2006-07-06 14:22:41 -07:00
if (host -> headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
{
2007-08-31 19:42:31 -07:00
header.sentTime = ENET_HOST_TO_NET_16 (host -> serviceTime & 0xFFFF);
2004-10-20 10:17:51 -07:00
2006-07-06 14:22:41 -07:00
host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
}
else
host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
#ifdef USE_CRC32
header.checksum = enet_crc32 (host -> buffers, host -> bufferCount);
#endif
2004-10-20 10:17:51 -07:00
2007-08-31 19:42:31 -07:00
currentPeer -> lastSendTime = host -> serviceTime;
2004-10-20 10:17:51 -07:00
sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
enet_protocol_remove_sent_unreliable_commands (currentPeer);
if (sentLength < 0)
return -1;
}
return 0;
}
/** Sends any queued packets on the host specified to its designated peers.
@param host host to flush
@remarks this function need only be used in circumstances where one wishes to send queued packets earlier than in a call to enet_host_service().
@ingroup host
*/
void
enet_host_flush (ENetHost * host)
{
2007-08-31 19:42:31 -07:00
host -> serviceTime = enet_time_get ();
2004-10-20 10:17:51 -07:00
enet_protocol_send_outgoing_commands (host, NULL, 0);
}
2007-08-31 19:42:31 -07:00
/** Checks for any queued events on the host and dispatches one if available.
@param host host to check for events
@param event an event structure where event details will be placed if available
@retval > 0 if an event was dispatched
@retval 0 if no events are available
@retval < 0 on failure
@ingroup host
*/
int
enet_host_check_events (ENetHost * host, ENetEvent * event)
{
if (event == NULL) return -1;
event -> type = ENET_EVENT_TYPE_NONE;
event -> peer = NULL;
event -> packet = NULL;
return enet_protocol_dispatch_incoming_commands (host, event);
}
2004-10-20 10:17:51 -07:00
/** Waits for events on the host specified and shuttles packets between
the host and its peers.
@param host host to service
@param event an event structure where event details will be placed if one occurs
2006-03-14 16:21:50 -07:00
if event == NULL then no events will be delivered
2004-10-20 10:17:51 -07:00
@param timeout number of milliseconds that ENet should wait for events
2004-11-30 16:32:16 -07:00
@retval > 0 if an event occurred within the specified time limit
2004-10-20 10:17:51 -07:00
@retval 0 if no event occurred
2004-11-30 16:32:16 -07:00
@retval < 0 on failure
2004-10-20 10:17:51 -07:00
@remarks enet_host_service should be called fairly regularly for adequate performance
@ingroup host
*/
int
enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
{
enet_uint32 waitCondition;
2006-03-14 16:21:50 -07:00
if (event != NULL)
2004-10-20 10:17:51 -07:00
{
2006-03-14 16:21:50 -07:00
event -> type = ENET_EVENT_TYPE_NONE;
event -> peer = NULL;
event -> packet = NULL;
switch (enet_protocol_dispatch_incoming_commands (host, event))
{
case 1:
return 1;
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
case -1:
perror ("Error dispatching incoming packets");
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
return -1;
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
default:
break;
}
2004-10-20 10:17:51 -07:00
}
2006-03-14 16:21:50 -07:00
2007-08-31 19:42:31 -07:00
host -> serviceTime = enet_time_get ();
2004-10-20 10:17:51 -07:00
2007-08-31 19:42:31 -07:00
timeout += host -> serviceTime;
2004-10-20 10:17:51 -07:00
do
{
2007-08-31 19:42:31 -07:00
if (ENET_TIME_DIFFERENCE (host -> serviceTime, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
2004-10-20 10:17:51 -07:00
enet_host_bandwidth_throttle (host);
switch (enet_protocol_send_outgoing_commands (host, event, 1))
{
case 1:
return 1;
case -1:
perror ("Error sending outgoing packets");
return -1;
default:
break;
}
switch (enet_protocol_receive_incoming_commands (host, event))
{
case 1:
return 1;
case -1:
perror ("Error receiving incoming packets");
return -1;
default:
break;
}
switch (enet_protocol_send_outgoing_commands (host, event, 1))
{
case 1:
return 1;
case -1:
perror ("Error sending outgoing packets");
return -1;
default:
break;
}
2006-03-14 16:21:50 -07:00
if (event != NULL)
2004-10-20 10:17:51 -07:00
{
2006-03-14 16:21:50 -07:00
switch (enet_protocol_dispatch_incoming_commands (host, event))
{
case 1:
return 1;
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
case -1:
perror ("Error dispatching incoming packets");
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
return -1;
2004-10-20 10:17:51 -07:00
2006-03-14 16:21:50 -07:00
default:
break;
}
2004-10-20 10:17:51 -07:00
}
2007-08-31 19:42:31 -07:00
host -> serviceTime = enet_time_get ();
2004-10-20 10:17:51 -07:00
2007-08-31 19:42:31 -07:00
if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
2004-10-20 10:17:51 -07:00
return 0;
waitCondition = ENET_SOCKET_WAIT_RECEIVE;
2007-08-31 19:42:31 -07:00
if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
2004-10-20 10:17:51 -07:00
return -1;
2007-08-31 19:42:31 -07:00
host -> serviceTime = enet_time_get ();
2004-10-20 10:17:51 -07:00
} while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
return 0;
}