protocol.c 70 KB

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