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"
|
|
|
|
|
2022-08-12 05:46:05 -07:00
|
|
|
static const size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
|
2006-07-06 14:22:41 -07:00
|
|
|
{
|
|
|
|
0,
|
|
|
|
sizeof (ENetProtocolAcknowledge),
|
|
|
|
sizeof (ENetProtocolConnect),
|
|
|
|
sizeof (ENetProtocolVerifyConnect),
|
|
|
|
sizeof (ENetProtocolDisconnect),
|
|
|
|
sizeof (ENetProtocolPing),
|
|
|
|
sizeof (ENetProtocolSendReliable),
|
|
|
|
sizeof (ENetProtocolSendUnreliable),
|
|
|
|
sizeof (ENetProtocolSendFragment),
|
|
|
|
sizeof (ENetProtocolSendUnsequenced),
|
|
|
|
sizeof (ENetProtocolBandwidthLimit),
|
|
|
|
sizeof (ENetProtocolThrottleConfigure),
|
2011-05-31 00:48:27 -07:00
|
|
|
sizeof (ENetProtocolSendFragment)
|
2006-07-06 14:22:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t
|
|
|
|
enet_protocol_command_size (enet_uint8 commandNumber)
|
|
|
|
{
|
|
|
|
return commandSizes [commandNumber & ENET_PROTOCOL_COMMAND_MASK];
|
|
|
|
}
|
|
|
|
|
2013-05-11 11:53:55 -07:00
|
|
|
static void
|
|
|
|
enet_protocol_change_state (ENetHost * host, ENetPeer * peer, ENetPeerState state)
|
|
|
|
{
|
|
|
|
if (state == ENET_PEER_STATE_CONNECTED || state == ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
enet_peer_on_connect (peer);
|
|
|
|
else
|
|
|
|
enet_peer_on_disconnect (peer);
|
|
|
|
|
|
|
|
peer -> state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
enet_protocol_dispatch_state (ENetHost * host, ENetPeer * peer, ENetPeerState state)
|
|
|
|
{
|
2013-05-12 01:04:00 -07:00
|
|
|
enet_protocol_change_state (host, peer, state);
|
2013-05-11 11:53:55 -07:00
|
|
|
|
2020-04-04 09:30:54 -07:00
|
|
|
if (! (peer -> flags & ENET_PEER_FLAG_NEEDS_DISPATCH))
|
2013-05-11 11:53:55 -07:00
|
|
|
{
|
|
|
|
enet_list_insert (enet_list_end (& host -> dispatchQueue), & peer -> dispatchList);
|
|
|
|
|
2020-04-04 09:30:54 -07:00
|
|
|
peer -> flags |= ENET_PEER_FLAG_NEEDS_DISPATCH;
|
2013-05-11 11:53:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
static int
|
|
|
|
enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
|
|
|
|
{
|
2010-05-13 09:55:59 -07:00
|
|
|
while (! enet_list_empty (& host -> dispatchQueue))
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2010-05-13 09:55:59 -07:00
|
|
|
ENetPeer * peer = (ENetPeer *) enet_list_remove (enet_list_begin (& host -> dispatchQueue));
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-04-04 09:30:54 -07:00
|
|
|
peer -> flags &= ~ ENET_PEER_FLAG_NEEDS_DISPATCH;
|
2010-05-13 09:55:59 -07:00
|
|
|
|
|
|
|
switch (peer -> 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:
|
2013-05-11 11:53:55 -07:00
|
|
|
enet_protocol_change_state (host, peer, ENET_PEER_STATE_CONNECTED);
|
2006-03-14 16:21:50 -07:00
|
|
|
|
|
|
|
event -> type = ENET_EVENT_TYPE_CONNECT;
|
2010-05-13 09:55:59 -07:00
|
|
|
event -> peer = peer;
|
2010-06-09 14:16:23 -07:00
|
|
|
event -> data = peer -> eventData;
|
2006-03-14 16:21:50 -07:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case ENET_PEER_STATE_ZOMBIE:
|
2004-10-20 10:17:51 -07:00
|
|
|
host -> recalculateBandwidthLimits = 1;
|
|
|
|
|
|
|
|
event -> type = ENET_EVENT_TYPE_DISCONNECT;
|
2010-05-13 09:55:59 -07:00
|
|
|
event -> peer = peer;
|
2010-06-09 14:16:23 -07:00
|
|
|
event -> data = peer -> eventData;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_peer_reset (peer);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
case ENET_PEER_STATE_CONNECTED:
|
|
|
|
if (enet_list_empty (& peer -> dispatchedCommands))
|
2004-10-20 10:17:51 -07:00
|
|
|
continue;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
event -> packet = enet_peer_receive (peer, & event -> channelID);
|
2004-10-20 10:17:51 -07:00
|
|
|
if (event -> packet == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
event -> type = ENET_EVENT_TYPE_RECEIVE;
|
2010-05-13 09:55:59 -07:00
|
|
|
event -> peer = peer;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
if (! enet_list_empty (& peer -> dispatchedCommands))
|
|
|
|
{
|
2020-04-04 09:30:54 -07:00
|
|
|
peer -> flags |= ENET_PEER_FLAG_NEEDS_DISPATCH;
|
2010-05-13 09:55:59 -07:00
|
|
|
|
|
|
|
enet_list_insert (enet_list_end (& host -> dispatchQueue), & peer -> dispatchList);
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return 1;
|
2012-07-31 06:12:11 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
2010-05-13 09:55:59 -07:00
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
if (event != NULL)
|
2006-03-14 16:21:50 -07:00
|
|
|
{
|
2013-05-11 11:53:55 -07:00
|
|
|
enet_protocol_change_state (host, peer, ENET_PEER_STATE_CONNECTED);
|
2006-03-14 16:21:50 -07:00
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
event -> type = ENET_EVENT_TYPE_CONNECT;
|
|
|
|
event -> peer = peer;
|
2010-06-09 14:16:23 -07:00
|
|
|
event -> data = peer -> eventData;
|
2006-03-14 16:21:50 -07:00
|
|
|
}
|
2010-05-13 09:55:59 -07:00
|
|
|
else
|
|
|
|
enet_protocol_dispatch_state (host, peer, peer -> state == ENET_PEER_STATE_CONNECTING ? ENET_PEER_STATE_CONNECTION_SUCCEEDED : ENET_PEER_STATE_CONNECTION_PENDING);
|
2006-03-14 16:21:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
enet_protocol_notify_disconnect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
|
|
|
|
{
|
|
|
|
if (peer -> state >= ENET_PEER_STATE_CONNECTION_PENDING)
|
2010-05-13 09:55:59 -07:00
|
|
|
host -> recalculateBandwidthLimits = 1;
|
2006-03-14 16:21:50 -07:00
|
|
|
|
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
|
2010-05-13 09:55:59 -07:00
|
|
|
if (event != NULL)
|
2006-03-14 16:21:50 -07:00
|
|
|
{
|
|
|
|
event -> type = ENET_EVENT_TYPE_DISCONNECT;
|
|
|
|
event -> peer = peer;
|
|
|
|
event -> data = 0;
|
|
|
|
|
|
|
|
enet_peer_reset (peer);
|
|
|
|
}
|
2010-05-13 09:55:59 -07:00
|
|
|
else
|
2010-06-09 14:16:23 -07:00
|
|
|
{
|
|
|
|
peer -> eventData = 0;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
|
2010-06-09 14:16:23 -07:00
|
|
|
}
|
2006-03-14 16:21:50 -07:00
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
static void
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer, ENetList * sentUnreliableCommands)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
ENetOutgoingCommand * outgoingCommand;
|
|
|
|
|
2023-02-04 21:20:39 -07:00
|
|
|
if (enet_list_empty (sentUnreliableCommands))
|
2019-01-27 12:46:45 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2023-02-04 21:20:39 -07:00
|
|
|
outgoingCommand = (ENetOutgoingCommand *) enet_list_front (sentUnreliableCommands);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
enet_list_remove (& outgoingCommand -> outgoingCommandList);
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet != NULL)
|
|
|
|
{
|
|
|
|
-- outgoingCommand -> packet -> referenceCount;
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet -> referenceCount == 0)
|
2013-02-27 07:29:22 -07:00
|
|
|
{
|
|
|
|
outgoingCommand -> packet -> flags |= ENET_PACKET_FLAG_SENT;
|
|
|
|
|
|
|
|
enet_packet_destroy (outgoingCommand -> packet);
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
enet_free (outgoingCommand);
|
2023-02-04 21:20:39 -07:00
|
|
|
} while (! enet_list_empty (sentUnreliableCommands));
|
2019-01-27 12:46:45 -07:00
|
|
|
|
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
|
2023-02-04 20:16:19 -07:00
|
|
|
! enet_peer_has_outgoing_commands (peer))
|
2019-01-27 12:46:45 -07:00
|
|
|
enet_peer_disconnect (peer, peer -> eventData);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2023-02-04 20:16:19 -07:00
|
|
|
static ENetOutgoingCommand *
|
|
|
|
enet_protocol_find_sent_reliable_command (ENetList * list, enet_uint16 reliableSequenceNumber, enet_uint8 channelID)
|
|
|
|
{
|
|
|
|
ENetListIterator currentCommand;
|
|
|
|
|
|
|
|
for (currentCommand = enet_list_begin (list);
|
|
|
|
currentCommand != enet_list_end (list);
|
|
|
|
currentCommand = enet_list_next (currentCommand))
|
|
|
|
{
|
|
|
|
ENetOutgoingCommand * outgoingCommand = (ENetOutgoingCommand *) currentCommand;
|
|
|
|
|
|
|
|
if (! (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (outgoingCommand -> sendAttempts < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
|
|
|
|
outgoingCommand -> command.header.channelID == channelID)
|
|
|
|
return outgoingCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
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
|
|
|
{
|
2011-12-31 05:37:26 -07:00
|
|
|
ENetOutgoingCommand * outgoingCommand = NULL;
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetListIterator currentCommand;
|
|
|
|
ENetProtocolCommand commandNumber;
|
2011-01-31 01:27:39 -07:00
|
|
|
int wasSent = 1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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))
|
2007-10-13 19:09:03 -07:00
|
|
|
{
|
2023-02-04 20:16:19 -07:00
|
|
|
outgoingCommand = enet_protocol_find_sent_reliable_command (& peer -> outgoingCommands, reliableSequenceNumber, channelID);
|
|
|
|
if (outgoingCommand == NULL)
|
|
|
|
outgoingCommand = enet_protocol_find_sent_reliable_command (& peer -> outgoingSendReliableCommands, reliableSequenceNumber, channelID);
|
2011-01-31 01:27:39 -07:00
|
|
|
|
|
|
|
wasSent = 0;
|
2007-10-13 19:09:03 -07:00
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2012-09-18 06:38:10 -07:00
|
|
|
if (outgoingCommand == NULL)
|
|
|
|
return ENET_PROTOCOL_COMMAND_NONE;
|
|
|
|
|
2007-10-12 10:37:31 -07:00
|
|
|
if (channelID < peer -> channelCount)
|
2007-10-11 22:18:46 -07:00
|
|
|
{
|
2007-10-12 10:37:31 -07:00
|
|
|
ENetChannel * channel = & peer -> channels [channelID];
|
|
|
|
enet_uint16 reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
if (channel -> reliableWindows [reliableWindow] > 0)
|
|
|
|
{
|
|
|
|
-- channel -> reliableWindows [reliableWindow];
|
|
|
|
if (! channel -> reliableWindows [reliableWindow])
|
2024-06-10 08:54:11 -07:00
|
|
|
channel -> usedReliableWindows &= ~ (1u << reliableWindow);
|
2007-10-12 10:37:31 -07:00
|
|
|
}
|
2007-10-11 22:18:46 -07:00
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2007-11-16 13:29:49 -07:00
|
|
|
commandNumber = (ENetProtocolCommand) (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK);
|
2007-10-11 19:27:45 -07:00
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
enet_list_remove (& outgoingCommand -> outgoingCommandList);
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet != NULL)
|
|
|
|
{
|
2011-01-31 01:27:39 -07:00
|
|
|
if (wasSent)
|
|
|
|
peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
-- outgoingCommand -> packet -> referenceCount;
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet -> referenceCount == 0)
|
2013-02-27 07:29:22 -07:00
|
|
|
{
|
|
|
|
outgoingCommand -> packet -> flags |= ENET_PACKET_FLAG_SENT;
|
|
|
|
|
|
|
|
enet_packet_destroy (outgoingCommand -> packet);
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-06-09 14:16:23 -07:00
|
|
|
enet_uint8 incomingSessionID, outgoingSessionID;
|
|
|
|
enet_uint32 mtu, windowSize;
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetChannel * channel;
|
2013-08-18 07:20:17 -07:00
|
|
|
size_t channelCount, duplicatePeers = 0;
|
|
|
|
ENetPeer * currentPeer, * peer = NULL;
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetProtocol verifyCommand;
|
|
|
|
|
|
|
|
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)
|
2013-08-18 07:20:17 -07:00
|
|
|
{
|
|
|
|
if (peer == NULL)
|
|
|
|
peer = currentPeer;
|
|
|
|
}
|
|
|
|
else
|
2013-11-17 11:19:17 -07:00
|
|
|
if (currentPeer -> state != ENET_PEER_STATE_CONNECTING &&
|
|
|
|
currentPeer -> address.host == host -> receivedAddress.host)
|
2013-08-18 07:20:17 -07:00
|
|
|
{
|
|
|
|
if (currentPeer -> address.port == host -> receivedAddress.port &&
|
|
|
|
currentPeer -> connectID == command -> connect.connectID)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
++ duplicatePeers;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2013-08-18 07:20:17 -07:00
|
|
|
if (peer == NULL || duplicatePeers >= host -> duplicatePeers)
|
2004-10-20 10:17:51 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-05-14 11:20:20 -07:00
|
|
|
if (channelCount > host -> channelLimit)
|
|
|
|
channelCount = host -> channelLimit;
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
|
|
|
|
if (peer -> channels == NULL)
|
2010-05-05 16:37:06 -07:00
|
|
|
return NULL;
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> channelCount = channelCount;
|
|
|
|
peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
|
|
|
|
peer -> connectID = command -> connect.connectID;
|
|
|
|
peer -> address = host -> receivedAddress;
|
2023-04-01 11:49:22 -07:00
|
|
|
peer -> mtu = host -> mtu;
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
|
|
|
|
peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
|
|
|
|
peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
|
|
|
|
peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
|
|
|
|
peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
|
|
|
|
peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
|
|
|
|
peer -> eventData = ENET_NET_TO_HOST_32 (command -> connect.data);
|
|
|
|
|
|
|
|
incomingSessionID = command -> connect.incomingSessionID == 0xFF ? peer -> outgoingSessionID : command -> connect.incomingSessionID;
|
2010-06-09 14:16:23 -07:00
|
|
|
incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
|
2013-08-18 07:20:17 -07:00
|
|
|
if (incomingSessionID == peer -> outgoingSessionID)
|
2010-06-09 14:16:23 -07:00
|
|
|
incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> outgoingSessionID = incomingSessionID;
|
2010-06-09 14:16:23 -07:00
|
|
|
|
2013-08-18 07:20:17 -07:00
|
|
|
outgoingSessionID = command -> connect.outgoingSessionID == 0xFF ? peer -> incomingSessionID : command -> connect.outgoingSessionID;
|
2010-06-09 14:16:23 -07:00
|
|
|
outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
|
2013-08-18 07:20:17 -07:00
|
|
|
if (outgoingSessionID == peer -> incomingSessionID)
|
2010-06-09 14:16:23 -07:00
|
|
|
outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> incomingSessionID = outgoingSessionID;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2013-08-18 07:20:17 -07:00
|
|
|
for (channel = peer -> channels;
|
|
|
|
channel < & peer -> channels [channelCount];
|
2004-10-20 10:17:51 -07:00
|
|
|
++ channel)
|
|
|
|
{
|
|
|
|
channel -> outgoingReliableSequenceNumber = 0;
|
|
|
|
channel -> outgoingUnreliableSequenceNumber = 0;
|
|
|
|
channel -> incomingReliableSequenceNumber = 0;
|
2011-05-31 00:48:27 -07:00
|
|
|
channel -> incomingUnreliableSequenceNumber = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
enet_list_clear (& channel -> incomingReliableCommands);
|
|
|
|
enet_list_clear (& channel -> incomingUnreliableCommands);
|
2007-10-11 19:27:45 -07:00
|
|
|
|
2007-10-11 22:18:46 -07:00
|
|
|
channel -> usedReliableWindows = 0;
|
2007-10-11 19:27:45 -07:00
|
|
|
memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows));
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
mtu = ENET_NET_TO_HOST_32 (command -> connect.mtu);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
|
|
|
|
mtu = ENET_PROTOCOL_MINIMUM_MTU;
|
|
|
|
else
|
|
|
|
if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
|
|
|
|
mtu = ENET_PROTOCOL_MAXIMUM_MTU;
|
|
|
|
|
2023-03-09 12:42:38 -07:00
|
|
|
if (mtu < peer -> mtu)
|
|
|
|
peer -> mtu = mtu;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (host -> outgoingBandwidth == 0 &&
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> incomingBandwidth == 0)
|
|
|
|
peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
|
2007-07-12 20:25:16 -07:00
|
|
|
else
|
|
|
|
if (host -> outgoingBandwidth == 0 ||
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> incomingBandwidth == 0)
|
|
|
|
peer -> windowSize = (ENET_MAX (host -> outgoingBandwidth, peer -> incomingBandwidth) /
|
2007-07-12 20:25:16 -07:00
|
|
|
ENET_PEER_WINDOW_SIZE_SCALE) *
|
|
|
|
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
|
2004-10-20 10:17:51 -07:00
|
|
|
else
|
2013-08-18 07:20:17 -07:00
|
|
|
peer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, peer -> incomingBandwidth) /
|
2004-10-20 10:17:51 -07:00
|
|
|
ENET_PEER_WINDOW_SIZE_SCALE) *
|
|
|
|
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
|
|
|
|
|
2013-08-18 07:20:17 -07:00
|
|
|
if (peer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
|
|
|
|
peer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
|
2004-10-20 10:17:51 -07:00
|
|
|
else
|
2013-08-18 07:20:17 -07:00
|
|
|
if (peer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
|
|
|
|
peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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;
|
2013-08-18 07:20:17 -07:00
|
|
|
verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (peer -> incomingPeerID);
|
2010-06-09 14:16:23 -07:00
|
|
|
verifyCommand.verifyConnect.incomingSessionID = incomingSessionID;
|
|
|
|
verifyCommand.verifyConnect.outgoingSessionID = outgoingSessionID;
|
2013-08-18 07:20:17 -07:00
|
|
|
verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_32 (peer -> mtu);
|
2004-10-20 10:17:51 -07:00
|
|
|
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);
|
2013-08-18 07:20:17 -07:00
|
|
|
verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (peer -> packetThrottleInterval);
|
|
|
|
verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (peer -> packetThrottleAcceleration);
|
|
|
|
verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (peer -> packetThrottleDeceleration);
|
|
|
|
verifyCommand.verifyConnect.connectID = peer -> connectID;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2013-08-18 07:20:17 -07:00
|
|
|
enet_peer_queue_outgoing_command (peer, & verifyCommand, NULL, 0, 0);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2013-08-18 07:20:17 -07:00
|
|
|
return peer;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2006-07-06 14:22:41 -07:00
|
|
|
size_t dataLength;
|
|
|
|
|
|
|
|
if (command -> header.channelID >= peer -> channelCount ||
|
2014-04-14 11:21:21 -07:00
|
|
|
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
|
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
|
|
|
dataLength = ENET_NET_TO_HOST_16 (command -> sendReliable.dataLength);
|
|
|
|
* currentData += dataLength;
|
2014-04-14 09:11:44 -07:00
|
|
|
if (dataLength > host -> maximumPacketSize ||
|
2012-01-06 08:11:04 -07:00
|
|
|
* currentData < host -> receivedData ||
|
|
|
|
* currentData > & host -> receivedData [host -> receivedDataLength])
|
2006-07-06 14:22:41 -07:00
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2014-04-14 11:21:21 -07:00
|
|
|
if (enet_peer_queue_incoming_command (peer, command, (const enet_uint8 *) command + sizeof (ENetProtocolSendReliable), dataLength, ENET_PACKET_FLAG_RELIABLE, 0) == NULL)
|
2006-10-03 08:39:29 -07:00
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
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
|
|
|
{
|
|
|
|
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 ||
|
2014-04-14 11:21:21 -07:00
|
|
|
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
|
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
|
|
|
dataLength = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.dataLength);
|
|
|
|
* currentData += dataLength;
|
2014-04-14 09:11:44 -07:00
|
|
|
if (dataLength > host -> maximumPacketSize ||
|
2012-01-06 08:11:04 -07:00
|
|
|
* currentData < host -> receivedData ||
|
|
|
|
* currentData > & host -> receivedData [host -> receivedDataLength])
|
2006-07-06 14:22:41 -07:00
|
|
|
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;
|
2007-10-12 16:10:28 -07:00
|
|
|
|
|
|
|
if (unsequencedGroup < peer -> incomingUnsequencedGroup)
|
|
|
|
unsequencedGroup += 0x10000;
|
|
|
|
|
|
|
|
if (unsequencedGroup >= (enet_uint32) peer -> incomingUnsequencedGroup + ENET_PEER_FREE_UNSEQUENCED_WINDOWS * ENET_PEER_UNSEQUENCED_WINDOW_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsequencedGroup &= 0xFFFF;
|
|
|
|
|
|
|
|
if (unsequencedGroup - index != peer -> incomingUnsequencedGroup)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
peer -> incomingUnsequencedGroup = unsequencedGroup - index;
|
|
|
|
|
|
|
|
memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
|
|
|
|
}
|
|
|
|
else
|
2024-06-10 08:54:11 -07:00
|
|
|
if (peer -> unsequencedWindow [index / 32] & (1u << (index % 32)))
|
2006-07-06 14:22:41 -07:00
|
|
|
return 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2014-04-14 11:21:21 -07:00
|
|
|
if (enet_peer_queue_incoming_command (peer, command, (const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced), dataLength, ENET_PACKET_FLAG_UNSEQUENCED, 0) == NULL)
|
2006-10-03 08:39:29 -07:00
|
|
|
return -1;
|
2010-05-05 16:37:06 -07:00
|
|
|
|
2024-06-10 08:54:11 -07:00
|
|
|
peer -> unsequencedWindow [index / 32] |= 1u << (index % 32);
|
2010-05-05 16:37:06 -07:00
|
|
|
|
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
|
|
|
{
|
2006-07-06 14:22:41 -07:00
|
|
|
size_t dataLength;
|
|
|
|
|
|
|
|
if (command -> header.channelID >= peer -> channelCount ||
|
2014-04-14 11:21:21 -07:00
|
|
|
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
|
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
|
|
|
dataLength = ENET_NET_TO_HOST_16 (command -> sendUnreliable.dataLength);
|
|
|
|
* currentData += dataLength;
|
2014-04-14 09:11:44 -07:00
|
|
|
if (dataLength > host -> maximumPacketSize ||
|
2012-01-06 08:11:04 -07:00
|
|
|
* currentData < host -> receivedData ||
|
|
|
|
* currentData > & host -> receivedData [host -> receivedDataLength])
|
2006-07-06 14:22:41 -07:00
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2014-04-14 11:21:21 -07:00
|
|
|
if (enet_peer_queue_incoming_command (peer, command, (const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable), dataLength, 0, 0) == NULL)
|
2006-10-03 08:39:29 -07:00
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
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;
|
2007-10-12 16:10:28 -07:00
|
|
|
enet_uint16 startWindow, currentWindow;
|
2004-10-20 10:17:51 -07:00
|
|
|
ENetListIterator currentCommand;
|
2007-10-12 16:10:28 -07:00
|
|
|
ENetIncomingCommand * startCommand = NULL;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
if (command -> header.channelID >= peer -> channelCount ||
|
2014-04-14 11:21:21 -07:00
|
|
|
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
|
2006-07-06 14:22:41 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
|
|
|
|
* currentData += fragmentLength;
|
2023-02-04 20:16:19 -07:00
|
|
|
if (fragmentLength <= 0 ||
|
|
|
|
fragmentLength > host -> maximumPacketSize ||
|
2012-01-06 08:11:04 -07:00
|
|
|
* currentData < host -> receivedData ||
|
|
|
|
* currentData > & host -> receivedData [host -> receivedDataLength])
|
2006-07-06 14:22:41 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
channel = & peer -> channels [command -> header.channelID];
|
|
|
|
startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
|
2007-10-12 16:10:28 -07:00
|
|
|
startWindow = startSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2007-10-12 16:10:28 -07:00
|
|
|
if (startSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
startWindow += ENET_PEER_RELIABLE_WINDOWS;
|
|
|
|
|
|
|
|
if (startWindow < currentWindow || startWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
|
2006-07-06 14:22:41 -07:00
|
|
|
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);
|
|
|
|
|
2012-01-06 08:11:04 -07:00
|
|
|
if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
|
|
|
|
fragmentNumber >= fragmentCount ||
|
2014-04-14 09:11:44 -07:00
|
|
|
totalLength > host -> maximumPacketSize ||
|
2023-02-04 20:16:19 -07:00
|
|
|
totalLength < fragmentCount ||
|
2012-01-06 08:11:04 -07:00
|
|
|
fragmentOffset >= totalLength ||
|
|
|
|
fragmentLength > totalLength - fragmentOffset)
|
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))
|
|
|
|
{
|
2007-10-12 16:10:28 -07:00
|
|
|
ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2007-10-12 16:10:28 -07:00
|
|
|
if (startSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
2004-10-20 10:17:51 -07:00
|
|
|
break;
|
2007-10-12 16:10:28 -07:00
|
|
|
|
|
|
|
if (incomingCommand -> reliableSequenceNumber <= startSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < startSequenceNumber)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_FRAGMENT ||
|
|
|
|
totalLength != incomingCommand -> packet -> dataLength ||
|
|
|
|
fragmentCount != incomingCommand -> fragmentCount)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
startCommand = incomingCommand;
|
|
|
|
break;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2007-10-12 16:10:28 -07:00
|
|
|
if (startCommand == NULL)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
ENetProtocol hostCommand = * command;
|
2006-10-03 08:39:29 -07:00
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
hostCommand.header.reliableSequenceNumber = startSequenceNumber;
|
|
|
|
|
2014-04-14 11:21:21 -07:00
|
|
|
startCommand = enet_peer_queue_incoming_command (peer, & hostCommand, NULL, totalLength, ENET_PACKET_FLAG_RELIABLE, fragmentCount);
|
2007-10-12 16:10:28 -07:00
|
|
|
if (startCommand == NULL)
|
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2024-06-10 08:54:11 -07:00
|
|
|
if ((startCommand -> fragments [fragmentNumber / 32] & (1u << (fragmentNumber % 32))) == 0)
|
2004-12-09 11:41:02 -07:00
|
|
|
{
|
|
|
|
-- startCommand -> fragmentsRemaining;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2024-06-10 08:54:11 -07:00
|
|
|
startCommand -> fragments [fragmentNumber / 32] |= (1u << (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);
|
2010-06-05 13:31:31 -07:00
|
|
|
|
|
|
|
if (startCommand -> fragmentsRemaining <= 0)
|
2020-08-23 13:45:15 -07:00
|
|
|
enet_peer_dispatch_incoming_reliable_commands (peer, channel, NULL);
|
2004-12-09 11:41:02 -07:00
|
|
|
}
|
2006-07-06 14:22:41 -07:00
|
|
|
|
|
|
|
return 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
static int
|
|
|
|
enet_protocol_handle_send_unreliable_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
|
|
|
|
{
|
|
|
|
enet_uint32 fragmentNumber,
|
|
|
|
fragmentCount,
|
|
|
|
fragmentOffset,
|
|
|
|
fragmentLength,
|
|
|
|
reliableSequenceNumber,
|
|
|
|
startSequenceNumber,
|
|
|
|
totalLength;
|
|
|
|
enet_uint16 reliableWindow, currentWindow;
|
|
|
|
ENetChannel * channel;
|
|
|
|
ENetListIterator currentCommand;
|
|
|
|
ENetIncomingCommand * startCommand = NULL;
|
|
|
|
|
|
|
|
if (command -> header.channelID >= peer -> channelCount ||
|
2014-04-14 11:21:21 -07:00
|
|
|
(peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
|
2011-05-31 00:48:27 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
|
|
|
|
* currentData += fragmentLength;
|
2014-04-14 09:11:44 -07:00
|
|
|
if (fragmentLength > host -> maximumPacketSize ||
|
2012-01-06 08:11:04 -07:00
|
|
|
* currentData < host -> receivedData ||
|
|
|
|
* currentData > & host -> receivedData [host -> receivedDataLength])
|
2011-05-31 00:48:27 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
channel = & peer -> channels [command -> header.channelID];
|
|
|
|
reliableSequenceNumber = command -> header.reliableSequenceNumber;
|
|
|
|
startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
|
|
|
|
|
|
|
|
reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
|
|
|
|
if (reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
|
|
|
|
|
|
|
|
if (reliableWindow < currentWindow || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber &&
|
|
|
|
startSequenceNumber <= channel -> incomingUnreliableSequenceNumber)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-01-06 08:11:04 -07:00
|
|
|
if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
|
|
|
|
fragmentNumber >= fragmentCount ||
|
2014-04-14 09:11:44 -07:00
|
|
|
totalLength > host -> maximumPacketSize ||
|
2012-01-06 08:11:04 -07:00
|
|
|
fragmentOffset >= totalLength ||
|
|
|
|
fragmentLength > totalLength - fragmentOffset)
|
2011-05-31 00:48:27 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
|
|
|
|
currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
|
|
|
|
currentCommand = enet_list_previous (currentCommand))
|
|
|
|
{
|
|
|
|
ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
|
|
|
|
|
|
|
|
if (reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (incomingCommand -> reliableSequenceNumber > reliableSequenceNumber)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (incomingCommand -> unreliableSequenceNumber <= startSequenceNumber)
|
|
|
|
{
|
|
|
|
if (incomingCommand -> unreliableSequenceNumber < startSequenceNumber)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT ||
|
|
|
|
totalLength != incomingCommand -> packet -> dataLength ||
|
|
|
|
fragmentCount != incomingCommand -> fragmentCount)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
startCommand = incomingCommand;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startCommand == NULL)
|
|
|
|
{
|
2014-04-14 11:21:21 -07:00
|
|
|
startCommand = enet_peer_queue_incoming_command (peer, command, NULL, totalLength, ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT, fragmentCount);
|
2011-05-31 00:48:27 -07:00
|
|
|
if (startCommand == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-06-10 08:54:11 -07:00
|
|
|
if ((startCommand -> fragments [fragmentNumber / 32] & (1u << (fragmentNumber % 32))) == 0)
|
2011-05-31 00:48:27 -07:00
|
|
|
{
|
|
|
|
-- startCommand -> fragmentsRemaining;
|
|
|
|
|
2024-06-10 08:54:11 -07:00
|
|
|
startCommand -> fragments [fragmentNumber / 32] |= (1u << (fragmentNumber % 32));
|
2011-05-31 00:48:27 -07:00
|
|
|
|
|
|
|
if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
|
|
|
|
fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
|
|
|
|
|
|
|
|
memcpy (startCommand -> packet -> data + fragmentOffset,
|
|
|
|
(enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
|
|
|
|
fragmentLength);
|
|
|
|
|
|
|
|
if (startCommand -> fragmentsRemaining <= 0)
|
2020-08-23 13:45:15 -07:00
|
|
|
enet_peer_dispatch_incoming_unreliable_commands (peer, channel, NULL);
|
2011-05-31 00:48:27 -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_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
|
|
|
|
{
|
2013-01-07 11:46:31 -07:00
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
return -1;
|
|
|
|
|
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)
|
|
|
|
{
|
2013-01-07 11:46:31 -07:00
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
return -1;
|
|
|
|
|
2013-05-11 11:53:55 -07:00
|
|
|
if (peer -> incomingBandwidth != 0)
|
|
|
|
-- host -> bandwidthLimitedPeers;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
|
|
|
|
peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
|
|
|
|
|
2013-05-11 11:53:55 -07:00
|
|
|
if (peer -> incomingBandwidth != 0)
|
|
|
|
++ host -> bandwidthLimitedPeers;
|
|
|
|
|
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;
|
2015-03-09 11:54:01 -07:00
|
|
|
else
|
|
|
|
if (peer -> incomingBandwidth == 0 || host -> outgoingBandwidth == 0)
|
|
|
|
peer -> windowSize = (ENET_MAX (peer -> incomingBandwidth, host -> outgoingBandwidth) /
|
|
|
|
ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
|
2004-10-20 10:17:51 -07:00
|
|
|
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)
|
|
|
|
{
|
2013-01-07 11:46:31 -07:00
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
|
|
|
|
return -1;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
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)
|
|
|
|
{
|
2013-01-07 11:46:31 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ZOMBIE || peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT)
|
2008-07-30 02:31:29 -07:00
|
|
|
return 0;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
enet_peer_reset_queues (peer);
|
|
|
|
|
2013-11-27 07:33:09 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_CONNECTION_SUCCEEDED || peer -> state == ENET_PEER_STATE_DISCONNECTING || peer -> state == ENET_PEER_STATE_CONNECTING)
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
|
2006-07-06 14:22:41 -07:00
|
|
|
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)
|
2013-05-11 11:53:55 -07:00
|
|
|
enet_protocol_change_state (host, peer, ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT);
|
2004-10-20 10:17:51 -07:00
|
|
|
else
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
|
2006-03-14 16:21:50 -07:00
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
if (peer -> state != ENET_PEER_STATE_DISCONNECTED)
|
|
|
|
peer -> eventData = ENET_NET_TO_HOST_32 (command -> disconnect.data);
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
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;
|
|
|
|
|
2013-01-07 11:46:31 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ZOMBIE)
|
|
|
|
return 0;
|
|
|
|
|
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
|
|
|
roundTripTime = ENET_TIME_DIFFERENCE (host -> serviceTime, receivedSentTime);
|
2020-04-03 00:48:38 -07:00
|
|
|
roundTripTime = ENET_MAX (roundTripTime, 1);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-03-03 13:34:06 -07:00
|
|
|
if (peer -> lastReceiveTime > 0)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2020-04-13 16:32:13 -07:00
|
|
|
enet_peer_throttle (peer, roundTripTime);
|
|
|
|
|
2020-09-08 10:39:54 -07:00
|
|
|
peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
|
2020-09-03 14:22:05 -07:00
|
|
|
|
2020-09-08 10:39:54 -07:00
|
|
|
if (roundTripTime >= peer -> roundTripTime)
|
|
|
|
{
|
|
|
|
enet_uint32 diff = roundTripTime - peer -> roundTripTime;
|
|
|
|
peer -> roundTripTimeVariance += diff / 4;
|
|
|
|
peer -> roundTripTime += diff / 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enet_uint32 diff = peer -> roundTripTime - roundTripTime;
|
|
|
|
peer -> roundTripTimeVariance += diff / 4;
|
|
|
|
peer -> roundTripTime -= diff / 8;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-03 13:34:06 -07:00
|
|
|
peer -> roundTripTime = roundTripTime;
|
2020-09-08 10:39:54 -07:00
|
|
|
peer -> roundTripTimeVariance = (roundTripTime + 1) / 2;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:39:54 -07:00
|
|
|
if (peer -> roundTripTime < peer -> lowestRoundTripTime)
|
|
|
|
peer -> lowestRoundTripTime = peer -> roundTripTime;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-09-08 10:39:54 -07:00
|
|
|
if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
|
|
|
|
peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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;
|
2020-09-08 10:39:54 -07:00
|
|
|
peer -> lastRoundTripTimeVariance = ENET_MAX (peer -> highestRoundTripTimeVariance, 1);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-03 13:34:06 -07:00
|
|
|
peer -> lastReceiveTime = ENET_MAX (host -> serviceTime, 1);
|
|
|
|
peer -> earliestTimeout = 0;
|
|
|
|
|
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:
|
2023-02-04 20:16:19 -07:00
|
|
|
if (! enet_peer_has_outgoing_commands (peer))
|
2010-06-09 14:16:23 -07:00
|
|
|
enet_peer_disconnect (peer, peer -> eventData);
|
2006-07-06 14:22:41 -07:00
|
|
|
break;
|
2012-07-31 06:12:11 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
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)
|
|
|
|
{
|
2010-06-09 14:16:23 -07:00
|
|
|
enet_uint32 mtu, windowSize;
|
2010-05-14 11:20:20 -07:00
|
|
|
size_t channelCount;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
if (peer -> state != ENET_PEER_STATE_CONNECTING)
|
2008-01-09 07:03:32 -07:00
|
|
|
return 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2010-05-14 11:20:20 -07:00
|
|
|
channelCount = ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount);
|
|
|
|
|
|
|
|
if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT ||
|
2004-10-20 10:17:51 -07:00
|
|
|
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
|
|
|
|
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
|
2010-06-09 14:16:23 -07:00
|
|
|
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration ||
|
|
|
|
command -> verifyConnect.connectID != peer -> connectID)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> eventData = 0;
|
|
|
|
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
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);
|
|
|
|
|
2010-05-14 11:20:20 -07:00
|
|
|
if (channelCount < peer -> channelCount)
|
|
|
|
peer -> channelCount = channelCount;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> incomingSessionID = command -> verifyConnect.incomingSessionID;
|
|
|
|
peer -> outgoingSessionID = command -> verifyConnect.outgoingSessionID;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
mtu = ENET_NET_TO_HOST_32 (command -> verifyConnect.mtu);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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;
|
2010-06-09 14:16:23 -07:00
|
|
|
enet_uint8 sessionID;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
if (host -> receivedDataLength < (size_t) & ((ENetProtocolHeader *) 0) -> sentTime)
|
2004-10-20 10:17:51 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
header = (ENetProtocolHeader *) host -> receivedData;
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
peerID = ENET_NET_TO_HOST_16 (header -> peerID);
|
2010-06-09 14:16:23 -07:00
|
|
|
sessionID = (peerID & ENET_PROTOCOL_HEADER_SESSION_MASK) >> ENET_PROTOCOL_HEADER_SESSION_SHIFT;
|
2006-07-06 14:22:41 -07:00
|
|
|
flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
|
2010-06-09 14:16:23 -07:00
|
|
|
peerID &= ~ (ENET_PROTOCOL_HEADER_FLAG_MASK | ENET_PROTOCOL_HEADER_SESSION_MASK);
|
|
|
|
|
|
|
|
headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : (size_t) & ((ENetProtocolHeader *) 0) -> sentTime);
|
|
|
|
if (host -> checksum != NULL)
|
|
|
|
headerSize += sizeof (enet_uint32);
|
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 ||
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> state == ENET_PEER_STATE_ZOMBIE ||
|
2011-05-30 12:00:26 -07:00
|
|
|
((host -> receivedAddress.host != peer -> address.host ||
|
|
|
|
host -> receivedAddress.port != peer -> address.port) &&
|
2010-06-09 14:16:23 -07:00
|
|
|
peer -> address.host != ENET_HOST_BROADCAST) ||
|
|
|
|
(peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
|
|
|
|
sessionID != peer -> incomingSessionID))
|
2004-10-20 10:17:51 -07:00
|
|
|
return 0;
|
2010-06-09 14:16:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ENET_PROTOCOL_HEADER_FLAG_COMPRESSED)
|
|
|
|
{
|
|
|
|
size_t originalSize;
|
|
|
|
if (host -> compressor.context == NULL || host -> compressor.decompress == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
originalSize = host -> compressor.decompress (host -> compressor.context,
|
|
|
|
host -> receivedData + headerSize,
|
|
|
|
host -> receivedDataLength - headerSize,
|
|
|
|
host -> packetData [1] + headerSize,
|
|
|
|
sizeof (host -> packetData [1]) - headerSize);
|
|
|
|
if (originalSize <= 0 || originalSize > sizeof (host -> packetData [1]) - headerSize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy (host -> packetData [1], header, headerSize);
|
|
|
|
host -> receivedData = host -> packetData [1];
|
|
|
|
host -> receivedDataLength = headerSize + originalSize;
|
|
|
|
}
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
if (host -> checksum != NULL)
|
|
|
|
{
|
2024-03-05 08:46:29 -07:00
|
|
|
enet_uint32 * checksum = (enet_uint32 *) & host -> receivedData [headerSize - sizeof (enet_uint32)];
|
|
|
|
enet_uint32 desiredChecksum, newChecksum;
|
2010-06-09 14:16:23 -07:00
|
|
|
ENetBuffer buffer;
|
2024-03-05 08:46:29 -07:00
|
|
|
/* Checksum may be an unaligned pointer, use memcpy to avoid undefined behaviour. */
|
|
|
|
memcpy (& desiredChecksum, checksum, sizeof (enet_uint32));
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2024-03-05 08:46:29 -07:00
|
|
|
newChecksum = peer != NULL ? peer -> connectID : 0;
|
|
|
|
memcpy (checksum, & newChecksum, sizeof (enet_uint32));
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
buffer.data = host -> receivedData;
|
|
|
|
buffer.dataLength = host -> receivedDataLength;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
if (host -> checksum (& buffer, 1) != desiredChecksum)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer != NULL)
|
|
|
|
{
|
2006-07-06 14:22:41 -07:00
|
|
|
peer -> address.host = host -> receivedAddress.host;
|
|
|
|
peer -> address.port = host -> receivedAddress.port;
|
|
|
|
peer -> incomingDataTotal += host -> receivedDataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-01-07 11:46:31 -07:00
|
|
|
switch (commandNumber)
|
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:
|
2013-01-07 11:46:31 -07:00
|
|
|
if (peer != NULL)
|
|
|
|
goto commandError;
|
2004-10-20 10:17:51 -07:00
|
|
|
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;
|
|
|
|
|
2011-05-31 00:48:27 -07:00
|
|
|
case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
|
|
|
|
if (enet_protocol_handle_send_unreliable_fragment (host, peer, command, & currentData))
|
|
|
|
goto commandError;
|
|
|
|
break;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
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:
|
2013-01-07 11:46:31 -07:00
|
|
|
case ENET_PEER_STATE_DISCONNECTED:
|
|
|
|
case ENET_PEER_STATE_ZOMBIE:
|
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)
|
|
|
|
{
|
2014-09-29 05:49:34 -07:00
|
|
|
int packets;
|
|
|
|
|
|
|
|
for (packets = 0; packets < 256; ++ packets)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
int receivedLength;
|
|
|
|
ENetBuffer buffer;
|
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
buffer.data = host -> packetData [0];
|
|
|
|
buffer.dataLength = sizeof (host -> packetData [0]);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
receivedLength = enet_socket_receive (host -> socket,
|
|
|
|
& host -> receivedAddress,
|
|
|
|
& buffer,
|
|
|
|
1);
|
|
|
|
|
2023-07-23 18:05:47 -07:00
|
|
|
if (receivedLength == -2)
|
|
|
|
continue;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
if (receivedLength < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (receivedLength == 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
host -> receivedData = host -> packetData [0];
|
2004-10-20 10:17:51 -07:00
|
|
|
host -> receivedDataLength = receivedLength;
|
2010-05-14 16:41:14 -07:00
|
|
|
|
|
|
|
host -> totalReceivedData += receivedLength;
|
|
|
|
host -> totalReceivedPackets ++;
|
2012-10-02 03:33:07 -07:00
|
|
|
|
|
|
|
if (host -> intercept != NULL)
|
|
|
|
{
|
|
|
|
switch (host -> intercept (host, event))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
switch (enet_protocol_handle_incoming_commands (host, event))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:58:08 -07:00
|
|
|
return 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-03-23 08:22:32 -07:00
|
|
|
enet_uint16 reliableSequenceNumber;
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
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
|
|
|
{
|
2023-02-04 20:48:18 -07:00
|
|
|
peer -> flags |= ENET_PEER_FLAG_CONTINUE_SENDING;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
|
|
|
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;
|
2013-03-23 08:22:32 -07:00
|
|
|
|
|
|
|
reliableSequenceNumber = ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
|
|
|
|
command -> header.channelID = acknowledgement -> command.header.channelID;
|
2013-03-23 08:22:32 -07:00
|
|
|
command -> header.reliableSequenceNumber = reliableSequenceNumber;
|
|
|
|
command -> acknowledge.receivedReliableSequenceNumber = reliableSequenceNumber;
|
2006-07-06 14:22:41 -07:00
|
|
|
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)
|
2010-05-13 09:55:59 -07:00
|
|
|
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
enet_list_remove (& acknowledgement -> acknowledgementList);
|
|
|
|
enet_free (acknowledgement);
|
|
|
|
|
|
|
|
++ command;
|
|
|
|
++ buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
host -> commandCount = command - host -> commands;
|
|
|
|
host -> bufferCount = buffer - host -> buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
|
|
|
|
{
|
|
|
|
ENetOutgoingCommand * outgoingCommand;
|
2023-02-04 20:16:19 -07:00
|
|
|
ENetListIterator currentCommand, insertPosition, insertSendReliablePosition;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
currentCommand = enet_list_begin (& peer -> sentReliableCommands);
|
2020-07-23 01:42:59 -07:00
|
|
|
insertPosition = enet_list_begin (& peer -> outgoingCommands);
|
2023-02-04 20:16:19 -07:00
|
|
|
insertSendReliablePosition = enet_list_begin (& peer -> outgoingSendReliableCommands);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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 &&
|
2012-05-29 19:05:34 -07:00
|
|
|
(ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMaximum ||
|
2024-06-10 08:54:11 -07:00
|
|
|
((1u << (outgoingCommand -> sendAttempts - 1)) >= peer -> timeoutLimit &&
|
2012-05-29 19:05:34 -07:00
|
|
|
ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMinimum)))
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
++ peer -> packetsLost;
|
|
|
|
|
|
|
|
outgoingCommand -> roundTripTimeout *= 2;
|
|
|
|
|
2023-02-04 20:16:19 -07:00
|
|
|
if (outgoingCommand -> packet != NULL)
|
|
|
|
{
|
|
|
|
peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
|
|
|
|
|
|
|
|
enet_list_insert (insertSendReliablePosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
enet_list_insert (insertPosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-31 01:27:39 -07:00
|
|
|
static int
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_protocol_check_outgoing_commands (ENetHost * host, ENetPeer * peer, ENetList * sentUnreliableCommands)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
ENetProtocol * command = & host -> commands [host -> commandCount];
|
|
|
|
ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
|
|
|
|
ENetOutgoingCommand * outgoingCommand;
|
2023-02-04 20:16:19 -07:00
|
|
|
ENetListIterator currentCommand, currentSendReliableCommand;
|
2021-04-25 20:44:51 -07:00
|
|
|
ENetChannel *channel = NULL;
|
|
|
|
enet_uint16 reliableWindow = 0;
|
2007-10-11 19:27:45 -07:00
|
|
|
size_t commandSize;
|
2023-02-04 20:16:19 -07:00
|
|
|
int windowWrap = 0, canPing = 1;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
currentCommand = enet_list_begin (& peer -> outgoingCommands);
|
2023-02-04 20:16:19 -07:00
|
|
|
currentSendReliableCommand = enet_list_begin (& peer -> outgoingSendReliableCommands);
|
|
|
|
|
|
|
|
for (;;)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
2023-02-04 20:16:19 -07:00
|
|
|
if (currentCommand != enet_list_end (& peer -> outgoingCommands))
|
|
|
|
{
|
|
|
|
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
|
|
|
|
|
|
|
|
if (currentSendReliableCommand != enet_list_end (& peer -> outgoingSendReliableCommands) &&
|
|
|
|
ENET_TIME_LESS (((ENetOutgoingCommand *) currentSendReliableCommand) -> queueTime, outgoingCommand -> queueTime))
|
|
|
|
goto useSendReliableCommand;
|
|
|
|
|
|
|
|
currentCommand = enet_list_next (currentCommand);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (currentSendReliableCommand != enet_list_end (& peer -> outgoingSendReliableCommands))
|
|
|
|
{
|
|
|
|
useSendReliableCommand:
|
|
|
|
outgoingCommand = (ENetOutgoingCommand *) currentSendReliableCommand;
|
|
|
|
currentSendReliableCommand = enet_list_next (currentSendReliableCommand);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2007-10-11 19:27:45 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
|
2011-01-31 01:27:39 -07:00
|
|
|
{
|
2020-07-23 01:42:59 -07:00
|
|
|
channel = outgoingCommand -> command.header.channelID < peer -> channelCount ? & peer -> channels [outgoingCommand -> command.header.channelID] : NULL;
|
|
|
|
reliableWindow = outgoingCommand -> reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
|
|
|
|
if (channel != NULL)
|
2011-01-31 01:27:39 -07:00
|
|
|
{
|
2023-02-04 20:16:19 -07:00
|
|
|
if (windowWrap)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
if (outgoingCommand -> sendAttempts < 1 &&
|
2020-07-23 01:42:59 -07:00
|
|
|
! (outgoingCommand -> reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE) &&
|
|
|
|
(channel -> reliableWindows [(reliableWindow + ENET_PEER_RELIABLE_WINDOWS - 1) % ENET_PEER_RELIABLE_WINDOWS] >= ENET_PEER_RELIABLE_WINDOW_SIZE ||
|
2024-06-10 08:54:11 -07:00
|
|
|
channel -> usedReliableWindows & ((((1u << (ENET_PEER_FREE_RELIABLE_WINDOWS + 2)) - 1) << reliableWindow) |
|
|
|
|
(((1u << (ENET_PEER_FREE_RELIABLE_WINDOWS + 2)) - 1) >> (ENET_PEER_RELIABLE_WINDOWS - reliableWindow)))))
|
2020-07-23 01:42:59 -07:00
|
|
|
{
|
2023-02-04 20:16:19 -07:00
|
|
|
windowWrap = 1;
|
|
|
|
currentSendReliableCommand = enet_list_end (& peer -> outgoingSendReliableCommands);
|
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
continue;
|
|
|
|
}
|
2011-01-31 01:27:39 -07:00
|
|
|
}
|
2023-02-04 20:16:19 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
if (outgoingCommand -> packet != NULL)
|
2011-01-31 01:27:39 -07:00
|
|
|
{
|
2023-02-04 20:16:19 -07:00
|
|
|
enet_uint32 windowSize = (peer -> packetThrottle * peer -> windowSize) / ENET_PEER_PACKET_THROTTLE_SCALE;
|
|
|
|
|
|
|
|
if (peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > ENET_MAX (windowSize, peer -> mtu))
|
2020-07-23 01:42:59 -07:00
|
|
|
{
|
2023-02-04 20:16:19 -07:00
|
|
|
currentSendReliableCommand = enet_list_end (& peer -> outgoingSendReliableCommands);
|
2011-01-30 23:17:58 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
continue;
|
|
|
|
}
|
2011-01-31 01:27:39 -07:00
|
|
|
}
|
2011-01-30 23:17:58 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
canPing = 0;
|
|
|
|
}
|
2011-01-31 01:27:39 -07:00
|
|
|
|
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)] ||
|
2011-01-31 01:27:39 -07:00
|
|
|
peer -> mtu - host -> packetSize < commandSize ||
|
|
|
|
(outgoingCommand -> packet != NULL &&
|
|
|
|
(enet_uint16) (peer -> mtu - host -> packetSize) < (enet_uint16) (commandSize + outgoingCommand -> fragmentLength)))
|
2006-07-06 14:22:41 -07:00
|
|
|
{
|
2023-02-04 20:48:18 -07:00
|
|
|
peer -> flags |= ENET_PEER_FLAG_CONTINUE_SENDING;
|
|
|
|
|
2006-07-06 14:22:41 -07:00
|
|
|
break;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
|
2007-10-11 19:27:45 -07:00
|
|
|
{
|
2020-07-23 01:42:59 -07:00
|
|
|
if (channel != NULL && outgoingCommand -> sendAttempts < 1)
|
|
|
|
{
|
2024-06-10 08:54:11 -07:00
|
|
|
channel -> usedReliableWindows |= 1u << reliableWindow;
|
2020-07-23 01:42:59 -07:00
|
|
|
++ channel -> reliableWindows [reliableWindow];
|
|
|
|
}
|
2007-10-11 19:27:45 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
++ outgoingCommand -> sendAttempts;
|
2007-10-11 19:27:45 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
if (outgoingCommand -> roundTripTimeout == 0)
|
2023-02-04 22:55:34 -07:00
|
|
|
outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
|
2020-07-23 01:42:59 -07:00
|
|
|
|
|
|
|
if (enet_list_empty (& peer -> sentReliableCommands))
|
|
|
|
peer -> nextTimeout = host -> serviceTime + outgoingCommand -> roundTripTimeout;
|
|
|
|
|
|
|
|
enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
|
|
|
|
enet_list_remove (& outgoingCommand -> outgoingCommandList));
|
|
|
|
|
|
|
|
outgoingCommand -> sentTime = host -> serviceTime;
|
|
|
|
|
|
|
|
host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
|
|
|
|
|
|
|
|
peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
2020-07-23 01:42:59 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (outgoingCommand -> packet != NULL && outgoingCommand -> fragmentOffset == 0)
|
|
|
|
{
|
|
|
|
peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
|
|
|
|
peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
|
|
|
|
|
|
|
|
if (peer -> packetThrottleCounter > peer -> packetThrottle)
|
|
|
|
{
|
|
|
|
enet_uint16 reliableSequenceNumber = outgoingCommand -> reliableSequenceNumber,
|
|
|
|
unreliableSequenceNumber = outgoingCommand -> unreliableSequenceNumber;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
-- outgoingCommand -> packet -> referenceCount;
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet -> referenceCount == 0)
|
|
|
|
enet_packet_destroy (outgoingCommand -> packet);
|
|
|
|
|
|
|
|
enet_list_remove (& outgoingCommand -> outgoingCommandList);
|
|
|
|
enet_free (outgoingCommand);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
if (currentCommand == enet_list_end (& peer -> outgoingCommands))
|
|
|
|
break;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
outgoingCommand = (ENetOutgoingCommand *) currentCommand;
|
|
|
|
if (outgoingCommand -> reliableSequenceNumber != reliableSequenceNumber ||
|
|
|
|
outgoingCommand -> unreliableSequenceNumber != unreliableSequenceNumber)
|
|
|
|
break;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
currentCommand = enet_list_next (currentCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_list_remove (& outgoingCommand -> outgoingCommandList);
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet != NULL)
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_list_insert (enet_list_end (sentUnreliableCommands), outgoingCommand);
|
2020-07-23 01:42:59 -07:00
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
* command = outgoingCommand -> command;
|
|
|
|
|
|
|
|
if (outgoingCommand -> packet != NULL)
|
|
|
|
{
|
|
|
|
++ buffer;
|
|
|
|
|
|
|
|
buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
|
|
|
|
buffer -> dataLength = outgoingCommand -> fragmentLength;
|
|
|
|
|
|
|
|
host -> packetSize += outgoingCommand -> fragmentLength;
|
|
|
|
}
|
2020-07-23 01:42:59 -07:00
|
|
|
else
|
|
|
|
if (! (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE))
|
|
|
|
enet_free (outgoingCommand);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
++ peer -> packetsSent;
|
|
|
|
|
|
|
|
++ command;
|
|
|
|
++ buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
host -> commandCount = command - host -> commands;
|
|
|
|
host -> bufferCount = buffer - host -> buffers;
|
2011-01-31 01:27:39 -07:00
|
|
|
|
2020-07-23 01:42:59 -07:00
|
|
|
if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
|
2023-02-04 20:16:19 -07:00
|
|
|
! enet_peer_has_outgoing_commands (peer) &&
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_list_empty (sentUnreliableCommands))
|
2020-07-23 01:42:59 -07:00
|
|
|
enet_peer_disconnect (peer, peer -> eventData);
|
|
|
|
|
2011-01-31 01:27:39 -07:00
|
|
|
return canPing;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
|
|
|
|
{
|
2010-06-09 14:16:23 -07:00
|
|
|
enet_uint8 headerData [sizeof (ENetProtocolHeader) + sizeof (enet_uint32)];
|
|
|
|
ENetProtocolHeader * header = (ENetProtocolHeader *) headerData;
|
2023-02-04 21:20:39 -07:00
|
|
|
int sentLength = 0;
|
2010-06-09 14:16:23 -07:00
|
|
|
size_t shouldCompress = 0;
|
2023-02-04 21:20:39 -07:00
|
|
|
ENetList sentUnreliableCommands;
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_list_clear (& sentUnreliableCommands);
|
|
|
|
|
|
|
|
for (int sendPass = 0, continueSending = 0; sendPass <= continueSending; ++ sendPass)
|
2023-02-04 20:48:18 -07:00
|
|
|
for (ENetPeer * currentPeer = host -> peers;
|
2004-10-20 10:17:51 -07:00
|
|
|
currentPeer < & host -> peers [host -> peerCount];
|
|
|
|
++ currentPeer)
|
|
|
|
{
|
|
|
|
if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
|
2023-02-04 20:48:18 -07:00
|
|
|
currentPeer -> state == ENET_PEER_STATE_ZOMBIE ||
|
|
|
|
(sendPass > 0 && ! (currentPeer -> flags & ENET_PEER_FLAG_CONTINUE_SENDING)))
|
2004-10-20 10:17:51 -07:00
|
|
|
continue;
|
|
|
|
|
2023-02-04 20:48:18 -07:00
|
|
|
currentPeer -> flags &= ~ ENET_PEER_FLAG_CONTINUE_SENDING;
|
|
|
|
|
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)
|
2012-01-04 21:12:46 -07:00
|
|
|
{
|
|
|
|
if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
|
|
|
|
return 1;
|
|
|
|
else
|
2023-02-04 20:48:18 -07:00
|
|
|
goto nextPeer;
|
2012-01-04 21:12:46 -07:00
|
|
|
}
|
2006-07-06 14:22:41 -07:00
|
|
|
|
2023-02-04 20:16:19 -07:00
|
|
|
if (((enet_list_empty (& currentPeer -> outgoingCommands) &&
|
|
|
|
enet_list_empty (& currentPeer -> outgoingSendReliableCommands)) ||
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_protocol_check_outgoing_commands (host, currentPeer, & sentUnreliableCommands)) &&
|
2011-01-31 01:27:39 -07:00
|
|
|
enet_list_empty (& currentPeer -> sentReliableCommands) &&
|
2012-05-29 19:05:34 -07:00
|
|
|
ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime) >= currentPeer -> pingInterval &&
|
2004-10-20 10:17:51 -07:00
|
|
|
currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
|
|
|
|
{
|
|
|
|
enet_peer_ping (currentPeer);
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_protocol_check_outgoing_commands (host, currentPeer, & sentUnreliableCommands);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (host -> commandCount == 0)
|
2023-02-04 20:48:18 -07:00
|
|
|
goto nextPeer;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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
|
2023-02-04 20:16:19 -07:00
|
|
|
printf ("peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %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 -> outgoingCommands) + enet_list_size (& currentPeer -> outgoingSendReliableCommands), 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
|
|
|
|
|
2020-04-15 21:15:18 -07:00
|
|
|
currentPeer -> packetLossVariance = (currentPeer -> packetLossVariance * 3 + ENET_DIFFERENCE (packetLoss, currentPeer -> packetLoss)) / 4;
|
|
|
|
currentPeer -> packetLoss = (currentPeer -> packetLoss * 7 + packetLoss) / 8;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-09 14:16:23 -07:00
|
|
|
host -> buffers -> data = headerData;
|
2006-07-06 14:22:41 -07:00
|
|
|
if (host -> headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
|
|
|
|
{
|
2010-06-09 14:16:23 -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;
|
2010-06-09 14:16:23 -07:00
|
|
|
|
|
|
|
shouldCompress = 0;
|
|
|
|
if (host -> compressor.context != NULL && host -> compressor.compress != NULL)
|
|
|
|
{
|
|
|
|
size_t originalSize = host -> packetSize - sizeof(ENetProtocolHeader),
|
|
|
|
compressedSize = host -> compressor.compress (host -> compressor.context,
|
|
|
|
& host -> buffers [1], host -> bufferCount - 1,
|
|
|
|
originalSize,
|
|
|
|
host -> packetData [1],
|
|
|
|
originalSize);
|
|
|
|
if (compressedSize > 0 && compressedSize < originalSize)
|
|
|
|
{
|
|
|
|
host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_COMPRESSED;
|
|
|
|
shouldCompress = compressedSize;
|
|
|
|
#ifdef ENET_DEBUG_COMPRESS
|
2013-08-08 23:41:57 -07:00
|
|
|
printf ("peer %u: compressed %u -> %u (%u%%)\n", currentPeer -> incomingPeerID, originalSize, compressedSize, (compressedSize * 100) / originalSize);
|
2010-06-09 14:16:23 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID)
|
|
|
|
host -> headerFlags |= currentPeer -> outgoingSessionID << ENET_PROTOCOL_HEADER_SESSION_SHIFT;
|
|
|
|
header -> peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID | host -> headerFlags);
|
2010-05-20 09:55:27 -07:00
|
|
|
if (host -> checksum != NULL)
|
2010-06-09 14:16:23 -07:00
|
|
|
{
|
|
|
|
enet_uint32 * checksum = (enet_uint32 *) & headerData [host -> buffers -> dataLength];
|
2024-03-05 08:46:29 -07:00
|
|
|
enet_uint32 newChecksum = currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer -> connectID : 0;
|
|
|
|
/* Checksum may be unaligned, use memcpy to avoid undefined behaviour. */
|
|
|
|
memcpy(checksum, & newChecksum, sizeof (enet_uint32));
|
2010-06-09 14:16:23 -07:00
|
|
|
host -> buffers -> dataLength += sizeof (enet_uint32);
|
2024-03-05 08:46:29 -07:00
|
|
|
newChecksum = host -> checksum (host -> buffers, host -> bufferCount);
|
|
|
|
memcpy(checksum, & newChecksum, sizeof (enet_uint32));
|
2010-06-09 14:16:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldCompress > 0)
|
|
|
|
{
|
|
|
|
host -> buffers [1].data = host -> packetData [1];
|
|
|
|
host -> buffers [1].dataLength = shouldCompress;
|
|
|
|
host -> bufferCount = 2;
|
|
|
|
}
|
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);
|
|
|
|
|
2023-02-04 21:20:39 -07:00
|
|
|
enet_protocol_remove_sent_unreliable_commands (currentPeer, & sentUnreliableCommands);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (sentLength < 0)
|
|
|
|
return -1;
|
2010-05-14 16:41:14 -07:00
|
|
|
|
|
|
|
host -> totalSentData += sentLength;
|
|
|
|
host -> totalSentPackets ++;
|
2023-02-04 20:48:18 -07:00
|
|
|
|
|
|
|
nextPeer:
|
|
|
|
if (currentPeer -> flags & ENET_PEER_FLAG_CONTINUE_SENDING)
|
|
|
|
continueSending = sendPass + 1;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2013-03-19 02:26:54 -07:00
|
|
|
#ifdef ENET_DEBUG
|
2006-03-14 16:21:50 -07:00
|
|
|
perror ("Error dispatching incoming packets");
|
2013-03-19 02:26:54 -07:00
|
|
|
#endif
|
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:
|
2013-03-19 02:26:54 -07:00
|
|
|
#ifdef ENET_DEBUG
|
2004-10-20 10:17:51 -07:00
|
|
|
perror ("Error sending outgoing packets");
|
2013-03-19 02:26:54 -07:00
|
|
|
#endif
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (enet_protocol_receive_incoming_commands (host, event))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case -1:
|
2013-03-19 02:26:54 -07:00
|
|
|
#ifdef ENET_DEBUG
|
2004-10-20 10:17:51 -07:00
|
|
|
perror ("Error receiving incoming packets");
|
2013-03-19 02:26:54 -07:00
|
|
|
#endif
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (enet_protocol_send_outgoing_commands (host, event, 1))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case -1:
|
2013-03-19 02:26:54 -07:00
|
|
|
#ifdef ENET_DEBUG
|
2004-10-20 10:17:51 -07:00
|
|
|
perror ("Error sending outgoing packets");
|
2013-03-19 02:26:54 -07:00
|
|
|
#endif
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
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:
|
2013-03-19 02:26:54 -07:00
|
|
|
#ifdef ENET_DEBUG
|
2006-03-14 16:21:50 -07:00
|
|
|
perror ("Error dispatching incoming packets");
|
2013-03-19 02:26:54 -07:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2014-02-21 02:20:20 -07:00
|
|
|
if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
|
|
|
|
return 0;
|
|
|
|
|
2013-05-10 07:49:44 -07:00
|
|
|
do
|
|
|
|
{
|
|
|
|
host -> serviceTime = enet_time_get ();
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2013-05-10 07:49:44 -07:00
|
|
|
if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
|
|
|
|
return 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2013-05-10 07:49:44 -07:00
|
|
|
waitCondition = ENET_SOCKET_WAIT_RECEIVE | ENET_SOCKET_WAIT_INTERRUPT;
|
|
|
|
|
|
|
|
if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (waitCondition & ENET_SOCKET_WAIT_INTERRUPT);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2007-08-31 19:42:31 -07:00
|
|
|
host -> serviceTime = enet_time_get ();
|
2013-05-10 07:49:44 -07:00
|
|
|
} while (waitCondition & ENET_SOCKET_WAIT_RECEIVE);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|