protocol.c 69 KB

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