protocol.c 61 KB

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