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