protocol.c 53 KB

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