protocol.c 68 KB

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