protocol.c 52 KB

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