protocol.c 51 KB

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