12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913 |
- /**
- @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"
- static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
- {
- 0,
- sizeof (ENetProtocolAcknowledge),
- sizeof (ENetProtocolConnect),
- sizeof (ENetProtocolVerifyConnect),
- sizeof (ENetProtocolDisconnect),
- sizeof (ENetProtocolPing),
- sizeof (ENetProtocolSendReliable),
- sizeof (ENetProtocolSendUnreliable),
- sizeof (ENetProtocolSendFragment),
- sizeof (ENetProtocolSendUnsequenced),
- sizeof (ENetProtocolBandwidthLimit),
- sizeof (ENetProtocolThrottleConfigure),
- sizeof (ENetProtocolSendFragment)
- };
- size_t
- enet_protocol_command_size (enet_uint8 commandNumber)
- {
- return commandSizes [commandNumber & ENET_PROTOCOL_COMMAND_MASK];
- }
- 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)
- {
- enet_protocol_change_state (host, peer, state);
- if (! peer -> needsDispatch)
- {
- enet_list_insert (enet_list_end (& host -> dispatchQueue), & peer -> dispatchList);
- peer -> needsDispatch = 1;
- }
- }
- static int
- enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
- {
- while (! enet_list_empty (& host -> dispatchQueue))
- {
- ENetPeer * peer = (ENetPeer *) enet_list_remove (enet_list_begin (& host -> dispatchQueue));
- peer -> needsDispatch = 0;
- switch (peer -> state)
- {
- case ENET_PEER_STATE_CONNECTION_PENDING:
- case ENET_PEER_STATE_CONNECTION_SUCCEEDED:
- enet_protocol_change_state (host, peer, ENET_PEER_STATE_CONNECTED);
- event -> type = ENET_EVENT_TYPE_CONNECT;
- event -> peer = peer;
- event -> data = peer -> eventData;
- return 1;
-
- case ENET_PEER_STATE_ZOMBIE:
- host -> recalculateBandwidthLimits = 1;
- event -> type = ENET_EVENT_TYPE_DISCONNECT;
- event -> peer = peer;
- event -> data = peer -> eventData;
- enet_peer_reset (peer);
- return 1;
- case ENET_PEER_STATE_CONNECTED:
- if (enet_list_empty (& peer -> dispatchedCommands))
- continue;
- event -> packet = enet_peer_receive (peer, & event -> channelID);
- if (event -> packet == NULL)
- continue;
-
- event -> type = ENET_EVENT_TYPE_RECEIVE;
- event -> peer = peer;
- if (! enet_list_empty (& peer -> dispatchedCommands))
- {
- peer -> needsDispatch = 1;
-
- enet_list_insert (enet_list_end (& host -> dispatchQueue), & peer -> dispatchList);
- }
- return 1;
- default:
- break;
- }
- }
- return 0;
- }
- static void
- enet_protocol_notify_connect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
- {
- host -> recalculateBandwidthLimits = 1;
- if (event != NULL)
- {
- enet_protocol_change_state (host, peer, ENET_PEER_STATE_CONNECTED);
- event -> type = ENET_EVENT_TYPE_CONNECT;
- event -> peer = peer;
- event -> data = peer -> eventData;
- }
- else
- enet_protocol_dispatch_state (host, peer, peer -> state == ENET_PEER_STATE_CONNECTING ? ENET_PEER_STATE_CONNECTION_SUCCEEDED : ENET_PEER_STATE_CONNECTION_PENDING);
- }
- static void
- enet_protocol_notify_disconnect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
- {
- if (peer -> state >= ENET_PEER_STATE_CONNECTION_PENDING)
- host -> recalculateBandwidthLimits = 1;
- if (peer -> state != ENET_PEER_STATE_CONNECTING && peer -> state < ENET_PEER_STATE_CONNECTION_SUCCEEDED)
- enet_peer_reset (peer);
- else
- if (event != NULL)
- {
- event -> type = ENET_EVENT_TYPE_DISCONNECT;
- event -> peer = peer;
- event -> data = 0;
- enet_peer_reset (peer);
- }
- else
- {
- peer -> eventData = 0;
- enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
- }
- }
- static void
- enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer)
- {
- ENetOutgoingCommand * outgoingCommand;
- while (! enet_list_empty (& peer -> sentUnreliableCommands))
- {
- outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
-
- enet_list_remove (& outgoingCommand -> outgoingCommandList);
- if (outgoingCommand -> packet != NULL)
- {
- -- outgoingCommand -> packet -> referenceCount;
- if (outgoingCommand -> packet -> referenceCount == 0)
- {
- outgoingCommand -> packet -> flags |= ENET_PACKET_FLAG_SENT;
-
- enet_packet_destroy (outgoingCommand -> packet);
- }
- }
- enet_free (outgoingCommand);
- }
- }
- static ENetProtocolCommand
- enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint16 reliableSequenceNumber, enet_uint8 channelID)
- {
- ENetOutgoingCommand * outgoingCommand = NULL;
- ENetListIterator currentCommand;
- ENetProtocolCommand commandNumber;
- int wasSent = 1;
- 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))
- {
- for (currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
- currentCommand != enet_list_end (& peer -> outgoingReliableCommands);
- currentCommand = enet_list_next (currentCommand))
- {
- outgoingCommand = (ENetOutgoingCommand *) currentCommand;
- if (outgoingCommand -> sendAttempts < 1) return ENET_PROTOCOL_COMMAND_NONE;
- if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
- outgoingCommand -> command.header.channelID == channelID)
- break;
- }
- if (currentCommand == enet_list_end (& peer -> outgoingReliableCommands))
- return ENET_PROTOCOL_COMMAND_NONE;
- wasSent = 0;
- }
- if (outgoingCommand == NULL)
- return ENET_PROTOCOL_COMMAND_NONE;
- if (channelID < peer -> channelCount)
- {
- 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])
- channel -> usedReliableWindows &= ~ (1 << reliableWindow);
- }
- }
- commandNumber = (ENetProtocolCommand) (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK);
-
- enet_list_remove (& outgoingCommand -> outgoingCommandList);
- if (outgoingCommand -> packet != NULL)
- {
- if (wasSent)
- peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
- -- outgoingCommand -> packet -> referenceCount;
- if (outgoingCommand -> packet -> referenceCount == 0)
- {
- outgoingCommand -> packet -> flags |= ENET_PACKET_FLAG_SENT;
- enet_packet_destroy (outgoingCommand -> packet);
- }
- }
- enet_free (outgoingCommand);
- if (enet_list_empty (& peer -> sentReliableCommands))
- return commandNumber;
-
- outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
-
- peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
- return commandNumber;
- }
- static ENetPeer *
- enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENetProtocol * command)
- {
- enet_uint8 incomingSessionID, outgoingSessionID;
- enet_uint32 mtu, windowSize;
- ENetChannel * channel;
- size_t channelCount, duplicatePeers = 0;
- ENetPeer * currentPeer, * peer = NULL;
- 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)
- {
- if (peer == NULL)
- peer = currentPeer;
- }
- else
- if (currentPeer -> state != ENET_PEER_STATE_CONNECTING &&
- currentPeer -> address.host == host -> receivedAddress.host)
- {
- if (currentPeer -> address.port == host -> receivedAddress.port &&
- currentPeer -> connectID == command -> connect.connectID)
- return NULL;
- ++ duplicatePeers;
- }
- }
- if (peer == NULL || duplicatePeers >= host -> duplicatePeers)
- return NULL;
- if (channelCount > host -> channelLimit)
- channelCount = host -> channelLimit;
- peer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
- if (peer -> channels == NULL)
- return NULL;
- peer -> channelCount = channelCount;
- peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
- peer -> connectID = command -> connect.connectID;
- peer -> address = host -> receivedAddress;
- 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;
- incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
- if (incomingSessionID == peer -> outgoingSessionID)
- incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
- peer -> outgoingSessionID = incomingSessionID;
- outgoingSessionID = command -> connect.outgoingSessionID == 0xFF ? peer -> incomingSessionID : command -> connect.outgoingSessionID;
- outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
- if (outgoingSessionID == peer -> incomingSessionID)
- outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
- peer -> incomingSessionID = outgoingSessionID;
- for (channel = peer -> channels;
- channel < & peer -> channels [channelCount];
- ++ channel)
- {
- channel -> outgoingReliableSequenceNumber = 0;
- channel -> outgoingUnreliableSequenceNumber = 0;
- channel -> incomingReliableSequenceNumber = 0;
- channel -> incomingUnreliableSequenceNumber = 0;
- enet_list_clear (& channel -> incomingReliableCommands);
- enet_list_clear (& channel -> incomingUnreliableCommands);
- channel -> usedReliableWindows = 0;
- memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows));
- }
- mtu = ENET_NET_TO_HOST_32 (command -> connect.mtu);
- if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
- mtu = ENET_PROTOCOL_MINIMUM_MTU;
- else
- if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
- mtu = ENET_PROTOCOL_MAXIMUM_MTU;
- peer -> mtu = mtu;
- if (host -> outgoingBandwidth == 0 &&
- peer -> incomingBandwidth == 0)
- peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
- else
- if (host -> outgoingBandwidth == 0 ||
- peer -> incomingBandwidth == 0)
- peer -> windowSize = (ENET_MAX (host -> outgoingBandwidth, peer -> incomingBandwidth) /
- ENET_PEER_WINDOW_SIZE_SCALE) *
- ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
- else
- peer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, peer -> incomingBandwidth) /
- 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;
- 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;
- verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
- verifyCommand.header.channelID = 0xFF;
- verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (peer -> incomingPeerID);
- verifyCommand.verifyConnect.incomingSessionID = incomingSessionID;
- verifyCommand.verifyConnect.outgoingSessionID = outgoingSessionID;
- verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_32 (peer -> mtu);
- verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
- verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
- verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
- verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
- verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (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;
- enet_peer_queue_outgoing_command (peer, & verifyCommand, NULL, 0, 0);
- return peer;
- }
- static int
- enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
- {
- size_t dataLength;
- if (command -> header.channelID >= peer -> channelCount ||
- (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
- return -1;
- dataLength = ENET_NET_TO_HOST_16 (command -> sendReliable.dataLength);
- * currentData += dataLength;
- if (dataLength > host -> maximumPacketSize ||
- * currentData < host -> receivedData ||
- * currentData > & host -> receivedData [host -> receivedDataLength])
- return -1;
- if (enet_peer_queue_incoming_command (peer, command, (const enet_uint8 *) command + sizeof (ENetProtocolSendReliable), dataLength, ENET_PACKET_FLAG_RELIABLE, 0) == NULL)
- return -1;
- return 0;
- }
- static int
- enet_protocol_handle_send_unsequenced (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
- {
- enet_uint32 unsequencedGroup, index;
- size_t dataLength;
- if (command -> header.channelID >= peer -> channelCount ||
- (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
- return -1;
- dataLength = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.dataLength);
- * currentData += dataLength;
- if (dataLength > host -> maximumPacketSize ||
- * currentData < host -> receivedData ||
- * currentData > & host -> receivedData [host -> receivedDataLength])
- return -1;
- unsequencedGroup = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.unsequencedGroup);
- index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
-
- 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)
- {
- peer -> incomingUnsequencedGroup = unsequencedGroup - index;
- memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
- }
- else
- if (peer -> unsequencedWindow [index / 32] & (1 << (index % 32)))
- return 0;
-
- if (enet_peer_queue_incoming_command (peer, command, (const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced), dataLength, ENET_PACKET_FLAG_UNSEQUENCED, 0) == NULL)
- return -1;
-
- peer -> unsequencedWindow [index / 32] |= 1 << (index % 32);
-
- return 0;
- }
- static int
- enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
- {
- size_t dataLength;
- if (command -> header.channelID >= peer -> channelCount ||
- (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
- return -1;
- dataLength = ENET_NET_TO_HOST_16 (command -> sendUnreliable.dataLength);
- * currentData += dataLength;
- if (dataLength > host -> maximumPacketSize ||
- * currentData < host -> receivedData ||
- * currentData > & host -> receivedData [host -> receivedDataLength])
- return -1;
- if (enet_peer_queue_incoming_command (peer, command, (const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable), dataLength, 0, 0) == NULL)
- return -1;
- return 0;
- }
- static int
- enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
- {
- enet_uint32 fragmentNumber,
- fragmentCount,
- fragmentOffset,
- fragmentLength,
- startSequenceNumber,
- totalLength;
- ENetChannel * channel;
- enet_uint16 startWindow, currentWindow;
- ENetListIterator currentCommand;
- ENetIncomingCommand * startCommand = NULL;
- if (command -> header.channelID >= peer -> channelCount ||
- (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
- return -1;
- fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
- * currentData += fragmentLength;
- if (fragmentLength > host -> maximumPacketSize ||
- * currentData < host -> receivedData ||
- * currentData > & host -> receivedData [host -> receivedDataLength])
- return -1;
- channel = & peer -> channels [command -> header.channelID];
- startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
- startWindow = startSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
- currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
- if (startSequenceNumber < channel -> incomingReliableSequenceNumber)
- startWindow += ENET_PEER_RELIABLE_WINDOWS;
- if (startWindow < currentWindow || startWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
- 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);
-
- if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
- fragmentNumber >= fragmentCount ||
- totalLength > host -> maximumPacketSize ||
- fragmentOffset >= totalLength ||
- fragmentLength > totalLength - fragmentOffset)
- return -1;
-
- for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
- currentCommand != enet_list_end (& channel -> incomingReliableCommands);
- currentCommand = enet_list_previous (currentCommand))
- {
- ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
- if (startSequenceNumber >= channel -> incomingReliableSequenceNumber)
- {
- if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
- continue;
- }
- else
- if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
- break;
- 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;
- }
- }
-
- if (startCommand == NULL)
- {
- ENetProtocol hostCommand = * command;
- hostCommand.header.reliableSequenceNumber = startSequenceNumber;
- startCommand = enet_peer_queue_incoming_command (peer, & hostCommand, NULL, totalLength, ENET_PACKET_FLAG_RELIABLE, fragmentCount);
- if (startCommand == NULL)
- return -1;
- }
-
- if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
- {
- -- startCommand -> fragmentsRemaining;
- startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
- 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)
- enet_peer_dispatch_incoming_reliable_commands (peer, channel);
- }
- return 0;
- }
- 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 ||
- (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
- return -1;
- fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
- * currentData += fragmentLength;
- if (fragmentLength > host -> maximumPacketSize ||
- * currentData < host -> receivedData ||
- * currentData > & host -> receivedData [host -> receivedDataLength])
- 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);
- if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
- fragmentNumber >= fragmentCount ||
- totalLength > host -> maximumPacketSize ||
- fragmentOffset >= totalLength ||
- fragmentLength > totalLength - fragmentOffset)
- 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)
- {
- startCommand = enet_peer_queue_incoming_command (peer, command, NULL, totalLength, ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT, fragmentCount);
- if (startCommand == NULL)
- return -1;
- }
- if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
- {
- -- startCommand -> fragmentsRemaining;
- startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
- 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)
- enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
- }
- return 0;
- }
- static int
- enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
- {
- if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
- return -1;
- return 0;
- }
- static int
- enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
- {
- if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
- return -1;
- if (peer -> incomingBandwidth != 0)
- -- host -> bandwidthLimitedPeers;
- peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
- peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
- if (peer -> incomingBandwidth != 0)
- ++ host -> bandwidthLimitedPeers;
- if (peer -> incomingBandwidth == 0 && host -> outgoingBandwidth == 0)
- peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
- 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;
- 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;
- return 0;
- }
- static int
- enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
- {
- if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
- return -1;
- 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);
- return 0;
- }
- static int
- enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
- {
- if (peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ZOMBIE || peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT)
- return 0;
- enet_peer_reset_queues (peer);
- if (peer -> state == ENET_PEER_STATE_CONNECTION_SUCCEEDED || peer -> state == ENET_PEER_STATE_DISCONNECTING || peer -> state == ENET_PEER_STATE_CONNECTING)
- enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
- else
- if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
- {
- if (peer -> state == ENET_PEER_STATE_CONNECTION_PENDING) host -> recalculateBandwidthLimits = 1;
- enet_peer_reset (peer);
- }
- else
- if (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
- enet_protocol_change_state (host, peer, ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT);
- else
- enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
- if (peer -> state != ENET_PEER_STATE_DISCONNECTED)
- peer -> eventData = ENET_NET_TO_HOST_32 (command -> disconnect.data);
- return 0;
- }
- static int
- enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
- {
- enet_uint32 roundTripTime,
- receivedSentTime,
- receivedReliableSequenceNumber;
- ENetProtocolCommand commandNumber;
- if (peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ZOMBIE)
- return 0;
- receivedSentTime = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedSentTime);
- receivedSentTime |= host -> serviceTime & 0xFFFF0000;
- if ((receivedSentTime & 0x8000) > (host -> serviceTime & 0x8000))
- receivedSentTime -= 0x10000;
- if (ENET_TIME_LESS (host -> serviceTime, receivedSentTime))
- return 0;
- peer -> lastReceiveTime = host -> serviceTime;
- peer -> earliestTimeout = 0;
- roundTripTime = ENET_TIME_DIFFERENCE (host -> serviceTime, receivedSentTime);
- enet_peer_throttle (peer, roundTripTime);
- peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
- if (roundTripTime >= peer -> roundTripTime)
- {
- peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
- peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
- }
- else
- {
- peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
- peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
- }
- if (peer -> roundTripTime < peer -> lowestRoundTripTime)
- peer -> lowestRoundTripTime = peer -> roundTripTime;
- if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
- peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
- if (peer -> packetThrottleEpoch == 0 ||
- ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
- {
- peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
- peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
- peer -> lowestRoundTripTime = peer -> roundTripTime;
- peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
- peer -> packetThrottleEpoch = host -> serviceTime;
- }
- receivedReliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedReliableSequenceNumber);
- 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)
- return -1;
- enet_protocol_notify_connect (host, peer, event);
- break;
- case ENET_PEER_STATE_DISCONNECTING:
- if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
- return -1;
- enet_protocol_notify_disconnect (host, peer, event);
- break;
- case ENET_PEER_STATE_DISCONNECT_LATER:
- if (enet_list_empty (& peer -> outgoingReliableCommands) &&
- enet_list_empty (& peer -> outgoingUnreliableCommands) &&
- enet_list_empty (& peer -> sentReliableCommands))
- enet_peer_disconnect (peer, peer -> eventData);
- break;
- default:
- break;
- }
-
- return 0;
- }
- static int
- enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
- {
- enet_uint32 mtu, windowSize;
- size_t channelCount;
- if (peer -> state != ENET_PEER_STATE_CONNECTING)
- return 0;
- channelCount = ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount);
- if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT ||
- ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
- ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
- ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration ||
- command -> verifyConnect.connectID != peer -> connectID)
- {
- peer -> eventData = 0;
- enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
- return -1;
- }
- enet_protocol_remove_sent_reliable_command (peer, 1, 0xFF);
-
- if (channelCount < peer -> channelCount)
- peer -> channelCount = channelCount;
- peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
- peer -> incomingSessionID = command -> verifyConnect.incomingSessionID;
- peer -> outgoingSessionID = command -> verifyConnect.outgoingSessionID;
- mtu = ENET_NET_TO_HOST_32 (command -> verifyConnect.mtu);
- if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
- mtu = ENET_PROTOCOL_MINIMUM_MTU;
- else
- if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
- mtu = ENET_PROTOCOL_MAXIMUM_MTU;
- if (mtu < peer -> mtu)
- peer -> mtu = mtu;
- windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
- if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
- windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
- if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
- windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
- if (windowSize < peer -> windowSize)
- peer -> windowSize = windowSize;
- peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
- peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
- enet_protocol_notify_connect (host, peer, event);
- return 0;
- }
- static int
- enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
- {
- ENetProtocolHeader * header;
- ENetProtocol * command;
- ENetPeer * peer;
- enet_uint8 * currentData;
- size_t headerSize;
- enet_uint16 peerID, flags;
- enet_uint8 sessionID;
- if (host -> receivedDataLength < (size_t) & ((ENetProtocolHeader *) 0) -> sentTime)
- return 0;
- header = (ENetProtocolHeader *) host -> receivedData;
- peerID = ENET_NET_TO_HOST_16 (header -> peerID);
- sessionID = (peerID & ENET_PROTOCOL_HEADER_SESSION_MASK) >> ENET_PROTOCOL_HEADER_SESSION_SHIFT;
- flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
- 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);
- if (peerID == ENET_PROTOCOL_MAXIMUM_PEER_ID)
- peer = NULL;
- else
- if (peerID >= host -> peerCount)
- return 0;
- else
- {
- peer = & host -> peers [peerID];
- if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
- peer -> state == ENET_PEER_STATE_ZOMBIE ||
- ((host -> receivedAddress.host != peer -> address.host ||
- host -> receivedAddress.port != peer -> address.port) &&
- peer -> address.host != ENET_HOST_BROADCAST) ||
- (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
- sessionID != peer -> incomingSessionID))
- return 0;
- }
-
- 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;
- }
- if (host -> checksum != NULL)
- {
- enet_uint32 * checksum = (enet_uint32 *) & host -> receivedData [headerSize - sizeof (enet_uint32)],
- desiredChecksum = * checksum;
- ENetBuffer buffer;
- * checksum = peer != NULL ? peer -> connectID : 0;
- buffer.data = host -> receivedData;
- buffer.dataLength = host -> receivedDataLength;
- if (host -> checksum (& buffer, 1) != desiredChecksum)
- return 0;
- }
-
- if (peer != NULL)
- {
- peer -> address.host = host -> receivedAddress.host;
- peer -> address.port = host -> receivedAddress.port;
- peer -> incomingDataTotal += host -> receivedDataLength;
- }
-
- currentData = host -> receivedData + headerSize;
-
- while (currentData < & host -> receivedData [host -> receivedDataLength])
- {
- enet_uint8 commandNumber;
- size_t commandSize;
- command = (ENetProtocol *) currentData;
- if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
- break;
- 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])
- break;
- currentData += commandSize;
- if (peer == NULL && commandNumber != ENET_PROTOCOL_COMMAND_CONNECT)
- break;
-
- command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
- switch (commandNumber)
- {
- case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
- if (enet_protocol_handle_acknowledge (host, event, peer, command))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_CONNECT:
- if (peer != NULL)
- goto commandError;
- peer = enet_protocol_handle_connect (host, header, command);
- if (peer == NULL)
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
- if (enet_protocol_handle_verify_connect (host, event, peer, command))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_DISCONNECT:
- if (enet_protocol_handle_disconnect (host, peer, command))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_PING:
- if (enet_protocol_handle_ping (host, peer, command))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
- if (enet_protocol_handle_send_reliable (host, peer, command, & currentData))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
- if (enet_protocol_handle_send_unreliable (host, peer, command, & currentData))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
- if (enet_protocol_handle_send_unsequenced (host, peer, command, & currentData))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
- if (enet_protocol_handle_send_fragment (host, peer, command, & currentData))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
- if (enet_protocol_handle_bandwidth_limit (host, peer, command))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
- if (enet_protocol_handle_throttle_configure (host, peer, command))
- goto commandError;
- break;
- case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
- if (enet_protocol_handle_send_unreliable_fragment (host, peer, command, & currentData))
- goto commandError;
- break;
- default:
- goto commandError;
- }
- if (peer != NULL &&
- (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
- {
- enet_uint16 sentTime;
- if (! (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME))
- break;
- sentTime = ENET_NET_TO_HOST_16 (header -> sentTime);
- switch (peer -> state)
- {
- case ENET_PEER_STATE_DISCONNECTING:
- case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
- case ENET_PEER_STATE_DISCONNECTED:
- case ENET_PEER_STATE_ZOMBIE:
- break;
- case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
- if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
- enet_peer_queue_acknowledgement (peer, command, sentTime);
- break;
- default:
- enet_peer_queue_acknowledgement (peer, command, sentTime);
- break;
- }
- }
- }
- commandError:
- if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
- return 1;
- return 0;
- }
-
- static int
- enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
- {
- int packets;
- for (packets = 0; packets < 256; ++ packets)
- {
- int receivedLength;
- ENetBuffer buffer;
- buffer.data = host -> packetData [0];
- buffer.dataLength = sizeof (host -> packetData [0]);
- receivedLength = enet_socket_receive (host -> socket,
- & host -> receivedAddress,
- & buffer,
- 1);
- if (receivedLength < 0)
- return -1;
- if (receivedLength == 0)
- return 0;
- host -> receivedData = host -> packetData [0];
- host -> receivedDataLength = receivedLength;
-
- host -> totalReceivedData += receivedLength;
- host -> totalReceivedPackets ++;
- 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;
- }
- }
-
- switch (enet_protocol_handle_incoming_commands (host, event))
- {
- case 1:
- return 1;
-
- case -1:
- return -1;
- default:
- break;
- }
- }
- return -1;
- }
- static void
- enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
- {
- ENetProtocol * command = & host -> commands [host -> commandCount];
- ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
- ENetAcknowledgement * acknowledgement;
- ENetListIterator currentAcknowledgement;
- enet_uint16 reliableSequenceNumber;
-
- 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))
- {
- host -> continueSending = 1;
- break;
- }
- acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
-
- currentAcknowledgement = enet_list_next (currentAcknowledgement);
- buffer -> data = command;
- buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
- host -> packetSize += buffer -> dataLength;
- reliableSequenceNumber = ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
-
- command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
- command -> header.channelID = acknowledgement -> command.header.channelID;
- command -> header.reliableSequenceNumber = reliableSequenceNumber;
- command -> acknowledge.receivedReliableSequenceNumber = reliableSequenceNumber;
- command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_16 (acknowledgement -> sentTime);
-
- if ((acknowledgement -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
- enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
- enet_list_remove (& acknowledgement -> acknowledgementList);
- enet_free (acknowledgement);
- ++ command;
- ++ buffer;
- }
- host -> commandCount = command - host -> commands;
- host -> bufferCount = buffer - host -> buffers;
- }
- static void
- enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
- {
- ENetProtocol * command = & host -> commands [host -> commandCount];
- ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
- ENetOutgoingCommand * outgoingCommand;
- ENetListIterator currentCommand;
- currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
-
- while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
- {
- size_t commandSize;
- outgoingCommand = (ENetOutgoingCommand *) currentCommand;
- commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
- if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
- buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
- peer -> mtu - host -> packetSize < commandSize ||
- (outgoingCommand -> packet != NULL &&
- peer -> mtu - host -> packetSize < commandSize + outgoingCommand -> fragmentLength))
- {
- host -> continueSending = 1;
- break;
- }
- currentCommand = enet_list_next (currentCommand);
- 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);
- if (currentCommand == enet_list_end (& peer -> outgoingUnreliableCommands))
- break;
- outgoingCommand = (ENetOutgoingCommand *) currentCommand;
- if (outgoingCommand -> reliableSequenceNumber != reliableSequenceNumber ||
- outgoingCommand -> unreliableSequenceNumber != unreliableSequenceNumber)
- break;
- currentCommand = enet_list_next (currentCommand);
- }
-
- continue;
- }
- }
- buffer -> data = command;
- buffer -> dataLength = commandSize;
-
- host -> packetSize += buffer -> dataLength;
- * command = outgoingCommand -> command;
-
- enet_list_remove (& outgoingCommand -> outgoingCommandList);
- if (outgoingCommand -> packet != NULL)
- {
- ++ buffer;
-
- buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
- buffer -> dataLength = outgoingCommand -> fragmentLength;
- host -> packetSize += buffer -> dataLength;
- enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
- }
- else
- enet_free (outgoingCommand);
- ++ command;
- ++ buffer;
- }
- host -> commandCount = command - host -> commands;
- host -> bufferCount = buffer - host -> buffers;
- if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
- enet_list_empty (& peer -> outgoingReliableCommands) &&
- enet_list_empty (& peer -> outgoingUnreliableCommands) &&
- enet_list_empty (& peer -> sentReliableCommands))
- enet_peer_disconnect (peer, peer -> eventData);
- }
- static int
- enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
- {
- ENetOutgoingCommand * outgoingCommand;
- ENetListIterator currentCommand, insertPosition;
- currentCommand = enet_list_begin (& peer -> sentReliableCommands);
- insertPosition = enet_list_begin (& peer -> outgoingReliableCommands);
- while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
- {
- outgoingCommand = (ENetOutgoingCommand *) currentCommand;
- currentCommand = enet_list_next (currentCommand);
- if (ENET_TIME_DIFFERENCE (host -> serviceTime, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
- continue;
- if (peer -> earliestTimeout == 0 ||
- ENET_TIME_LESS (outgoingCommand -> sentTime, peer -> earliestTimeout))
- peer -> earliestTimeout = outgoingCommand -> sentTime;
- if (peer -> earliestTimeout != 0 &&
- (ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMaximum ||
- (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit &&
- ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMinimum)))
- {
- enet_protocol_notify_disconnect (host, peer, event);
- return 1;
- }
- if (outgoingCommand -> packet != NULL)
- peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
-
- ++ peer -> packetsLost;
- outgoingCommand -> roundTripTimeout *= 2;
- enet_list_insert (insertPosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
- if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
- ! enet_list_empty (& peer -> sentReliableCommands))
- {
- outgoingCommand = (ENetOutgoingCommand *) currentCommand;
- peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
- }
- }
-
- return 0;
- }
- static int
- enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
- {
- ENetProtocol * command = & host -> commands [host -> commandCount];
- ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
- ENetOutgoingCommand * outgoingCommand;
- ENetListIterator currentCommand;
- ENetChannel *channel;
- enet_uint16 reliableWindow;
- size_t commandSize;
- int windowExceeded = 0, windowWrap = 0, canPing = 1;
- currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
-
- while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
- {
- outgoingCommand = (ENetOutgoingCommand *) currentCommand;
- 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)
- {
- if (! windowWrap &&
- outgoingCommand -> sendAttempts < 1 &&
- ! (outgoingCommand -> reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE) &&
- (channel -> reliableWindows [(reliableWindow + ENET_PEER_RELIABLE_WINDOWS - 1) % ENET_PEER_RELIABLE_WINDOWS] >= ENET_PEER_RELIABLE_WINDOW_SIZE ||
- channel -> usedReliableWindows & ((((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) << reliableWindow) |
- (((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) >> (ENET_PEER_RELIABLE_WINDOWS - reliableWindow)))))
- windowWrap = 1;
- if (windowWrap)
- {
- currentCommand = enet_list_next (currentCommand);
-
- continue;
- }
- }
-
- if (outgoingCommand -> packet != NULL)
- {
- if (! windowExceeded)
- {
- enet_uint32 windowSize = (peer -> packetThrottle * peer -> windowSize) / ENET_PEER_PACKET_THROTTLE_SCALE;
-
- if (peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > ENET_MAX (windowSize, peer -> mtu))
- windowExceeded = 1;
- }
- if (windowExceeded)
- {
- currentCommand = enet_list_next (currentCommand);
- continue;
- }
- }
- canPing = 0;
- commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
- if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
- buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
- peer -> mtu - host -> packetSize < commandSize ||
- (outgoingCommand -> packet != NULL &&
- (enet_uint16) (peer -> mtu - host -> packetSize) < (enet_uint16) (commandSize + outgoingCommand -> fragmentLength)))
- {
- host -> continueSending = 1;
-
- break;
- }
- currentCommand = enet_list_next (currentCommand);
- if (channel != NULL && outgoingCommand -> sendAttempts < 1)
- {
- channel -> usedReliableWindows |= 1 << reliableWindow;
- ++ channel -> reliableWindows [reliableWindow];
- }
- ++ outgoingCommand -> sendAttempts;
-
- if (outgoingCommand -> roundTripTimeout == 0)
- {
- outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
- outgoingCommand -> roundTripTimeoutLimit = peer -> timeoutLimit * outgoingCommand -> roundTripTimeout;
- }
- 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;
- buffer -> data = command;
- buffer -> dataLength = commandSize;
- host -> packetSize += buffer -> dataLength;
- host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
- * command = outgoingCommand -> command;
- if (outgoingCommand -> packet != NULL)
- {
- ++ buffer;
-
- buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
- buffer -> dataLength = outgoingCommand -> fragmentLength;
- host -> packetSize += outgoingCommand -> fragmentLength;
- peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
- }
- ++ peer -> packetsSent;
-
- ++ command;
- ++ buffer;
- }
- host -> commandCount = command - host -> commands;
- host -> bufferCount = buffer - host -> buffers;
- return canPing;
- }
- static int
- enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
- {
- enet_uint8 headerData [sizeof (ENetProtocolHeader) + sizeof (enet_uint32)];
- ENetProtocolHeader * header = (ENetProtocolHeader *) headerData;
- ENetPeer * currentPeer;
- int sentLength;
- size_t shouldCompress = 0;
-
- host -> continueSending = 1;
- while (host -> continueSending)
- for (host -> continueSending = 0,
- currentPeer = host -> peers;
- currentPeer < & host -> peers [host -> peerCount];
- ++ currentPeer)
- {
- if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
- currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
- continue;
- host -> headerFlags = 0;
- host -> commandCount = 0;
- host -> bufferCount = 1;
- host -> packetSize = sizeof (ENetProtocolHeader);
- if (! enet_list_empty (& currentPeer -> acknowledgements))
- enet_protocol_send_acknowledgements (host, currentPeer);
- if (checkForTimeouts != 0 &&
- ! enet_list_empty (& currentPeer -> sentReliableCommands) &&
- ENET_TIME_GREATER_EQUAL (host -> serviceTime, currentPeer -> nextTimeout) &&
- enet_protocol_check_timeouts (host, currentPeer, event) == 1)
- {
- if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
- return 1;
- else
- continue;
- }
- if ((enet_list_empty (& currentPeer -> outgoingReliableCommands) ||
- enet_protocol_send_reliable_outgoing_commands (host, currentPeer)) &&
- enet_list_empty (& currentPeer -> sentReliableCommands) &&
- ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime) >= currentPeer -> pingInterval &&
- currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
- {
- enet_peer_ping (currentPeer);
- enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
- }
-
- if (! enet_list_empty (& currentPeer -> outgoingUnreliableCommands))
- enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
- if (host -> commandCount == 0)
- continue;
- if (currentPeer -> packetLossEpoch == 0)
- currentPeer -> packetLossEpoch = host -> serviceTime;
- else
- if (ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
- currentPeer -> packetsSent > 0)
- {
- enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
- #ifdef ENET_DEBUG
- printf ("peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands) : 0);
- #endif
-
- currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
- if (packetLoss >= currentPeer -> packetLoss)
- {
- currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
- currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
- }
- else
- {
- currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
- currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
- }
- currentPeer -> packetLossEpoch = host -> serviceTime;
- currentPeer -> packetsSent = 0;
- currentPeer -> packetsLost = 0;
- }
- host -> buffers -> data = headerData;
- if (host -> headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
- {
- header -> sentTime = ENET_HOST_TO_NET_16 (host -> serviceTime & 0xFFFF);
- host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
- }
- else
- host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
- 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
- printf ("peer %u: compressed %u -> %u (%u%%)\n", currentPeer -> incomingPeerID, originalSize, compressedSize, (compressedSize * 100) / originalSize);
- #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);
- if (host -> checksum != NULL)
- {
- enet_uint32 * checksum = (enet_uint32 *) & headerData [host -> buffers -> dataLength];
- * checksum = currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer -> connectID : 0;
- host -> buffers -> dataLength += sizeof (enet_uint32);
- * checksum = host -> checksum (host -> buffers, host -> bufferCount);
- }
- if (shouldCompress > 0)
- {
- host -> buffers [1].data = host -> packetData [1];
- host -> buffers [1].dataLength = shouldCompress;
- host -> bufferCount = 2;
- }
- currentPeer -> lastSendTime = host -> serviceTime;
- sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
- enet_protocol_remove_sent_unreliable_commands (currentPeer);
- if (sentLength < 0)
- return -1;
- host -> totalSentData += sentLength;
- host -> totalSentPackets ++;
- }
-
- 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)
- {
- host -> serviceTime = enet_time_get ();
- enet_protocol_send_outgoing_commands (host, NULL, 0);
- }
- /** 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);
- }
- /** 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
- if event == NULL then no events will be delivered
- @param timeout number of milliseconds that ENet should wait for events
- @retval > 0 if an event occurred within the specified time limit
- @retval 0 if no event occurred
- @retval < 0 on failure
- @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;
- if (event != NULL)
- {
- event -> type = ENET_EVENT_TYPE_NONE;
- event -> peer = NULL;
- event -> packet = NULL;
- switch (enet_protocol_dispatch_incoming_commands (host, event))
- {
- case 1:
- return 1;
- case -1:
- #ifdef ENET_DEBUG
- perror ("Error dispatching incoming packets");
- #endif
- return -1;
- default:
- break;
- }
- }
- host -> serviceTime = enet_time_get ();
-
- timeout += host -> serviceTime;
- do
- {
- if (ENET_TIME_DIFFERENCE (host -> serviceTime, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
- enet_host_bandwidth_throttle (host);
- switch (enet_protocol_send_outgoing_commands (host, event, 1))
- {
- case 1:
- return 1;
- case -1:
- #ifdef ENET_DEBUG
- perror ("Error sending outgoing packets");
- #endif
- return -1;
- default:
- break;
- }
- switch (enet_protocol_receive_incoming_commands (host, event))
- {
- case 1:
- return 1;
- case -1:
- #ifdef ENET_DEBUG
- perror ("Error receiving incoming packets");
- #endif
- return -1;
- default:
- break;
- }
- switch (enet_protocol_send_outgoing_commands (host, event, 1))
- {
- case 1:
- return 1;
- case -1:
- #ifdef ENET_DEBUG
- perror ("Error sending outgoing packets");
- #endif
- return -1;
- default:
- break;
- }
- if (event != NULL)
- {
- switch (enet_protocol_dispatch_incoming_commands (host, event))
- {
- case 1:
- return 1;
- case -1:
- #ifdef ENET_DEBUG
- perror ("Error dispatching incoming packets");
- #endif
- return -1;
- default:
- break;
- }
- }
- if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
- return 0;
- do
- {
- host -> serviceTime = enet_time_get ();
- if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
- return 0;
- 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);
- host -> serviceTime = enet_time_get ();
- } while (waitCondition & ENET_SOCKET_WAIT_RECEIVE);
- return 0;
- }
|