protocol.c 70 KB

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