protocol.c 69 KB

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