protocol.c 51 KB

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