protocol.c 53 KB

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