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. 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. ((host -> receivedAddress.host != peer -> address.host ||
  828. host -> receivedAddress.port != peer -> address.port) &&
  829. peer -> address.host != ENET_HOST_BROADCAST) ||
  830. (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
  831. sessionID != peer -> incomingSessionID))
  832. return 0;
  833. }
  834. if (flags & ENET_PROTOCOL_HEADER_FLAG_COMPRESSED)
  835. {
  836. size_t originalSize;
  837. if (host -> compressor.context == NULL || host -> compressor.decompress == NULL)
  838. return 0;
  839. originalSize = host -> compressor.decompress (host -> compressor.context,
  840. host -> receivedData + headerSize,
  841. host -> receivedDataLength - headerSize,
  842. host -> packetData [1] + headerSize,
  843. sizeof (host -> packetData [1]) - headerSize);
  844. if (originalSize <= 0 || originalSize > sizeof (host -> packetData [1]) - headerSize)
  845. return 0;
  846. memcpy (host -> packetData [1], header, headerSize);
  847. host -> receivedData = host -> packetData [1];
  848. host -> receivedDataLength = headerSize + originalSize;
  849. }
  850. if (host -> checksum != NULL)
  851. {
  852. enet_uint32 * checksum = (enet_uint32 *) & host -> receivedData [headerSize - sizeof (enet_uint32)];
  853. enet_uint32 desiredChecksum, newChecksum;
  854. ENetBuffer buffer;
  855. /* Checksum may be an unaligned pointer, use memcpy to avoid undefined behaviour. */
  856. memcpy (& desiredChecksum, checksum, sizeof (enet_uint32));
  857. newChecksum = peer != NULL ? peer -> connectID : 0;
  858. memcpy (checksum, & newChecksum, sizeof (enet_uint32));
  859. buffer.data = host -> receivedData;
  860. buffer.dataLength = host -> receivedDataLength;
  861. if (host -> checksum (& buffer, 1) != desiredChecksum)
  862. return 0;
  863. }
  864. if (peer != NULL)
  865. {
  866. peer -> address.host = host -> receivedAddress.host;
  867. peer -> address.port = host -> receivedAddress.port;
  868. peer -> incomingDataTotal += host -> receivedDataLength;
  869. }
  870. currentData = host -> receivedData + headerSize;
  871. while (currentData < & host -> receivedData [host -> receivedDataLength])
  872. {
  873. enet_uint8 commandNumber;
  874. size_t commandSize;
  875. command = (ENetProtocol *) currentData;
  876. if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
  877. break;
  878. commandNumber = command -> header.command & ENET_PROTOCOL_COMMAND_MASK;
  879. if (commandNumber >= ENET_PROTOCOL_COMMAND_COUNT)
  880. break;
  881. commandSize = commandSizes [commandNumber];
  882. if (commandSize == 0 || currentData + commandSize > & host -> receivedData [host -> receivedDataLength])
  883. break;
  884. currentData += commandSize;
  885. if (peer == NULL && commandNumber != ENET_PROTOCOL_COMMAND_CONNECT)
  886. break;
  887. command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
  888. switch (commandNumber)
  889. {
  890. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  891. if (enet_protocol_handle_acknowledge (host, event, peer, command))
  892. goto commandError;
  893. break;
  894. case ENET_PROTOCOL_COMMAND_CONNECT:
  895. if (peer != NULL)
  896. goto commandError;
  897. peer = enet_protocol_handle_connect (host, header, command);
  898. if (peer == NULL)
  899. goto commandError;
  900. break;
  901. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  902. if (enet_protocol_handle_verify_connect (host, event, peer, command))
  903. goto commandError;
  904. break;
  905. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  906. if (enet_protocol_handle_disconnect (host, peer, command))
  907. goto commandError;
  908. break;
  909. case ENET_PROTOCOL_COMMAND_PING:
  910. if (enet_protocol_handle_ping (host, peer, command))
  911. goto commandError;
  912. break;
  913. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  914. if (enet_protocol_handle_send_reliable (host, peer, command, & currentData))
  915. goto commandError;
  916. break;
  917. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  918. if (enet_protocol_handle_send_unreliable (host, peer, command, & currentData))
  919. goto commandError;
  920. break;
  921. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  922. if (enet_protocol_handle_send_unsequenced (host, peer, command, & currentData))
  923. goto commandError;
  924. break;
  925. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  926. if (enet_protocol_handle_send_fragment (host, peer, command, & currentData))
  927. goto commandError;
  928. break;
  929. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  930. if (enet_protocol_handle_bandwidth_limit (host, peer, command))
  931. goto commandError;
  932. break;
  933. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  934. if (enet_protocol_handle_throttle_configure (host, peer, command))
  935. goto commandError;
  936. break;
  937. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
  938. if (enet_protocol_handle_send_unreliable_fragment (host, peer, command, & currentData))
  939. goto commandError;
  940. break;
  941. default:
  942. goto commandError;
  943. }
  944. if (peer != NULL &&
  945. (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
  946. {
  947. enet_uint16 sentTime;
  948. if (! (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME))
  949. break;
  950. sentTime = ENET_NET_TO_HOST_16 (header -> sentTime);
  951. switch (peer -> state)
  952. {
  953. case ENET_PEER_STATE_DISCONNECTING:
  954. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  955. case ENET_PEER_STATE_DISCONNECTED:
  956. case ENET_PEER_STATE_ZOMBIE:
  957. break;
  958. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  959. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  960. enet_peer_queue_acknowledgement (peer, command, sentTime);
  961. break;
  962. default:
  963. enet_peer_queue_acknowledgement (peer, command, sentTime);
  964. break;
  965. }
  966. }
  967. }
  968. commandError:
  969. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  970. return 1;
  971. return 0;
  972. }
  973. static int
  974. enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
  975. {
  976. int packets;
  977. for (packets = 0; packets < 256; ++ packets)
  978. {
  979. int receivedLength;
  980. ENetBuffer buffer;
  981. buffer.data = host -> packetData [0];
  982. buffer.dataLength = sizeof (host -> packetData [0]);
  983. receivedLength = enet_socket_receive (host -> socket,
  984. & host -> receivedAddress,
  985. & buffer,
  986. 1);
  987. if (receivedLength == -2)
  988. continue;
  989. if (receivedLength < 0)
  990. return -1;
  991. if (receivedLength == 0)
  992. return 0;
  993. host -> receivedData = host -> packetData [0];
  994. host -> receivedDataLength = receivedLength;
  995. host -> totalReceivedData += receivedLength;
  996. host -> totalReceivedPackets ++;
  997. if (host -> intercept != NULL)
  998. {
  999. switch (host -> intercept (host, event))
  1000. {
  1001. case 1:
  1002. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  1003. return 1;
  1004. continue;
  1005. case -1:
  1006. return -1;
  1007. default:
  1008. break;
  1009. }
  1010. }
  1011. switch (enet_protocol_handle_incoming_commands (host, event))
  1012. {
  1013. case 1:
  1014. return 1;
  1015. case -1:
  1016. return -1;
  1017. default:
  1018. break;
  1019. }
  1020. }
  1021. return 0;
  1022. }
  1023. static void
  1024. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  1025. {
  1026. ENetProtocol * command = & host -> commands [host -> commandCount];
  1027. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  1028. ENetAcknowledgement * acknowledgement;
  1029. ENetListIterator currentAcknowledgement;
  1030. enet_uint16 reliableSequenceNumber;
  1031. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  1032. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  1033. {
  1034. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  1035. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  1036. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  1037. {
  1038. peer -> flags |= ENET_PEER_FLAG_CONTINUE_SENDING;
  1039. break;
  1040. }
  1041. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  1042. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  1043. buffer -> data = command;
  1044. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  1045. host -> packetSize += buffer -> dataLength;
  1046. reliableSequenceNumber = ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
  1047. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  1048. command -> header.channelID = acknowledgement -> command.header.channelID;
  1049. command -> header.reliableSequenceNumber = reliableSequenceNumber;
  1050. command -> acknowledge.receivedReliableSequenceNumber = reliableSequenceNumber;
  1051. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_16 (acknowledgement -> sentTime);
  1052. if ((acknowledgement -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  1053. enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
  1054. enet_list_remove (& acknowledgement -> acknowledgementList);
  1055. enet_free (acknowledgement);
  1056. ++ command;
  1057. ++ buffer;
  1058. }
  1059. host -> commandCount = command - host -> commands;
  1060. host -> bufferCount = buffer - host -> buffers;
  1061. }
  1062. static int
  1063. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  1064. {
  1065. ENetOutgoingCommand * outgoingCommand;
  1066. ENetListIterator currentCommand, insertPosition, insertSendReliablePosition;
  1067. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  1068. insertPosition = enet_list_begin (& peer -> outgoingCommands);
  1069. insertSendReliablePosition = enet_list_begin (& peer -> outgoingSendReliableCommands);
  1070. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  1071. {
  1072. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1073. currentCommand = enet_list_next (currentCommand);
  1074. if (ENET_TIME_DIFFERENCE (host -> serviceTime, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  1075. continue;
  1076. if (peer -> earliestTimeout == 0 ||
  1077. ENET_TIME_LESS (outgoingCommand -> sentTime, peer -> earliestTimeout))
  1078. peer -> earliestTimeout = outgoingCommand -> sentTime;
  1079. if (peer -> earliestTimeout != 0 &&
  1080. (ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMaximum ||
  1081. ((1 << (outgoingCommand -> sendAttempts - 1)) >= peer -> timeoutLimit &&
  1082. ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMinimum)))
  1083. {
  1084. enet_protocol_notify_disconnect (host, peer, event);
  1085. return 1;
  1086. }
  1087. ++ peer -> packetsLost;
  1088. outgoingCommand -> roundTripTimeout *= 2;
  1089. if (outgoingCommand -> packet != NULL)
  1090. {
  1091. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  1092. enet_list_insert (insertSendReliablePosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
  1093. }
  1094. else
  1095. enet_list_insert (insertPosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
  1096. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  1097. ! enet_list_empty (& peer -> sentReliableCommands))
  1098. {
  1099. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1100. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  1101. }
  1102. }
  1103. return 0;
  1104. }
  1105. static int
  1106. enet_protocol_check_outgoing_commands (ENetHost * host, ENetPeer * peer, ENetList * sentUnreliableCommands)
  1107. {
  1108. ENetProtocol * command = & host -> commands [host -> commandCount];
  1109. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  1110. ENetOutgoingCommand * outgoingCommand;
  1111. ENetListIterator currentCommand, currentSendReliableCommand;
  1112. ENetChannel *channel = NULL;
  1113. enet_uint16 reliableWindow = 0;
  1114. size_t commandSize;
  1115. int windowWrap = 0, canPing = 1;
  1116. currentCommand = enet_list_begin (& peer -> outgoingCommands);
  1117. currentSendReliableCommand = enet_list_begin (& peer -> outgoingSendReliableCommands);
  1118. for (;;)
  1119. {
  1120. if (currentCommand != enet_list_end (& peer -> outgoingCommands))
  1121. {
  1122. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1123. if (currentSendReliableCommand != enet_list_end (& peer -> outgoingSendReliableCommands) &&
  1124. ENET_TIME_LESS (((ENetOutgoingCommand *) currentSendReliableCommand) -> queueTime, outgoingCommand -> queueTime))
  1125. goto useSendReliableCommand;
  1126. currentCommand = enet_list_next (currentCommand);
  1127. }
  1128. else
  1129. if (currentSendReliableCommand != enet_list_end (& peer -> outgoingSendReliableCommands))
  1130. {
  1131. useSendReliableCommand:
  1132. outgoingCommand = (ENetOutgoingCommand *) currentSendReliableCommand;
  1133. currentSendReliableCommand = enet_list_next (currentSendReliableCommand);
  1134. }
  1135. else
  1136. break;
  1137. if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  1138. {
  1139. channel = outgoingCommand -> command.header.channelID < peer -> channelCount ? & peer -> channels [outgoingCommand -> command.header.channelID] : NULL;
  1140. reliableWindow = outgoingCommand -> reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1141. if (channel != NULL)
  1142. {
  1143. if (windowWrap)
  1144. continue;
  1145. else
  1146. if (outgoingCommand -> sendAttempts < 1 &&
  1147. ! (outgoingCommand -> reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE) &&
  1148. (channel -> reliableWindows [(reliableWindow + ENET_PEER_RELIABLE_WINDOWS - 1) % ENET_PEER_RELIABLE_WINDOWS] >= ENET_PEER_RELIABLE_WINDOW_SIZE ||
  1149. channel -> usedReliableWindows & ((((1 << (ENET_PEER_FREE_RELIABLE_WINDOWS + 2)) - 1) << reliableWindow) |
  1150. (((1 << (ENET_PEER_FREE_RELIABLE_WINDOWS + 2)) - 1) >> (ENET_PEER_RELIABLE_WINDOWS - reliableWindow)))))
  1151. {
  1152. windowWrap = 1;
  1153. currentSendReliableCommand = enet_list_end (& peer -> outgoingSendReliableCommands);
  1154. continue;
  1155. }
  1156. }
  1157. if (outgoingCommand -> packet != NULL)
  1158. {
  1159. enet_uint32 windowSize = (peer -> packetThrottle * peer -> windowSize) / ENET_PEER_PACKET_THROTTLE_SCALE;
  1160. if (peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > ENET_MAX (windowSize, peer -> mtu))
  1161. {
  1162. currentSendReliableCommand = enet_list_end (& peer -> outgoingSendReliableCommands);
  1163. continue;
  1164. }
  1165. }
  1166. canPing = 0;
  1167. }
  1168. commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  1169. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  1170. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  1171. peer -> mtu - host -> packetSize < commandSize ||
  1172. (outgoingCommand -> packet != NULL &&
  1173. (enet_uint16) (peer -> mtu - host -> packetSize) < (enet_uint16) (commandSize + outgoingCommand -> fragmentLength)))
  1174. {
  1175. peer -> flags |= ENET_PEER_FLAG_CONTINUE_SENDING;
  1176. break;
  1177. }
  1178. if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  1179. {
  1180. if (channel != NULL && outgoingCommand -> sendAttempts < 1)
  1181. {
  1182. channel -> usedReliableWindows |= 1 << reliableWindow;
  1183. ++ channel -> reliableWindows [reliableWindow];
  1184. }
  1185. ++ outgoingCommand -> sendAttempts;
  1186. if (outgoingCommand -> roundTripTimeout == 0)
  1187. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  1188. if (enet_list_empty (& peer -> sentReliableCommands))
  1189. peer -> nextTimeout = host -> serviceTime + outgoingCommand -> roundTripTimeout;
  1190. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  1191. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  1192. outgoingCommand -> sentTime = host -> serviceTime;
  1193. host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
  1194. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  1195. }
  1196. else
  1197. {
  1198. if (outgoingCommand -> packet != NULL && outgoingCommand -> fragmentOffset == 0)
  1199. {
  1200. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  1201. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  1202. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  1203. {
  1204. enet_uint16 reliableSequenceNumber = outgoingCommand -> reliableSequenceNumber,
  1205. unreliableSequenceNumber = outgoingCommand -> unreliableSequenceNumber;
  1206. for (;;)
  1207. {
  1208. -- outgoingCommand -> packet -> referenceCount;
  1209. if (outgoingCommand -> packet -> referenceCount == 0)
  1210. enet_packet_destroy (outgoingCommand -> packet);
  1211. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  1212. enet_free (outgoingCommand);
  1213. if (currentCommand == enet_list_end (& peer -> outgoingCommands))
  1214. break;
  1215. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1216. if (outgoingCommand -> reliableSequenceNumber != reliableSequenceNumber ||
  1217. outgoingCommand -> unreliableSequenceNumber != unreliableSequenceNumber)
  1218. break;
  1219. currentCommand = enet_list_next (currentCommand);
  1220. }
  1221. continue;
  1222. }
  1223. }
  1224. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  1225. if (outgoingCommand -> packet != NULL)
  1226. enet_list_insert (enet_list_end (sentUnreliableCommands), outgoingCommand);
  1227. }
  1228. buffer -> data = command;
  1229. buffer -> dataLength = commandSize;
  1230. host -> packetSize += buffer -> dataLength;
  1231. * command = outgoingCommand -> command;
  1232. if (outgoingCommand -> packet != NULL)
  1233. {
  1234. ++ buffer;
  1235. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  1236. buffer -> dataLength = outgoingCommand -> fragmentLength;
  1237. host -> packetSize += outgoingCommand -> fragmentLength;
  1238. }
  1239. else
  1240. if (! (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE))
  1241. enet_free (outgoingCommand);
  1242. ++ peer -> packetsSent;
  1243. ++ command;
  1244. ++ buffer;
  1245. }
  1246. host -> commandCount = command - host -> commands;
  1247. host -> bufferCount = buffer - host -> buffers;
  1248. if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
  1249. ! enet_peer_has_outgoing_commands (peer) &&
  1250. enet_list_empty (sentUnreliableCommands))
  1251. enet_peer_disconnect (peer, peer -> eventData);
  1252. return canPing;
  1253. }
  1254. static int
  1255. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  1256. {
  1257. enet_uint8 headerData [sizeof (ENetProtocolHeader) + sizeof (enet_uint32)];
  1258. ENetProtocolHeader * header = (ENetProtocolHeader *) headerData;
  1259. int sentLength = 0;
  1260. size_t shouldCompress = 0;
  1261. ENetList sentUnreliableCommands;
  1262. enet_list_clear (& sentUnreliableCommands);
  1263. for (int sendPass = 0, continueSending = 0; sendPass <= continueSending; ++ sendPass)
  1264. for (ENetPeer * currentPeer = host -> peers;
  1265. currentPeer < & host -> peers [host -> peerCount];
  1266. ++ currentPeer)
  1267. {
  1268. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  1269. currentPeer -> state == ENET_PEER_STATE_ZOMBIE ||
  1270. (sendPass > 0 && ! (currentPeer -> flags & ENET_PEER_FLAG_CONTINUE_SENDING)))
  1271. continue;
  1272. currentPeer -> flags &= ~ ENET_PEER_FLAG_CONTINUE_SENDING;
  1273. host -> headerFlags = 0;
  1274. host -> commandCount = 0;
  1275. host -> bufferCount = 1;
  1276. host -> packetSize = sizeof (ENetProtocolHeader);
  1277. if (! enet_list_empty (& currentPeer -> acknowledgements))
  1278. enet_protocol_send_acknowledgements (host, currentPeer);
  1279. if (checkForTimeouts != 0 &&
  1280. ! enet_list_empty (& currentPeer -> sentReliableCommands) &&
  1281. ENET_TIME_GREATER_EQUAL (host -> serviceTime, currentPeer -> nextTimeout) &&
  1282. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  1283. {
  1284. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  1285. return 1;
  1286. else
  1287. goto nextPeer;
  1288. }
  1289. if (((enet_list_empty (& currentPeer -> outgoingCommands) &&
  1290. enet_list_empty (& currentPeer -> outgoingSendReliableCommands)) ||
  1291. enet_protocol_check_outgoing_commands (host, currentPeer, & sentUnreliableCommands)) &&
  1292. enet_list_empty (& currentPeer -> sentReliableCommands) &&
  1293. ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime) >= currentPeer -> pingInterval &&
  1294. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  1295. {
  1296. enet_peer_ping (currentPeer);
  1297. enet_protocol_check_outgoing_commands (host, currentPeer, & sentUnreliableCommands);
  1298. }
  1299. if (host -> commandCount == 0)
  1300. goto nextPeer;
  1301. if (currentPeer -> packetLossEpoch == 0)
  1302. currentPeer -> packetLossEpoch = host -> serviceTime;
  1303. else
  1304. if (ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  1305. currentPeer -> packetsSent > 0)
  1306. {
  1307. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  1308. #ifdef ENET_DEBUG
  1309. 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);
  1310. #endif
  1311. currentPeer -> packetLossVariance = (currentPeer -> packetLossVariance * 3 + ENET_DIFFERENCE (packetLoss, currentPeer -> packetLoss)) / 4;
  1312. currentPeer -> packetLoss = (currentPeer -> packetLoss * 7 + packetLoss) / 8;
  1313. currentPeer -> packetLossEpoch = host -> serviceTime;
  1314. currentPeer -> packetsSent = 0;
  1315. currentPeer -> packetsLost = 0;
  1316. }
  1317. host -> buffers -> data = headerData;
  1318. if (host -> headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
  1319. {
  1320. header -> sentTime = ENET_HOST_TO_NET_16 (host -> serviceTime & 0xFFFF);
  1321. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  1322. }
  1323. else
  1324. host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
  1325. shouldCompress = 0;
  1326. if (host -> compressor.context != NULL && host -> compressor.compress != NULL)
  1327. {
  1328. size_t originalSize = host -> packetSize - sizeof(ENetProtocolHeader),
  1329. compressedSize = host -> compressor.compress (host -> compressor.context,
  1330. & host -> buffers [1], host -> bufferCount - 1,
  1331. originalSize,
  1332. host -> packetData [1],
  1333. originalSize);
  1334. if (compressedSize > 0 && compressedSize < originalSize)
  1335. {
  1336. host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_COMPRESSED;
  1337. shouldCompress = compressedSize;
  1338. #ifdef ENET_DEBUG_COMPRESS
  1339. printf ("peer %u: compressed %u -> %u (%u%%)\n", currentPeer -> incomingPeerID, originalSize, compressedSize, (compressedSize * 100) / originalSize);
  1340. #endif
  1341. }
  1342. }
  1343. if (currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID)
  1344. host -> headerFlags |= currentPeer -> outgoingSessionID << ENET_PROTOCOL_HEADER_SESSION_SHIFT;
  1345. header -> peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID | host -> headerFlags);
  1346. if (host -> checksum != NULL)
  1347. {
  1348. enet_uint32 * checksum = (enet_uint32 *) & headerData [host -> buffers -> dataLength];
  1349. enet_uint32 newChecksum = currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer -> connectID : 0;
  1350. /* Checksum may be unaligned, use memcpy to avoid undefined behaviour. */
  1351. memcpy(checksum, & newChecksum, sizeof (enet_uint32));
  1352. host -> buffers -> dataLength += sizeof (enet_uint32);
  1353. newChecksum = host -> checksum (host -> buffers, host -> bufferCount);
  1354. memcpy(checksum, & newChecksum, sizeof (enet_uint32));
  1355. }
  1356. if (shouldCompress > 0)
  1357. {
  1358. host -> buffers [1].data = host -> packetData [1];
  1359. host -> buffers [1].dataLength = shouldCompress;
  1360. host -> bufferCount = 2;
  1361. }
  1362. currentPeer -> lastSendTime = host -> serviceTime;
  1363. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  1364. enet_protocol_remove_sent_unreliable_commands (currentPeer, & sentUnreliableCommands);
  1365. if (sentLength < 0)
  1366. return -1;
  1367. host -> totalSentData += sentLength;
  1368. host -> totalSentPackets ++;
  1369. nextPeer:
  1370. if (currentPeer -> flags & ENET_PEER_FLAG_CONTINUE_SENDING)
  1371. continueSending = sendPass + 1;
  1372. }
  1373. return 0;
  1374. }
  1375. /** Sends any queued packets on the host specified to its designated peers.
  1376. @param host host to flush
  1377. @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().
  1378. @ingroup host
  1379. */
  1380. void
  1381. enet_host_flush (ENetHost * host)
  1382. {
  1383. host -> serviceTime = enet_time_get ();
  1384. enet_protocol_send_outgoing_commands (host, NULL, 0);
  1385. }
  1386. /** Checks for any queued events on the host and dispatches one if available.
  1387. @param host host to check for events
  1388. @param event an event structure where event details will be placed if available
  1389. @retval > 0 if an event was dispatched
  1390. @retval 0 if no events are available
  1391. @retval < 0 on failure
  1392. @ingroup host
  1393. */
  1394. int
  1395. enet_host_check_events (ENetHost * host, ENetEvent * event)
  1396. {
  1397. if (event == NULL) return -1;
  1398. event -> type = ENET_EVENT_TYPE_NONE;
  1399. event -> peer = NULL;
  1400. event -> packet = NULL;
  1401. return enet_protocol_dispatch_incoming_commands (host, event);
  1402. }
  1403. /** Waits for events on the host specified and shuttles packets between
  1404. the host and its peers.
  1405. @param host host to service
  1406. @param event an event structure where event details will be placed if one occurs
  1407. if event == NULL then no events will be delivered
  1408. @param timeout number of milliseconds that ENet should wait for events
  1409. @retval > 0 if an event occurred within the specified time limit
  1410. @retval 0 if no event occurred
  1411. @retval < 0 on failure
  1412. @remarks enet_host_service should be called fairly regularly for adequate performance
  1413. @ingroup host
  1414. */
  1415. int
  1416. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  1417. {
  1418. enet_uint32 waitCondition;
  1419. if (event != NULL)
  1420. {
  1421. event -> type = ENET_EVENT_TYPE_NONE;
  1422. event -> peer = NULL;
  1423. event -> packet = NULL;
  1424. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1425. {
  1426. case 1:
  1427. return 1;
  1428. case -1:
  1429. #ifdef ENET_DEBUG
  1430. perror ("Error dispatching incoming packets");
  1431. #endif
  1432. return -1;
  1433. default:
  1434. break;
  1435. }
  1436. }
  1437. host -> serviceTime = enet_time_get ();
  1438. timeout += host -> serviceTime;
  1439. do
  1440. {
  1441. if (ENET_TIME_DIFFERENCE (host -> serviceTime, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  1442. enet_host_bandwidth_throttle (host);
  1443. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  1444. {
  1445. case 1:
  1446. return 1;
  1447. case -1:
  1448. #ifdef ENET_DEBUG
  1449. perror ("Error sending outgoing packets");
  1450. #endif
  1451. return -1;
  1452. default:
  1453. break;
  1454. }
  1455. switch (enet_protocol_receive_incoming_commands (host, event))
  1456. {
  1457. case 1:
  1458. return 1;
  1459. case -1:
  1460. #ifdef ENET_DEBUG
  1461. perror ("Error receiving incoming packets");
  1462. #endif
  1463. return -1;
  1464. default:
  1465. break;
  1466. }
  1467. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  1468. {
  1469. case 1:
  1470. return 1;
  1471. case -1:
  1472. #ifdef ENET_DEBUG
  1473. perror ("Error sending outgoing packets");
  1474. #endif
  1475. return -1;
  1476. default:
  1477. break;
  1478. }
  1479. if (event != NULL)
  1480. {
  1481. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1482. {
  1483. case 1:
  1484. return 1;
  1485. case -1:
  1486. #ifdef ENET_DEBUG
  1487. perror ("Error dispatching incoming packets");
  1488. #endif
  1489. return -1;
  1490. default:
  1491. break;
  1492. }
  1493. }
  1494. if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
  1495. return 0;
  1496. do
  1497. {
  1498. host -> serviceTime = enet_time_get ();
  1499. if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
  1500. return 0;
  1501. waitCondition = ENET_SOCKET_WAIT_RECEIVE | ENET_SOCKET_WAIT_INTERRUPT;
  1502. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
  1503. return -1;
  1504. }
  1505. while (waitCondition & ENET_SOCKET_WAIT_INTERRUPT);
  1506. host -> serviceTime = enet_time_get ();
  1507. } while (waitCondition & ENET_SOCKET_WAIT_RECEIVE);
  1508. return 0;
  1509. }