protocol.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 enet_uint32 timeCurrent;
  12. static int
  13. enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
  14. {
  15. ENetPeer * currentPeer = host -> lastServicedPeer;
  16. ENetChannel * channel;
  17. do
  18. {
  19. ++ currentPeer;
  20. if (currentPeer >= & host -> peers [host -> peerCount])
  21. currentPeer = host -> peers;
  22. switch (currentPeer -> state)
  23. {
  24. case ENET_PEER_STATE_CONNECTION_PENDING:
  25. currentPeer -> state = ENET_PEER_STATE_CONNECTED;
  26. event -> type = ENET_EVENT_TYPE_CONNECT;
  27. event -> peer = currentPeer;
  28. return 1;
  29. case ENET_PEER_STATE_ZOMBIE:
  30. host -> recalculateBandwidthLimits = 1;
  31. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  32. event -> peer = currentPeer;
  33. event -> data = currentPeer -> disconnectData;
  34. enet_peer_reset (currentPeer);
  35. host -> lastServicedPeer = currentPeer;
  36. return 1;
  37. }
  38. if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
  39. continue;
  40. for (channel = currentPeer -> channels;
  41. channel < & currentPeer -> channels [currentPeer -> channelCount];
  42. ++ channel)
  43. {
  44. if (enet_list_empty (& channel -> incomingReliableCommands) &&
  45. enet_list_empty (& channel -> incomingUnreliableCommands))
  46. continue;
  47. event -> packet = enet_peer_receive (currentPeer, channel - currentPeer -> channels);
  48. if (event -> packet == NULL)
  49. continue;
  50. event -> type = ENET_EVENT_TYPE_RECEIVE;
  51. event -> peer = currentPeer;
  52. event -> channelID = (enet_uint8) (channel - currentPeer -> channels);
  53. host -> lastServicedPeer = currentPeer;
  54. return 1;
  55. }
  56. } while (currentPeer != host -> lastServicedPeer);
  57. return 0;
  58. }
  59. static void
  60. enet_protocol_notify_connect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  61. {
  62. host -> recalculateBandwidthLimits = 1;
  63. if (event == NULL)
  64. peer -> state = ENET_PEER_STATE_CONNECTION_PENDING;
  65. else
  66. {
  67. peer -> state = ENET_PEER_STATE_CONNECTED;
  68. event -> type = ENET_EVENT_TYPE_CONNECT;
  69. event -> peer = peer;
  70. }
  71. }
  72. static void
  73. enet_protocol_notify_disconnect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  74. {
  75. if (peer -> state >= ENET_PEER_STATE_CONNECTION_PENDING)
  76. host -> recalculateBandwidthLimits = 1;
  77. if (peer -> state < ENET_PEER_STATE_CONNECTED)
  78. enet_peer_reset (peer);
  79. else
  80. if (event == NULL)
  81. peer -> state = ENET_PEER_STATE_ZOMBIE;
  82. else
  83. {
  84. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  85. event -> peer = peer;
  86. event -> data = 0;
  87. enet_peer_reset (peer);
  88. }
  89. }
  90. static void
  91. enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer)
  92. {
  93. ENetOutgoingCommand * outgoingCommand;
  94. while (enet_list_empty (& peer -> sentUnreliableCommands) == 0)
  95. {
  96. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
  97. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  98. if (outgoingCommand -> packet != NULL)
  99. {
  100. -- outgoingCommand -> packet -> referenceCount;
  101. if (outgoingCommand -> packet -> referenceCount == 0)
  102. enet_packet_destroy (outgoingCommand -> packet);
  103. }
  104. enet_free (outgoingCommand);
  105. }
  106. }
  107. static ENetProtocolCommand
  108. enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint32 reliableSequenceNumber, enet_uint8 channelID)
  109. {
  110. ENetOutgoingCommand * outgoingCommand;
  111. ENetListIterator currentCommand;
  112. ENetProtocolCommand commandNumber;
  113. for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  114. currentCommand != enet_list_end (& peer -> sentReliableCommands);
  115. currentCommand = enet_list_next (currentCommand))
  116. {
  117. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  118. if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
  119. outgoingCommand -> command.header.channelID == channelID)
  120. break;
  121. }
  122. if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
  123. return ENET_PROTOCOL_COMMAND_NONE;
  124. commandNumber = outgoingCommand -> command.header.command;
  125. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  126. if (outgoingCommand -> packet != NULL)
  127. {
  128. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  129. -- outgoingCommand -> packet -> referenceCount;
  130. if (outgoingCommand -> packet -> referenceCount == 0)
  131. enet_packet_destroy (outgoingCommand -> packet);
  132. }
  133. enet_free (outgoingCommand);
  134. if (enet_list_empty (& peer -> sentReliableCommands))
  135. return commandNumber;
  136. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
  137. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  138. return commandNumber;
  139. }
  140. static ENetPeer *
  141. enet_protocol_handle_connect (ENetHost * host, const ENetProtocolHeader * header, const ENetProtocol * command)
  142. {
  143. enet_uint16 mtu;
  144. enet_uint32 windowSize;
  145. ENetChannel * channel;
  146. size_t channelCount;
  147. ENetPeer * currentPeer;
  148. ENetProtocol verifyCommand;
  149. if (command -> header.commandLength < sizeof (ENetProtocolConnect))
  150. return NULL;
  151. channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
  152. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT ||
  153. channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  154. return NULL;
  155. for (currentPeer = host -> peers;
  156. currentPeer < & host -> peers [host -> peerCount];
  157. ++ currentPeer)
  158. {
  159. if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
  160. currentPeer -> address.host == host -> receivedAddress.host &&
  161. currentPeer -> address.port == host -> receivedAddress.port &&
  162. currentPeer -> challenge == header -> challenge)
  163. return NULL;
  164. }
  165. for (currentPeer = host -> peers;
  166. currentPeer < & host -> peers [host -> peerCount];
  167. ++ currentPeer)
  168. {
  169. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
  170. break;
  171. }
  172. if (currentPeer >= & host -> peers [host -> peerCount])
  173. return NULL;
  174. currentPeer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
  175. currentPeer -> challenge = header -> challenge;
  176. currentPeer -> address = host -> receivedAddress;
  177. currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
  178. currentPeer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
  179. currentPeer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
  180. currentPeer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
  181. currentPeer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
  182. currentPeer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
  183. currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
  184. currentPeer -> channelCount = channelCount;
  185. for (channel = currentPeer -> channels;
  186. channel < & currentPeer -> channels [channelCount];
  187. ++ channel)
  188. {
  189. channel -> outgoingReliableSequenceNumber = 0;
  190. channel -> outgoingUnreliableSequenceNumber = 0;
  191. channel -> incomingReliableSequenceNumber = 0;
  192. channel -> incomingUnreliableSequenceNumber = 0;
  193. enet_list_clear (& channel -> incomingReliableCommands);
  194. enet_list_clear (& channel -> incomingUnreliableCommands);
  195. }
  196. mtu = ENET_NET_TO_HOST_16 (command -> connect.mtu);
  197. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  198. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  199. else
  200. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  201. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  202. currentPeer -> mtu = mtu;
  203. if (host -> outgoingBandwidth == 0 &&
  204. currentPeer -> incomingBandwidth == 0)
  205. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  206. else
  207. currentPeer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
  208. ENET_PEER_WINDOW_SIZE_SCALE) *
  209. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  210. if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  211. currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  212. else
  213. if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  214. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  215. if (host -> incomingBandwidth == 0)
  216. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  217. else
  218. windowSize = (host -> incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) *
  219. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  220. if (windowSize > ENET_NET_TO_HOST_32 (command -> connect.windowSize))
  221. windowSize = ENET_NET_TO_HOST_32 (command -> connect.windowSize);
  222. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  223. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  224. else
  225. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  226. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  227. verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT;
  228. verifyCommand.header.channelID = 0xFF;
  229. verifyCommand.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  230. verifyCommand.header.commandLength = sizeof (ENetProtocolVerifyConnect);
  231. verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  232. verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_16 (currentPeer -> mtu);
  233. verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
  234. verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  235. verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  236. verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  237. verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  238. verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  239. verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  240. enet_peer_queue_outgoing_command (currentPeer, & verifyCommand, NULL, 0, 0);
  241. return currentPeer;
  242. }
  243. static void
  244. enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  245. {
  246. ENetPacket * packet;
  247. if (command -> header.commandLength <= sizeof (ENetProtocolSendReliable) ||
  248. command -> header.channelID >= peer -> channelCount ||
  249. peer -> state != ENET_PEER_STATE_CONNECTED)
  250. return;
  251. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable),
  252. command -> header.commandLength - sizeof (ENetProtocolSendReliable),
  253. ENET_PACKET_FLAG_RELIABLE);
  254. enet_peer_queue_incoming_command (peer, command, packet, 0);
  255. }
  256. static void
  257. enet_protocol_handle_send_unsequenced (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  258. {
  259. ENetPacket * packet;
  260. enet_uint32 unsequencedGroup, index;
  261. if (command -> header.commandLength <= sizeof (ENetProtocolSendUnsequenced) ||
  262. command -> header.channelID >= peer -> channelCount ||
  263. peer -> state != ENET_PEER_STATE_CONNECTED)
  264. return;
  265. unsequencedGroup = ENET_NET_TO_HOST_32 (command -> sendUnsequenced.unsequencedGroup);
  266. index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
  267. if (unsequencedGroup >= peer -> incomingUnsequencedGroup + ENET_PEER_UNSEQUENCED_WINDOW_SIZE)
  268. {
  269. peer -> incomingUnsequencedGroup = unsequencedGroup - index;
  270. memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
  271. }
  272. else
  273. if (unsequencedGroup < peer -> incomingUnsequencedGroup ||
  274. peer -> unsequencedWindow [index / 32] & (1 << (index % 32)))
  275. return;
  276. peer -> unsequencedWindow [index / 32] |= 1 << (index % 32);
  277. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced),
  278. command -> header.commandLength - sizeof (ENetProtocolSendUnsequenced),
  279. ENET_PACKET_FLAG_UNSEQUENCED);
  280. enet_peer_queue_incoming_command (peer, command, packet, 0);
  281. }
  282. static void
  283. enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  284. {
  285. ENetPacket * packet;
  286. if (command -> header.commandLength <= sizeof (ENetProtocolSendUnreliable) ||
  287. command -> header.channelID >= peer -> channelCount ||
  288. peer -> state != ENET_PEER_STATE_CONNECTED)
  289. return;
  290. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable),
  291. command -> header.commandLength - sizeof (ENetProtocolSendUnreliable),
  292. 0);
  293. enet_peer_queue_incoming_command (peer, command, packet, 0);
  294. }
  295. static void
  296. enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  297. {
  298. enet_uint32 fragmentNumber,
  299. fragmentCount,
  300. fragmentOffset,
  301. fragmentLength,
  302. startSequenceNumber,
  303. totalLength;
  304. ENetChannel * channel;
  305. ENetListIterator currentCommand;
  306. ENetIncomingCommand * startCommand;
  307. if (command -> header.commandLength <= sizeof (ENetProtocolSendFragment) ||
  308. command -> header.channelID >= peer -> channelCount ||
  309. peer -> state != ENET_PEER_STATE_CONNECTED)
  310. return;
  311. startSequenceNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.startSequenceNumber);
  312. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  313. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  314. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  315. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  316. fragmentLength = command -> header.commandLength - sizeof (ENetProtocolSendFragment);
  317. if (fragmentOffset >= totalLength ||
  318. fragmentOffset + fragmentLength > totalLength ||
  319. fragmentNumber >= fragmentCount)
  320. return;
  321. channel = & peer -> channels [command -> header.channelID];
  322. if (startSequenceNumber <= channel -> incomingReliableSequenceNumber)
  323. return;
  324. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  325. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  326. currentCommand = enet_list_previous (currentCommand))
  327. {
  328. startCommand = (ENetIncomingCommand *) currentCommand;
  329. if (startCommand -> command.header.command == ENET_PROTOCOL_COMMAND_SEND_FRAGMENT &&
  330. startCommand -> command.sendFragment.startSequenceNumber == startSequenceNumber)
  331. break;
  332. }
  333. if (currentCommand == enet_list_end (& channel -> incomingReliableCommands))
  334. {
  335. ENetProtocol hostCommand = * command;
  336. hostCommand.header.reliableSequenceNumber = startSequenceNumber;
  337. hostCommand.sendFragment.startSequenceNumber = startSequenceNumber;
  338. hostCommand.sendFragment.fragmentNumber = fragmentNumber;
  339. hostCommand.sendFragment.fragmentCount = fragmentCount;
  340. hostCommand.sendFragment.fragmentOffset = fragmentOffset;
  341. hostCommand.sendFragment.totalLength = totalLength;
  342. startCommand = enet_peer_queue_incoming_command (peer,
  343. & hostCommand,
  344. enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE),
  345. fragmentCount);
  346. }
  347. else
  348. if (totalLength != startCommand -> packet -> dataLength ||
  349. fragmentCount != startCommand -> fragmentCount)
  350. return;
  351. if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
  352. {
  353. -- startCommand -> fragmentsRemaining;
  354. startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  355. if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
  356. fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
  357. memcpy (startCommand -> packet -> data + fragmentOffset,
  358. (enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
  359. fragmentLength);
  360. }
  361. }
  362. static void
  363. enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  364. {
  365. if (command -> header.commandLength < sizeof (ENetProtocolPing))
  366. return;
  367. }
  368. static void
  369. enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  370. {
  371. if (command -> header.commandLength < sizeof (ENetProtocolBandwidthLimit))
  372. return;
  373. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
  374. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
  375. if (peer -> incomingBandwidth == 0 &&
  376. host -> outgoingBandwidth == 0)
  377. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  378. else
  379. peer -> windowSize = (ENET_MIN (peer -> incomingBandwidth, host -> outgoingBandwidth) /
  380. ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  381. if (peer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  382. peer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  383. else
  384. if (peer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  385. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  386. }
  387. static void
  388. enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  389. {
  390. if (command -> header.commandLength < sizeof (ENetProtocolThrottleConfigure))
  391. return;
  392. peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
  393. peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
  394. peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
  395. }
  396. static void
  397. enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  398. {
  399. if (command -> header.commandLength < sizeof (ENetProtocolDisconnect))
  400. return;
  401. enet_peer_reset_queues (peer);
  402. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  403. {
  404. if (peer -> state == ENET_PEER_STATE_CONNECTION_PENDING) host -> recalculateBandwidthLimits = 1;
  405. enet_peer_reset (peer);
  406. }
  407. else
  408. if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
  409. peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
  410. else
  411. peer -> state = ENET_PEER_STATE_ZOMBIE;
  412. peer -> disconnectData = command -> disconnect.data;
  413. }
  414. static int
  415. enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  416. {
  417. enet_uint32 roundTripTime,
  418. receivedSentTime,
  419. receivedReliableSequenceNumber;
  420. ENetProtocolCommand commandNumber;
  421. if (command -> header.commandLength < sizeof (ENetProtocolAcknowledge))
  422. return 0;
  423. receivedSentTime = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedSentTime);
  424. if (ENET_TIME_LESS (timeCurrent, receivedSentTime))
  425. return 0;
  426. peer -> lastReceiveTime = timeCurrent;
  427. peer -> earliestTimeout = 0;
  428. roundTripTime = ENET_TIME_DIFFERENCE (timeCurrent, receivedSentTime);
  429. enet_peer_throttle (peer, roundTripTime);
  430. peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
  431. if (roundTripTime >= peer -> roundTripTime)
  432. {
  433. peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
  434. peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
  435. }
  436. else
  437. {
  438. peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
  439. peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
  440. }
  441. if (peer -> roundTripTime < peer -> lowestRoundTripTime)
  442. peer -> lowestRoundTripTime = peer -> roundTripTime;
  443. if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
  444. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  445. if (peer -> packetThrottleEpoch == 0 ||
  446. ENET_TIME_DIFFERENCE(timeCurrent, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
  447. {
  448. peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
  449. peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
  450. peer -> lowestRoundTripTime = peer -> roundTripTime;
  451. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  452. peer -> packetThrottleEpoch = timeCurrent;
  453. }
  454. receivedReliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedReliableSequenceNumber);
  455. commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
  456. switch (peer -> state)
  457. {
  458. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  459. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
  460. return 0;
  461. enet_protocol_notify_connect (host, peer, event);
  462. return 1;
  463. case ENET_PEER_STATE_DISCONNECTING:
  464. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
  465. return 0;
  466. enet_protocol_notify_disconnect (host, peer, event);
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. static void
  472. enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  473. {
  474. enet_uint16 mtu;
  475. enet_uint32 windowSize;
  476. if (event == NULL ||
  477. command -> header.commandLength < sizeof (ENetProtocolVerifyConnect) ||
  478. peer -> state != ENET_PEER_STATE_CONNECTING)
  479. return;
  480. if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
  481. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
  482. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
  483. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
  484. {
  485. peer -> state = ENET_PEER_STATE_ZOMBIE;
  486. return;
  487. }
  488. peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
  489. mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
  490. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  491. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  492. else
  493. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  494. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  495. if (mtu < peer -> mtu)
  496. peer -> mtu = mtu;
  497. windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
  498. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  499. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  500. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  501. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  502. if (windowSize < peer -> windowSize)
  503. peer -> windowSize = windowSize;
  504. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
  505. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
  506. enet_protocol_notify_connect (host, peer, event);
  507. }
  508. static int
  509. enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
  510. {
  511. ENetProtocolHeader * header;
  512. ENetProtocol * command;
  513. ENetPeer * peer;
  514. enet_uint8 * currentData;
  515. size_t commandCount;
  516. if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
  517. return 0;
  518. header = (ENetProtocolHeader *) host -> receivedData;
  519. header -> peerID = ENET_NET_TO_HOST_16 (header -> peerID);
  520. header -> sentTime = ENET_NET_TO_HOST_32 (header -> sentTime);
  521. if (header -> peerID == 0xFFFF)
  522. peer = NULL;
  523. else
  524. if (header -> peerID >= host -> peerCount)
  525. return 0;
  526. else
  527. {
  528. peer = & host -> peers [header -> peerID];
  529. if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  530. peer -> state == ENET_PEER_STATE_ZOMBIE ||
  531. (host -> receivedAddress.host != peer -> address.host &&
  532. peer -> address.host != ENET_HOST_BROADCAST) ||
  533. header -> challenge != peer -> challenge)
  534. return 0;
  535. else
  536. {
  537. peer -> address.host = host -> receivedAddress.host;
  538. peer -> address.port = host -> receivedAddress.port;
  539. }
  540. }
  541. if (peer != NULL)
  542. peer -> incomingDataTotal += host -> receivedDataLength;
  543. commandCount = header -> commandCount;
  544. currentData = host -> receivedData + sizeof (ENetProtocolHeader);
  545. while (commandCount > 0 &&
  546. currentData < & host -> receivedData [host -> receivedDataLength])
  547. {
  548. command = (ENetProtocol *) currentData;
  549. if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
  550. break;
  551. command -> header.commandLength = ENET_NET_TO_HOST_32 (command -> header.commandLength);
  552. if (command -> header.commandLength <= 0 ||
  553. command -> header.commandLength > & host -> receivedData [host -> receivedDataLength] - currentData)
  554. break;
  555. -- commandCount;
  556. currentData += command -> header.commandLength;
  557. if (peer == NULL && command -> header.command != ENET_PROTOCOL_COMMAND_CONNECT)
  558. break;
  559. command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> header.reliableSequenceNumber);
  560. switch (command -> header.command)
  561. {
  562. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  563. enet_protocol_handle_acknowledge (host, event, peer, command);
  564. break;
  565. case ENET_PROTOCOL_COMMAND_CONNECT:
  566. peer = enet_protocol_handle_connect (host, header, command);
  567. break;
  568. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  569. enet_protocol_handle_verify_connect (host, event, peer, command);
  570. break;
  571. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  572. enet_protocol_handle_disconnect (host, peer, command);
  573. break;
  574. case ENET_PROTOCOL_COMMAND_PING:
  575. enet_protocol_handle_ping (host, peer, command);
  576. break;
  577. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  578. enet_protocol_handle_send_reliable (host, peer, command);
  579. break;
  580. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  581. enet_protocol_handle_send_unreliable (host, peer, command);
  582. break;
  583. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  584. enet_protocol_handle_send_unsequenced (host, peer, command);
  585. break;
  586. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  587. enet_protocol_handle_send_fragment (host, peer, command);
  588. break;
  589. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  590. enet_protocol_handle_bandwidth_limit (host, peer, command);
  591. break;
  592. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  593. enet_protocol_handle_throttle_configure (host, peer, command);
  594. break;
  595. default:
  596. break;
  597. }
  598. if (peer != NULL &&
  599. (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE) != 0)
  600. {
  601. switch (peer -> state)
  602. {
  603. case ENET_PEER_STATE_DISCONNECTING:
  604. break;
  605. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  606. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  607. enet_peer_queue_acknowledgement (peer, command, header -> sentTime);
  608. break;
  609. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  610. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  611. enet_peer_queue_acknowledgement (peer, command, header -> sentTime);
  612. break;
  613. default:
  614. enet_peer_queue_acknowledgement (peer, command, header -> sentTime);
  615. break;
  616. }
  617. }
  618. }
  619. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  620. return 1;
  621. return 0;
  622. }
  623. static int
  624. enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
  625. {
  626. for (;;)
  627. {
  628. int receivedLength;
  629. ENetBuffer buffer;
  630. buffer.data = host -> receivedData;
  631. buffer.dataLength = sizeof (host -> receivedData);
  632. receivedLength = enet_socket_receive (host -> socket,
  633. & host -> receivedAddress,
  634. & buffer,
  635. 1);
  636. if (receivedLength < 0)
  637. return -1;
  638. if (receivedLength == 0)
  639. return 0;
  640. host -> receivedDataLength = receivedLength;
  641. switch (enet_protocol_handle_incoming_commands (host, event))
  642. {
  643. case 1:
  644. return 1;
  645. case -1:
  646. return -1;
  647. default:
  648. break;
  649. }
  650. }
  651. return -1;
  652. }
  653. static void
  654. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  655. {
  656. ENetProtocol * command = & host -> commands [host -> commandCount];
  657. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  658. ENetAcknowledgement * acknowledgement;
  659. ENetListIterator currentAcknowledgement;
  660. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  661. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  662. {
  663. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  664. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  665. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  666. break;
  667. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  668. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  669. buffer -> data = command;
  670. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  671. host -> packetSize += buffer -> dataLength;
  672. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  673. command -> header.channelID = acknowledgement -> command.header.channelID;
  674. command -> header.flags = 0;
  675. command -> header.commandLength = ENET_HOST_TO_NET_32 (sizeof (ENetProtocolAcknowledge));
  676. command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_32 (acknowledgement -> command.header.reliableSequenceNumber);
  677. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_32 (acknowledgement -> sentTime);
  678. if (acknowledgement -> command.header.command == ENET_PROTOCOL_COMMAND_DISCONNECT)
  679. peer -> state = ENET_PEER_STATE_ZOMBIE;
  680. enet_list_remove (& acknowledgement -> acknowledgementList);
  681. enet_free (acknowledgement);
  682. ++ command;
  683. ++ buffer;
  684. }
  685. host -> commandCount = command - host -> commands;
  686. host -> bufferCount = buffer - host -> buffers;
  687. }
  688. static void
  689. enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  690. {
  691. ENetProtocol * command = & host -> commands [host -> commandCount];
  692. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  693. ENetOutgoingCommand * outgoingCommand;
  694. ENetListIterator currentCommand;
  695. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  696. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  697. {
  698. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  699. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  700. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  701. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength ||
  702. (outgoingCommand -> packet != NULL &&
  703. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength +
  704. outgoingCommand -> packet -> dataLength))
  705. break;
  706. currentCommand = enet_list_next (currentCommand);
  707. if (outgoingCommand -> packet != NULL)
  708. {
  709. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  710. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  711. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  712. {
  713. -- outgoingCommand -> packet -> referenceCount;
  714. if (outgoingCommand -> packet -> referenceCount == 0)
  715. enet_packet_destroy (outgoingCommand -> packet);
  716. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  717. enet_free (outgoingCommand);
  718. continue;
  719. }
  720. }
  721. buffer -> data = command;
  722. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  723. host -> packetSize += buffer -> dataLength;
  724. * command = outgoingCommand -> command;
  725. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  726. if (outgoingCommand -> packet != NULL)
  727. {
  728. ++ buffer;
  729. buffer -> data = outgoingCommand -> packet -> data;
  730. buffer -> dataLength = outgoingCommand -> packet -> dataLength;
  731. command -> header.commandLength += buffer -> dataLength;
  732. host -> packetSize += buffer -> dataLength;
  733. enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  734. }
  735. else
  736. enet_free (outgoingCommand);
  737. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  738. ++ command;
  739. ++ buffer;
  740. }
  741. host -> commandCount = command - host -> commands;
  742. host -> bufferCount = buffer - host -> buffers;
  743. }
  744. static int
  745. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  746. {
  747. ENetOutgoingCommand * outgoingCommand;
  748. ENetListIterator currentCommand;
  749. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  750. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  751. {
  752. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  753. currentCommand = enet_list_next (currentCommand);
  754. if (ENET_TIME_DIFFERENCE (timeCurrent, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  755. continue;
  756. if(peer -> earliestTimeout == 0 ||
  757. ENET_TIME_LESS(outgoingCommand -> sentTime, peer -> earliestTimeout))
  758. peer -> earliestTimeout = outgoingCommand -> sentTime;
  759. if (peer -> earliestTimeout != 0 &&
  760. (ENET_TIME_DIFFERENCE(timeCurrent, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MAXIMUM ||
  761. (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit &&
  762. ENET_TIME_DIFFERENCE(timeCurrent, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MINIMUM)))
  763. {
  764. enet_protocol_notify_disconnect (host, peer, event);
  765. return 1;
  766. }
  767. if (outgoingCommand -> packet != NULL)
  768. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  769. ++ peer -> packetsLost;
  770. outgoingCommand -> roundTripTimeout *= 2;
  771. enet_list_insert (enet_list_begin (& peer -> outgoingReliableCommands),
  772. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  773. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  774. enet_list_empty (& peer -> sentReliableCommands) == 0)
  775. {
  776. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  777. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  778. }
  779. }
  780. return 0;
  781. }
  782. static void
  783. enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  784. {
  785. ENetProtocol * command = & host -> commands [host -> commandCount];
  786. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  787. ENetOutgoingCommand * outgoingCommand;
  788. ENetListIterator currentCommand;
  789. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  790. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  791. {
  792. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  793. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  794. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  795. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength)
  796. break;
  797. currentCommand = enet_list_next (currentCommand);
  798. if (outgoingCommand -> packet != NULL)
  799. {
  800. if ((enet_uint16) (peer -> mtu - host -> packetSize) <
  801. (enet_uint16) (outgoingCommand -> command.header.commandLength +
  802. outgoingCommand -> fragmentLength) ||
  803. peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > peer -> windowSize)
  804. break;
  805. }
  806. if (outgoingCommand -> roundTripTimeout == 0)
  807. {
  808. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  809. outgoingCommand -> roundTripTimeoutLimit = ENET_PEER_TIMEOUT_LIMIT * outgoingCommand -> roundTripTimeout;
  810. }
  811. if (enet_list_empty (& peer -> sentReliableCommands))
  812. peer -> nextTimeout = timeCurrent + outgoingCommand -> roundTripTimeout;
  813. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  814. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  815. outgoingCommand -> sentTime = timeCurrent;
  816. buffer -> data = command;
  817. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  818. host -> packetSize += buffer -> dataLength;
  819. * command = outgoingCommand -> command;
  820. if (outgoingCommand -> packet != NULL)
  821. {
  822. ++ buffer;
  823. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  824. buffer -> dataLength = outgoingCommand -> fragmentLength;
  825. command -> header.commandLength += outgoingCommand -> fragmentLength;
  826. host -> packetSize += outgoingCommand -> fragmentLength;
  827. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  828. }
  829. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  830. ++ peer -> packetsSent;
  831. ++ command;
  832. ++ buffer;
  833. }
  834. host -> commandCount = command - host -> commands;
  835. host -> bufferCount = buffer - host -> buffers;
  836. }
  837. static int
  838. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  839. {
  840. size_t packetsSent = 1;
  841. ENetProtocolHeader header;
  842. ENetPeer * currentPeer;
  843. int sentLength;
  844. while (packetsSent > 0)
  845. for (currentPeer = host -> peers,
  846. packetsSent = 0;
  847. currentPeer < & host -> peers [host -> peerCount];
  848. ++ currentPeer)
  849. {
  850. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  851. currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  852. continue;
  853. host -> commandCount = 0;
  854. host -> bufferCount = 1;
  855. host -> packetSize = sizeof (ENetProtocolHeader);
  856. if (enet_list_empty (& currentPeer -> acknowledgements) == 0)
  857. enet_protocol_send_acknowledgements (host, currentPeer);
  858. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol))
  859. {
  860. if (checkForTimeouts != 0 &&
  861. enet_list_empty (& currentPeer -> sentReliableCommands) == 0 &&
  862. ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) &&
  863. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  864. return 1;
  865. }
  866. if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0)
  867. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  868. else
  869. if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
  870. ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
  871. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  872. {
  873. enet_peer_ping (currentPeer);
  874. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  875. }
  876. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol) &&
  877. enet_list_empty (& currentPeer -> outgoingUnreliableCommands) == 0)
  878. enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
  879. if (host -> commandCount == 0)
  880. continue;
  881. if (currentPeer -> packetLossEpoch == 0)
  882. currentPeer -> packetLossEpoch = timeCurrent;
  883. else
  884. if (ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  885. currentPeer -> packetsSent > 0)
  886. {
  887. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  888. #ifdef ENET_DEBUG
  889. #ifdef WIN32
  890. printf (
  891. #else
  892. fprintf (stderr,
  893. #endif
  894. "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, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands));
  895. #endif
  896. currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
  897. if (packetLoss >= currentPeer -> packetLoss)
  898. {
  899. currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
  900. currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
  901. }
  902. else
  903. {
  904. currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
  905. currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
  906. }
  907. currentPeer -> packetLossEpoch = timeCurrent;
  908. currentPeer -> packetsSent = 0;
  909. currentPeer -> packetsLost = 0;
  910. }
  911. header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID);
  912. header.flags = 0;
  913. header.commandCount = host -> commandCount;
  914. header.sentTime = ENET_HOST_TO_NET_32 (timeCurrent);
  915. header.challenge = currentPeer -> challenge;
  916. host -> buffers -> data = & header;
  917. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  918. currentPeer -> lastSendTime = timeCurrent;
  919. ++ packetsSent;
  920. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  921. enet_protocol_remove_sent_unreliable_commands (currentPeer);
  922. if (sentLength < 0)
  923. return -1;
  924. }
  925. return 0;
  926. }
  927. /** Sends any queued packets on the host specified to its designated peers.
  928. @param host host to flush
  929. @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().
  930. @ingroup host
  931. */
  932. void
  933. enet_host_flush (ENetHost * host)
  934. {
  935. timeCurrent = enet_time_get ();
  936. enet_protocol_send_outgoing_commands (host, NULL, 0);
  937. }
  938. /** Waits for events on the host specified and shuttles packets between
  939. the host and its peers.
  940. @param host host to service
  941. @param event an event structure where event details will be placed if one occurs
  942. if event == NULL then no events will be delivered
  943. @param timeout number of milliseconds that ENet should wait for events
  944. @retval > 0 if an event occurred within the specified time limit
  945. @retval 0 if no event occurred
  946. @retval < 0 on failure
  947. @remarks enet_host_service should be called fairly regularly for adequate performance
  948. @ingroup host
  949. */
  950. int
  951. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  952. {
  953. enet_uint32 waitCondition;
  954. if (event != NULL)
  955. {
  956. event -> type = ENET_EVENT_TYPE_NONE;
  957. event -> peer = NULL;
  958. event -> packet = NULL;
  959. switch (enet_protocol_dispatch_incoming_commands (host, event))
  960. {
  961. case 1:
  962. return 1;
  963. case -1:
  964. perror ("Error dispatching incoming packets");
  965. return -1;
  966. default:
  967. break;
  968. }
  969. }
  970. timeCurrent = enet_time_get ();
  971. timeout += timeCurrent;
  972. do
  973. {
  974. if (ENET_TIME_DIFFERENCE (timeCurrent, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  975. enet_host_bandwidth_throttle (host);
  976. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  977. {
  978. case 1:
  979. return 1;
  980. case -1:
  981. perror ("Error sending outgoing packets");
  982. return -1;
  983. default:
  984. break;
  985. }
  986. switch (enet_protocol_receive_incoming_commands (host, event))
  987. {
  988. case 1:
  989. return 1;
  990. case -1:
  991. perror ("Error receiving incoming packets");
  992. return -1;
  993. default:
  994. break;
  995. }
  996. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  997. {
  998. case 1:
  999. return 1;
  1000. case -1:
  1001. perror ("Error sending outgoing packets");
  1002. return -1;
  1003. default:
  1004. break;
  1005. }
  1006. if (event != NULL)
  1007. {
  1008. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1009. {
  1010. case 1:
  1011. return 1;
  1012. case -1:
  1013. perror ("Error dispatching incoming packets");
  1014. return -1;
  1015. default:
  1016. break;
  1017. }
  1018. }
  1019. timeCurrent = enet_time_get ();
  1020. if (ENET_TIME_GREATER_EQUAL (timeCurrent, timeout))
  1021. return 0;
  1022. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  1023. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, timeCurrent)) != 0)
  1024. return -1;
  1025. timeCurrent = enet_time_get ();
  1026. } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  1027. return 0;
  1028. }