protocol.c 55 KB

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