protocol.c 69 KB

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