LLUDPServer.cs 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Reflection;
  34. using System.Threading;
  35. using log4net;
  36. using NDesk.Options;
  37. using Nini.Config;
  38. using OpenMetaverse.Packets;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Console;
  41. using OpenSim.Framework.Monitoring;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenMetaverse;
  44. using TokenBucket = OpenSim.Region.ClientStack.LindenUDP.TokenBucket;
  45. namespace OpenSim.Region.ClientStack.LindenUDP
  46. {
  47. /// <summary>
  48. /// A shim around LLUDPServer that implements the IClientNetworkServer interface
  49. /// </summary>
  50. public sealed class LLUDPServerShim : IClientNetworkServer
  51. {
  52. LLUDPServer m_udpServer;
  53. public LLUDPServerShim()
  54. {
  55. }
  56. public void Initialise(IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port, IConfigSource configSource, AgentCircuitManager circuitManager)
  57. {
  58. m_udpServer = new LLUDPServer(listenIP, ref port, proxyPortOffsetParm, allow_alternate_port, configSource, circuitManager);
  59. }
  60. public void AddScene(IScene scene)
  61. {
  62. m_udpServer.AddScene(scene);
  63. StatsManager.RegisterStat(
  64. new Stat(
  65. "IncomingUDPReceivesCount",
  66. "Number of UDP receives performed",
  67. "Number of UDP receives performed",
  68. "",
  69. "clientstack",
  70. scene.Name,
  71. StatType.Pull,
  72. MeasuresOfInterest.AverageChangeOverTime,
  73. stat => stat.Value = m_udpServer.UdpReceives,
  74. StatVerbosity.Debug));
  75. StatsManager.RegisterStat(
  76. new Stat(
  77. "IncomingPacketsProcessedCount",
  78. "Number of inbound LL protocol packets processed",
  79. "Number of inbound LL protocol packets processed",
  80. "",
  81. "clientstack",
  82. scene.Name,
  83. StatType.Pull,
  84. MeasuresOfInterest.AverageChangeOverTime,
  85. stat => stat.Value = m_udpServer.IncomingPacketsProcessed,
  86. StatVerbosity.Debug));
  87. StatsManager.RegisterStat(
  88. new Stat(
  89. "OutgoingUDPSendsCount",
  90. "Number of UDP sends performed",
  91. "Number of UDP sends performed",
  92. "",
  93. "clientstack",
  94. scene.Name,
  95. StatType.Pull,
  96. MeasuresOfInterest.AverageChangeOverTime,
  97. stat => stat.Value = m_udpServer.UdpSends,
  98. StatVerbosity.Debug));
  99. StatsManager.RegisterStat(
  100. new Stat(
  101. "AverageUDPProcessTime",
  102. "Average number of milliseconds taken to process each incoming UDP packet in a sample.",
  103. "This is for initial receive processing which is separate from the later client LL packet processing stage.",
  104. "ms",
  105. "clientstack",
  106. scene.Name,
  107. StatType.Pull,
  108. MeasuresOfInterest.None,
  109. stat => stat.Value = m_udpServer.AverageReceiveTicksForLastSamplePeriod / TimeSpan.TicksPerMillisecond,
  110. // stat =>
  111. // stat.Value = Math.Round(m_udpServer.AverageReceiveTicksForLastSamplePeriod / TimeSpan.TicksPerMillisecond, 7),
  112. StatVerbosity.Debug));
  113. }
  114. public bool HandlesRegion(Location x)
  115. {
  116. return m_udpServer.HandlesRegion(x);
  117. }
  118. public void Start()
  119. {
  120. m_udpServer.Start();
  121. }
  122. public void Stop()
  123. {
  124. m_udpServer.Stop();
  125. }
  126. }
  127. /// <summary>
  128. /// The LLUDP server for a region. This handles incoming and outgoing
  129. /// packets for all UDP connections to the region
  130. /// </summary>
  131. public class LLUDPServer : OpenSimUDPBase
  132. {
  133. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  134. /// <summary>Maximum transmission unit, or UDP packet size, for the LLUDP protocol</summary>
  135. public const int MTU = 1400;
  136. /// <summary>
  137. /// Default packet debug level given to new clients
  138. /// </summary>
  139. public int DefaultClientPacketDebugLevel { get; set; }
  140. /// <summary>The measured resolution of Environment.TickCount</summary>
  141. public readonly float TickCountResolution;
  142. /// <summary>Number of prim updates to put on the queue each time the
  143. /// OnQueueEmpty event is triggered for updates</summary>
  144. public readonly int PrimUpdatesPerCallback;
  145. /// <summary>Number of texture packets to put on the queue each time the
  146. /// OnQueueEmpty event is triggered for textures</summary>
  147. public readonly int TextureSendLimit;
  148. /// <summary>Handlers for incoming packets</summary>
  149. //PacketEventDictionary packetEvents = new PacketEventDictionary();
  150. /// <summary>Incoming packets that are awaiting handling</summary>
  151. private OpenMetaverse.BlockingQueue<IncomingPacket> packetInbox = new OpenMetaverse.BlockingQueue<IncomingPacket>();
  152. /// <summary></summary>
  153. //private UDPClientCollection m_clients = new UDPClientCollection();
  154. /// <summary>Bandwidth throttle for this UDP server</summary>
  155. protected TokenBucket m_throttle;
  156. /// <summary>Bandwidth throttle rates for this UDP server</summary>
  157. public ThrottleRates ThrottleRates { get; private set; }
  158. /// <summary>Manages authentication for agent circuits</summary>
  159. private AgentCircuitManager m_circuitManager;
  160. /// <summary>Reference to the scene this UDP server is attached to</summary>
  161. protected Scene m_scene;
  162. /// <summary>The X/Y coordinates of the scene this UDP server is attached to</summary>
  163. private Location m_location;
  164. /// <summary>The size of the receive buffer for the UDP socket. This value
  165. /// is passed up to the operating system and used in the system networking
  166. /// stack. Use zero to leave this value as the default</summary>
  167. private int m_recvBufferSize;
  168. /// <summary>Flag to process packets asynchronously or synchronously</summary>
  169. private bool m_asyncPacketHandling;
  170. /// <summary>Tracks whether or not a packet was sent each round so we know
  171. /// whether or not to sleep</summary>
  172. private bool m_packetSent;
  173. /// <summary>Environment.TickCount of the last time that packet stats were reported to the scene</summary>
  174. private int m_elapsedMSSinceLastStatReport = 0;
  175. /// <summary>Environment.TickCount of the last time the outgoing packet handler executed</summary>
  176. private int m_tickLastOutgoingPacketHandler;
  177. /// <summary>Keeps track of the number of elapsed milliseconds since the last time the outgoing packet handler looped</summary>
  178. private int m_elapsedMSOutgoingPacketHandler;
  179. /// <summary>Keeps track of the number of 100 millisecond periods elapsed in the outgoing packet handler executed</summary>
  180. private int m_elapsed100MSOutgoingPacketHandler;
  181. /// <summary>Keeps track of the number of 500 millisecond periods elapsed in the outgoing packet handler executed</summary>
  182. private int m_elapsed500MSOutgoingPacketHandler;
  183. /// <summary>Flag to signal when clients should check for resends</summary>
  184. protected bool m_resendUnacked;
  185. /// <summary>Flag to signal when clients should send ACKs</summary>
  186. protected bool m_sendAcks;
  187. /// <summary>Flag to signal when clients should send pings</summary>
  188. protected bool m_sendPing;
  189. /// <summary>
  190. /// Event used to signal when queued packets are available for sending.
  191. /// </summary>
  192. /// <remarks>
  193. /// This allows the outbound loop to only operate when there is data to send rather than continuously polling.
  194. /// Some data is sent immediately and not queued. That data would not trigger this event.
  195. /// </remarks>
  196. private AutoResetEvent m_dataPresentEvent = new AutoResetEvent(false);
  197. private Pool<IncomingPacket> m_incomingPacketPool;
  198. /// <summary>
  199. /// Stat for number of packets in the main pool awaiting use.
  200. /// </summary>
  201. private Stat m_poolCountStat;
  202. /// <summary>
  203. /// Stat for number of packets in the inbound packet pool awaiting use.
  204. /// </summary>
  205. private Stat m_incomingPacketPoolStat;
  206. private int m_defaultRTO = 0;
  207. private int m_maxRTO = 0;
  208. private int m_ackTimeout = 0;
  209. private int m_pausedAckTimeout = 0;
  210. private bool m_disableFacelights = false;
  211. public Socket Server { get { return null; } }
  212. private int m_malformedCount = 0; // Guard against a spamming attack
  213. /// <summary>
  214. /// Record current outgoing client for monitoring purposes.
  215. /// </summary>
  216. private IClientAPI m_currentOutgoingClient;
  217. /// <summary>
  218. /// Recording current incoming client for monitoring purposes.
  219. /// </summary>
  220. private IClientAPI m_currentIncomingClient;
  221. public LLUDPServer(
  222. IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port,
  223. IConfigSource configSource, AgentCircuitManager circuitManager)
  224. : base(listenIP, (int)port)
  225. {
  226. #region Environment.TickCount Measurement
  227. // Measure the resolution of Environment.TickCount
  228. TickCountResolution = 0f;
  229. for (int i = 0; i < 5; i++)
  230. {
  231. int start = Environment.TickCount;
  232. int now = start;
  233. while (now == start)
  234. now = Environment.TickCount;
  235. TickCountResolution += (float)(now - start) * 0.2f;
  236. }
  237. m_log.Info("[LLUDPSERVER]: Average Environment.TickCount resolution: " + TickCountResolution + "ms");
  238. TickCountResolution = (float)Math.Ceiling(TickCountResolution);
  239. #endregion Environment.TickCount Measurement
  240. m_circuitManager = circuitManager;
  241. int sceneThrottleBps = 0;
  242. bool usePools = false;
  243. IConfig config = configSource.Configs["ClientStack.LindenUDP"];
  244. if (config != null)
  245. {
  246. m_asyncPacketHandling = config.GetBoolean("async_packet_handling", true);
  247. m_recvBufferSize = config.GetInt("client_socket_rcvbuf_size", 0);
  248. sceneThrottleBps = config.GetInt("scene_throttle_max_bps", 0);
  249. PrimUpdatesPerCallback = config.GetInt("PrimUpdatesPerCallback", 100);
  250. TextureSendLimit = config.GetInt("TextureSendLimit", 20);
  251. m_defaultRTO = config.GetInt("DefaultRTO", 0);
  252. m_maxRTO = config.GetInt("MaxRTO", 0);
  253. m_disableFacelights = config.GetBoolean("DisableFacelights", false);
  254. m_ackTimeout = 1000 * config.GetInt("AckTimeout", 60);
  255. m_pausedAckTimeout = 1000 * config.GetInt("PausedAckTimeout", 300);
  256. }
  257. else
  258. {
  259. PrimUpdatesPerCallback = 100;
  260. TextureSendLimit = 20;
  261. m_ackTimeout = 1000 * 60; // 1 minute
  262. m_pausedAckTimeout = 1000 * 300; // 5 minutes
  263. }
  264. // FIXME: This actually only needs to be done once since the PacketPool is shared across all servers.
  265. // However, there is no harm in temporarily doing it multiple times.
  266. IConfig packetConfig = configSource.Configs["PacketPool"];
  267. if (packetConfig != null)
  268. {
  269. PacketPool.Instance.RecyclePackets = packetConfig.GetBoolean("RecyclePackets", true);
  270. PacketPool.Instance.RecycleDataBlocks = packetConfig.GetBoolean("RecycleDataBlocks", true);
  271. usePools = packetConfig.GetBoolean("RecycleBaseUDPPackets", usePools);
  272. }
  273. #region BinaryStats
  274. config = configSource.Configs["Statistics.Binary"];
  275. m_shouldCollectStats = false;
  276. if (config != null)
  277. {
  278. m_shouldCollectStats = config.GetBoolean("Enabled", false);
  279. binStatsMaxFilesize = TimeSpan.FromSeconds(config.GetInt("packet_headers_period_seconds", 300));
  280. binStatsDir = config.GetString("stats_dir", ".");
  281. m_aggregatedBWStats = config.GetBoolean("aggregatedBWStats", false);
  282. }
  283. #endregion BinaryStats
  284. m_throttle = new TokenBucket(null, sceneThrottleBps);
  285. ThrottleRates = new ThrottleRates(configSource);
  286. if (usePools)
  287. EnablePools();
  288. }
  289. public void Start()
  290. {
  291. StartInbound();
  292. StartOutbound();
  293. m_elapsedMSSinceLastStatReport = Environment.TickCount;
  294. }
  295. private void StartInbound()
  296. {
  297. m_log.InfoFormat(
  298. "[LLUDPSERVER]: Starting inbound packet processing for the LLUDP server in {0} mode with UsePools = {1}",
  299. m_asyncPacketHandling ? "asynchronous" : "synchronous", UsePools);
  300. base.StartInbound(m_recvBufferSize, m_asyncPacketHandling);
  301. // This thread will process the packets received that are placed on the packetInbox
  302. Watchdog.StartThread(
  303. IncomingPacketHandler,
  304. string.Format("Incoming Packets ({0})", m_scene.RegionInfo.RegionName),
  305. ThreadPriority.Normal,
  306. false,
  307. true,
  308. GetWatchdogIncomingAlarmData,
  309. Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS);
  310. }
  311. private new void StartOutbound()
  312. {
  313. m_log.Info("[LLUDPSERVER]: Starting outbound packet processing for the LLUDP server");
  314. base.StartOutbound();
  315. Watchdog.StartThread(
  316. OutgoingPacketHandler,
  317. string.Format("Outgoing Packets ({0})", m_scene.RegionInfo.RegionName),
  318. ThreadPriority.Normal,
  319. false,
  320. true,
  321. GetWatchdogOutgoingAlarmData,
  322. Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS);
  323. }
  324. public void Stop()
  325. {
  326. m_log.Info("[LLUDPSERVER]: Shutting down the LLUDP server for " + m_scene.RegionInfo.RegionName);
  327. base.StopOutbound();
  328. base.StopInbound();
  329. }
  330. protected override bool EnablePools()
  331. {
  332. if (!UsePools)
  333. {
  334. base.EnablePools();
  335. m_incomingPacketPool = new Pool<IncomingPacket>(() => new IncomingPacket(), 500);
  336. return true;
  337. }
  338. return false;
  339. }
  340. protected override bool DisablePools()
  341. {
  342. if (UsePools)
  343. {
  344. base.DisablePools();
  345. StatsManager.DeregisterStat(m_incomingPacketPoolStat);
  346. // We won't null out the pool to avoid a race condition with code that may be in the middle of using it.
  347. return true;
  348. }
  349. return false;
  350. }
  351. /// <summary>
  352. /// This is a seperate method so that it can be called once we have an m_scene to distinguish different scene
  353. /// stats.
  354. /// </summary>
  355. private void EnablePoolStats()
  356. {
  357. m_poolCountStat
  358. = new Stat(
  359. "UDPPacketBufferPoolCount",
  360. "Objects within the UDPPacketBuffer pool",
  361. "The number of objects currently stored within the UDPPacketBuffer pool",
  362. "",
  363. "clientstack",
  364. m_scene.Name,
  365. StatType.Pull,
  366. stat => stat.Value = Pool.Count,
  367. StatVerbosity.Debug);
  368. StatsManager.RegisterStat(m_poolCountStat);
  369. m_incomingPacketPoolStat
  370. = new Stat(
  371. "IncomingPacketPoolCount",
  372. "Objects within incoming packet pool",
  373. "The number of objects currently stored within the incoming packet pool",
  374. "",
  375. "clientstack",
  376. m_scene.Name,
  377. StatType.Pull,
  378. stat => stat.Value = m_incomingPacketPool.Count,
  379. StatVerbosity.Debug);
  380. StatsManager.RegisterStat(m_incomingPacketPoolStat);
  381. }
  382. /// <summary>
  383. /// Disables pool stats.
  384. /// </summary>
  385. private void DisablePoolStats()
  386. {
  387. StatsManager.DeregisterStat(m_poolCountStat);
  388. m_poolCountStat = null;
  389. StatsManager.DeregisterStat(m_incomingPacketPoolStat);
  390. m_incomingPacketPoolStat = null;
  391. }
  392. /// <summary>
  393. /// If the outgoing UDP thread times out, then return client that was being processed to help with debugging.
  394. /// </summary>
  395. /// <returns></returns>
  396. private string GetWatchdogIncomingAlarmData()
  397. {
  398. return string.Format(
  399. "Client is {0}",
  400. m_currentIncomingClient != null ? m_currentIncomingClient.Name : "none");
  401. }
  402. /// <summary>
  403. /// If the outgoing UDP thread times out, then return client that was being processed to help with debugging.
  404. /// </summary>
  405. /// <returns></returns>
  406. private string GetWatchdogOutgoingAlarmData()
  407. {
  408. return string.Format(
  409. "Client is {0}",
  410. m_currentOutgoingClient != null ? m_currentOutgoingClient.Name : "none");
  411. }
  412. public void AddScene(IScene scene)
  413. {
  414. if (m_scene != null)
  415. {
  416. m_log.Error("[LLUDPSERVER]: AddScene() called on an LLUDPServer that already has a scene");
  417. return;
  418. }
  419. if (!(scene is Scene))
  420. {
  421. m_log.Error("[LLUDPSERVER]: AddScene() called with an unrecognized scene type " + scene.GetType());
  422. return;
  423. }
  424. m_scene = (Scene)scene;
  425. m_location = new Location(m_scene.RegionInfo.RegionHandle);
  426. StatsManager.RegisterStat(
  427. new Stat(
  428. "InboxPacketsCount",
  429. "Number of LL protocol packets waiting for the second stage of processing after initial receive.",
  430. "Number of LL protocol packets waiting for the second stage of processing after initial receive.",
  431. "",
  432. "clientstack",
  433. scene.Name,
  434. StatType.Pull,
  435. MeasuresOfInterest.AverageChangeOverTime,
  436. stat => stat.Value = packetInbox.Count,
  437. StatVerbosity.Debug));
  438. // XXX: These stats are also pool stats but we register them separately since they are currently not
  439. // turned on and off by EnablePools()/DisablePools()
  440. StatsManager.RegisterStat(
  441. new PercentageStat(
  442. "PacketsReused",
  443. "Packets reused",
  444. "Number of packets reused out of all requests to the packet pool",
  445. "clientstack",
  446. m_scene.Name,
  447. StatType.Pull,
  448. stat =>
  449. { PercentageStat pstat = (PercentageStat)stat;
  450. pstat.Consequent = PacketPool.Instance.PacketsRequested;
  451. pstat.Antecedent = PacketPool.Instance.PacketsReused; },
  452. StatVerbosity.Debug));
  453. StatsManager.RegisterStat(
  454. new PercentageStat(
  455. "PacketDataBlocksReused",
  456. "Packet data blocks reused",
  457. "Number of data blocks reused out of all requests to the packet pool",
  458. "clientstack",
  459. m_scene.Name,
  460. StatType.Pull,
  461. stat =>
  462. { PercentageStat pstat = (PercentageStat)stat;
  463. pstat.Consequent = PacketPool.Instance.BlocksRequested;
  464. pstat.Antecedent = PacketPool.Instance.BlocksReused; },
  465. StatVerbosity.Debug));
  466. StatsManager.RegisterStat(
  467. new Stat(
  468. "PacketsPoolCount",
  469. "Objects within the packet pool",
  470. "The number of objects currently stored within the packet pool",
  471. "",
  472. "clientstack",
  473. m_scene.Name,
  474. StatType.Pull,
  475. stat => stat.Value = PacketPool.Instance.PacketsPooled,
  476. StatVerbosity.Debug));
  477. StatsManager.RegisterStat(
  478. new Stat(
  479. "PacketDataBlocksPoolCount",
  480. "Objects within the packet data block pool",
  481. "The number of objects currently stored within the packet data block pool",
  482. "",
  483. "clientstack",
  484. m_scene.Name,
  485. StatType.Pull,
  486. stat => stat.Value = PacketPool.Instance.BlocksPooled,
  487. StatVerbosity.Debug));
  488. // We delay enabling pool stats to AddScene() instead of Initialize() so that we can distinguish pool stats by
  489. // scene name
  490. if (UsePools)
  491. EnablePoolStats();
  492. MainConsole.Instance.Commands.AddCommand(
  493. "Debug", false, "debug lludp packet",
  494. "debug lludp packet [--default] <level> [<avatar-first-name> <avatar-last-name>]",
  495. "Turn on packet debugging",
  496. "If level > 255 then all incoming and outgoing packets are logged.\n"
  497. + "If level <= 255 then incoming AgentUpdate and outgoing SimStats and SimulatorViewerTimeMessage packets are not logged.\n"
  498. + "If level <= 200 then incoming RequestImage and outgoing ImagePacket, ImageData, LayerData and CoarseLocationUpdate packets are not logged.\n"
  499. + "If level <= 100 then incoming ViewerEffect and AgentAnimation and outgoing ViewerEffect and AvatarAnimation packets are not logged.\n"
  500. + "If level <= 50 then outgoing ImprovedTerseObjectUpdate packets are not logged.\n"
  501. + "If level <= 0 then no packets are logged.\n"
  502. + "If --default is specified then the level becomes the default logging level for all subsequent agents.\n"
  503. + "In this case, you cannot also specify an avatar name.\n"
  504. + "If an avatar name is given then only packets from that avatar are logged.",
  505. HandlePacketCommand);
  506. MainConsole.Instance.Commands.AddCommand(
  507. "Debug",
  508. false,
  509. "debug lludp start",
  510. "debug lludp start <in|out|all>",
  511. "Control LLUDP packet processing.",
  512. "No effect if packet processing has already started.\n"
  513. + "in - start inbound processing.\n"
  514. + "out - start outbound processing.\n"
  515. + "all - start in and outbound processing.\n",
  516. HandleStartCommand);
  517. MainConsole.Instance.Commands.AddCommand(
  518. "Debug",
  519. false,
  520. "debug lludp stop",
  521. "debug lludp stop <in|out|all>",
  522. "Stop LLUDP packet processing.",
  523. "No effect if packet processing has already stopped.\n"
  524. + "in - stop inbound processing.\n"
  525. + "out - stop outbound processing.\n"
  526. + "all - stop in and outbound processing.\n",
  527. HandleStopCommand);
  528. MainConsole.Instance.Commands.AddCommand(
  529. "Debug",
  530. false,
  531. "debug lludp pool",
  532. "debug lludp pool <on|off>",
  533. "Turn object pooling within the lludp component on or off.",
  534. HandlePoolCommand);
  535. MainConsole.Instance.Commands.AddCommand(
  536. "Debug",
  537. false,
  538. "debug lludp status",
  539. "debug lludp status",
  540. "Return status of LLUDP packet processing.",
  541. HandleStatusCommand);
  542. MainConsole.Instance.Commands.AddCommand(
  543. "Debug",
  544. false,
  545. "debug lludp toggle agentupdate",
  546. "debug lludp toggle agentupdate",
  547. "Toggle whether agentupdate packets are processed or simply discarded.",
  548. HandleAgentUpdateCommand);
  549. }
  550. private void HandlePacketCommand(string module, string[] args)
  551. {
  552. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  553. return;
  554. bool setAsDefaultLevel = false;
  555. OptionSet optionSet = new OptionSet().Add("default", o => setAsDefaultLevel = o != null);
  556. List<string> filteredArgs = optionSet.Parse(args);
  557. string name = null;
  558. if (filteredArgs.Count == 6)
  559. {
  560. if (!setAsDefaultLevel)
  561. {
  562. name = string.Format("{0} {1}", filteredArgs[4], filteredArgs[5]);
  563. }
  564. else
  565. {
  566. MainConsole.Instance.OutputFormat("ERROR: Cannot specify a user name when setting default logging level");
  567. return;
  568. }
  569. }
  570. if (filteredArgs.Count > 3)
  571. {
  572. int newDebug;
  573. if (int.TryParse(filteredArgs[3], out newDebug))
  574. {
  575. if (setAsDefaultLevel)
  576. {
  577. DefaultClientPacketDebugLevel = newDebug;
  578. MainConsole.Instance.OutputFormat(
  579. "Debug packet debug for new clients set to {0}", DefaultClientPacketDebugLevel);
  580. }
  581. else
  582. {
  583. m_scene.ForEachScenePresence(sp =>
  584. {
  585. if (name == null || sp.Name == name)
  586. {
  587. MainConsole.Instance.OutputFormat(
  588. "Packet debug for {0} ({1}) set to {2} in {3}",
  589. sp.Name, sp.IsChildAgent ? "child" : "root", newDebug, m_scene.Name);
  590. sp.ControllingClient.DebugPacketLevel = newDebug;
  591. }
  592. });
  593. }
  594. }
  595. else
  596. {
  597. MainConsole.Instance.Output("Usage: debug lludp packet [--default] 0..255 [<first-name> <last-name>]");
  598. }
  599. }
  600. }
  601. private void HandleStartCommand(string module, string[] args)
  602. {
  603. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  604. return;
  605. if (args.Length != 4)
  606. {
  607. MainConsole.Instance.Output("Usage: debug lludp start <in|out|all>");
  608. return;
  609. }
  610. string subCommand = args[3];
  611. if (subCommand == "in" || subCommand == "all")
  612. StartInbound();
  613. if (subCommand == "out" || subCommand == "all")
  614. StartOutbound();
  615. }
  616. private void HandleStopCommand(string module, string[] args)
  617. {
  618. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  619. return;
  620. if (args.Length != 4)
  621. {
  622. MainConsole.Instance.Output("Usage: debug lludp stop <in|out|all>");
  623. return;
  624. }
  625. string subCommand = args[3];
  626. if (subCommand == "in" || subCommand == "all")
  627. StopInbound();
  628. if (subCommand == "out" || subCommand == "all")
  629. StopOutbound();
  630. }
  631. private void HandlePoolCommand(string module, string[] args)
  632. {
  633. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  634. return;
  635. if (args.Length != 4)
  636. {
  637. MainConsole.Instance.Output("Usage: debug lludp pool <on|off>");
  638. return;
  639. }
  640. string enabled = args[3];
  641. if (enabled == "on")
  642. {
  643. if (EnablePools())
  644. {
  645. EnablePoolStats();
  646. MainConsole.Instance.OutputFormat("Packet pools enabled on {0}", m_scene.Name);
  647. }
  648. }
  649. else if (enabled == "off")
  650. {
  651. if (DisablePools())
  652. {
  653. DisablePoolStats();
  654. MainConsole.Instance.OutputFormat("Packet pools disabled on {0}", m_scene.Name);
  655. }
  656. }
  657. else
  658. {
  659. MainConsole.Instance.Output("Usage: debug lludp pool <on|off>");
  660. }
  661. }
  662. bool m_discardAgentUpdates;
  663. private void HandleAgentUpdateCommand(string module, string[] args)
  664. {
  665. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  666. return;
  667. m_discardAgentUpdates = !m_discardAgentUpdates;
  668. MainConsole.Instance.OutputFormat(
  669. "Discard AgentUpdates now {0} for {1}", m_discardAgentUpdates, m_scene.Name);
  670. }
  671. private void HandleStatusCommand(string module, string[] args)
  672. {
  673. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  674. return;
  675. MainConsole.Instance.OutputFormat(
  676. "IN LLUDP packet processing for {0} is {1}", m_scene.Name, IsRunningInbound ? "enabled" : "disabled");
  677. MainConsole.Instance.OutputFormat(
  678. "OUT LLUDP packet processing for {0} is {1}", m_scene.Name, IsRunningOutbound ? "enabled" : "disabled");
  679. MainConsole.Instance.OutputFormat("LLUDP pools in {0} are {1}", m_scene.Name, UsePools ? "on" : "off");
  680. MainConsole.Instance.OutputFormat(
  681. "Packet debug level for new clients is {0}", DefaultClientPacketDebugLevel);
  682. }
  683. public bool HandlesRegion(Location x)
  684. {
  685. return x == m_location;
  686. }
  687. public void BroadcastPacket(Packet packet, ThrottleOutPacketType category, bool sendToPausedAgents, bool allowSplitting)
  688. {
  689. // CoarseLocationUpdate and AvatarGroupsReply packets cannot be split in an automated way
  690. if ((packet.Type == PacketType.CoarseLocationUpdate || packet.Type == PacketType.AvatarGroupsReply) && allowSplitting)
  691. allowSplitting = false;
  692. if (allowSplitting && packet.HasVariableBlocks)
  693. {
  694. byte[][] datas = packet.ToBytesMultiple();
  695. int packetCount = datas.Length;
  696. if (packetCount < 1)
  697. m_log.Error("[LLUDPSERVER]: Failed to split " + packet.Type + " with estimated length " + packet.Length);
  698. for (int i = 0; i < packetCount; i++)
  699. {
  700. byte[] data = datas[i];
  701. m_scene.ForEachClient(
  702. delegate(IClientAPI client)
  703. {
  704. if (client is LLClientView)
  705. SendPacketData(((LLClientView)client).UDPClient, data, packet.Type, category, null);
  706. }
  707. );
  708. }
  709. }
  710. else
  711. {
  712. byte[] data = packet.ToBytes();
  713. m_scene.ForEachClient(
  714. delegate(IClientAPI client)
  715. {
  716. if (client is LLClientView)
  717. SendPacketData(((LLClientView)client).UDPClient, data, packet.Type, category, null);
  718. }
  719. );
  720. }
  721. }
  722. /// <summary>
  723. /// Start the process of sending a packet to the client.
  724. /// </summary>
  725. /// <param name="udpClient"></param>
  726. /// <param name="packet"></param>
  727. /// <param name="category"></param>
  728. /// <param name="allowSplitting"></param>
  729. /// <param name="method">
  730. /// The method to call if the packet is not acked by the client. If null, then a standard
  731. /// resend of the packet is done.
  732. /// </param>
  733. public virtual void SendPacket(
  734. LLUDPClient udpClient, Packet packet, ThrottleOutPacketType category, bool allowSplitting, UnackedPacketMethod method)
  735. {
  736. // CoarseLocationUpdate packets cannot be split in an automated way
  737. if (packet.Type == PacketType.CoarseLocationUpdate && allowSplitting)
  738. allowSplitting = false;
  739. if (allowSplitting && packet.HasVariableBlocks)
  740. {
  741. byte[][] datas = packet.ToBytesMultiple();
  742. int packetCount = datas.Length;
  743. if (packetCount < 1)
  744. m_log.Error("[LLUDPSERVER]: Failed to split " + packet.Type + " with estimated length " + packet.Length);
  745. for (int i = 0; i < packetCount; i++)
  746. {
  747. byte[] data = datas[i];
  748. SendPacketData(udpClient, data, packet.Type, category, method);
  749. }
  750. }
  751. else
  752. {
  753. byte[] data = packet.ToBytes();
  754. SendPacketData(udpClient, data, packet.Type, category, method);
  755. }
  756. PacketPool.Instance.ReturnPacket(packet);
  757. m_dataPresentEvent.Set();
  758. }
  759. /// <summary>
  760. /// Start the process of sending a packet to the client.
  761. /// </summary>
  762. /// <param name="udpClient"></param>
  763. /// <param name="data"></param>
  764. /// <param name="type"></param>
  765. /// <param name="category"></param>
  766. /// <param name="method">
  767. /// The method to call if the packet is not acked by the client. If null, then a standard
  768. /// resend of the packet is done.
  769. /// </param>
  770. public void SendPacketData(
  771. LLUDPClient udpClient, byte[] data, PacketType type, ThrottleOutPacketType category, UnackedPacketMethod method)
  772. {
  773. int dataLength = data.Length;
  774. bool doZerocode = (data[0] & Helpers.MSG_ZEROCODED) != 0;
  775. bool doCopy = true;
  776. // Frequency analysis of outgoing packet sizes shows a large clump of packets at each end of the spectrum.
  777. // The vast majority of packets are less than 200 bytes, although due to asset transfers and packet splitting
  778. // there are a decent number of packets in the 1000-1140 byte range. We allocate one of two sizes of data here
  779. // to accomodate for both common scenarios and provide ample room for ACK appending in both
  780. int bufferSize = (dataLength > 180) ? LLUDPServer.MTU : 200;
  781. UDPPacketBuffer buffer = new UDPPacketBuffer(udpClient.RemoteEndPoint, bufferSize);
  782. // Zerocode if needed
  783. if (doZerocode)
  784. {
  785. try
  786. {
  787. dataLength = Helpers.ZeroEncode(data, dataLength, buffer.Data);
  788. doCopy = false;
  789. }
  790. catch (IndexOutOfRangeException)
  791. {
  792. // The packet grew larger than the bufferSize while zerocoding.
  793. // Remove the MSG_ZEROCODED flag and send the unencoded data
  794. // instead
  795. m_log.Debug("[LLUDPSERVER]: Packet exceeded buffer size during zerocoding for " + type + ". DataLength=" + dataLength +
  796. " and BufferLength=" + buffer.Data.Length + ". Removing MSG_ZEROCODED flag");
  797. data[0] = (byte)(data[0] & ~Helpers.MSG_ZEROCODED);
  798. }
  799. }
  800. // If the packet data wasn't already copied during zerocoding, copy it now
  801. if (doCopy)
  802. {
  803. if (dataLength <= buffer.Data.Length)
  804. {
  805. Buffer.BlockCopy(data, 0, buffer.Data, 0, dataLength);
  806. }
  807. else
  808. {
  809. bufferSize = dataLength;
  810. buffer = new UDPPacketBuffer(udpClient.RemoteEndPoint, bufferSize);
  811. // m_log.Error("[LLUDPSERVER]: Packet exceeded buffer size! This could be an indication of packet assembly not obeying the MTU. Type=" +
  812. // type + ", DataLength=" + dataLength + ", BufferLength=" + buffer.Data.Length + ". Dropping packet");
  813. Buffer.BlockCopy(data, 0, buffer.Data, 0, dataLength);
  814. }
  815. }
  816. buffer.DataLength = dataLength;
  817. #region Queue or Send
  818. OutgoingPacket outgoingPacket = new OutgoingPacket(udpClient, buffer, category, null);
  819. // If we were not provided a method for handling unacked, use the UDPServer default method
  820. outgoingPacket.UnackedMethod = ((method == null) ? delegate(OutgoingPacket oPacket) { ResendUnacked(oPacket); } : method);
  821. // If a Linden Lab 1.23.5 client receives an update packet after a kill packet for an object, it will
  822. // continue to display the deleted object until relog. Therefore, we need to always queue a kill object
  823. // packet so that it isn't sent before a queued update packet.
  824. bool requestQueue = type == PacketType.KillObject;
  825. if (!outgoingPacket.Client.EnqueueOutgoing(outgoingPacket, requestQueue))
  826. SendPacketFinal(outgoingPacket);
  827. #endregion Queue or Send
  828. }
  829. public void SendAcks(LLUDPClient udpClient)
  830. {
  831. uint ack;
  832. if (udpClient.PendingAcks.Dequeue(out ack))
  833. {
  834. List<PacketAckPacket.PacketsBlock> blocks = new List<PacketAckPacket.PacketsBlock>();
  835. PacketAckPacket.PacketsBlock block = new PacketAckPacket.PacketsBlock();
  836. block.ID = ack;
  837. blocks.Add(block);
  838. while (udpClient.PendingAcks.Dequeue(out ack))
  839. {
  840. block = new PacketAckPacket.PacketsBlock();
  841. block.ID = ack;
  842. blocks.Add(block);
  843. }
  844. PacketAckPacket packet = new PacketAckPacket();
  845. packet.Header.Reliable = false;
  846. packet.Packets = blocks.ToArray();
  847. SendPacket(udpClient, packet, ThrottleOutPacketType.Unknown, true, null);
  848. }
  849. }
  850. public void SendPing(LLUDPClient udpClient)
  851. {
  852. StartPingCheckPacket pc = (StartPingCheckPacket)PacketPool.Instance.GetPacket(PacketType.StartPingCheck);
  853. pc.Header.Reliable = false;
  854. pc.PingID.PingID = (byte)udpClient.CurrentPingSequence++;
  855. // We *could* get OldestUnacked, but it would hurt performance and not provide any benefit
  856. pc.PingID.OldestUnacked = 0;
  857. SendPacket(udpClient, pc, ThrottleOutPacketType.Unknown, false, null);
  858. }
  859. public void CompletePing(LLUDPClient udpClient, byte pingID)
  860. {
  861. CompletePingCheckPacket completePing = new CompletePingCheckPacket();
  862. completePing.PingID.PingID = pingID;
  863. SendPacket(udpClient, completePing, ThrottleOutPacketType.Unknown, false, null);
  864. }
  865. public void HandleUnacked(LLClientView client)
  866. {
  867. LLUDPClient udpClient = client.UDPClient;
  868. if (!udpClient.IsConnected)
  869. return;
  870. // Disconnect an agent if no packets are received for some time
  871. int timeoutTicks = m_ackTimeout;
  872. // Allow more slack if the client is "paused" eg file upload dialogue is open
  873. // Some sort of limit is needed in case the client crashes, loses its network connection
  874. // or some other disaster prevents it from sendung the AgentResume
  875. if (udpClient.IsPaused)
  876. timeoutTicks = m_pausedAckTimeout;
  877. if (client.IsActive &&
  878. (Environment.TickCount & Int32.MaxValue) - udpClient.TickLastPacketReceived > timeoutTicks)
  879. {
  880. // We must set IsActive synchronously so that we can stop the packet loop reinvoking this method, even
  881. // though it's set later on by LLClientView.Close()
  882. client.IsActive = false;
  883. // Fire this out on a different thread so that we don't hold up outgoing packet processing for
  884. // everybody else if this is being called due to an ack timeout.
  885. // This is the same as processing as the async process of a logout request.
  886. Util.FireAndForget(o => DeactivateClientDueToTimeout(client));
  887. return;
  888. }
  889. // Get a list of all of the packets that have been sitting unacked longer than udpClient.RTO
  890. List<OutgoingPacket> expiredPackets = udpClient.NeedAcks.GetExpiredPackets(udpClient.RTO);
  891. if (expiredPackets != null)
  892. {
  893. //m_log.Debug("[LLUDPSERVER]: Handling " + expiredPackets.Count + " packets to " + udpClient.AgentID + ", RTO=" + udpClient.RTO);
  894. // Exponential backoff of the retransmission timeout
  895. udpClient.BackoffRTO();
  896. for (int i = 0; i < expiredPackets.Count; ++i)
  897. expiredPackets[i].UnackedMethod(expiredPackets[i]);
  898. }
  899. }
  900. public void ResendUnacked(OutgoingPacket outgoingPacket)
  901. {
  902. //m_log.DebugFormat("[LLUDPSERVER]: Resending packet #{0} (attempt {1}), {2}ms have passed",
  903. // outgoingPacket.SequenceNumber, outgoingPacket.ResendCount, Environment.TickCount - outgoingPacket.TickCount);
  904. // Set the resent flag
  905. outgoingPacket.Buffer.Data[0] = (byte)(outgoingPacket.Buffer.Data[0] | Helpers.MSG_RESENT);
  906. outgoingPacket.Category = ThrottleOutPacketType.Resend;
  907. // Bump up the resend count on this packet
  908. Interlocked.Increment(ref outgoingPacket.ResendCount);
  909. // Requeue or resend the packet
  910. if (!outgoingPacket.Client.EnqueueOutgoing(outgoingPacket, false))
  911. SendPacketFinal(outgoingPacket);
  912. }
  913. public void Flush(LLUDPClient udpClient)
  914. {
  915. // FIXME: Implement?
  916. }
  917. /// <summary>
  918. /// Actually send a packet to a client.
  919. /// </summary>
  920. /// <param name="outgoingPacket"></param>
  921. internal void SendPacketFinal(OutgoingPacket outgoingPacket)
  922. {
  923. UDPPacketBuffer buffer = outgoingPacket.Buffer;
  924. byte flags = buffer.Data[0];
  925. bool isResend = (flags & Helpers.MSG_RESENT) != 0;
  926. bool isReliable = (flags & Helpers.MSG_RELIABLE) != 0;
  927. bool isZerocoded = (flags & Helpers.MSG_ZEROCODED) != 0;
  928. LLUDPClient udpClient = outgoingPacket.Client;
  929. if (!udpClient.IsConnected)
  930. return;
  931. #region ACK Appending
  932. int dataLength = buffer.DataLength;
  933. // NOTE: I'm seeing problems with some viewers when ACKs are appended to zerocoded packets so I've disabled that here
  934. if (!isZerocoded)
  935. {
  936. // Keep appending ACKs until there is no room left in the buffer or there are
  937. // no more ACKs to append
  938. uint ackCount = 0;
  939. uint ack;
  940. while (dataLength + 5 < buffer.Data.Length && udpClient.PendingAcks.Dequeue(out ack))
  941. {
  942. Utils.UIntToBytesBig(ack, buffer.Data, dataLength);
  943. dataLength += 4;
  944. ++ackCount;
  945. }
  946. if (ackCount > 0)
  947. {
  948. // Set the last byte of the packet equal to the number of appended ACKs
  949. buffer.Data[dataLength++] = (byte)ackCount;
  950. // Set the appended ACKs flag on this packet
  951. buffer.Data[0] = (byte)(buffer.Data[0] | Helpers.MSG_APPENDED_ACKS);
  952. }
  953. }
  954. buffer.DataLength = dataLength;
  955. #endregion ACK Appending
  956. #region Sequence Number Assignment
  957. if (!isResend)
  958. {
  959. // Not a resend, assign a new sequence number
  960. uint sequenceNumber = (uint)Interlocked.Increment(ref udpClient.CurrentSequence);
  961. Utils.UIntToBytesBig(sequenceNumber, buffer.Data, 1);
  962. outgoingPacket.SequenceNumber = sequenceNumber;
  963. if (isReliable)
  964. {
  965. // Add this packet to the list of ACK responses we are waiting on from the server
  966. udpClient.NeedAcks.Add(outgoingPacket);
  967. }
  968. }
  969. else
  970. {
  971. Interlocked.Increment(ref udpClient.PacketsResent);
  972. }
  973. #endregion Sequence Number Assignment
  974. // Stats tracking
  975. Interlocked.Increment(ref udpClient.PacketsSent);
  976. // Put the UDP payload on the wire
  977. AsyncBeginSend(buffer);
  978. // Keep track of when this packet was sent out (right now)
  979. outgoingPacket.TickCount = Environment.TickCount & Int32.MaxValue;
  980. }
  981. public override void PacketReceived(UDPPacketBuffer buffer)
  982. {
  983. // Debugging/Profiling
  984. //try { Thread.CurrentThread.Name = "PacketReceived (" + m_scene.RegionInfo.RegionName + ")"; }
  985. //catch (Exception) { }
  986. // m_log.DebugFormat(
  987. // "[LLUDPSERVER]: Packet received from {0} in {1}", buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  988. LLUDPClient udpClient = null;
  989. Packet packet = null;
  990. int packetEnd = buffer.DataLength - 1;
  991. IPEndPoint endPoint = (IPEndPoint)buffer.RemoteEndPoint;
  992. #region Decoding
  993. if (buffer.DataLength < 7)
  994. {
  995. // m_log.WarnFormat(
  996. // "[LLUDPSERVER]: Dropping undersized packet with {0} bytes received from {1} in {2}",
  997. // buffer.DataLength, buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  998. return; // Drop undersized packet
  999. }
  1000. int headerLen = 7;
  1001. if (buffer.Data[6] == 0xFF)
  1002. {
  1003. if (buffer.Data[7] == 0xFF)
  1004. headerLen = 10;
  1005. else
  1006. headerLen = 8;
  1007. }
  1008. if (buffer.DataLength < headerLen)
  1009. {
  1010. // m_log.WarnFormat(
  1011. // "[LLUDPSERVER]: Dropping packet with malformed header received from {0} in {1}",
  1012. // buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  1013. return; // Malformed header
  1014. }
  1015. try
  1016. {
  1017. // packet = Packet.BuildPacket(buffer.Data, ref packetEnd,
  1018. // // Only allocate a buffer for zerodecoding if the packet is zerocoded
  1019. // ((buffer.Data[0] & Helpers.MSG_ZEROCODED) != 0) ? new byte[4096] : null);
  1020. // If OpenSimUDPBase.UsePool == true (which is currently separate from the PacketPool) then we
  1021. // assume that packet construction does not retain a reference to byte[] buffer.Data (instead, all
  1022. // bytes are copied out).
  1023. packet = PacketPool.Instance.GetPacket(buffer.Data, ref packetEnd,
  1024. // Only allocate a buffer for zerodecoding if the packet is zerocoded
  1025. ((buffer.Data[0] & Helpers.MSG_ZEROCODED) != 0) ? new byte[4096] : null);
  1026. }
  1027. catch (MalformedDataException)
  1028. {
  1029. }
  1030. catch (IndexOutOfRangeException)
  1031. {
  1032. // m_log.WarnFormat(
  1033. // "[LLUDPSERVER]: Dropping short packet received from {0} in {1}",
  1034. // buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  1035. return; // Drop short packet
  1036. }
  1037. catch (Exception e)
  1038. {
  1039. if (m_malformedCount < 100)
  1040. m_log.DebugFormat("[LLUDPSERVER]: Dropped malformed packet: " + e.ToString());
  1041. m_malformedCount++;
  1042. if ((m_malformedCount % 100000) == 0)
  1043. m_log.DebugFormat("[LLUDPSERVER]: Received {0} malformed packets so far, probable network attack.", m_malformedCount);
  1044. }
  1045. // Fail-safe check
  1046. if (packet == null)
  1047. {
  1048. m_log.ErrorFormat("[LLUDPSERVER]: Malformed data, cannot parse {0} byte packet from {1}:",
  1049. buffer.DataLength, buffer.RemoteEndPoint);
  1050. m_log.Error(Utils.BytesToHexString(buffer.Data, buffer.DataLength, null));
  1051. return;
  1052. }
  1053. #endregion Decoding
  1054. #region Packet to Client Mapping
  1055. // UseCircuitCode handling
  1056. if (packet.Type == PacketType.UseCircuitCode)
  1057. {
  1058. // We need to copy the endpoint so that it doesn't get changed when another thread reuses the
  1059. // buffer.
  1060. object[] array = new object[] { new IPEndPoint(endPoint.Address, endPoint.Port), packet };
  1061. Util.FireAndForget(HandleUseCircuitCode, array);
  1062. return;
  1063. }
  1064. else if (packet.Type == PacketType.CompleteAgentMovement)
  1065. {
  1066. // Send ack straight away to let the viewer know that we got it.
  1067. SendAckImmediate(endPoint, packet.Header.Sequence);
  1068. // We need to copy the endpoint so that it doesn't get changed when another thread reuses the
  1069. // buffer.
  1070. object[] array = new object[] { new IPEndPoint(endPoint.Address, endPoint.Port), packet };
  1071. Util.FireAndForget(HandleCompleteMovementIntoRegion, array);
  1072. return;
  1073. }
  1074. // Determine which agent this packet came from
  1075. IClientAPI client;
  1076. if (!m_scene.TryGetClient(endPoint, out client) || !(client is LLClientView))
  1077. {
  1078. //m_log.Debug("[LLUDPSERVER]: Received a " + packet.Type + " packet from an unrecognized source: " + address + " in " + m_scene.RegionInfo.RegionName);
  1079. return;
  1080. }
  1081. udpClient = ((LLClientView)client).UDPClient;
  1082. if (!udpClient.IsConnected)
  1083. return;
  1084. #endregion Packet to Client Mapping
  1085. // Stats tracking
  1086. Interlocked.Increment(ref udpClient.PacketsReceived);
  1087. int now = Environment.TickCount & Int32.MaxValue;
  1088. udpClient.TickLastPacketReceived = now;
  1089. #region ACK Receiving
  1090. // Handle appended ACKs
  1091. if (packet.Header.AppendedAcks && packet.Header.AckList != null)
  1092. {
  1093. // m_log.DebugFormat(
  1094. // "[LLUDPSERVER]: Handling {0} appended acks from {1} in {2}",
  1095. // packet.Header.AckList.Length, client.Name, m_scene.Name);
  1096. for (int i = 0; i < packet.Header.AckList.Length; i++)
  1097. udpClient.NeedAcks.Acknowledge(packet.Header.AckList[i], now, packet.Header.Resent);
  1098. }
  1099. // Handle PacketAck packets
  1100. if (packet.Type == PacketType.PacketAck)
  1101. {
  1102. PacketAckPacket ackPacket = (PacketAckPacket)packet;
  1103. // m_log.DebugFormat(
  1104. // "[LLUDPSERVER]: Handling {0} packet acks for {1} in {2}",
  1105. // ackPacket.Packets.Length, client.Name, m_scene.Name);
  1106. for (int i = 0; i < ackPacket.Packets.Length; i++)
  1107. udpClient.NeedAcks.Acknowledge(ackPacket.Packets[i].ID, now, packet.Header.Resent);
  1108. // We don't need to do anything else with PacketAck packets
  1109. return;
  1110. }
  1111. #endregion ACK Receiving
  1112. #region ACK Sending
  1113. if (packet.Header.Reliable)
  1114. {
  1115. // m_log.DebugFormat(
  1116. // "[LLUDPSERVER]: Adding ack request for {0} {1} from {2} in {3}",
  1117. // packet.Type, packet.Header.Sequence, client.Name, m_scene.Name);
  1118. udpClient.PendingAcks.Enqueue(packet.Header.Sequence);
  1119. // This is a somewhat odd sequence of steps to pull the client.BytesSinceLastACK value out,
  1120. // add the current received bytes to it, test if 2*MTU bytes have been sent, if so remove
  1121. // 2*MTU bytes from the value and send ACKs, and finally add the local value back to
  1122. // client.BytesSinceLastACK. Lockless thread safety
  1123. int bytesSinceLastACK = Interlocked.Exchange(ref udpClient.BytesSinceLastACK, 0);
  1124. bytesSinceLastACK += buffer.DataLength;
  1125. if (bytesSinceLastACK > LLUDPServer.MTU * 2)
  1126. {
  1127. bytesSinceLastACK -= LLUDPServer.MTU * 2;
  1128. SendAcks(udpClient);
  1129. }
  1130. Interlocked.Add(ref udpClient.BytesSinceLastACK, bytesSinceLastACK);
  1131. }
  1132. #endregion ACK Sending
  1133. #region Incoming Packet Accounting
  1134. // Check the archive of received reliable packet IDs to see whether we already received this packet
  1135. if (packet.Header.Reliable && !udpClient.PacketArchive.TryEnqueue(packet.Header.Sequence))
  1136. {
  1137. if (packet.Header.Resent)
  1138. m_log.DebugFormat(
  1139. "[LLUDPSERVER]: Received a resend of already processed packet #{0}, type {1} from {2}",
  1140. packet.Header.Sequence, packet.Type, client.Name);
  1141. else
  1142. m_log.WarnFormat(
  1143. "[LLUDPSERVER]: Received a duplicate (not marked as resend) of packet #{0}, type {1} from {2}",
  1144. packet.Header.Sequence, packet.Type, client.Name);
  1145. // Avoid firing a callback twice for the same packet
  1146. return;
  1147. }
  1148. #endregion Incoming Packet Accounting
  1149. #region BinaryStats
  1150. LogPacketHeader(true, udpClient.CircuitCode, 0, packet.Type, (ushort)packet.Length);
  1151. #endregion BinaryStats
  1152. if (packet.Type == PacketType.AgentUpdate)
  1153. {
  1154. if (m_discardAgentUpdates)
  1155. return;
  1156. ((LLClientView)client).TotalAgentUpdates++;
  1157. AgentUpdatePacket agentUpdate = (AgentUpdatePacket)packet;
  1158. LLClientView llClient = client as LLClientView;
  1159. if (agentUpdate.AgentData.SessionID != client.SessionId
  1160. || agentUpdate.AgentData.AgentID != client.AgentId
  1161. || !(llClient == null || llClient.CheckAgentUpdateSignificance(agentUpdate.AgentData)) )
  1162. {
  1163. PacketPool.Instance.ReturnPacket(packet);
  1164. return;
  1165. }
  1166. }
  1167. #region Ping Check Handling
  1168. if (packet.Type == PacketType.StartPingCheck)
  1169. {
  1170. // m_log.DebugFormat("[LLUDPSERVER]: Handling ping from {0} in {1}", client.Name, m_scene.Name);
  1171. // We don't need to do anything else with ping checks
  1172. StartPingCheckPacket startPing = (StartPingCheckPacket)packet;
  1173. CompletePing(udpClient, startPing.PingID.PingID);
  1174. if ((Environment.TickCount - m_elapsedMSSinceLastStatReport) >= 3000)
  1175. {
  1176. udpClient.SendPacketStats();
  1177. m_elapsedMSSinceLastStatReport = Environment.TickCount;
  1178. }
  1179. return;
  1180. }
  1181. else if (packet.Type == PacketType.CompletePingCheck)
  1182. {
  1183. // We don't currently track client ping times
  1184. return;
  1185. }
  1186. #endregion Ping Check Handling
  1187. IncomingPacket incomingPacket;
  1188. // Inbox insertion
  1189. if (UsePools)
  1190. {
  1191. incomingPacket = m_incomingPacketPool.GetObject();
  1192. incomingPacket.Client = (LLClientView)client;
  1193. incomingPacket.Packet = packet;
  1194. }
  1195. else
  1196. {
  1197. incomingPacket = new IncomingPacket((LLClientView)client, packet);
  1198. }
  1199. packetInbox.Enqueue(incomingPacket);
  1200. }
  1201. #region BinaryStats
  1202. public class PacketLogger
  1203. {
  1204. public DateTime StartTime;
  1205. public string Path = null;
  1206. public System.IO.BinaryWriter Log = null;
  1207. }
  1208. public static PacketLogger PacketLog;
  1209. protected static bool m_shouldCollectStats = false;
  1210. // Number of seconds to log for
  1211. static TimeSpan binStatsMaxFilesize = TimeSpan.FromSeconds(300);
  1212. static object binStatsLogLock = new object();
  1213. static string binStatsDir = "";
  1214. //for Aggregated In/Out BW logging
  1215. static bool m_aggregatedBWStats = false;
  1216. static long m_aggregatedBytesIn = 0;
  1217. static long m_aggregatedByestOut = 0;
  1218. static object aggBWStatsLock = new object();
  1219. public static long AggregatedLLUDPBytesIn
  1220. {
  1221. get { return m_aggregatedBytesIn; }
  1222. }
  1223. public static long AggregatedLLUDPBytesOut
  1224. {
  1225. get {return m_aggregatedByestOut;}
  1226. }
  1227. public static void LogPacketHeader(bool incoming, uint circuit, byte flags, PacketType packetType, ushort size)
  1228. {
  1229. if (m_aggregatedBWStats)
  1230. {
  1231. lock (aggBWStatsLock)
  1232. {
  1233. if (incoming)
  1234. m_aggregatedBytesIn += size;
  1235. else
  1236. m_aggregatedByestOut += size;
  1237. }
  1238. }
  1239. if (!m_shouldCollectStats) return;
  1240. // Binary logging format is TTTTTTTTCCCCFPPPSS, T=Time, C=Circuit, F=Flags, P=PacketType, S=size
  1241. // Put the incoming bit into the least significant bit of the flags byte
  1242. if (incoming)
  1243. flags |= 0x01;
  1244. else
  1245. flags &= 0xFE;
  1246. // Put the flags byte into the most significant bits of the type integer
  1247. uint type = (uint)packetType;
  1248. type |= (uint)flags << 24;
  1249. // m_log.Debug("1 LogPacketHeader(): Outside lock");
  1250. lock (binStatsLogLock)
  1251. {
  1252. DateTime now = DateTime.Now;
  1253. // m_log.Debug("2 LogPacketHeader(): Inside lock. now is " + now.Ticks);
  1254. try
  1255. {
  1256. if (PacketLog == null || (now > PacketLog.StartTime + binStatsMaxFilesize))
  1257. {
  1258. if (PacketLog != null && PacketLog.Log != null)
  1259. {
  1260. PacketLog.Log.Close();
  1261. }
  1262. // First log file or time has expired, start writing to a new log file
  1263. PacketLog = new PacketLogger();
  1264. PacketLog.StartTime = now;
  1265. PacketLog.Path = (binStatsDir.Length > 0 ? binStatsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "")
  1266. + String.Format("packets-{0}.log", now.ToString("yyyyMMddHHmmss"));
  1267. PacketLog.Log = new BinaryWriter(File.Open(PacketLog.Path, FileMode.Append, FileAccess.Write));
  1268. }
  1269. // Serialize the data
  1270. byte[] output = new byte[18];
  1271. Buffer.BlockCopy(BitConverter.GetBytes(now.Ticks), 0, output, 0, 8);
  1272. Buffer.BlockCopy(BitConverter.GetBytes(circuit), 0, output, 8, 4);
  1273. Buffer.BlockCopy(BitConverter.GetBytes(type), 0, output, 12, 4);
  1274. Buffer.BlockCopy(BitConverter.GetBytes(size), 0, output, 16, 2);
  1275. // Write the serialized data to disk
  1276. if (PacketLog != null && PacketLog.Log != null)
  1277. PacketLog.Log.Write(output);
  1278. }
  1279. catch (Exception ex)
  1280. {
  1281. m_log.Error("Packet statistics gathering failed: " + ex.Message, ex);
  1282. if (PacketLog.Log != null)
  1283. {
  1284. PacketLog.Log.Close();
  1285. }
  1286. PacketLog = null;
  1287. }
  1288. }
  1289. }
  1290. #endregion BinaryStats
  1291. private void HandleUseCircuitCode(object o)
  1292. {
  1293. IPEndPoint endPoint = null;
  1294. IClientAPI client = null;
  1295. try
  1296. {
  1297. // DateTime startTime = DateTime.Now;
  1298. object[] array = (object[])o;
  1299. endPoint = (IPEndPoint)array[0];
  1300. UseCircuitCodePacket uccp = (UseCircuitCodePacket)array[1];
  1301. m_log.DebugFormat(
  1302. "[LLUDPSERVER]: Handling UseCircuitCode request for circuit {0} to {1} from IP {2}",
  1303. uccp.CircuitCode.Code, m_scene.RegionInfo.RegionName, endPoint);
  1304. AuthenticateResponse sessionInfo;
  1305. if (IsClientAuthorized(uccp, out sessionInfo))
  1306. {
  1307. // Begin the process of adding the client to the simulator
  1308. client
  1309. = AddClient(
  1310. uccp.CircuitCode.Code,
  1311. uccp.CircuitCode.ID,
  1312. uccp.CircuitCode.SessionID,
  1313. endPoint,
  1314. sessionInfo);
  1315. // Send ack straight away to let the viewer know that the connection is active.
  1316. // The client will be null if it already exists (e.g. if on a region crossing the client sends a use
  1317. // circuit code to the existing child agent. This is not particularly obvious.
  1318. SendAckImmediate(endPoint, uccp.Header.Sequence);
  1319. // We only want to send initial data to new clients, not ones which are being converted from child to root.
  1320. if (client != null)
  1321. {
  1322. AgentCircuitData aCircuit = m_scene.AuthenticateHandler.GetAgentCircuitData(uccp.CircuitCode.Code);
  1323. bool tp = (aCircuit.teleportFlags > 0);
  1324. // Let's delay this for TP agents, otherwise the viewer doesn't know where to get resources from
  1325. if (!tp)
  1326. client.SceneAgent.SendInitialDataToMe();
  1327. }
  1328. }
  1329. else
  1330. {
  1331. // Don't create clients for unauthorized requesters.
  1332. m_log.WarnFormat(
  1333. "[LLUDPSERVER]: Ignoring connection request for {0} to {1} with unknown circuit code {2} from IP {3}",
  1334. uccp.CircuitCode.ID, m_scene.RegionInfo.RegionName, uccp.CircuitCode.Code, endPoint);
  1335. }
  1336. // m_log.DebugFormat(
  1337. // "[LLUDPSERVER]: Handling UseCircuitCode request from {0} took {1}ms",
  1338. // buffer.RemoteEndPoint, (DateTime.Now - startTime).Milliseconds);
  1339. }
  1340. catch (Exception e)
  1341. {
  1342. m_log.ErrorFormat(
  1343. "[LLUDPSERVER]: UseCircuitCode handling from endpoint {0}, client {1} {2} failed. Exception {3}{4}",
  1344. endPoint != null ? endPoint.ToString() : "n/a",
  1345. client != null ? client.Name : "unknown",
  1346. client != null ? client.AgentId.ToString() : "unknown",
  1347. e.Message,
  1348. e.StackTrace);
  1349. }
  1350. }
  1351. private void HandleCompleteMovementIntoRegion(object o)
  1352. {
  1353. IPEndPoint endPoint = null;
  1354. IClientAPI client = null;
  1355. try
  1356. {
  1357. object[] array = (object[])o;
  1358. endPoint = (IPEndPoint)array[0];
  1359. CompleteAgentMovementPacket packet = (CompleteAgentMovementPacket)array[1];
  1360. // Determine which agent this packet came from
  1361. int count = 20;
  1362. bool ready = false;
  1363. while (!ready && count-- > 0)
  1364. {
  1365. // Let's make sure there is an active client attached to a scene presence,
  1366. // otherwise there won't be any handlers for this packet
  1367. if (m_scene.TryGetClient(endPoint, out client) && client.IsActive && client.SceneAgent != null)
  1368. {
  1369. LLClientView llClientView = (LLClientView)client;
  1370. LLUDPClient udpClient = llClientView.UDPClient;
  1371. if (udpClient != null && udpClient.IsConnected)
  1372. ready = true;
  1373. else
  1374. {
  1375. m_log.Debug("[LLUDPSERVER]: Received a CompleteMovementIntoRegion in " + m_scene.RegionInfo.RegionName + " (not ready yet)");
  1376. Thread.Sleep(200);
  1377. }
  1378. }
  1379. else
  1380. {
  1381. m_log.Debug("[LLUDPSERVER]: Received a CompleteMovementIntoRegion in " + m_scene.RegionInfo.RegionName + " (not ready yet)");
  1382. Thread.Sleep(200);
  1383. }
  1384. }
  1385. if (client == null)
  1386. return;
  1387. IncomingPacket incomingPacket1;
  1388. // Inbox insertion
  1389. if (UsePools)
  1390. {
  1391. incomingPacket1 = m_incomingPacketPool.GetObject();
  1392. incomingPacket1.Client = (LLClientView)client;
  1393. incomingPacket1.Packet = packet;
  1394. }
  1395. else
  1396. {
  1397. incomingPacket1 = new IncomingPacket((LLClientView)client, packet);
  1398. }
  1399. packetInbox.Enqueue(incomingPacket1);
  1400. }
  1401. catch (Exception e)
  1402. {
  1403. m_log.ErrorFormat(
  1404. "[LLUDPSERVER]: CompleteMovementIntoRegion handling from endpoint {0}, client {1} {2} failed. Exception {3}{4}",
  1405. endPoint != null ? endPoint.ToString() : "n/a",
  1406. client != null ? client.Name : "unknown",
  1407. client != null ? client.AgentId.ToString() : "unknown",
  1408. e.Message,
  1409. e.StackTrace);
  1410. }
  1411. }
  1412. /// <summary>
  1413. /// Send an ack immediately to the given endpoint.
  1414. /// </summary>
  1415. /// <remarks>
  1416. /// FIXME: Might be possible to use SendPacketData() like everything else, but this will require refactoring so
  1417. /// that we can obtain the UDPClient easily at this point.
  1418. /// </remarks>
  1419. /// <param name="remoteEndpoint"></param>
  1420. /// <param name="sequenceNumber"></param>
  1421. private void SendAckImmediate(IPEndPoint remoteEndpoint, uint sequenceNumber)
  1422. {
  1423. PacketAckPacket ack = new PacketAckPacket();
  1424. ack.Header.Reliable = false;
  1425. ack.Packets = new PacketAckPacket.PacketsBlock[1];
  1426. ack.Packets[0] = new PacketAckPacket.PacketsBlock();
  1427. ack.Packets[0].ID = sequenceNumber;
  1428. SendAckImmediate(remoteEndpoint, ack);
  1429. }
  1430. public virtual void SendAckImmediate(IPEndPoint remoteEndpoint, PacketAckPacket ack)
  1431. {
  1432. byte[] packetData = ack.ToBytes();
  1433. int length = packetData.Length;
  1434. UDPPacketBuffer buffer = new UDPPacketBuffer(remoteEndpoint, length);
  1435. buffer.DataLength = length;
  1436. Buffer.BlockCopy(packetData, 0, buffer.Data, 0, length);
  1437. AsyncBeginSend(buffer);
  1438. }
  1439. private bool IsClientAuthorized(UseCircuitCodePacket useCircuitCode, out AuthenticateResponse sessionInfo)
  1440. {
  1441. UUID agentID = useCircuitCode.CircuitCode.ID;
  1442. UUID sessionID = useCircuitCode.CircuitCode.SessionID;
  1443. uint circuitCode = useCircuitCode.CircuitCode.Code;
  1444. sessionInfo = m_circuitManager.AuthenticateSession(sessionID, agentID, circuitCode);
  1445. return sessionInfo.Authorised;
  1446. }
  1447. /// <summary>
  1448. /// Add a client.
  1449. /// </summary>
  1450. /// <param name="circuitCode"></param>
  1451. /// <param name="agentID"></param>
  1452. /// <param name="sessionID"></param>
  1453. /// <param name="remoteEndPoint"></param>
  1454. /// <param name="sessionInfo"></param>
  1455. /// <returns>The client if it was added. Null if the client already existed.</returns>
  1456. protected virtual IClientAPI AddClient(
  1457. uint circuitCode, UUID agentID, UUID sessionID, IPEndPoint remoteEndPoint, AuthenticateResponse sessionInfo)
  1458. {
  1459. IClientAPI client = null;
  1460. // We currently synchronize this code across the whole scene to avoid issues such as
  1461. // http://opensimulator.org/mantis/view.php?id=5365 However, once locking per agent circuit can be done
  1462. // consistently, this lock could probably be removed.
  1463. lock (this)
  1464. {
  1465. if (!m_scene.TryGetClient(agentID, out client))
  1466. {
  1467. LLUDPClient udpClient = new LLUDPClient(this, ThrottleRates, m_throttle, circuitCode, agentID, remoteEndPoint, m_defaultRTO, m_maxRTO);
  1468. client = new LLClientView(m_scene, this, udpClient, sessionInfo, agentID, sessionID, circuitCode);
  1469. client.OnLogout += LogoutHandler;
  1470. client.DebugPacketLevel = DefaultClientPacketDebugLevel;
  1471. ((LLClientView)client).DisableFacelights = m_disableFacelights;
  1472. client.Start();
  1473. }
  1474. }
  1475. return client;
  1476. }
  1477. /// <summary>
  1478. /// Deactivates the client if we don't receive any packets within a certain amount of time (default 60 seconds).
  1479. /// </summary>
  1480. /// <remarks>
  1481. /// If a connection is active then we will always receive packets even if nothing else is happening, due to
  1482. /// regular client pings.
  1483. /// </remarks>
  1484. /// <param name='client'></param>
  1485. private void DeactivateClientDueToTimeout(LLClientView client)
  1486. {
  1487. lock (client.CloseSyncLock)
  1488. {
  1489. m_log.WarnFormat(
  1490. "[LLUDPSERVER]: Ack timeout, disconnecting {0} agent for {1} in {2}",
  1491. client.SceneAgent.IsChildAgent ? "child" : "root", client.Name, m_scene.RegionInfo.RegionName);
  1492. StatsManager.SimExtraStats.AddAbnormalClientThreadTermination();
  1493. if (!client.SceneAgent.IsChildAgent)
  1494. client.Kick("Simulator logged you out due to connection timeout");
  1495. client.CloseWithoutChecks();
  1496. }
  1497. }
  1498. private void IncomingPacketHandler()
  1499. {
  1500. // Set this culture for the thread that incoming packets are received
  1501. // on to en-US to avoid number parsing issues
  1502. Culture.SetCurrentCulture();
  1503. while (IsRunningInbound)
  1504. {
  1505. try
  1506. {
  1507. IncomingPacket incomingPacket = null;
  1508. /*
  1509. // HACK: This is a test to try and rate limit packet handling on Mono.
  1510. // If it works, a more elegant solution can be devised
  1511. if (Util.FireAndForgetCount() < 2)
  1512. {
  1513. //m_log.Debug("[LLUDPSERVER]: Incoming packet handler is sleeping");
  1514. Thread.Sleep(30);
  1515. }
  1516. */
  1517. if (packetInbox.Dequeue(100, ref incomingPacket))
  1518. {
  1519. ProcessInPacket(incomingPacket);//, incomingPacket); Util.FireAndForget(ProcessInPacket, incomingPacket);
  1520. if (UsePools)
  1521. m_incomingPacketPool.ReturnObject(incomingPacket);
  1522. }
  1523. }
  1524. catch (Exception ex)
  1525. {
  1526. m_log.Error("[LLUDPSERVER]: Error in the incoming packet handler loop: " + ex.Message, ex);
  1527. }
  1528. Watchdog.UpdateThread();
  1529. }
  1530. if (packetInbox.Count > 0)
  1531. m_log.Warn("[LLUDPSERVER]: IncomingPacketHandler is shutting down, dropping " + packetInbox.Count + " packets");
  1532. packetInbox.Clear();
  1533. Watchdog.RemoveThread();
  1534. }
  1535. private void OutgoingPacketHandler()
  1536. {
  1537. // Set this culture for the thread that outgoing packets are sent
  1538. // on to en-US to avoid number parsing issues
  1539. Culture.SetCurrentCulture();
  1540. // Typecast the function to an Action<IClientAPI> once here to avoid allocating a new
  1541. // Action generic every round
  1542. Action<IClientAPI> clientPacketHandler = ClientOutgoingPacketHandler;
  1543. while (base.IsRunningOutbound)
  1544. {
  1545. try
  1546. {
  1547. m_packetSent = false;
  1548. #region Update Timers
  1549. m_resendUnacked = false;
  1550. m_sendAcks = false;
  1551. m_sendPing = false;
  1552. // Update elapsed time
  1553. int thisTick = Environment.TickCount & Int32.MaxValue;
  1554. if (m_tickLastOutgoingPacketHandler > thisTick)
  1555. m_elapsedMSOutgoingPacketHandler += ((Int32.MaxValue - m_tickLastOutgoingPacketHandler) + thisTick);
  1556. else
  1557. m_elapsedMSOutgoingPacketHandler += (thisTick - m_tickLastOutgoingPacketHandler);
  1558. m_tickLastOutgoingPacketHandler = thisTick;
  1559. // Check for pending outgoing resends every 100ms
  1560. if (m_elapsedMSOutgoingPacketHandler >= 100)
  1561. {
  1562. m_resendUnacked = true;
  1563. m_elapsedMSOutgoingPacketHandler = 0;
  1564. m_elapsed100MSOutgoingPacketHandler += 1;
  1565. }
  1566. // Check for pending outgoing ACKs every 500ms
  1567. if (m_elapsed100MSOutgoingPacketHandler >= 5)
  1568. {
  1569. m_sendAcks = true;
  1570. m_elapsed100MSOutgoingPacketHandler = 0;
  1571. m_elapsed500MSOutgoingPacketHandler += 1;
  1572. }
  1573. // Send pings to clients every 5000ms
  1574. if (m_elapsed500MSOutgoingPacketHandler >= 10)
  1575. {
  1576. m_sendPing = true;
  1577. m_elapsed500MSOutgoingPacketHandler = 0;
  1578. }
  1579. #endregion Update Timers
  1580. // Use this for emergency monitoring -- bug hunting
  1581. //if (m_scene.EmergencyMonitoring)
  1582. // clientPacketHandler = MonitoredClientOutgoingPacketHandler;
  1583. //else
  1584. // clientPacketHandler = ClientOutgoingPacketHandler;
  1585. // Handle outgoing packets, resends, acknowledgements, and pings for each
  1586. // client. m_packetSent will be set to true if a packet is sent
  1587. m_scene.ForEachClient(clientPacketHandler);
  1588. m_currentOutgoingClient = null;
  1589. // If nothing was sent, sleep for the minimum amount of time before a
  1590. // token bucket could get more tokens
  1591. //if (!m_packetSent)
  1592. // Thread.Sleep((int)TickCountResolution);
  1593. //
  1594. // Instead, now wait for data present to be explicitly signalled. Evidence so far is that with
  1595. // modern mono it reduces CPU base load since there is no more continuous polling.
  1596. m_dataPresentEvent.WaitOne(100);
  1597. Watchdog.UpdateThread();
  1598. }
  1599. catch (Exception ex)
  1600. {
  1601. m_log.Error("[LLUDPSERVER]: OutgoingPacketHandler loop threw an exception: " + ex.Message, ex);
  1602. }
  1603. }
  1604. Watchdog.RemoveThread();
  1605. }
  1606. protected void ClientOutgoingPacketHandler(IClientAPI client)
  1607. {
  1608. m_currentOutgoingClient = client;
  1609. try
  1610. {
  1611. if (client is LLClientView)
  1612. {
  1613. LLClientView llClient = (LLClientView)client;
  1614. LLUDPClient udpClient = llClient.UDPClient;
  1615. if (udpClient.IsConnected)
  1616. {
  1617. if (m_resendUnacked)
  1618. HandleUnacked(llClient);
  1619. if (m_sendAcks)
  1620. SendAcks(udpClient);
  1621. if (m_sendPing)
  1622. SendPing(udpClient);
  1623. // Dequeue any outgoing packets that are within the throttle limits
  1624. if (udpClient.DequeueOutgoing())
  1625. m_packetSent = true;
  1626. }
  1627. }
  1628. }
  1629. catch (Exception ex)
  1630. {
  1631. m_log.Error(
  1632. string.Format("[LLUDPSERVER]: OutgoingPacketHandler iteration for {0} threw ", client.Name), ex);
  1633. }
  1634. }
  1635. #region Emergency Monitoring
  1636. // Alternative packet handler fuull of instrumentation
  1637. // Handy for hunting bugs
  1638. private Stopwatch watch1 = new Stopwatch();
  1639. private Stopwatch watch2 = new Stopwatch();
  1640. private float avgProcessingTicks = 0;
  1641. private float avgResendUnackedTicks = 0;
  1642. private float avgSendAcksTicks = 0;
  1643. private float avgSendPingTicks = 0;
  1644. private float avgDequeueTicks = 0;
  1645. private long nticks = 0;
  1646. private long nticksUnack = 0;
  1647. private long nticksAck = 0;
  1648. private long nticksPing = 0;
  1649. private int npacksSent = 0;
  1650. private int npackNotSent = 0;
  1651. /// <summary>
  1652. /// Number of inbound packets processed since startup.
  1653. /// </summary>
  1654. public long IncomingPacketsProcessed { get; private set; }
  1655. private void MonitoredClientOutgoingPacketHandler(IClientAPI client)
  1656. {
  1657. nticks++;
  1658. watch1.Start();
  1659. m_currentOutgoingClient = client;
  1660. try
  1661. {
  1662. if (client is LLClientView)
  1663. {
  1664. LLClientView llClient = (LLClientView)client;
  1665. LLUDPClient udpClient = llClient.UDPClient;
  1666. if (udpClient.IsConnected)
  1667. {
  1668. if (m_resendUnacked)
  1669. {
  1670. nticksUnack++;
  1671. watch2.Start();
  1672. HandleUnacked(llClient);
  1673. watch2.Stop();
  1674. avgResendUnackedTicks = (nticksUnack - 1)/(float)nticksUnack * avgResendUnackedTicks + (watch2.ElapsedTicks / (float)nticksUnack);
  1675. watch2.Reset();
  1676. }
  1677. if (m_sendAcks)
  1678. {
  1679. nticksAck++;
  1680. watch2.Start();
  1681. SendAcks(udpClient);
  1682. watch2.Stop();
  1683. avgSendAcksTicks = (nticksAck - 1) / (float)nticksAck * avgSendAcksTicks + (watch2.ElapsedTicks / (float)nticksAck);
  1684. watch2.Reset();
  1685. }
  1686. if (m_sendPing)
  1687. {
  1688. nticksPing++;
  1689. watch2.Start();
  1690. SendPing(udpClient);
  1691. watch2.Stop();
  1692. avgSendPingTicks = (nticksPing - 1) / (float)nticksPing * avgSendPingTicks + (watch2.ElapsedTicks / (float)nticksPing);
  1693. watch2.Reset();
  1694. }
  1695. watch2.Start();
  1696. // Dequeue any outgoing packets that are within the throttle limits
  1697. if (udpClient.DequeueOutgoing())
  1698. {
  1699. m_packetSent = true;
  1700. npacksSent++;
  1701. }
  1702. else
  1703. {
  1704. npackNotSent++;
  1705. }
  1706. watch2.Stop();
  1707. avgDequeueTicks = (nticks - 1) / (float)nticks * avgDequeueTicks + (watch2.ElapsedTicks / (float)nticks);
  1708. watch2.Reset();
  1709. }
  1710. else
  1711. {
  1712. m_log.WarnFormat("[LLUDPSERVER]: Client is not connected");
  1713. }
  1714. }
  1715. }
  1716. catch (Exception ex)
  1717. {
  1718. m_log.Error("[LLUDPSERVER]: OutgoingPacketHandler iteration for " + client.Name +
  1719. " threw an exception: " + ex.Message, ex);
  1720. }
  1721. watch1.Stop();
  1722. avgProcessingTicks = (nticks - 1) / (float)nticks * avgProcessingTicks + (watch1.ElapsedTicks / (float)nticks);
  1723. watch1.Reset();
  1724. // reuse this -- it's every ~100ms
  1725. if (m_scene.EmergencyMonitoring && nticks % 100 == 0)
  1726. {
  1727. m_log.InfoFormat("[LLUDPSERVER]: avg processing ticks: {0} avg unacked: {1} avg acks: {2} avg ping: {3} avg dequeue: {4} (TickCountRes: {5} sent: {6} notsent: {7})",
  1728. avgProcessingTicks, avgResendUnackedTicks, avgSendAcksTicks, avgSendPingTicks, avgDequeueTicks, TickCountResolution, npacksSent, npackNotSent);
  1729. npackNotSent = npacksSent = 0;
  1730. }
  1731. }
  1732. #endregion
  1733. private void ProcessInPacket(IncomingPacket incomingPacket)
  1734. {
  1735. Packet packet = incomingPacket.Packet;
  1736. LLClientView client = incomingPacket.Client;
  1737. if (client.IsActive)
  1738. {
  1739. m_currentIncomingClient = client;
  1740. try
  1741. {
  1742. // Process this packet
  1743. client.ProcessInPacket(packet);
  1744. }
  1745. catch (ThreadAbortException)
  1746. {
  1747. // If something is trying to abort the packet processing thread, take that as a hint that it's time to shut down
  1748. m_log.Info("[LLUDPSERVER]: Caught a thread abort, shutting down the LLUDP server");
  1749. Stop();
  1750. }
  1751. catch (Exception e)
  1752. {
  1753. // Don't let a failure in an individual client thread crash the whole sim.
  1754. m_log.Error(
  1755. string.Format(
  1756. "[LLUDPSERVER]: Client packet handler for {0} for packet {1} threw ",
  1757. client.Name, packet.Type),
  1758. e);
  1759. }
  1760. finally
  1761. {
  1762. m_currentIncomingClient = null;
  1763. }
  1764. }
  1765. else
  1766. {
  1767. m_log.DebugFormat(
  1768. "[LLUDPSERVER]: Dropped incoming {0} for dead client {1} in {2}",
  1769. packet.Type, client.Name, m_scene.RegionInfo.RegionName);
  1770. }
  1771. IncomingPacketsProcessed++;
  1772. }
  1773. protected void LogoutHandler(IClientAPI client)
  1774. {
  1775. client.SendLogoutPacket();
  1776. if (!client.IsLoggingOut)
  1777. {
  1778. client.IsLoggingOut = true;
  1779. client.Close();
  1780. }
  1781. }
  1782. }
  1783. }