protocol.c 67 KB

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