protocol.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. /**
  2. @file protocol.c
  3. @brief ENet protocol functions
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #define ENET_BUILDING_LIB 1
  8. #include "enet/utility.h"
  9. #include "enet/time.h"
  10. #include "enet/enet.h"
  11. static size_t commandSizes[ENET_PROTOCOL_COMMAND_COUNT] =
  12. { 0, sizeof(ENetProtocolAcknowledge), sizeof(ENetProtocolConnect),
  13. sizeof(ENetProtocolVerifyConnect), sizeof(ENetProtocolDisconnect), sizeof(ENetProtocolPing),
  14. sizeof(ENetProtocolSendReliable), sizeof(ENetProtocolSendUnreliable),
  15. sizeof(ENetProtocolSendFragment), sizeof(ENetProtocolSendUnsequenced),
  16. sizeof(ENetProtocolBandwidthLimit), sizeof(ENetProtocolThrottleConfigure),
  17. sizeof(ENetProtocolSendFragment) };
  18. size_t enet_protocol_command_size(enet_uint8 commandNumber)
  19. {
  20. return commandSizes[commandNumber & ENET_PROTOCOL_COMMAND_MASK];
  21. }
  22. static int enet_protocol_dispatch_incoming_commands(ENetHost * host, ENetEvent * event)
  23. {
  24. while (!enet_list_empty (& host -> dispatchQueue))
  25. {
  26. ENetPeer * peer = (ENetPeer *) enet_list_remove(enet_list_begin (& host -> dispatchQueue));
  27. peer->needsDispatch = 0;
  28. switch (peer->state)
  29. {
  30. case ENET_PEER_STATE_CONNECTION_PENDING:
  31. case ENET_PEER_STATE_CONNECTION_SUCCEEDED:
  32. peer->state = ENET_PEER_STATE_CONNECTED;
  33. event->type = ENET_EVENT_TYPE_CONNECT;
  34. event->peer = peer;
  35. event->data = peer->eventData;
  36. return 1;
  37. case ENET_PEER_STATE_ZOMBIE:
  38. host->recalculateBandwidthLimits = 1;
  39. event->type = ENET_EVENT_TYPE_DISCONNECT;
  40. event->peer = peer;
  41. event->data = peer->eventData;
  42. enet_peer_reset(peer);
  43. return 1;
  44. case ENET_PEER_STATE_CONNECTED:
  45. if (enet_list_empty (& peer -> dispatchedCommands))
  46. continue;
  47. event->packet = enet_peer_receive(peer, &event->channelID);
  48. if (event->packet == NULL)
  49. continue;
  50. event->type = ENET_EVENT_TYPE_RECEIVE;
  51. event->peer = peer;
  52. if (!enet_list_empty (& peer -> dispatchedCommands))
  53. {
  54. peer->needsDispatch = 1;
  55. enet_list_insert(enet_list_end (& host -> dispatchQueue), &peer->dispatchList);
  56. }
  57. return 1;
  58. default:
  59. break;
  60. }
  61. }
  62. return 0;
  63. }
  64. static void enet_protocol_dispatch_state(ENetHost * host, ENetPeer * peer, ENetPeerState state)
  65. {
  66. peer->state = state;
  67. if (!peer->needsDispatch)
  68. {
  69. enet_list_insert(enet_list_end (& host -> dispatchQueue), &peer->dispatchList);
  70. peer->needsDispatch = 1;
  71. }
  72. }
  73. static void enet_protocol_notify_connect(ENetHost * host, ENetPeer * peer, ENetEvent * event)
  74. {
  75. host->recalculateBandwidthLimits = 1;
  76. if (event != NULL)
  77. {
  78. peer->state = ENET_PEER_STATE_CONNECTED;
  79. event->type = ENET_EVENT_TYPE_CONNECT;
  80. event->peer = peer;
  81. event->data = peer->eventData;
  82. }
  83. else
  84. enet_protocol_dispatch_state(host, peer,
  85. peer->state == ENET_PEER_STATE_CONNECTING ?
  86. ENET_PEER_STATE_CONNECTION_SUCCEEDED : ENET_PEER_STATE_CONNECTION_PENDING);
  87. }
  88. static void enet_protocol_notify_disconnect(ENetHost * host, ENetPeer * peer, ENetEvent * event)
  89. {
  90. if (peer->state >= ENET_PEER_STATE_CONNECTION_PENDING)
  91. host->recalculateBandwidthLimits = 1;
  92. if (peer->state != ENET_PEER_STATE_CONNECTING
  93. && peer->state < ENET_PEER_STATE_CONNECTION_SUCCEEDED)
  94. enet_peer_reset(peer);
  95. else if (event != NULL)
  96. {
  97. event->type = ENET_EVENT_TYPE_DISCONNECT;
  98. event->peer = peer;
  99. event->data = 0;
  100. enet_peer_reset(peer);
  101. }
  102. else
  103. {
  104. peer->eventData = 0;
  105. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  106. }
  107. }
  108. static void enet_protocol_remove_sent_unreliable_commands(ENetPeer * peer)
  109. {
  110. ENetOutgoingCommand * outgoingCommand;
  111. while (!enet_list_empty (& peer -> sentUnreliableCommands))
  112. {
  113. outgoingCommand =
  114. (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
  115. enet_list_remove(&outgoingCommand->outgoingCommandList);
  116. if (outgoingCommand->packet != NULL)
  117. {
  118. --outgoingCommand->packet->referenceCount;
  119. if (outgoingCommand->packet->referenceCount == 0)
  120. enet_packet_destroy(outgoingCommand->packet);
  121. }
  122. enet_free(outgoingCommand);
  123. }
  124. }
  125. static ENetProtocolCommand enet_protocol_remove_sent_reliable_command(ENetPeer * peer,
  126. enet_uint16 reliableSequenceNumber, enet_uint8 channelID)
  127. {
  128. ENetOutgoingCommand * outgoingCommand = NULL;
  129. ENetListIterator currentCommand;
  130. ENetProtocolCommand commandNumber;
  131. int wasSent = 1;
  132. for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  133. currentCommand != enet_list_end (& peer -> sentReliableCommands); currentCommand =
  134. enet_list_next (currentCommand))
  135. {
  136. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  137. if (outgoingCommand->reliableSequenceNumber == reliableSequenceNumber
  138. && outgoingCommand->command.header.channelID == channelID)
  139. break;
  140. }
  141. if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
  142. {
  143. for (currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  144. currentCommand != enet_list_end (& peer -> outgoingReliableCommands);
  145. currentCommand = enet_list_next (currentCommand))
  146. {
  147. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  148. if (outgoingCommand->sendAttempts < 1)
  149. return ENET_PROTOCOL_COMMAND_NONE;
  150. if (outgoingCommand->reliableSequenceNumber == reliableSequenceNumber
  151. && outgoingCommand->command.header.channelID == channelID)
  152. break;
  153. }
  154. if (currentCommand == enet_list_end (& peer -> outgoingReliableCommands))
  155. return ENET_PROTOCOL_COMMAND_NONE;
  156. wasSent = 0;
  157. }
  158. if (channelID < peer->channelCount)
  159. {
  160. ENetChannel * channel = &peer->channels[channelID];
  161. enet_uint16 reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  162. if (channel->reliableWindows[reliableWindow] > 0)
  163. {
  164. --channel->reliableWindows[reliableWindow];
  165. if (!channel->reliableWindows[reliableWindow])
  166. channel->usedReliableWindows &= ~(1 << reliableWindow);
  167. }
  168. }
  169. commandNumber = (ENetProtocolCommand) (outgoingCommand->command.header.command
  170. & ENET_PROTOCOL_COMMAND_MASK);
  171. enet_list_remove(&outgoingCommand->outgoingCommandList);
  172. if (outgoingCommand->packet != NULL)
  173. {
  174. if (wasSent)
  175. peer->reliableDataInTransit -= outgoingCommand->fragmentLength;
  176. --outgoingCommand->packet->referenceCount;
  177. if (outgoingCommand->packet->referenceCount == 0)
  178. enet_packet_destroy(outgoingCommand->packet);
  179. }
  180. enet_free(outgoingCommand);
  181. if (enet_list_empty (& peer -> sentReliableCommands))
  182. return commandNumber;
  183. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
  184. peer->nextTimeout = outgoingCommand->sentTime + outgoingCommand->roundTripTimeout;
  185. return commandNumber;
  186. }
  187. static ENetPeer *enet_protocol_handle_connect(ENetHost * host, ENetProtocolHeader * header, ENetProtocol * command)
  188. {
  189. enet_uint8 incomingSessionID, outgoingSessionID;
  190. enet_uint32 mtu, windowSize;
  191. ENetChannel * channel;
  192. size_t channelCount;
  193. ENetPeer * currentPeer;
  194. ENetProtocol verifyCommand;
  195. channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
  196. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT
  197. || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  198. return NULL;
  199. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer)
  200. {
  201. if (currentPeer->state != ENET_PEER_STATE_DISCONNECTED
  202. && currentPeer->address.host == host->receivedAddress.host
  203. && currentPeer->address.port == host->receivedAddress.port
  204. && currentPeer->connectID == command->connect.connectID)
  205. return NULL;
  206. }
  207. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer)
  208. {
  209. if (currentPeer->state == ENET_PEER_STATE_DISCONNECTED)
  210. break;
  211. }
  212. if (currentPeer >= &host->peers[host->peerCount])
  213. return NULL;
  214. if (channelCount > host->channelLimit)
  215. channelCount = host->channelLimit;
  216. currentPeer->channels = (ENetChannel *) enet_malloc(channelCount * sizeof(ENetChannel));
  217. if (currentPeer->channels == NULL)
  218. return NULL;
  219. currentPeer->channelCount = channelCount;
  220. currentPeer->state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
  221. currentPeer->connectID = command->connect.connectID;
  222. currentPeer->address = host->receivedAddress;
  223. currentPeer->outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
  224. currentPeer->incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
  225. currentPeer->outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
  226. currentPeer->packetThrottleInterval =
  227. ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
  228. currentPeer->packetThrottleAcceleration =
  229. ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
  230. currentPeer->packetThrottleDeceleration =
  231. ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
  232. currentPeer->eventData = ENET_NET_TO_HOST_32 (command -> connect.data);
  233. incomingSessionID =
  234. command->connect.incomingSessionID == 0xFF ?
  235. currentPeer->outgoingSessionID : command->connect.incomingSessionID;
  236. incomingSessionID = (incomingSessionID + 1)
  237. & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  238. if (incomingSessionID == currentPeer->outgoingSessionID)
  239. incomingSessionID = (incomingSessionID + 1)
  240. & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  241. currentPeer->outgoingSessionID = incomingSessionID;
  242. outgoingSessionID =
  243. command->connect.outgoingSessionID == 0xFF ?
  244. currentPeer->incomingSessionID : command->connect.outgoingSessionID;
  245. outgoingSessionID = (outgoingSessionID + 1)
  246. & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  247. if (outgoingSessionID == currentPeer->incomingSessionID)
  248. outgoingSessionID = (outgoingSessionID + 1)
  249. & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  250. currentPeer->incomingSessionID = outgoingSessionID;
  251. for (channel = currentPeer->channels; channel < &currentPeer->channels[channelCount]; ++channel)
  252. {
  253. channel->outgoingReliableSequenceNumber = 0;
  254. channel->outgoingUnreliableSequenceNumber = 0;
  255. channel->incomingReliableSequenceNumber = 0;
  256. channel->incomingUnreliableSequenceNumber = 0;
  257. enet_list_clear(&channel->incomingReliableCommands);
  258. enet_list_clear(&channel->incomingUnreliableCommands);
  259. channel->usedReliableWindows = 0;
  260. memset(channel->reliableWindows, 0, sizeof(channel->reliableWindows));
  261. }
  262. mtu = ENET_NET_TO_HOST_32 (command -> connect.mtu);
  263. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  264. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  265. else if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  266. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  267. currentPeer->mtu = mtu;
  268. if (host->outgoingBandwidth == 0 && currentPeer->incomingBandwidth == 0)
  269. currentPeer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  270. else if (host->outgoingBandwidth == 0 || currentPeer->incomingBandwidth == 0)
  271. currentPeer->windowSize = (
  272. ENET_MAX (host -> outgoingBandwidth, currentPeer -> incomingBandwidth)
  273. / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  274. else
  275. currentPeer->windowSize = (
  276. ENET_MIN (host -> outgoingBandwidth, currentPeer -> incomingBandwidth)
  277. / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  278. if (currentPeer->windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  279. currentPeer->windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  280. else if (currentPeer->windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  281. currentPeer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  282. if (host->incomingBandwidth == 0)
  283. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  284. else
  285. windowSize = (host->incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE)
  286. * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  287. if (windowSize > ENET_NET_TO_HOST_32 (command -> connect.windowSize))
  288. windowSize = ENET_NET_TO_HOST_32 (command -> connect.windowSize);
  289. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  290. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  291. else if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  292. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  293. verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT
  294. | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  295. verifyCommand.header.channelID = 0xFF;
  296. verifyCommand.verifyConnect.outgoingPeerID =
  297. ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  298. verifyCommand.verifyConnect.incomingSessionID = incomingSessionID;
  299. verifyCommand.verifyConnect.outgoingSessionID = outgoingSessionID;
  300. verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_16 (currentPeer -> mtu);
  301. verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
  302. verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  303. verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  304. verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  305. verifyCommand.verifyConnect.packetThrottleInterval =
  306. ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  307. verifyCommand.verifyConnect.packetThrottleAcceleration =
  308. ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  309. verifyCommand.verifyConnect.packetThrottleDeceleration =
  310. ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  311. verifyCommand.verifyConnect.connectID = currentPeer->connectID;
  312. enet_peer_queue_outgoing_command(currentPeer, &verifyCommand, NULL, 0, 0);
  313. return currentPeer;
  314. }
  315. static int enet_protocol_handle_send_reliable(ENetHost * host, ENetPeer * peer,
  316. const ENetProtocol * command, enet_uint8 ** currentData)
  317. {
  318. ENetPacket * packet;
  319. size_t dataLength;
  320. if (command->header.channelID >= peer->channelCount
  321. || (peer->state != ENET_PEER_STATE_CONNECTED
  322. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER))
  323. return -1;
  324. dataLength = ENET_NET_TO_HOST_16 (command -> sendReliable.dataLength);
  325. *currentData += dataLength;
  326. if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || *currentData < host->receivedData
  327. || *currentData > &host->receivedData[host->receivedDataLength])
  328. return -1;
  329. packet = enet_packet_create((const enet_uint8 *) command + sizeof(ENetProtocolSendReliable),
  330. dataLength, ENET_PACKET_FLAG_RELIABLE);
  331. if (packet == NULL || enet_peer_queue_incoming_command(peer, command, packet, 0) == NULL)
  332. return -1;
  333. return 0;
  334. }
  335. static int enet_protocol_handle_send_unsequenced(ENetHost * host, ENetPeer * peer,
  336. const ENetProtocol * command, enet_uint8 ** currentData)
  337. {
  338. ENetPacket * packet;
  339. enet_uint32 unsequencedGroup, index;
  340. size_t dataLength;
  341. if (command->header.channelID >= peer->channelCount
  342. || (peer->state != ENET_PEER_STATE_CONNECTED
  343. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER))
  344. return -1;
  345. dataLength = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.dataLength);
  346. *currentData += dataLength;
  347. if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || *currentData < host->receivedData
  348. || *currentData > &host->receivedData[host->receivedDataLength])
  349. return -1;
  350. unsequencedGroup = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.unsequencedGroup);
  351. index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
  352. if (unsequencedGroup < peer->incomingUnsequencedGroup)
  353. unsequencedGroup += 0x10000;
  354. if (unsequencedGroup
  355. >= (enet_uint32) peer->incomingUnsequencedGroup
  356. + ENET_PEER_FREE_UNSEQUENCED_WINDOWS * ENET_PEER_UNSEQUENCED_WINDOW_SIZE)
  357. return 0;
  358. unsequencedGroup &= 0xFFFF;
  359. if (unsequencedGroup - index != peer->incomingUnsequencedGroup)
  360. {
  361. peer->incomingUnsequencedGroup = unsequencedGroup - index;
  362. memset(peer->unsequencedWindow, 0, sizeof(peer->unsequencedWindow));
  363. }
  364. else if (peer->unsequencedWindow[index / 32] & (1 << (index % 32)))
  365. return 0;
  366. packet = enet_packet_create((const enet_uint8 *) command + sizeof(ENetProtocolSendUnsequenced),
  367. dataLength, ENET_PACKET_FLAG_UNSEQUENCED);
  368. if (packet == NULL || enet_peer_queue_incoming_command(peer, command, packet, 0) == NULL)
  369. return -1;
  370. peer->unsequencedWindow[index / 32] |= 1 << (index % 32);
  371. return 0;
  372. }
  373. static int enet_protocol_handle_send_unreliable(ENetHost * host, ENetPeer * peer,
  374. const ENetProtocol * command, enet_uint8 ** currentData)
  375. {
  376. ENetPacket * packet;
  377. size_t dataLength;
  378. if (command->header.channelID >= peer->channelCount
  379. || (peer->state != ENET_PEER_STATE_CONNECTED
  380. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER))
  381. return -1;
  382. dataLength = ENET_NET_TO_HOST_16 (command -> sendUnreliable.dataLength);
  383. *currentData += dataLength;
  384. if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || *currentData < host->receivedData
  385. || *currentData > &host->receivedData[host->receivedDataLength])
  386. return -1;
  387. packet = enet_packet_create((const enet_uint8 *) command + sizeof(ENetProtocolSendUnreliable),
  388. dataLength, 0);
  389. if (packet == NULL || enet_peer_queue_incoming_command(peer, command, packet, 0) == NULL)
  390. return -1;
  391. return 0;
  392. }
  393. static int enet_protocol_handle_send_fragment(ENetHost * host, ENetPeer * peer,
  394. const ENetProtocol * command, enet_uint8 ** currentData)
  395. {
  396. enet_uint32 fragmentNumber, fragmentCount, fragmentOffset, fragmentLength, startSequenceNumber,
  397. totalLength;
  398. ENetChannel * channel;
  399. enet_uint16 startWindow, currentWindow;
  400. ENetListIterator currentCommand;
  401. ENetIncomingCommand * startCommand = NULL;
  402. if (command->header.channelID >= peer->channelCount
  403. || (peer->state != ENET_PEER_STATE_CONNECTED
  404. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER))
  405. return -1;
  406. fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
  407. *currentData += fragmentLength;
  408. if (fragmentLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || *currentData < host->receivedData
  409. || *currentData > &host->receivedData[host->receivedDataLength])
  410. return -1;
  411. channel = &peer->channels[command->header.channelID];
  412. startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
  413. startWindow = startSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  414. currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  415. if (startSequenceNumber < channel->incomingReliableSequenceNumber)
  416. startWindow += ENET_PEER_RELIABLE_WINDOWS;
  417. if (startWindow < currentWindow
  418. || startWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  419. return 0;
  420. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  421. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  422. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  423. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  424. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT || fragmentNumber >= fragmentCount
  425. || totalLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || fragmentOffset >= totalLength
  426. || fragmentLength > totalLength - fragmentOffset)
  427. return -1;
  428. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  429. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  430. currentCommand = enet_list_previous (currentCommand))
  431. {
  432. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  433. if (startSequenceNumber >= channel->incomingReliableSequenceNumber)
  434. {
  435. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber)
  436. continue;
  437. }
  438. else if (incomingCommand->reliableSequenceNumber >= channel->incomingReliableSequenceNumber)
  439. break;
  440. if (incomingCommand->reliableSequenceNumber <= startSequenceNumber)
  441. {
  442. if (incomingCommand->reliableSequenceNumber < startSequenceNumber)
  443. break;
  444. if ((incomingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK)
  445. != ENET_PROTOCOL_COMMAND_SEND_FRAGMENT
  446. || totalLength != incomingCommand->packet->dataLength
  447. || fragmentCount != incomingCommand->fragmentCount)
  448. return -1;
  449. startCommand = incomingCommand;
  450. break;
  451. }
  452. }
  453. if (startCommand == NULL)
  454. {
  455. ENetProtocol hostCommand = *command;
  456. ENetPacket * packet = enet_packet_create(NULL, totalLength, ENET_PACKET_FLAG_RELIABLE);
  457. if (packet == NULL)
  458. return -1;
  459. hostCommand.header.reliableSequenceNumber = startSequenceNumber;
  460. startCommand = enet_peer_queue_incoming_command(peer, &hostCommand, packet, fragmentCount);
  461. if (startCommand == NULL)
  462. return -1;
  463. }
  464. if ((startCommand->fragments[fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
  465. {
  466. --startCommand->fragmentsRemaining;
  467. startCommand->fragments[fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  468. if (fragmentOffset + fragmentLength > startCommand->packet->dataLength)
  469. fragmentLength = startCommand->packet->dataLength - fragmentOffset;
  470. memcpy(startCommand->packet->data + fragmentOffset,
  471. (enet_uint8 *) command + sizeof(ENetProtocolSendFragment), fragmentLength);
  472. if (startCommand->fragmentsRemaining <= 0)
  473. enet_peer_dispatch_incoming_reliable_commands(peer, channel);
  474. }
  475. return 0;
  476. }
  477. static int enet_protocol_handle_send_unreliable_fragment(ENetHost * host, ENetPeer * peer,
  478. const ENetProtocol * command, enet_uint8 ** currentData)
  479. {
  480. enet_uint32 fragmentNumber, fragmentCount, fragmentOffset, fragmentLength,
  481. reliableSequenceNumber, startSequenceNumber, totalLength;
  482. enet_uint16 reliableWindow, currentWindow;
  483. ENetChannel * channel;
  484. ENetListIterator currentCommand;
  485. ENetIncomingCommand * startCommand = NULL;
  486. if (command->header.channelID >= peer->channelCount
  487. || (peer->state != ENET_PEER_STATE_CONNECTED
  488. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER))
  489. return -1;
  490. fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
  491. *currentData += fragmentLength;
  492. if (fragmentLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || *currentData < host->receivedData
  493. || *currentData > &host->receivedData[host->receivedDataLength])
  494. return -1;
  495. channel = &peer->channels[command->header.channelID];
  496. reliableSequenceNumber = command->header.reliableSequenceNumber;
  497. startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
  498. reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  499. currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  500. if (reliableSequenceNumber < channel->incomingReliableSequenceNumber)
  501. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  502. if (reliableWindow < currentWindow
  503. || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  504. return 0;
  505. if (reliableSequenceNumber == channel->incomingReliableSequenceNumber
  506. && startSequenceNumber <= channel->incomingUnreliableSequenceNumber)
  507. return 0;
  508. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  509. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  510. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  511. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  512. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT || fragmentNumber >= fragmentCount
  513. || totalLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE || fragmentOffset >= totalLength
  514. || fragmentLength > totalLength - fragmentOffset)
  515. return -1;
  516. for (currentCommand =
  517. enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
  518. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  519. currentCommand = enet_list_previous (currentCommand))
  520. {
  521. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  522. if (reliableSequenceNumber >= channel->incomingReliableSequenceNumber)
  523. {
  524. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber)
  525. continue;
  526. }
  527. else if (incomingCommand->reliableSequenceNumber >= channel->incomingReliableSequenceNumber)
  528. break;
  529. if (incomingCommand->reliableSequenceNumber < reliableSequenceNumber)
  530. break;
  531. if (incomingCommand->reliableSequenceNumber > reliableSequenceNumber)
  532. continue;
  533. if (incomingCommand->unreliableSequenceNumber <= startSequenceNumber)
  534. {
  535. if (incomingCommand->unreliableSequenceNumber < startSequenceNumber)
  536. break;
  537. if ((incomingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK)
  538. != ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT
  539. || totalLength != incomingCommand->packet->dataLength
  540. || fragmentCount != incomingCommand->fragmentCount)
  541. return -1;
  542. startCommand = incomingCommand;
  543. break;
  544. }
  545. }
  546. if (startCommand == NULL)
  547. {
  548. ENetPacket * packet = enet_packet_create(NULL, totalLength,
  549. ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT);
  550. if (packet == NULL)
  551. return -1;
  552. startCommand = enet_peer_queue_incoming_command(peer, command, packet, fragmentCount);
  553. if (startCommand == NULL)
  554. return -1;
  555. }
  556. if ((startCommand->fragments[fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
  557. {
  558. --startCommand->fragmentsRemaining;
  559. startCommand->fragments[fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  560. if (fragmentOffset + fragmentLength > startCommand->packet->dataLength)
  561. fragmentLength = startCommand->packet->dataLength - fragmentOffset;
  562. memcpy(startCommand->packet->data + fragmentOffset,
  563. (enet_uint8 *) command + sizeof(ENetProtocolSendFragment), fragmentLength);
  564. if (startCommand->fragmentsRemaining <= 0)
  565. enet_peer_dispatch_incoming_unreliable_commands(peer, channel);
  566. }
  567. return 0;
  568. }
  569. static int enet_protocol_handle_ping(ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  570. {
  571. return 0;
  572. }
  573. static int enet_protocol_handle_bandwidth_limit(ENetHost * host, ENetPeer * peer,
  574. const ENetProtocol * command)
  575. {
  576. peer->incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
  577. peer->outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
  578. if (peer->incomingBandwidth == 0 && host->outgoingBandwidth == 0)
  579. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  580. else
  581. peer->windowSize = (ENET_MIN (peer -> incomingBandwidth, host -> outgoingBandwidth)
  582. / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  583. if (peer->windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  584. peer->windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  585. else if (peer->windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  586. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  587. return 0;
  588. }
  589. static int enet_protocol_handle_throttle_configure(ENetHost * host, ENetPeer * peer,
  590. const ENetProtocol * command)
  591. {
  592. peer->packetThrottleInterval =
  593. ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
  594. peer->packetThrottleAcceleration =
  595. ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
  596. peer->packetThrottleDeceleration =
  597. ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
  598. return 0;
  599. }
  600. static int enet_protocol_handle_disconnect(ENetHost * host, ENetPeer * peer,
  601. const ENetProtocol * command)
  602. {
  603. if (peer->state == ENET_PEER_STATE_ZOMBIE
  604. || peer->state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT)
  605. return 0;
  606. enet_peer_reset_queues(peer);
  607. if (peer->state == ENET_PEER_STATE_CONNECTION_SUCCEEDED
  608. || peer->state == ENET_PEER_STATE_DISCONNECTING)
  609. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  610. else if (peer->state != ENET_PEER_STATE_CONNECTED
  611. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
  612. {
  613. if (peer->state == ENET_PEER_STATE_CONNECTION_PENDING)
  614. host->recalculateBandwidthLimits = 1;
  615. enet_peer_reset(peer);
  616. }
  617. else if (command->header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  618. peer->state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
  619. else
  620. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  621. if (peer->state != ENET_PEER_STATE_DISCONNECTED)
  622. peer->eventData = ENET_NET_TO_HOST_32 (command -> disconnect.data);
  623. return 0;
  624. }
  625. static int enet_protocol_handle_acknowledge(ENetHost * host, ENetEvent * event, ENetPeer * peer,
  626. const ENetProtocol * command)
  627. {
  628. enet_uint32 roundTripTime, receivedSentTime, receivedReliableSequenceNumber;
  629. ENetProtocolCommand commandNumber;
  630. receivedSentTime = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedSentTime);
  631. receivedSentTime |= host->serviceTime & 0xFFFF0000;
  632. if ((receivedSentTime & 0x8000) > (host->serviceTime & 0x8000))
  633. receivedSentTime -= 0x10000;
  634. if (ENET_TIME_LESS (host -> serviceTime, receivedSentTime))
  635. return 0;
  636. peer->lastReceiveTime = host->serviceTime;
  637. peer->earliestTimeout = 0;
  638. roundTripTime = ENET_TIME_DIFFERENCE (host -> serviceTime, receivedSentTime);
  639. enet_peer_throttle(peer, roundTripTime);
  640. peer->roundTripTimeVariance -= peer->roundTripTimeVariance / 4;
  641. if (roundTripTime >= peer->roundTripTime)
  642. {
  643. peer->roundTripTime += (roundTripTime - peer->roundTripTime) / 8;
  644. peer->roundTripTimeVariance += (roundTripTime - peer->roundTripTime) / 4;
  645. }
  646. else
  647. {
  648. peer->roundTripTime -= (peer->roundTripTime - roundTripTime) / 8;
  649. peer->roundTripTimeVariance += (peer->roundTripTime - roundTripTime) / 4;
  650. }
  651. if (peer->roundTripTime < peer->lowestRoundTripTime)
  652. peer->lowestRoundTripTime = peer->roundTripTime;
  653. if (peer->roundTripTimeVariance > peer->highestRoundTripTimeVariance)
  654. peer->highestRoundTripTimeVariance = peer->roundTripTimeVariance;
  655. if (peer->packetThrottleEpoch == 0
  656. || ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> packetThrottleEpoch)
  657. >= peer->packetThrottleInterval)
  658. {
  659. peer->lastRoundTripTime = peer->lowestRoundTripTime;
  660. peer->lastRoundTripTimeVariance = peer->highestRoundTripTimeVariance;
  661. peer->lowestRoundTripTime = peer->roundTripTime;
  662. peer->highestRoundTripTimeVariance = peer->roundTripTimeVariance;
  663. peer->packetThrottleEpoch = host->serviceTime;
  664. }
  665. receivedReliableSequenceNumber =
  666. ENET_NET_TO_HOST_16 (command -> acknowledge.receivedReliableSequenceNumber);
  667. commandNumber = enet_protocol_remove_sent_reliable_command(peer, receivedReliableSequenceNumber,
  668. command->header.channelID);
  669. switch (peer->state)
  670. {
  671. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  672. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
  673. return -1;
  674. enet_protocol_notify_connect(host, peer, event);
  675. break;
  676. case ENET_PEER_STATE_DISCONNECTING:
  677. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
  678. return -1;
  679. enet_protocol_notify_disconnect(host, peer, event);
  680. break;
  681. case ENET_PEER_STATE_DISCONNECT_LATER:
  682. if (enet_list_empty (& peer -> outgoingReliableCommands)
  683. && enet_list_empty (& peer -> outgoingUnreliableCommands)
  684. && enet_list_empty (& peer -> sentReliableCommands))
  685. enet_peer_disconnect(peer, peer->eventData);
  686. break;
  687. default:
  688. break;
  689. }
  690. return 0;
  691. }
  692. static int enet_protocol_handle_verify_connect(ENetHost * host, ENetEvent * event, ENetPeer * peer,
  693. const ENetProtocol * command)
  694. {
  695. enet_uint32 mtu, windowSize;
  696. size_t channelCount;
  697. if (peer->state != ENET_PEER_STATE_CONNECTING)
  698. return 0;
  699. channelCount = ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount);
  700. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT
  701. || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
  702. || ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval)
  703. != peer->packetThrottleInterval
  704. || ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration)
  705. != peer->packetThrottleAcceleration
  706. || ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration)
  707. != peer->packetThrottleDeceleration
  708. || command->verifyConnect.connectID != peer->connectID)
  709. {
  710. peer->eventData = 0;
  711. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  712. return -1;
  713. }
  714. enet_protocol_remove_sent_reliable_command(peer, 1, 0xFF);
  715. if (channelCount < peer->channelCount)
  716. peer->channelCount = channelCount;
  717. peer->outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
  718. peer->incomingSessionID = command->verifyConnect.incomingSessionID;
  719. peer->outgoingSessionID = command->verifyConnect.outgoingSessionID;
  720. mtu = ENET_NET_TO_HOST_32 (command -> verifyConnect.mtu);
  721. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  722. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  723. else if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  724. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  725. if (mtu < peer->mtu)
  726. peer->mtu = mtu;
  727. windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
  728. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  729. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  730. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  731. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  732. if (windowSize < peer->windowSize)
  733. peer->windowSize = windowSize;
  734. peer->incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
  735. peer->outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
  736. enet_protocol_notify_connect(host, peer, event);
  737. return 0;
  738. }
  739. static int enet_protocol_handle_incoming_commands(ENetHost * host, ENetEvent * event)
  740. {
  741. ENetProtocolHeader * header;
  742. ENetProtocol * command;
  743. ENetPeer * peer;
  744. enet_uint8 * currentData;
  745. size_t headerSize;
  746. enet_uint16 peerID, flags;
  747. enet_uint8 sessionID;
  748. if (host->receivedDataLength < (size_t) & ((ENetProtocolHeader *) 0)->sentTime)
  749. return 0;
  750. header = (ENetProtocolHeader *) host->receivedData;
  751. peerID = ENET_NET_TO_HOST_16 (header -> peerID);
  752. sessionID = (peerID & ENET_PROTOCOL_HEADER_SESSION_MASK) >> ENET_PROTOCOL_HEADER_SESSION_SHIFT;
  753. flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
  754. peerID &= ~(ENET_PROTOCOL_HEADER_FLAG_MASK | ENET_PROTOCOL_HEADER_SESSION_MASK);
  755. headerSize = (
  756. flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ?
  757. sizeof(ENetProtocolHeader) : (size_t) & ((ENetProtocolHeader *) 0)->sentTime);
  758. if (host->checksum != NULL)
  759. headerSize += sizeof(enet_uint32);
  760. if (peerID == ENET_PROTOCOL_MAXIMUM_PEER_ID)
  761. peer = NULL;
  762. else if (peerID >= host->peerCount)
  763. return 0;
  764. else
  765. {
  766. peer = &host->peers[peerID];
  767. if (peer->state == ENET_PEER_STATE_DISCONNECTED || peer->state == ENET_PEER_STATE_ZOMBIE
  768. || ((host->receivedAddress.host != peer->address.host
  769. || host->receivedAddress.port != peer->address.port)
  770. && peer->address.host != ENET_HOST_BROADCAST)
  771. || (peer->outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID
  772. && sessionID != peer->incomingSessionID))
  773. return 0;
  774. }
  775. if (flags & ENET_PROTOCOL_HEADER_FLAG_COMPRESSED)
  776. {
  777. size_t originalSize;
  778. if (host->compressor.context == NULL || host->compressor.decompress == NULL)
  779. return 0;
  780. originalSize = host->compressor.decompress(host->compressor.context,
  781. host->receivedData + headerSize, host->receivedDataLength - headerSize,
  782. host->packetData[1] + headerSize, sizeof(host->packetData[1]) - headerSize);
  783. if (originalSize <= 0 || originalSize > sizeof(host->packetData[1]) - headerSize)
  784. return 0;
  785. memcpy(host->packetData[1], header, headerSize);
  786. host->receivedData = host->packetData[1];
  787. host->receivedDataLength = headerSize + originalSize;
  788. }
  789. if (host->checksum != NULL)
  790. {
  791. enet_uint32 * checksum = (enet_uint32 *) &host->receivedData[headerSize
  792. - sizeof(enet_uint32)], desiredChecksum = *checksum;
  793. ENetBuffer buffer;
  794. *checksum = peer != NULL ? peer->connectID : 0;
  795. buffer.data = host->receivedData;
  796. buffer.dataLength = host->receivedDataLength;
  797. if (host->checksum(&buffer, 1) != desiredChecksum)
  798. return 0;
  799. }
  800. if (peer != NULL)
  801. {
  802. peer->address.host = host->receivedAddress.host;
  803. peer->address.port = host->receivedAddress.port;
  804. peer->incomingDataTotal += host->receivedDataLength;
  805. }
  806. currentData = host->receivedData + headerSize;
  807. while (currentData < &host->receivedData[host->receivedDataLength])
  808. {
  809. enet_uint8 commandNumber;
  810. size_t commandSize;
  811. command = (ENetProtocol *) currentData;
  812. if (currentData + sizeof(ENetProtocolCommandHeader)
  813. > &host->receivedData[host->receivedDataLength])
  814. break;
  815. commandNumber = command->header.command & ENET_PROTOCOL_COMMAND_MASK;
  816. if (commandNumber >= ENET_PROTOCOL_COMMAND_COUNT)
  817. break;
  818. commandSize = commandSizes[commandNumber];
  819. if (commandSize == 0
  820. || currentData + commandSize > &host->receivedData[host->receivedDataLength])
  821. break;
  822. currentData += commandSize;
  823. if (peer == NULL && commandNumber != ENET_PROTOCOL_COMMAND_CONNECT)
  824. break;
  825. command->header.reliableSequenceNumber =
  826. ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
  827. switch (command->header.command & ENET_PROTOCOL_COMMAND_MASK)
  828. {
  829. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  830. if (enet_protocol_handle_acknowledge(host, event, peer, command))
  831. goto commandError;
  832. break;
  833. case ENET_PROTOCOL_COMMAND_CONNECT:
  834. peer = enet_protocol_handle_connect(host, header, command);
  835. if (peer == NULL)
  836. goto commandError;
  837. break;
  838. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  839. if (enet_protocol_handle_verify_connect(host, event, peer, command))
  840. goto commandError;
  841. break;
  842. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  843. if (enet_protocol_handle_disconnect(host, peer, command))
  844. goto commandError;
  845. break;
  846. case ENET_PROTOCOL_COMMAND_PING:
  847. if (enet_protocol_handle_ping(host, peer, command))
  848. goto commandError;
  849. break;
  850. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  851. if (enet_protocol_handle_send_reliable(host, peer, command, &currentData))
  852. goto commandError;
  853. break;
  854. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  855. if (enet_protocol_handle_send_unreliable(host, peer, command, &currentData))
  856. goto commandError;
  857. break;
  858. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  859. if (enet_protocol_handle_send_unsequenced(host, peer, command, &currentData))
  860. goto commandError;
  861. break;
  862. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  863. if (enet_protocol_handle_send_fragment(host, peer, command, &currentData))
  864. goto commandError;
  865. break;
  866. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  867. if (enet_protocol_handle_bandwidth_limit(host, peer, command))
  868. goto commandError;
  869. break;
  870. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  871. if (enet_protocol_handle_throttle_configure(host, peer, command))
  872. goto commandError;
  873. break;
  874. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
  875. if (enet_protocol_handle_send_unreliable_fragment(host, peer, command,
  876. &currentData))
  877. goto commandError;
  878. break;
  879. default:
  880. goto commandError;
  881. }
  882. if (peer != NULL && (command->header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
  883. {
  884. enet_uint16 sentTime;
  885. if (!(flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME))
  886. break;
  887. sentTime = ENET_NET_TO_HOST_16 (header -> sentTime);
  888. switch (peer->state)
  889. {
  890. case ENET_PEER_STATE_DISCONNECTING:
  891. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  892. break;
  893. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  894. if ((command->header.command & ENET_PROTOCOL_COMMAND_MASK)
  895. == ENET_PROTOCOL_COMMAND_DISCONNECT)
  896. enet_peer_queue_acknowledgement(peer, command, sentTime);
  897. break;
  898. default:
  899. enet_peer_queue_acknowledgement(peer, command, sentTime);
  900. break;
  901. }
  902. }
  903. }
  904. commandError: if (event != NULL && event->type != ENET_EVENT_TYPE_NONE)
  905. return 1;
  906. return 0;
  907. }
  908. static int enet_protocol_receive_incoming_commands(ENetHost * host, ENetEvent * event)
  909. {
  910. for (;;)
  911. {
  912. int receivedLength;
  913. ENetBuffer buffer;
  914. buffer.data = host->packetData[0];
  915. buffer.dataLength = sizeof(host->packetData[0]);
  916. receivedLength = enet_socket_receive(host->socket, &host->receivedAddress, &buffer, 1);
  917. if (receivedLength < 0)
  918. return -1;
  919. if (receivedLength == 0)
  920. return 0;
  921. host->receivedData = host->packetData[0];
  922. host->receivedDataLength = receivedLength;
  923. host->totalReceivedData += receivedLength;
  924. host->totalReceivedPackets++;
  925. switch (enet_protocol_handle_incoming_commands(host, event))
  926. {
  927. case 1:
  928. return 1;
  929. case -1:
  930. return -1;
  931. default:
  932. break;
  933. }
  934. }
  935. return -1;
  936. }
  937. static void enet_protocol_send_acknowledgements(ENetHost * host, ENetPeer * peer)
  938. {
  939. ENetProtocol * command = &host->commands[host->commandCount];
  940. ENetBuffer * buffer = &host->buffers[host->bufferCount];
  941. ENetAcknowledgement * acknowledgement;
  942. ENetListIterator currentAcknowledgement;
  943. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  944. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  945. {
  946. if (command >= &host->commands[sizeof(host->commands) / sizeof(ENetProtocol)]
  947. || buffer >= &host->buffers[sizeof(host->buffers) / sizeof(ENetBuffer)]
  948. || peer->mtu - host->packetSize < sizeof(ENetProtocolAcknowledge))
  949. {
  950. host->continueSending = 1;
  951. break;
  952. }
  953. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  954. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  955. buffer->data = command;
  956. buffer->dataLength = sizeof(ENetProtocolAcknowledge);
  957. host->packetSize += buffer->dataLength;
  958. command->header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  959. command->header.channelID = acknowledgement->command.header.channelID;
  960. command->acknowledge.receivedReliableSequenceNumber =
  961. ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
  962. command->acknowledge.receivedSentTime = ENET_HOST_TO_NET_16 (acknowledgement -> sentTime);
  963. if ((acknowledgement->command.header.command & ENET_PROTOCOL_COMMAND_MASK)
  964. == ENET_PROTOCOL_COMMAND_DISCONNECT)
  965. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  966. enet_list_remove(&acknowledgement->acknowledgementList);
  967. enet_free(acknowledgement);
  968. ++command;
  969. ++buffer;
  970. }
  971. host->commandCount = command - host->commands;
  972. host->bufferCount = buffer - host->buffers;
  973. }
  974. static void enet_protocol_send_unreliable_outgoing_commands(ENetHost * host, ENetPeer * peer)
  975. {
  976. ENetProtocol * command = &host->commands[host->commandCount];
  977. ENetBuffer * buffer = &host->buffers[host->bufferCount];
  978. ENetOutgoingCommand * outgoingCommand;
  979. ENetListIterator currentCommand;
  980. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  981. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  982. {
  983. size_t commandSize;
  984. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  985. commandSize = commandSizes[outgoingCommand->command.header.command
  986. & ENET_PROTOCOL_COMMAND_MASK];
  987. if (command >= &host->commands[sizeof(host->commands) / sizeof(ENetProtocol)]
  988. || buffer + 1 >= &host->buffers[sizeof(host->buffers) / sizeof(ENetBuffer)]
  989. || peer->mtu - host->packetSize < commandSize
  990. || (outgoingCommand->packet != NULL
  991. && peer->mtu - host->packetSize
  992. < commandSize + outgoingCommand->fragmentLength))
  993. {
  994. host->continueSending = 1;
  995. break;
  996. }
  997. currentCommand = enet_list_next (currentCommand);
  998. if (outgoingCommand->packet != NULL && outgoingCommand->fragmentOffset == 0)
  999. {
  1000. peer->packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  1001. peer->packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  1002. if (peer->packetThrottleCounter > peer->packetThrottle)
  1003. {
  1004. enet_uint16 reliableSequenceNumber = outgoingCommand->reliableSequenceNumber,
  1005. unreliableSequenceNumber = outgoingCommand->unreliableSequenceNumber;
  1006. for (;;)
  1007. {
  1008. --outgoingCommand->packet->referenceCount;
  1009. if (outgoingCommand->packet->referenceCount == 0)
  1010. enet_packet_destroy(outgoingCommand->packet);
  1011. enet_list_remove(&outgoingCommand->outgoingCommandList);
  1012. enet_free(outgoingCommand);
  1013. if (currentCommand == enet_list_end (& peer -> outgoingUnreliableCommands))
  1014. break;
  1015. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1016. if (outgoingCommand->reliableSequenceNumber != reliableSequenceNumber
  1017. || outgoingCommand->unreliableSequenceNumber
  1018. != unreliableSequenceNumber)
  1019. break;
  1020. currentCommand = enet_list_next (currentCommand);
  1021. }
  1022. continue;
  1023. }
  1024. }
  1025. buffer->data = command;
  1026. buffer->dataLength = commandSize;
  1027. host->packetSize += buffer->dataLength;
  1028. *command = outgoingCommand->command;
  1029. enet_list_remove(&outgoingCommand->outgoingCommandList);
  1030. if (outgoingCommand->packet != NULL)
  1031. {
  1032. ++buffer;
  1033. buffer->data = outgoingCommand->packet->data + outgoingCommand->fragmentOffset;
  1034. buffer->dataLength = outgoingCommand->fragmentLength;
  1035. host->packetSize += buffer->dataLength;
  1036. enet_list_insert(enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  1037. }
  1038. else
  1039. enet_free(outgoingCommand);
  1040. ++command;
  1041. ++buffer;
  1042. }
  1043. host->commandCount = command - host->commands;
  1044. host->bufferCount = buffer - host->buffers;
  1045. if (peer->state == ENET_PEER_STATE_DISCONNECT_LATER&&
  1046. enet_list_empty (& peer -> outgoingReliableCommands) &&
  1047. enet_list_empty (& peer -> outgoingUnreliableCommands) &&
  1048. enet_list_empty (& peer -> sentReliableCommands))
  1049. enet_peer_disconnect(peer, peer->eventData);
  1050. }
  1051. static int enet_protocol_check_timeouts(ENetHost * host, ENetPeer * peer, ENetEvent * event)
  1052. {
  1053. ENetOutgoingCommand * outgoingCommand;
  1054. ENetListIterator currentCommand, insertPosition;
  1055. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  1056. insertPosition = enet_list_begin (& peer -> outgoingReliableCommands);
  1057. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  1058. {
  1059. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1060. currentCommand = enet_list_next (currentCommand);
  1061. if (ENET_TIME_DIFFERENCE (host -> serviceTime, outgoingCommand -> sentTime)
  1062. < outgoingCommand->roundTripTimeout)
  1063. continue;
  1064. if (peer->earliestTimeout == 0||
  1065. ENET_TIME_LESS (outgoingCommand -> sentTime, peer -> earliestTimeout))
  1066. peer->earliestTimeout = outgoingCommand->sentTime;
  1067. if (peer->earliestTimeout != 0
  1068. && (ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout)
  1069. >= peer->timeoutMaximum
  1070. || (outgoingCommand->roundTripTimeout
  1071. >= outgoingCommand->roundTripTimeoutLimit
  1072. && ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout)
  1073. >= peer->timeoutMinimum)))
  1074. {
  1075. enet_protocol_notify_disconnect(host, peer, event);
  1076. return 1;
  1077. }
  1078. if (outgoingCommand->packet != NULL)
  1079. peer->reliableDataInTransit -= outgoingCommand->fragmentLength;
  1080. ++peer->packetsLost;
  1081. outgoingCommand->roundTripTimeout *= 2;
  1082. enet_list_insert(insertPosition, enet_list_remove(&outgoingCommand->outgoingCommandList));
  1083. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands)
  1084. && !enet_list_empty (& peer -> sentReliableCommands))
  1085. {
  1086. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1087. peer->nextTimeout = outgoingCommand->sentTime + outgoingCommand->roundTripTimeout;
  1088. }
  1089. }
  1090. return 0;
  1091. }
  1092. static int enet_protocol_send_reliable_outgoing_commands(ENetHost * host, ENetPeer * peer)
  1093. {
  1094. ENetProtocol * command = &host->commands[host->commandCount];
  1095. ENetBuffer * buffer = &host->buffers[host->bufferCount];
  1096. ENetOutgoingCommand * outgoingCommand;
  1097. ENetListIterator currentCommand;
  1098. ENetChannel *channel;
  1099. enet_uint16 reliableWindow;
  1100. size_t commandSize;
  1101. int windowExceeded = 0, windowWrap = 0, canPing = 1;
  1102. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  1103. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  1104. {
  1105. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1106. channel =
  1107. outgoingCommand->command.header.channelID < peer->channelCount ?
  1108. &peer->channels[outgoingCommand->command.header.channelID] : NULL;
  1109. reliableWindow = outgoingCommand->reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1110. if (channel != NULL)
  1111. {
  1112. if (!windowWrap && outgoingCommand->sendAttempts < 1
  1113. && !(outgoingCommand->reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE)
  1114. && (channel->reliableWindows[(reliableWindow + ENET_PEER_RELIABLE_WINDOWS - 1)
  1115. % ENET_PEER_RELIABLE_WINDOWS] >= ENET_PEER_RELIABLE_WINDOW_SIZE
  1116. || channel->usedReliableWindows
  1117. & ((((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1)
  1118. << reliableWindow)
  1119. | (((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1)
  1120. >> (ENET_PEER_RELIABLE_WINDOW_SIZE
  1121. - reliableWindow)))))
  1122. windowWrap = 1;
  1123. if (windowWrap)
  1124. {
  1125. currentCommand = enet_list_next (currentCommand);
  1126. continue;
  1127. }
  1128. }
  1129. if (outgoingCommand->packet != NULL)
  1130. {
  1131. if (!windowExceeded)
  1132. {
  1133. enet_uint32 windowSize = (peer->packetThrottle * peer->windowSize)
  1134. / ENET_PEER_PACKET_THROTTLE_SCALE;
  1135. if (peer->reliableDataInTransit
  1136. + outgoingCommand->fragmentLength> ENET_MAX (windowSize, peer -> mtu))
  1137. windowExceeded = 1;
  1138. }
  1139. if (windowExceeded)
  1140. {
  1141. currentCommand = enet_list_next (currentCommand);
  1142. continue;
  1143. }
  1144. }
  1145. canPing = 0;
  1146. commandSize = commandSizes[outgoingCommand->command.header.command
  1147. & ENET_PROTOCOL_COMMAND_MASK];
  1148. if (command >= &host->commands[sizeof(host->commands) / sizeof(ENetProtocol)]
  1149. || buffer + 1 >= &host->buffers[sizeof(host->buffers) / sizeof(ENetBuffer)]
  1150. || peer->mtu - host->packetSize < commandSize
  1151. || (outgoingCommand->packet != NULL
  1152. && (enet_uint16) (peer->mtu - host->packetSize)
  1153. < (enet_uint16) (commandSize + outgoingCommand->fragmentLength)))
  1154. {
  1155. host->continueSending = 1;
  1156. break;
  1157. }
  1158. currentCommand = enet_list_next (currentCommand);
  1159. if (channel != NULL && outgoingCommand->sendAttempts < 1)
  1160. {
  1161. channel->usedReliableWindows |= 1 << reliableWindow;
  1162. ++channel->reliableWindows[reliableWindow];
  1163. }
  1164. ++outgoingCommand->sendAttempts;
  1165. if (outgoingCommand->roundTripTimeout == 0)
  1166. {
  1167. outgoingCommand->roundTripTimeout = peer->roundTripTime
  1168. + 4 * peer->roundTripTimeVariance;
  1169. outgoingCommand->roundTripTimeoutLimit = peer->timeoutLimit
  1170. * outgoingCommand->roundTripTimeout;
  1171. }
  1172. if (enet_list_empty (& peer -> sentReliableCommands))
  1173. peer->nextTimeout = host->serviceTime + outgoingCommand->roundTripTimeout;
  1174. enet_list_insert(enet_list_end (& peer -> sentReliableCommands),
  1175. enet_list_remove(&outgoingCommand->outgoingCommandList));
  1176. outgoingCommand->sentTime = host->serviceTime;
  1177. buffer->data = command;
  1178. buffer->dataLength = commandSize;
  1179. host->packetSize += buffer->dataLength;
  1180. host->headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
  1181. *command = outgoingCommand->command;
  1182. if (outgoingCommand->packet != NULL)
  1183. {
  1184. ++buffer;
  1185. buffer->data = outgoingCommand->packet->data + outgoingCommand->fragmentOffset;
  1186. buffer->dataLength = outgoingCommand->fragmentLength;
  1187. host->packetSize += outgoingCommand->fragmentLength;
  1188. peer->reliableDataInTransit += outgoingCommand->fragmentLength;
  1189. }
  1190. ++peer->packetsSent;
  1191. ++command;
  1192. ++buffer;
  1193. }
  1194. host->commandCount = command - host->commands;
  1195. host->bufferCount = buffer - host->buffers;
  1196. return canPing;
  1197. }
  1198. static int enet_protocol_send_outgoing_commands(ENetHost * host, ENetEvent * event,
  1199. int checkForTimeouts)
  1200. {
  1201. enet_uint8 headerData[sizeof(ENetProtocolHeader) + sizeof(enet_uint32)];
  1202. ENetProtocolHeader * header = (ENetProtocolHeader *) headerData;
  1203. ENetPeer * currentPeer;
  1204. int sentLength;
  1205. size_t shouldCompress = 0;
  1206. host->continueSending = 1;
  1207. while (host->continueSending)
  1208. for (host->continueSending = 0, currentPeer = host->peers;
  1209. currentPeer < &host->peers[host->peerCount]; ++currentPeer)
  1210. {
  1211. if (currentPeer->state == ENET_PEER_STATE_DISCONNECTED
  1212. || currentPeer->state == ENET_PEER_STATE_ZOMBIE)
  1213. continue;
  1214. host->headerFlags = 0;
  1215. host->commandCount = 0;
  1216. host->bufferCount = 1;
  1217. host->packetSize = sizeof(ENetProtocolHeader);
  1218. if (!enet_list_empty (& currentPeer -> acknowledgements))
  1219. enet_protocol_send_acknowledgements(host, currentPeer);
  1220. if (checkForTimeouts != 0 && !enet_list_empty (& currentPeer -> sentReliableCommands)
  1221. && ENET_TIME_GREATER_EQUAL (host -> serviceTime, currentPeer -> nextTimeout)
  1222. && enet_protocol_check_timeouts(host, currentPeer, event) == 1)
  1223. {
  1224. if (event != NULL && event->type != ENET_EVENT_TYPE_NONE)
  1225. return 1;
  1226. else
  1227. continue;
  1228. }
  1229. if ((enet_list_empty (& currentPeer -> outgoingReliableCommands)
  1230. || enet_protocol_send_reliable_outgoing_commands(host, currentPeer))
  1231. && enet_list_empty (& currentPeer -> sentReliableCommands)
  1232. && ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime)
  1233. >= currentPeer->pingInterval
  1234. && currentPeer->mtu - host->packetSize >= sizeof(ENetProtocolPing))
  1235. {
  1236. enet_peer_ping(currentPeer);
  1237. enet_protocol_send_reliable_outgoing_commands(host, currentPeer);
  1238. }
  1239. if (!enet_list_empty (& currentPeer -> outgoingUnreliableCommands))
  1240. enet_protocol_send_unreliable_outgoing_commands(host, currentPeer);
  1241. if (host->commandCount == 0)
  1242. continue;
  1243. if (currentPeer->packetLossEpoch == 0)
  1244. currentPeer->packetLossEpoch = host->serviceTime;
  1245. else if (ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> packetLossEpoch)
  1246. >= ENET_PEER_PACKET_LOSS_INTERVAL && currentPeer->packetsSent > 0)
  1247. {
  1248. enet_uint32 packetLoss = currentPeer->packetsLost * ENET_PEER_PACKET_LOSS_SCALE
  1249. / currentPeer->packetsSent;
  1250. #ifdef ENET_DEBUG
  1251. #ifdef WIN32
  1252. printf (
  1253. #else
  1254. fprintf (stderr,
  1255. #endif
  1256. "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);
  1257. #endif
  1258. currentPeer->packetLossVariance -= currentPeer->packetLossVariance / 4;
  1259. if (packetLoss >= currentPeer->packetLoss)
  1260. {
  1261. currentPeer->packetLoss += (packetLoss - currentPeer->packetLoss) / 8;
  1262. currentPeer->packetLossVariance += (packetLoss - currentPeer->packetLoss) / 4;
  1263. }
  1264. else
  1265. {
  1266. currentPeer->packetLoss -= (currentPeer->packetLoss - packetLoss) / 8;
  1267. currentPeer->packetLossVariance += (currentPeer->packetLoss - packetLoss) / 4;
  1268. }
  1269. currentPeer->packetLossEpoch = host->serviceTime;
  1270. currentPeer->packetsSent = 0;
  1271. currentPeer->packetsLost = 0;
  1272. }
  1273. host->buffers->data = headerData;
  1274. if (host->headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
  1275. {
  1276. header->sentTime = ENET_HOST_TO_NET_16 (host -> serviceTime & 0xFFFF);
  1277. host->buffers->dataLength = sizeof(ENetProtocolHeader);
  1278. }
  1279. else
  1280. host->buffers->dataLength = (size_t) & ((ENetProtocolHeader *) 0)->sentTime;
  1281. shouldCompress = 0;
  1282. if (host->compressor.context != NULL && host->compressor.compress != NULL)
  1283. {
  1284. size_t originalSize = host->packetSize - sizeof(ENetProtocolHeader),
  1285. compressedSize = host->compressor.compress(host->compressor.context,
  1286. &host->buffers[1], host->bufferCount - 1, originalSize,
  1287. host->packetData[1], originalSize);
  1288. if (compressedSize > 0 && compressedSize < originalSize)
  1289. {
  1290. host->headerFlags |= ENET_PROTOCOL_HEADER_FLAG_COMPRESSED;
  1291. shouldCompress = compressedSize;
  1292. #ifdef ENET_DEBUG_COMPRESS
  1293. #ifdef WIN32
  1294. printf (
  1295. #else
  1296. fprintf (stderr,
  1297. #endif
  1298. "peer %u: compressed %u -> %u (%u%%)\n", currentPeer -> incomingPeerID, originalSize, compressedSize, (compressedSize * 100) / originalSize);
  1299. #endif
  1300. }
  1301. }
  1302. if (currentPeer->outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID)
  1303. host->headerFlags |= currentPeer->outgoingSessionID
  1304. << ENET_PROTOCOL_HEADER_SESSION_SHIFT;
  1305. header->peerID =
  1306. ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID | host -> headerFlags);
  1307. if (host->checksum != NULL)
  1308. {
  1309. enet_uint32 * checksum = (enet_uint32 *) &headerData[host->buffers->dataLength];
  1310. *checksum =
  1311. currentPeer->outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ?
  1312. currentPeer->connectID : 0;
  1313. host->buffers->dataLength += sizeof(enet_uint32);
  1314. *checksum = host->checksum(host->buffers, host->bufferCount);
  1315. }
  1316. if (shouldCompress > 0)
  1317. {
  1318. host->buffers[1].data = host->packetData[1];
  1319. host->buffers[1].dataLength = shouldCompress;
  1320. host->bufferCount = 2;
  1321. }
  1322. currentPeer->lastSendTime = host->serviceTime;
  1323. sentLength = enet_socket_send(host->socket, &currentPeer->address, host->buffers,
  1324. host->bufferCount);
  1325. enet_protocol_remove_sent_unreliable_commands(currentPeer);
  1326. if (sentLength < 0)
  1327. return -1;
  1328. host->totalSentData += sentLength;
  1329. host->totalSentPackets++;
  1330. }
  1331. return 0;
  1332. }
  1333. /** Sends any queued packets on the host specified to its designated peers.
  1334. @param host host to flush
  1335. @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().
  1336. @ingroup host
  1337. */
  1338. void enet_host_flush(ENetHost * host)
  1339. {
  1340. host->serviceTime = enet_time_get();
  1341. enet_protocol_send_outgoing_commands(host, NULL, 0);
  1342. }
  1343. /** Checks for any queued events on the host and dispatches one if available.
  1344. @param host host to check for events
  1345. @param event an event structure where event details will be placed if available
  1346. @retval > 0 if an event was dispatched
  1347. @retval 0 if no events are available
  1348. @retval < 0 on failure
  1349. @ingroup host
  1350. */
  1351. int enet_host_check_events(ENetHost * host, ENetEvent * event)
  1352. {
  1353. if (event == NULL)
  1354. return -1;
  1355. event->type = ENET_EVENT_TYPE_NONE;
  1356. event->peer = NULL;
  1357. event->packet = NULL;
  1358. return enet_protocol_dispatch_incoming_commands(host, event);
  1359. }
  1360. /** Waits for events on the host specified and shuttles packets between
  1361. the host and its peers.
  1362. @param host host to service
  1363. @param event an event structure where event details will be placed if one occurs
  1364. if event == NULL then no events will be delivered
  1365. @param timeout number of milliseconds that ENet should wait for events
  1366. @retval > 0 if an event occurred within the specified time limit
  1367. @retval 0 if no event occurred
  1368. @retval < 0 on failure
  1369. @remarks enet_host_service should be called fairly regularly for adequate performance
  1370. @ingroup host
  1371. */
  1372. int enet_host_service(ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  1373. {
  1374. enet_uint32 waitCondition;
  1375. if (event != NULL)
  1376. {
  1377. event->type = ENET_EVENT_TYPE_NONE;
  1378. event->peer = NULL;
  1379. event->packet = NULL;
  1380. switch (enet_protocol_dispatch_incoming_commands(host, event))
  1381. {
  1382. case 1:
  1383. return 1;
  1384. case -1:
  1385. perror("Error dispatching incoming packets");
  1386. return -1;
  1387. default:
  1388. break;
  1389. }
  1390. }
  1391. host->serviceTime = enet_time_get();
  1392. timeout += host->serviceTime;
  1393. do
  1394. {
  1395. if (ENET_TIME_DIFFERENCE (host -> serviceTime, host -> bandwidthThrottleEpoch)
  1396. >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  1397. enet_host_bandwidth_throttle(host);
  1398. switch (enet_protocol_send_outgoing_commands(host, event, 1))
  1399. {
  1400. case 1:
  1401. return 1;
  1402. case -1:
  1403. perror("Error sending outgoing packets");
  1404. return -1;
  1405. default:
  1406. break;
  1407. }
  1408. switch (enet_protocol_receive_incoming_commands(host, event))
  1409. {
  1410. case 1:
  1411. return 1;
  1412. case -1:
  1413. perror("Error receiving incoming packets");
  1414. return -1;
  1415. default:
  1416. break;
  1417. }
  1418. switch (enet_protocol_send_outgoing_commands(host, event, 1))
  1419. {
  1420. case 1:
  1421. return 1;
  1422. case -1:
  1423. perror("Error sending outgoing packets");
  1424. return -1;
  1425. default:
  1426. break;
  1427. }
  1428. if (event != NULL)
  1429. {
  1430. switch (enet_protocol_dispatch_incoming_commands(host, event))
  1431. {
  1432. case 1:
  1433. return 1;
  1434. case -1:
  1435. perror("Error dispatching incoming packets");
  1436. return -1;
  1437. default:
  1438. break;
  1439. }
  1440. }
  1441. host->serviceTime = enet_time_get();
  1442. if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
  1443. return 0;
  1444. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  1445. if (enet_socket_wait(host->socket, &waitCondition,
  1446. ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
  1447. return -1;
  1448. host->serviceTime = enet_time_get();
  1449. }
  1450. while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  1451. return 0;
  1452. }