LLUDPServer.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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 Nini.Config;
  37. using OpenMetaverse.Packets;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Monitoring;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenMetaverse;
  42. using TokenBucket = OpenSim.Region.ClientStack.LindenUDP.TokenBucket;
  43. namespace OpenSim.Region.ClientStack.LindenUDP
  44. {
  45. /// <summary>
  46. /// A shim around LLUDPServer that implements the IClientNetworkServer interface
  47. /// </summary>
  48. public sealed class LLUDPServerShim : IClientNetworkServer
  49. {
  50. LLUDPServer m_udpServer;
  51. public LLUDPServerShim()
  52. {
  53. }
  54. public void Initialise(IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port, IConfigSource configSource, AgentCircuitManager circuitManager)
  55. {
  56. m_udpServer = new LLUDPServer(listenIP, ref port, proxyPortOffsetParm, allow_alternate_port, configSource, circuitManager);
  57. }
  58. public void NetworkStop()
  59. {
  60. m_udpServer.Stop();
  61. }
  62. public void AddScene(IScene scene)
  63. {
  64. m_udpServer.AddScene(scene);
  65. }
  66. public bool HandlesRegion(Location x)
  67. {
  68. return m_udpServer.HandlesRegion(x);
  69. }
  70. public void Start()
  71. {
  72. m_udpServer.Start();
  73. }
  74. public void Stop()
  75. {
  76. m_udpServer.Stop();
  77. }
  78. }
  79. /// <summary>
  80. /// The LLUDP server for a region. This handles incoming and outgoing
  81. /// packets for all UDP connections to the region
  82. /// </summary>
  83. public class LLUDPServer : OpenSimUDPBase
  84. {
  85. /// <summary>Maximum transmission unit, or UDP packet size, for the LLUDP protocol</summary>
  86. public const int MTU = 1400;
  87. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  88. /// <summary>The measured resolution of Environment.TickCount</summary>
  89. public readonly float TickCountResolution;
  90. /// <summary>Number of prim updates to put on the queue each time the
  91. /// OnQueueEmpty event is triggered for updates</summary>
  92. public readonly int PrimUpdatesPerCallback;
  93. /// <summary>Number of texture packets to put on the queue each time the
  94. /// OnQueueEmpty event is triggered for textures</summary>
  95. public readonly int TextureSendLimit;
  96. /// <summary>Handlers for incoming packets</summary>
  97. //PacketEventDictionary packetEvents = new PacketEventDictionary();
  98. /// <summary>Incoming packets that are awaiting handling</summary>
  99. private OpenMetaverse.BlockingQueue<IncomingPacket> packetInbox = new OpenMetaverse.BlockingQueue<IncomingPacket>();
  100. /// <summary></summary>
  101. //private UDPClientCollection m_clients = new UDPClientCollection();
  102. /// <summary>Bandwidth throttle for this UDP server</summary>
  103. protected TokenBucket m_throttle;
  104. /// <summary>Bandwidth throttle rates for this UDP server</summary>
  105. public ThrottleRates ThrottleRates { get; private set; }
  106. /// <summary>Manages authentication for agent circuits</summary>
  107. private AgentCircuitManager m_circuitManager;
  108. /// <summary>Reference to the scene this UDP server is attached to</summary>
  109. protected Scene m_scene;
  110. /// <summary>The X/Y coordinates of the scene this UDP server is attached to</summary>
  111. private Location m_location;
  112. /// <summary>The size of the receive buffer for the UDP socket. This value
  113. /// is passed up to the operating system and used in the system networking
  114. /// stack. Use zero to leave this value as the default</summary>
  115. private int m_recvBufferSize;
  116. /// <summary>Flag to process packets asynchronously or synchronously</summary>
  117. private bool m_asyncPacketHandling;
  118. /// <summary>Tracks whether or not a packet was sent each round so we know
  119. /// whether or not to sleep</summary>
  120. private bool m_packetSent;
  121. /// <summary>Environment.TickCount of the last time that packet stats were reported to the scene</summary>
  122. private int m_elapsedMSSinceLastStatReport = 0;
  123. /// <summary>Environment.TickCount of the last time the outgoing packet handler executed</summary>
  124. private int m_tickLastOutgoingPacketHandler;
  125. /// <summary>Keeps track of the number of elapsed milliseconds since the last time the outgoing packet handler looped</summary>
  126. private int m_elapsedMSOutgoingPacketHandler;
  127. /// <summary>Keeps track of the number of 100 millisecond periods elapsed in the outgoing packet handler executed</summary>
  128. private int m_elapsed100MSOutgoingPacketHandler;
  129. /// <summary>Keeps track of the number of 500 millisecond periods elapsed in the outgoing packet handler executed</summary>
  130. private int m_elapsed500MSOutgoingPacketHandler;
  131. /// <summary>Flag to signal when clients should check for resends</summary>
  132. protected bool m_resendUnacked;
  133. /// <summary>Flag to signal when clients should send ACKs</summary>
  134. protected bool m_sendAcks;
  135. /// <summary>Flag to signal when clients should send pings</summary>
  136. protected bool m_sendPing;
  137. private int m_defaultRTO = 0;
  138. private int m_maxRTO = 0;
  139. private int m_ackTimeout = 0;
  140. private int m_pausedAckTimeout = 0;
  141. private bool m_disableFacelights = false;
  142. public Socket Server { get { return null; } }
  143. private int m_malformedCount = 0; // Guard against a spamming attack
  144. /// <summary>
  145. /// Record current outgoing client for monitoring purposes.
  146. /// </summary>
  147. private IClientAPI m_currentOutgoingClient;
  148. /// <summary>
  149. /// Recording current incoming client for monitoring purposes.
  150. /// </summary>
  151. private IClientAPI m_currentIncomingClient;
  152. public LLUDPServer(IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port, IConfigSource configSource, AgentCircuitManager circuitManager)
  153. : base(listenIP, (int)port)
  154. {
  155. #region Environment.TickCount Measurement
  156. // Measure the resolution of Environment.TickCount
  157. TickCountResolution = 0f;
  158. for (int i = 0; i < 5; i++)
  159. {
  160. int start = Environment.TickCount;
  161. int now = start;
  162. while (now == start)
  163. now = Environment.TickCount;
  164. TickCountResolution += (float)(now - start) * 0.2f;
  165. }
  166. m_log.Info("[LLUDPSERVER]: Average Environment.TickCount resolution: " + TickCountResolution + "ms");
  167. TickCountResolution = (float)Math.Ceiling(TickCountResolution);
  168. #endregion Environment.TickCount Measurement
  169. m_circuitManager = circuitManager;
  170. int sceneThrottleBps = 0;
  171. IConfig config = configSource.Configs["ClientStack.LindenUDP"];
  172. if (config != null)
  173. {
  174. m_asyncPacketHandling = config.GetBoolean("async_packet_handling", true);
  175. m_recvBufferSize = config.GetInt("client_socket_rcvbuf_size", 0);
  176. sceneThrottleBps = config.GetInt("scene_throttle_max_bps", 0);
  177. PrimUpdatesPerCallback = config.GetInt("PrimUpdatesPerCallback", 100);
  178. TextureSendLimit = config.GetInt("TextureSendLimit", 20);
  179. m_defaultRTO = config.GetInt("DefaultRTO", 0);
  180. m_maxRTO = config.GetInt("MaxRTO", 0);
  181. m_disableFacelights = config.GetBoolean("DisableFacelights", false);
  182. m_ackTimeout = 1000 * config.GetInt("AckTimeout", 60);
  183. m_pausedAckTimeout = 1000 * config.GetInt("PausedAckTimeout", 300);
  184. }
  185. else
  186. {
  187. PrimUpdatesPerCallback = 100;
  188. TextureSendLimit = 20;
  189. m_ackTimeout = 1000 * 60; // 1 minute
  190. m_pausedAckTimeout = 1000 * 300; // 5 minutes
  191. }
  192. #region BinaryStats
  193. config = configSource.Configs["Statistics.Binary"];
  194. m_shouldCollectStats = false;
  195. if (config != null)
  196. {
  197. if (config.Contains("enabled") && config.GetBoolean("enabled"))
  198. {
  199. if (config.Contains("collect_packet_headers"))
  200. m_shouldCollectStats = config.GetBoolean("collect_packet_headers");
  201. if (config.Contains("packet_headers_period_seconds"))
  202. {
  203. binStatsMaxFilesize = TimeSpan.FromSeconds(config.GetInt("region_stats_period_seconds"));
  204. }
  205. if (config.Contains("stats_dir"))
  206. {
  207. binStatsDir = config.GetString("stats_dir");
  208. }
  209. }
  210. else
  211. {
  212. m_shouldCollectStats = false;
  213. }
  214. }
  215. #endregion BinaryStats
  216. m_throttle = new TokenBucket(null, sceneThrottleBps);
  217. ThrottleRates = new ThrottleRates(configSource);
  218. }
  219. public void Start()
  220. {
  221. if (m_scene == null)
  222. throw new InvalidOperationException("[LLUDPSERVER]: Cannot LLUDPServer.Start() without an IScene reference");
  223. m_log.InfoFormat(
  224. "[LLUDPSERVER]: Starting the LLUDP server in {0} mode",
  225. m_asyncPacketHandling ? "asynchronous" : "synchronous");
  226. base.Start(m_recvBufferSize, m_asyncPacketHandling);
  227. // Start the packet processing threads
  228. Watchdog.StartThread(
  229. IncomingPacketHandler,
  230. string.Format("Incoming Packets ({0})", m_scene.RegionInfo.RegionName),
  231. ThreadPriority.Normal,
  232. false,
  233. true,
  234. GetWatchdogIncomingAlarmData,
  235. Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS);
  236. Watchdog.StartThread(
  237. OutgoingPacketHandler,
  238. string.Format("Outgoing Packets ({0})", m_scene.RegionInfo.RegionName),
  239. ThreadPriority.Normal,
  240. false,
  241. true,
  242. GetWatchdogOutgoingAlarmData,
  243. Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS);
  244. m_elapsedMSSinceLastStatReport = Environment.TickCount;
  245. }
  246. /// <summary>
  247. /// If the outgoing UDP thread times out, then return client that was being processed to help with debugging.
  248. /// </summary>
  249. /// <returns></returns>
  250. private string GetWatchdogIncomingAlarmData()
  251. {
  252. return string.Format(
  253. "Client is {0}",
  254. m_currentIncomingClient != null ? m_currentIncomingClient.Name : "none");
  255. }
  256. /// <summary>
  257. /// If the outgoing UDP thread times out, then return client that was being processed to help with debugging.
  258. /// </summary>
  259. /// <returns></returns>
  260. private string GetWatchdogOutgoingAlarmData()
  261. {
  262. return string.Format(
  263. "Client is {0}",
  264. m_currentOutgoingClient != null ? m_currentOutgoingClient.Name : "none");
  265. }
  266. public new void Stop()
  267. {
  268. m_log.Info("[LLUDPSERVER]: Shutting down the LLUDP server for " + m_scene.RegionInfo.RegionName);
  269. base.Stop();
  270. }
  271. public void AddScene(IScene scene)
  272. {
  273. if (m_scene != null)
  274. {
  275. m_log.Error("[LLUDPSERVER]: AddScene() called on an LLUDPServer that already has a scene");
  276. return;
  277. }
  278. if (!(scene is Scene))
  279. {
  280. m_log.Error("[LLUDPSERVER]: AddScene() called with an unrecognized scene type " + scene.GetType());
  281. return;
  282. }
  283. m_scene = (Scene)scene;
  284. m_location = new Location(m_scene.RegionInfo.RegionHandle);
  285. }
  286. public bool HandlesRegion(Location x)
  287. {
  288. return x == m_location;
  289. }
  290. public void BroadcastPacket(Packet packet, ThrottleOutPacketType category, bool sendToPausedAgents, bool allowSplitting)
  291. {
  292. // CoarseLocationUpdate and AvatarGroupsReply packets cannot be split in an automated way
  293. if ((packet.Type == PacketType.CoarseLocationUpdate || packet.Type == PacketType.AvatarGroupsReply) && allowSplitting)
  294. allowSplitting = false;
  295. if (allowSplitting && packet.HasVariableBlocks)
  296. {
  297. byte[][] datas = packet.ToBytesMultiple();
  298. int packetCount = datas.Length;
  299. if (packetCount < 1)
  300. m_log.Error("[LLUDPSERVER]: Failed to split " + packet.Type + " with estimated length " + packet.Length);
  301. for (int i = 0; i < packetCount; i++)
  302. {
  303. byte[] data = datas[i];
  304. m_scene.ForEachClient(
  305. delegate(IClientAPI client)
  306. {
  307. if (client is LLClientView)
  308. SendPacketData(((LLClientView)client).UDPClient, data, packet.Type, category, null);
  309. }
  310. );
  311. }
  312. }
  313. else
  314. {
  315. byte[] data = packet.ToBytes();
  316. m_scene.ForEachClient(
  317. delegate(IClientAPI client)
  318. {
  319. if (client is LLClientView)
  320. SendPacketData(((LLClientView)client).UDPClient, data, packet.Type, category, null);
  321. }
  322. );
  323. }
  324. }
  325. /// <summary>
  326. /// Start the process of sending a packet to the client.
  327. /// </summary>
  328. /// <param name="udpClient"></param>
  329. /// <param name="packet"></param>
  330. /// <param name="category"></param>
  331. /// <param name="allowSplitting"></param>
  332. /// <param name="method">
  333. /// The method to call if the packet is not acked by the client. If null, then a standard
  334. /// resend of the packet is done.
  335. /// </param>
  336. public virtual void SendPacket(
  337. LLUDPClient udpClient, Packet packet, ThrottleOutPacketType category, bool allowSplitting, UnackedPacketMethod method)
  338. {
  339. // CoarseLocationUpdate packets cannot be split in an automated way
  340. if (packet.Type == PacketType.CoarseLocationUpdate && allowSplitting)
  341. allowSplitting = false;
  342. if (allowSplitting && packet.HasVariableBlocks)
  343. {
  344. byte[][] datas = packet.ToBytesMultiple();
  345. int packetCount = datas.Length;
  346. if (packetCount < 1)
  347. m_log.Error("[LLUDPSERVER]: Failed to split " + packet.Type + " with estimated length " + packet.Length);
  348. for (int i = 0; i < packetCount; i++)
  349. {
  350. byte[] data = datas[i];
  351. SendPacketData(udpClient, data, packet.Type, category, method);
  352. }
  353. }
  354. else
  355. {
  356. byte[] data = packet.ToBytes();
  357. SendPacketData(udpClient, data, packet.Type, category, method);
  358. }
  359. }
  360. /// <summary>
  361. /// Start the process of sending a packet to the client.
  362. /// </summary>
  363. /// <param name="udpClient"></param>
  364. /// <param name="data"></param>
  365. /// <param name="type"></param>
  366. /// <param name="category"></param>
  367. /// <param name="method">
  368. /// The method to call if the packet is not acked by the client. If null, then a standard
  369. /// resend of the packet is done.
  370. /// </param>
  371. public void SendPacketData(
  372. LLUDPClient udpClient, byte[] data, PacketType type, ThrottleOutPacketType category, UnackedPacketMethod method)
  373. {
  374. int dataLength = data.Length;
  375. bool doZerocode = (data[0] & Helpers.MSG_ZEROCODED) != 0;
  376. bool doCopy = true;
  377. // Frequency analysis of outgoing packet sizes shows a large clump of packets at each end of the spectrum.
  378. // The vast majority of packets are less than 200 bytes, although due to asset transfers and packet splitting
  379. // there are a decent number of packets in the 1000-1140 byte range. We allocate one of two sizes of data here
  380. // to accomodate for both common scenarios and provide ample room for ACK appending in both
  381. int bufferSize = (dataLength > 180) ? LLUDPServer.MTU : 200;
  382. UDPPacketBuffer buffer = new UDPPacketBuffer(udpClient.RemoteEndPoint, bufferSize);
  383. // Zerocode if needed
  384. if (doZerocode)
  385. {
  386. try
  387. {
  388. dataLength = Helpers.ZeroEncode(data, dataLength, buffer.Data);
  389. doCopy = false;
  390. }
  391. catch (IndexOutOfRangeException)
  392. {
  393. // The packet grew larger than the bufferSize while zerocoding.
  394. // Remove the MSG_ZEROCODED flag and send the unencoded data
  395. // instead
  396. m_log.Debug("[LLUDPSERVER]: Packet exceeded buffer size during zerocoding for " + type + ". DataLength=" + dataLength +
  397. " and BufferLength=" + buffer.Data.Length + ". Removing MSG_ZEROCODED flag");
  398. data[0] = (byte)(data[0] & ~Helpers.MSG_ZEROCODED);
  399. }
  400. }
  401. // If the packet data wasn't already copied during zerocoding, copy it now
  402. if (doCopy)
  403. {
  404. if (dataLength <= buffer.Data.Length)
  405. {
  406. Buffer.BlockCopy(data, 0, buffer.Data, 0, dataLength);
  407. }
  408. else
  409. {
  410. bufferSize = dataLength;
  411. buffer = new UDPPacketBuffer(udpClient.RemoteEndPoint, bufferSize);
  412. // m_log.Error("[LLUDPSERVER]: Packet exceeded buffer size! This could be an indication of packet assembly not obeying the MTU. Type=" +
  413. // type + ", DataLength=" + dataLength + ", BufferLength=" + buffer.Data.Length + ". Dropping packet");
  414. Buffer.BlockCopy(data, 0, buffer.Data, 0, dataLength);
  415. }
  416. }
  417. buffer.DataLength = dataLength;
  418. #region Queue or Send
  419. OutgoingPacket outgoingPacket = new OutgoingPacket(udpClient, buffer, category, null);
  420. // If we were not provided a method for handling unacked, use the UDPServer default method
  421. outgoingPacket.UnackedMethod = ((method == null) ? delegate(OutgoingPacket oPacket) { ResendUnacked(oPacket); } : method);
  422. // If a Linden Lab 1.23.5 client receives an update packet after a kill packet for an object, it will
  423. // continue to display the deleted object until relog. Therefore, we need to always queue a kill object
  424. // packet so that it isn't sent before a queued update packet.
  425. bool requestQueue = type == PacketType.KillObject;
  426. if (!outgoingPacket.Client.EnqueueOutgoing(outgoingPacket, requestQueue))
  427. SendPacketFinal(outgoingPacket);
  428. #endregion Queue or Send
  429. }
  430. public void SendAcks(LLUDPClient udpClient)
  431. {
  432. uint ack;
  433. if (udpClient.PendingAcks.Dequeue(out ack))
  434. {
  435. List<PacketAckPacket.PacketsBlock> blocks = new List<PacketAckPacket.PacketsBlock>();
  436. PacketAckPacket.PacketsBlock block = new PacketAckPacket.PacketsBlock();
  437. block.ID = ack;
  438. blocks.Add(block);
  439. while (udpClient.PendingAcks.Dequeue(out ack))
  440. {
  441. block = new PacketAckPacket.PacketsBlock();
  442. block.ID = ack;
  443. blocks.Add(block);
  444. }
  445. PacketAckPacket packet = new PacketAckPacket();
  446. packet.Header.Reliable = false;
  447. packet.Packets = blocks.ToArray();
  448. SendPacket(udpClient, packet, ThrottleOutPacketType.Unknown, true, null);
  449. }
  450. }
  451. public void SendPing(LLUDPClient udpClient)
  452. {
  453. StartPingCheckPacket pc = (StartPingCheckPacket)PacketPool.Instance.GetPacket(PacketType.StartPingCheck);
  454. pc.Header.Reliable = false;
  455. pc.PingID.PingID = (byte)udpClient.CurrentPingSequence++;
  456. // We *could* get OldestUnacked, but it would hurt performance and not provide any benefit
  457. pc.PingID.OldestUnacked = 0;
  458. SendPacket(udpClient, pc, ThrottleOutPacketType.Unknown, false, null);
  459. }
  460. public void CompletePing(LLUDPClient udpClient, byte pingID)
  461. {
  462. CompletePingCheckPacket completePing = new CompletePingCheckPacket();
  463. completePing.PingID.PingID = pingID;
  464. SendPacket(udpClient, completePing, ThrottleOutPacketType.Unknown, false, null);
  465. }
  466. public void HandleUnacked(LLClientView client)
  467. {
  468. LLUDPClient udpClient = client.UDPClient;
  469. if (!udpClient.IsConnected)
  470. return;
  471. // Disconnect an agent if no packets are received for some time
  472. int timeoutTicks = m_ackTimeout;
  473. // Allow more slack if the client is "paused" eg file upload dialogue is open
  474. // Some sort of limit is needed in case the client crashes, loses its network connection
  475. // or some other disaster prevents it from sendung the AgentResume
  476. if (udpClient.IsPaused)
  477. timeoutTicks = m_pausedAckTimeout;
  478. if (client.IsActive &&
  479. (Environment.TickCount & Int32.MaxValue) - udpClient.TickLastPacketReceived > timeoutTicks)
  480. {
  481. // We must set IsActive synchronously so that we can stop the packet loop reinvoking this method, even
  482. // though it's set later on by LLClientView.Close()
  483. client.IsActive = false;
  484. // Fire this out on a different thread so that we don't hold up outgoing packet processing for
  485. // everybody else if this is being called due to an ack timeout.
  486. // This is the same as processing as the async process of a logout request.
  487. Util.FireAndForget(o => DeactivateClientDueToTimeout(client));
  488. return;
  489. }
  490. // Get a list of all of the packets that have been sitting unacked longer than udpClient.RTO
  491. List<OutgoingPacket> expiredPackets = udpClient.NeedAcks.GetExpiredPackets(udpClient.RTO);
  492. if (expiredPackets != null)
  493. {
  494. //m_log.Debug("[LLUDPSERVER]: Handling " + expiredPackets.Count + " packets to " + udpClient.AgentID + ", RTO=" + udpClient.RTO);
  495. // Exponential backoff of the retransmission timeout
  496. udpClient.BackoffRTO();
  497. for (int i = 0; i < expiredPackets.Count; ++i)
  498. expiredPackets[i].UnackedMethod(expiredPackets[i]);
  499. }
  500. }
  501. public void ResendUnacked(OutgoingPacket outgoingPacket)
  502. {
  503. //m_log.DebugFormat("[LLUDPSERVER]: Resending packet #{0} (attempt {1}), {2}ms have passed",
  504. // outgoingPacket.SequenceNumber, outgoingPacket.ResendCount, Environment.TickCount - outgoingPacket.TickCount);
  505. // Set the resent flag
  506. outgoingPacket.Buffer.Data[0] = (byte)(outgoingPacket.Buffer.Data[0] | Helpers.MSG_RESENT);
  507. outgoingPacket.Category = ThrottleOutPacketType.Resend;
  508. // Bump up the resend count on this packet
  509. Interlocked.Increment(ref outgoingPacket.ResendCount);
  510. // Requeue or resend the packet
  511. if (!outgoingPacket.Client.EnqueueOutgoing(outgoingPacket, false))
  512. SendPacketFinal(outgoingPacket);
  513. }
  514. public void Flush(LLUDPClient udpClient)
  515. {
  516. // FIXME: Implement?
  517. }
  518. /// <summary>
  519. /// Actually send a packet to a client.
  520. /// </summary>
  521. /// <param name="outgoingPacket"></param>
  522. internal void SendPacketFinal(OutgoingPacket outgoingPacket)
  523. {
  524. UDPPacketBuffer buffer = outgoingPacket.Buffer;
  525. byte flags = buffer.Data[0];
  526. bool isResend = (flags & Helpers.MSG_RESENT) != 0;
  527. bool isReliable = (flags & Helpers.MSG_RELIABLE) != 0;
  528. bool isZerocoded = (flags & Helpers.MSG_ZEROCODED) != 0;
  529. LLUDPClient udpClient = outgoingPacket.Client;
  530. if (!udpClient.IsConnected)
  531. return;
  532. #region ACK Appending
  533. int dataLength = buffer.DataLength;
  534. // NOTE: I'm seeing problems with some viewers when ACKs are appended to zerocoded packets so I've disabled that here
  535. if (!isZerocoded)
  536. {
  537. // Keep appending ACKs until there is no room left in the buffer or there are
  538. // no more ACKs to append
  539. uint ackCount = 0;
  540. uint ack;
  541. while (dataLength + 5 < buffer.Data.Length && udpClient.PendingAcks.Dequeue(out ack))
  542. {
  543. Utils.UIntToBytesBig(ack, buffer.Data, dataLength);
  544. dataLength += 4;
  545. ++ackCount;
  546. }
  547. if (ackCount > 0)
  548. {
  549. // Set the last byte of the packet equal to the number of appended ACKs
  550. buffer.Data[dataLength++] = (byte)ackCount;
  551. // Set the appended ACKs flag on this packet
  552. buffer.Data[0] = (byte)(buffer.Data[0] | Helpers.MSG_APPENDED_ACKS);
  553. }
  554. }
  555. buffer.DataLength = dataLength;
  556. #endregion ACK Appending
  557. #region Sequence Number Assignment
  558. if (!isResend)
  559. {
  560. // Not a resend, assign a new sequence number
  561. uint sequenceNumber = (uint)Interlocked.Increment(ref udpClient.CurrentSequence);
  562. Utils.UIntToBytesBig(sequenceNumber, buffer.Data, 1);
  563. outgoingPacket.SequenceNumber = sequenceNumber;
  564. if (isReliable)
  565. {
  566. // Add this packet to the list of ACK responses we are waiting on from the server
  567. udpClient.NeedAcks.Add(outgoingPacket);
  568. }
  569. }
  570. else
  571. {
  572. Interlocked.Increment(ref udpClient.PacketsResent);
  573. }
  574. #endregion Sequence Number Assignment
  575. // Stats tracking
  576. Interlocked.Increment(ref udpClient.PacketsSent);
  577. // Put the UDP payload on the wire
  578. AsyncBeginSend(buffer);
  579. // Keep track of when this packet was sent out (right now)
  580. outgoingPacket.TickCount = Environment.TickCount & Int32.MaxValue;
  581. }
  582. public override void PacketReceived(UDPPacketBuffer buffer)
  583. {
  584. // Debugging/Profiling
  585. //try { Thread.CurrentThread.Name = "PacketReceived (" + m_scene.RegionInfo.RegionName + ")"; }
  586. //catch (Exception) { }
  587. // m_log.DebugFormat(
  588. // "[LLUDPSERVER]: Packet received from {0} in {1}", buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  589. LLUDPClient udpClient = null;
  590. Packet packet = null;
  591. int packetEnd = buffer.DataLength - 1;
  592. IPEndPoint address = (IPEndPoint)buffer.RemoteEndPoint;
  593. #region Decoding
  594. if (buffer.DataLength < 7)
  595. {
  596. // m_log.WarnFormat(
  597. // "[LLUDPSERVER]: Dropping undersized packet with {0} bytes received from {1} in {2}",
  598. // buffer.DataLength, buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  599. return; // Drop undersizd packet
  600. }
  601. int headerLen = 7;
  602. if (buffer.Data[6] == 0xFF)
  603. {
  604. if (buffer.Data[7] == 0xFF)
  605. headerLen = 10;
  606. else
  607. headerLen = 8;
  608. }
  609. if (buffer.DataLength < headerLen)
  610. {
  611. // m_log.WarnFormat(
  612. // "[LLUDPSERVER]: Dropping packet with malformed header received from {0} in {1}",
  613. // buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  614. return; // Malformed header
  615. }
  616. try
  617. {
  618. packet = Packet.BuildPacket(buffer.Data, ref packetEnd,
  619. // Only allocate a buffer for zerodecoding if the packet is zerocoded
  620. ((buffer.Data[0] & Helpers.MSG_ZEROCODED) != 0) ? new byte[4096] : null);
  621. }
  622. catch (MalformedDataException)
  623. {
  624. }
  625. catch (IndexOutOfRangeException)
  626. {
  627. // m_log.WarnFormat(
  628. // "[LLUDPSERVER]: Dropping short packet received from {0} in {1}",
  629. // buffer.RemoteEndPoint, m_scene.RegionInfo.RegionName);
  630. return; // Drop short packet
  631. }
  632. catch(Exception e)
  633. {
  634. if (m_malformedCount < 100)
  635. m_log.DebugFormat("[LLUDPSERVER]: Dropped malformed packet: " + e.ToString());
  636. m_malformedCount++;
  637. if ((m_malformedCount % 100000) == 0)
  638. m_log.DebugFormat("[LLUDPSERVER]: Received {0} malformed packets so far, probable network attack.", m_malformedCount);
  639. }
  640. // Fail-safe check
  641. if (packet == null)
  642. {
  643. m_log.ErrorFormat("[LLUDPSERVER]: Malformed data, cannot parse {0} byte packet from {1}:",
  644. buffer.DataLength, buffer.RemoteEndPoint);
  645. m_log.Error(Utils.BytesToHexString(buffer.Data, buffer.DataLength, null));
  646. return;
  647. }
  648. #endregion Decoding
  649. #region Packet to Client Mapping
  650. // UseCircuitCode handling
  651. if (packet.Type == PacketType.UseCircuitCode)
  652. {
  653. object[] array = new object[] { buffer, packet };
  654. Util.FireAndForget(HandleUseCircuitCode, array);
  655. return;
  656. }
  657. // Determine which agent this packet came from
  658. IClientAPI client;
  659. if (!m_scene.TryGetClient(address, out client) || !(client is LLClientView))
  660. {
  661. //m_log.Debug("[LLUDPSERVER]: Received a " + packet.Type + " packet from an unrecognized source: " + address + " in " + m_scene.RegionInfo.RegionName);
  662. return;
  663. }
  664. udpClient = ((LLClientView)client).UDPClient;
  665. if (!udpClient.IsConnected)
  666. return;
  667. #endregion Packet to Client Mapping
  668. // Stats tracking
  669. Interlocked.Increment(ref udpClient.PacketsReceived);
  670. int now = Environment.TickCount & Int32.MaxValue;
  671. udpClient.TickLastPacketReceived = now;
  672. #region ACK Receiving
  673. // Handle appended ACKs
  674. if (packet.Header.AppendedAcks && packet.Header.AckList != null)
  675. {
  676. for (int i = 0; i < packet.Header.AckList.Length; i++)
  677. udpClient.NeedAcks.Acknowledge(packet.Header.AckList[i], now, packet.Header.Resent);
  678. }
  679. // Handle PacketAck packets
  680. if (packet.Type == PacketType.PacketAck)
  681. {
  682. PacketAckPacket ackPacket = (PacketAckPacket)packet;
  683. for (int i = 0; i < ackPacket.Packets.Length; i++)
  684. udpClient.NeedAcks.Acknowledge(ackPacket.Packets[i].ID, now, packet.Header.Resent);
  685. // We don't need to do anything else with PacketAck packets
  686. return;
  687. }
  688. #endregion ACK Receiving
  689. #region ACK Sending
  690. if (packet.Header.Reliable)
  691. {
  692. udpClient.PendingAcks.Enqueue(packet.Header.Sequence);
  693. // This is a somewhat odd sequence of steps to pull the client.BytesSinceLastACK value out,
  694. // add the current received bytes to it, test if 2*MTU bytes have been sent, if so remove
  695. // 2*MTU bytes from the value and send ACKs, and finally add the local value back to
  696. // client.BytesSinceLastACK. Lockless thread safety
  697. int bytesSinceLastACK = Interlocked.Exchange(ref udpClient.BytesSinceLastACK, 0);
  698. bytesSinceLastACK += buffer.DataLength;
  699. if (bytesSinceLastACK > LLUDPServer.MTU * 2)
  700. {
  701. bytesSinceLastACK -= LLUDPServer.MTU * 2;
  702. SendAcks(udpClient);
  703. }
  704. Interlocked.Add(ref udpClient.BytesSinceLastACK, bytesSinceLastACK);
  705. }
  706. #endregion ACK Sending
  707. #region Incoming Packet Accounting
  708. // Check the archive of received reliable packet IDs to see whether we already received this packet
  709. if (packet.Header.Reliable && !udpClient.PacketArchive.TryEnqueue(packet.Header.Sequence))
  710. {
  711. if (packet.Header.Resent)
  712. m_log.DebugFormat(
  713. "[LLUDPSERVER]: Received a resend of already processed packet #{0}, type {1} from {2}",
  714. packet.Header.Sequence, packet.Type, client.Name);
  715. else
  716. m_log.WarnFormat(
  717. "[LLUDPSERVER]: Received a duplicate (not marked as resend) of packet #{0}, type {1} from {2}",
  718. packet.Header.Sequence, packet.Type, client.Name);
  719. // Avoid firing a callback twice for the same packet
  720. return;
  721. }
  722. #endregion Incoming Packet Accounting
  723. #region BinaryStats
  724. LogPacketHeader(true, udpClient.CircuitCode, 0, packet.Type, (ushort)packet.Length);
  725. #endregion BinaryStats
  726. #region Ping Check Handling
  727. if (packet.Type == PacketType.StartPingCheck)
  728. {
  729. // We don't need to do anything else with ping checks
  730. StartPingCheckPacket startPing = (StartPingCheckPacket)packet;
  731. CompletePing(udpClient, startPing.PingID.PingID);
  732. if ((Environment.TickCount - m_elapsedMSSinceLastStatReport) >= 3000)
  733. {
  734. udpClient.SendPacketStats();
  735. m_elapsedMSSinceLastStatReport = Environment.TickCount;
  736. }
  737. return;
  738. }
  739. else if (packet.Type == PacketType.CompletePingCheck)
  740. {
  741. // We don't currently track client ping times
  742. return;
  743. }
  744. #endregion Ping Check Handling
  745. // Inbox insertion
  746. packetInbox.Enqueue(new IncomingPacket((LLClientView)client, packet));
  747. }
  748. #region BinaryStats
  749. public class PacketLogger
  750. {
  751. public DateTime StartTime;
  752. public string Path = null;
  753. public System.IO.BinaryWriter Log = null;
  754. }
  755. public static PacketLogger PacketLog;
  756. protected static bool m_shouldCollectStats = false;
  757. // Number of seconds to log for
  758. static TimeSpan binStatsMaxFilesize = TimeSpan.FromSeconds(300);
  759. static object binStatsLogLock = new object();
  760. static string binStatsDir = "";
  761. public static void LogPacketHeader(bool incoming, uint circuit, byte flags, PacketType packetType, ushort size)
  762. {
  763. if (!m_shouldCollectStats) return;
  764. // Binary logging format is TTTTTTTTCCCCFPPPSS, T=Time, C=Circuit, F=Flags, P=PacketType, S=size
  765. // Put the incoming bit into the least significant bit of the flags byte
  766. if (incoming)
  767. flags |= 0x01;
  768. else
  769. flags &= 0xFE;
  770. // Put the flags byte into the most significant bits of the type integer
  771. uint type = (uint)packetType;
  772. type |= (uint)flags << 24;
  773. // m_log.Debug("1 LogPacketHeader(): Outside lock");
  774. lock (binStatsLogLock)
  775. {
  776. DateTime now = DateTime.Now;
  777. // m_log.Debug("2 LogPacketHeader(): Inside lock. now is " + now.Ticks);
  778. try
  779. {
  780. if (PacketLog == null || (now > PacketLog.StartTime + binStatsMaxFilesize))
  781. {
  782. if (PacketLog != null && PacketLog.Log != null)
  783. {
  784. PacketLog.Log.Close();
  785. }
  786. // First log file or time has expired, start writing to a new log file
  787. PacketLog = new PacketLogger();
  788. PacketLog.StartTime = now;
  789. PacketLog.Path = (binStatsDir.Length > 0 ? binStatsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "")
  790. + String.Format("packets-{0}.log", now.ToString("yyyyMMddHHmmss"));
  791. PacketLog.Log = new BinaryWriter(File.Open(PacketLog.Path, FileMode.Append, FileAccess.Write));
  792. }
  793. // Serialize the data
  794. byte[] output = new byte[18];
  795. Buffer.BlockCopy(BitConverter.GetBytes(now.Ticks), 0, output, 0, 8);
  796. Buffer.BlockCopy(BitConverter.GetBytes(circuit), 0, output, 8, 4);
  797. Buffer.BlockCopy(BitConverter.GetBytes(type), 0, output, 12, 4);
  798. Buffer.BlockCopy(BitConverter.GetBytes(size), 0, output, 16, 2);
  799. // Write the serialized data to disk
  800. if (PacketLog != null && PacketLog.Log != null)
  801. PacketLog.Log.Write(output);
  802. }
  803. catch (Exception ex)
  804. {
  805. m_log.Error("Packet statistics gathering failed: " + ex.Message, ex);
  806. if (PacketLog.Log != null)
  807. {
  808. PacketLog.Log.Close();
  809. }
  810. PacketLog = null;
  811. }
  812. }
  813. }
  814. #endregion BinaryStats
  815. private void HandleUseCircuitCode(object o)
  816. {
  817. IPEndPoint remoteEndPoint = null;
  818. IClientAPI client = null;
  819. try
  820. {
  821. // DateTime startTime = DateTime.Now;
  822. object[] array = (object[])o;
  823. UDPPacketBuffer buffer = (UDPPacketBuffer)array[0];
  824. UseCircuitCodePacket uccp = (UseCircuitCodePacket)array[1];
  825. m_log.DebugFormat(
  826. "[LLUDPSERVER]: Handling UseCircuitCode request for circuit {0} to {1} from IP {2}",
  827. uccp.CircuitCode.Code, m_scene.RegionInfo.RegionName, buffer.RemoteEndPoint);
  828. remoteEndPoint = (IPEndPoint)buffer.RemoteEndPoint;
  829. AuthenticateResponse sessionInfo;
  830. if (IsClientAuthorized(uccp, out sessionInfo))
  831. {
  832. // Begin the process of adding the client to the simulator
  833. client
  834. = AddClient(
  835. uccp.CircuitCode.Code,
  836. uccp.CircuitCode.ID,
  837. uccp.CircuitCode.SessionID,
  838. remoteEndPoint,
  839. sessionInfo);
  840. // Send ack straight away to let the viewer know that the connection is active.
  841. // The client will be null if it already exists (e.g. if on a region crossing the client sends a use
  842. // circuit code to the existing child agent. This is not particularly obvious.
  843. SendAckImmediate(remoteEndPoint, uccp.Header.Sequence);
  844. // We only want to send initial data to new clients, not ones which are being converted from child to root.
  845. if (client != null)
  846. client.SceneAgent.SendInitialDataToMe();
  847. }
  848. else
  849. {
  850. // Don't create clients for unauthorized requesters.
  851. m_log.WarnFormat(
  852. "[LLUDPSERVER]: Ignoring connection request for {0} to {1} with unknown circuit code {2} from IP {3}",
  853. uccp.CircuitCode.ID, m_scene.RegionInfo.RegionName, uccp.CircuitCode.Code, remoteEndPoint);
  854. }
  855. // m_log.DebugFormat(
  856. // "[LLUDPSERVER]: Handling UseCircuitCode request from {0} took {1}ms",
  857. // buffer.RemoteEndPoint, (DateTime.Now - startTime).Milliseconds);
  858. }
  859. catch (Exception e)
  860. {
  861. m_log.ErrorFormat(
  862. "[LLUDPSERVER]: UseCircuitCode handling from endpoint {0}, client {1} {2} failed. Exception {3}{4}",
  863. remoteEndPoint != null ? remoteEndPoint.ToString() : "n/a",
  864. client != null ? client.Name : "unknown",
  865. client != null ? client.AgentId.ToString() : "unknown",
  866. e.Message,
  867. e.StackTrace);
  868. }
  869. }
  870. /// <summary>
  871. /// Send an ack immediately to the given endpoint.
  872. /// </summary>
  873. /// <remarks>
  874. /// FIXME: Might be possible to use SendPacketData() like everything else, but this will require refactoring so
  875. /// that we can obtain the UDPClient easily at this point.
  876. /// </remarks>
  877. /// <param name="remoteEndpoint"></param>
  878. /// <param name="sequenceNumber"></param>
  879. private void SendAckImmediate(IPEndPoint remoteEndpoint, uint sequenceNumber)
  880. {
  881. PacketAckPacket ack = new PacketAckPacket();
  882. ack.Header.Reliable = false;
  883. ack.Packets = new PacketAckPacket.PacketsBlock[1];
  884. ack.Packets[0] = new PacketAckPacket.PacketsBlock();
  885. ack.Packets[0].ID = sequenceNumber;
  886. SendAckImmediate(remoteEndpoint, ack);
  887. }
  888. public virtual void SendAckImmediate(IPEndPoint remoteEndpoint, PacketAckPacket ack)
  889. {
  890. byte[] packetData = ack.ToBytes();
  891. int length = packetData.Length;
  892. UDPPacketBuffer buffer = new UDPPacketBuffer(remoteEndpoint, length);
  893. buffer.DataLength = length;
  894. Buffer.BlockCopy(packetData, 0, buffer.Data, 0, length);
  895. AsyncBeginSend(buffer);
  896. }
  897. private bool IsClientAuthorized(UseCircuitCodePacket useCircuitCode, out AuthenticateResponse sessionInfo)
  898. {
  899. UUID agentID = useCircuitCode.CircuitCode.ID;
  900. UUID sessionID = useCircuitCode.CircuitCode.SessionID;
  901. uint circuitCode = useCircuitCode.CircuitCode.Code;
  902. sessionInfo = m_circuitManager.AuthenticateSession(sessionID, agentID, circuitCode);
  903. return sessionInfo.Authorised;
  904. }
  905. /// <summary>
  906. /// Add a client.
  907. /// </summary>
  908. /// <param name="circuitCode"></param>
  909. /// <param name="agentID"></param>
  910. /// <param name="sessionID"></param>
  911. /// <param name="remoteEndPoint"></param>
  912. /// <param name="sessionInfo"></param>
  913. /// <returns>The client if it was added. Null if the client already existed.</returns>
  914. protected virtual IClientAPI AddClient(
  915. uint circuitCode, UUID agentID, UUID sessionID, IPEndPoint remoteEndPoint, AuthenticateResponse sessionInfo)
  916. {
  917. IClientAPI client = null;
  918. // In priciple there shouldn't be more than one thread here, ever.
  919. // But in case that happens, we need to synchronize this piece of code
  920. // because it's too important
  921. lock (this)
  922. {
  923. if (!m_scene.TryGetClient(agentID, out client))
  924. {
  925. LLUDPClient udpClient = new LLUDPClient(this, ThrottleRates, m_throttle, circuitCode, agentID, remoteEndPoint, m_defaultRTO, m_maxRTO);
  926. client = new LLClientView(m_scene, this, udpClient, sessionInfo, agentID, sessionID, circuitCode);
  927. client.OnLogout += LogoutHandler;
  928. ((LLClientView)client).DisableFacelights = m_disableFacelights;
  929. client.Start();
  930. }
  931. }
  932. return client;
  933. }
  934. /// <summary>
  935. /// Deactivates the client if we don't receive any packets within a certain amount of time (default 60 seconds).
  936. /// </summary>
  937. /// <remarks>
  938. /// If a connection is active then we will always receive packets even if nothing else is happening, due to
  939. /// regular client pings.
  940. /// </remarks>
  941. /// <param name='client'></param>
  942. private void DeactivateClientDueToTimeout(LLClientView client)
  943. {
  944. lock (client.CloseSyncLock)
  945. {
  946. m_log.WarnFormat(
  947. "[LLUDPSERVER]: Ack timeout, disconnecting {0} agent for {1} in {2}",
  948. client.SceneAgent.IsChildAgent ? "child" : "root", client.Name, m_scene.RegionInfo.RegionName);
  949. StatsManager.SimExtraStats.AddAbnormalClientThreadTermination();
  950. if (!client.SceneAgent.IsChildAgent)
  951. client.Kick("Simulator logged you out due to connection timeout");
  952. client.CloseWithoutChecks();
  953. }
  954. }
  955. private void IncomingPacketHandler()
  956. {
  957. // Set this culture for the thread that incoming packets are received
  958. // on to en-US to avoid number parsing issues
  959. Culture.SetCurrentCulture();
  960. while (base.IsRunning)
  961. {
  962. try
  963. {
  964. IncomingPacket incomingPacket = null;
  965. // HACK: This is a test to try and rate limit packet handling on Mono.
  966. // If it works, a more elegant solution can be devised
  967. if (Util.FireAndForgetCount() < 2)
  968. {
  969. //m_log.Debug("[LLUDPSERVER]: Incoming packet handler is sleeping");
  970. Thread.Sleep(30);
  971. }
  972. if (packetInbox.Dequeue(100, ref incomingPacket))
  973. ProcessInPacket(incomingPacket);//, incomingPacket); Util.FireAndForget(ProcessInPacket, incomingPacket);
  974. }
  975. catch (Exception ex)
  976. {
  977. m_log.Error("[LLUDPSERVER]: Error in the incoming packet handler loop: " + ex.Message, ex);
  978. }
  979. Watchdog.UpdateThread();
  980. }
  981. if (packetInbox.Count > 0)
  982. m_log.Warn("[LLUDPSERVER]: IncomingPacketHandler is shutting down, dropping " + packetInbox.Count + " packets");
  983. packetInbox.Clear();
  984. Watchdog.RemoveThread();
  985. }
  986. private void OutgoingPacketHandler()
  987. {
  988. // Set this culture for the thread that outgoing packets are sent
  989. // on to en-US to avoid number parsing issues
  990. Culture.SetCurrentCulture();
  991. // Typecast the function to an Action<IClientAPI> once here to avoid allocating a new
  992. // Action generic every round
  993. Action<IClientAPI> clientPacketHandler = ClientOutgoingPacketHandler;
  994. while (base.IsRunning)
  995. {
  996. try
  997. {
  998. m_packetSent = false;
  999. #region Update Timers
  1000. m_resendUnacked = false;
  1001. m_sendAcks = false;
  1002. m_sendPing = false;
  1003. // Update elapsed time
  1004. int thisTick = Environment.TickCount & Int32.MaxValue;
  1005. if (m_tickLastOutgoingPacketHandler > thisTick)
  1006. m_elapsedMSOutgoingPacketHandler += ((Int32.MaxValue - m_tickLastOutgoingPacketHandler) + thisTick);
  1007. else
  1008. m_elapsedMSOutgoingPacketHandler += (thisTick - m_tickLastOutgoingPacketHandler);
  1009. m_tickLastOutgoingPacketHandler = thisTick;
  1010. // Check for pending outgoing resends every 100ms
  1011. if (m_elapsedMSOutgoingPacketHandler >= 100)
  1012. {
  1013. m_resendUnacked = true;
  1014. m_elapsedMSOutgoingPacketHandler = 0;
  1015. m_elapsed100MSOutgoingPacketHandler += 1;
  1016. }
  1017. // Check for pending outgoing ACKs every 500ms
  1018. if (m_elapsed100MSOutgoingPacketHandler >= 5)
  1019. {
  1020. m_sendAcks = true;
  1021. m_elapsed100MSOutgoingPacketHandler = 0;
  1022. m_elapsed500MSOutgoingPacketHandler += 1;
  1023. }
  1024. // Send pings to clients every 5000ms
  1025. if (m_elapsed500MSOutgoingPacketHandler >= 10)
  1026. {
  1027. m_sendPing = true;
  1028. m_elapsed500MSOutgoingPacketHandler = 0;
  1029. }
  1030. #endregion Update Timers
  1031. // Use this for emergency monitoring -- bug hunting
  1032. //if (m_scene.EmergencyMonitoring)
  1033. // clientPacketHandler = MonitoredClientOutgoingPacketHandler;
  1034. //else
  1035. // clientPacketHandler = ClientOutgoingPacketHandler;
  1036. // Handle outgoing packets, resends, acknowledgements, and pings for each
  1037. // client. m_packetSent will be set to true if a packet is sent
  1038. m_scene.ForEachClient(clientPacketHandler);
  1039. m_currentOutgoingClient = null;
  1040. // If nothing was sent, sleep for the minimum amount of time before a
  1041. // token bucket could get more tokens
  1042. if (!m_packetSent)
  1043. Thread.Sleep((int)TickCountResolution);
  1044. Watchdog.UpdateThread();
  1045. }
  1046. catch (Exception ex)
  1047. {
  1048. m_log.Error("[LLUDPSERVER]: OutgoingPacketHandler loop threw an exception: " + ex.Message, ex);
  1049. }
  1050. }
  1051. Watchdog.RemoveThread();
  1052. }
  1053. protected void ClientOutgoingPacketHandler(IClientAPI client)
  1054. {
  1055. m_currentOutgoingClient = client;
  1056. try
  1057. {
  1058. if (client is LLClientView)
  1059. {
  1060. LLClientView llClient = (LLClientView)client;
  1061. LLUDPClient udpClient = llClient.UDPClient;
  1062. if (udpClient.IsConnected)
  1063. {
  1064. if (m_resendUnacked)
  1065. HandleUnacked(llClient);
  1066. if (m_sendAcks)
  1067. SendAcks(udpClient);
  1068. if (m_sendPing)
  1069. SendPing(udpClient);
  1070. // Dequeue any outgoing packets that are within the throttle limits
  1071. if (udpClient.DequeueOutgoing())
  1072. m_packetSent = true;
  1073. }
  1074. }
  1075. }
  1076. catch (Exception ex)
  1077. {
  1078. m_log.Error(
  1079. string.Format("[LLUDPSERVER]: OutgoingPacketHandler iteration for {0} threw ", client.Name), ex);
  1080. }
  1081. }
  1082. #region Emergency Monitoring
  1083. // Alternative packet handler fuull of instrumentation
  1084. // Handy for hunting bugs
  1085. private Stopwatch watch1 = new Stopwatch();
  1086. private Stopwatch watch2 = new Stopwatch();
  1087. private float avgProcessingTicks = 0;
  1088. private float avgResendUnackedTicks = 0;
  1089. private float avgSendAcksTicks = 0;
  1090. private float avgSendPingTicks = 0;
  1091. private float avgDequeueTicks = 0;
  1092. private long nticks = 0;
  1093. private long nticksUnack = 0;
  1094. private long nticksAck = 0;
  1095. private long nticksPing = 0;
  1096. private int npacksSent = 0;
  1097. private int npackNotSent = 0;
  1098. private void MonitoredClientOutgoingPacketHandler(IClientAPI client)
  1099. {
  1100. nticks++;
  1101. watch1.Start();
  1102. m_currentOutgoingClient = client;
  1103. try
  1104. {
  1105. if (client is LLClientView)
  1106. {
  1107. LLClientView llClient = (LLClientView)client;
  1108. LLUDPClient udpClient = llClient.UDPClient;
  1109. if (udpClient.IsConnected)
  1110. {
  1111. if (m_resendUnacked)
  1112. {
  1113. nticksUnack++;
  1114. watch2.Start();
  1115. HandleUnacked(llClient);
  1116. watch2.Stop();
  1117. avgResendUnackedTicks = (nticksUnack - 1)/(float)nticksUnack * avgResendUnackedTicks + (watch2.ElapsedTicks / (float)nticksUnack);
  1118. watch2.Reset();
  1119. }
  1120. if (m_sendAcks)
  1121. {
  1122. nticksAck++;
  1123. watch2.Start();
  1124. SendAcks(udpClient);
  1125. watch2.Stop();
  1126. avgSendAcksTicks = (nticksAck - 1) / (float)nticksAck * avgSendAcksTicks + (watch2.ElapsedTicks / (float)nticksAck);
  1127. watch2.Reset();
  1128. }
  1129. if (m_sendPing)
  1130. {
  1131. nticksPing++;
  1132. watch2.Start();
  1133. SendPing(udpClient);
  1134. watch2.Stop();
  1135. avgSendPingTicks = (nticksPing - 1) / (float)nticksPing * avgSendPingTicks + (watch2.ElapsedTicks / (float)nticksPing);
  1136. watch2.Reset();
  1137. }
  1138. watch2.Start();
  1139. // Dequeue any outgoing packets that are within the throttle limits
  1140. if (udpClient.DequeueOutgoing())
  1141. {
  1142. m_packetSent = true;
  1143. npacksSent++;
  1144. }
  1145. else
  1146. npackNotSent++;
  1147. watch2.Stop();
  1148. avgDequeueTicks = (nticks - 1) / (float)nticks * avgDequeueTicks + (watch2.ElapsedTicks / (float)nticks);
  1149. watch2.Reset();
  1150. }
  1151. else
  1152. m_log.WarnFormat("[LLUDPSERVER]: Client is not connected");
  1153. }
  1154. }
  1155. catch (Exception ex)
  1156. {
  1157. m_log.Error("[LLUDPSERVER]: OutgoingPacketHandler iteration for " + client.Name +
  1158. " threw an exception: " + ex.Message, ex);
  1159. }
  1160. watch1.Stop();
  1161. avgProcessingTicks = (nticks - 1) / (float)nticks * avgProcessingTicks + (watch1.ElapsedTicks / (float)nticks);
  1162. watch1.Reset();
  1163. // reuse this -- it's every ~100ms
  1164. if (m_scene.EmergencyMonitoring && nticks % 100 == 0)
  1165. {
  1166. 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})",
  1167. avgProcessingTicks, avgResendUnackedTicks, avgSendAcksTicks, avgSendPingTicks, avgDequeueTicks, TickCountResolution, npacksSent, npackNotSent);
  1168. npackNotSent = npacksSent = 0;
  1169. }
  1170. }
  1171. #endregion
  1172. private void ProcessInPacket(IncomingPacket incomingPacket)
  1173. {
  1174. Packet packet = incomingPacket.Packet;
  1175. LLClientView client = incomingPacket.Client;
  1176. if (client.IsActive)
  1177. {
  1178. m_currentIncomingClient = client;
  1179. try
  1180. {
  1181. // Process this packet
  1182. client.ProcessInPacket(packet);
  1183. }
  1184. catch (ThreadAbortException)
  1185. {
  1186. // If something is trying to abort the packet processing thread, take that as a hint that it's time to shut down
  1187. m_log.Info("[LLUDPSERVER]: Caught a thread abort, shutting down the LLUDP server");
  1188. Stop();
  1189. }
  1190. catch (Exception e)
  1191. {
  1192. // Don't let a failure in an individual client thread crash the whole sim.
  1193. m_log.Error(
  1194. string.Format(
  1195. "[LLUDPSERVER]: Client packet handler for {0} for packet {1} threw ",
  1196. client.Name, packet.Type),
  1197. e);
  1198. }
  1199. finally
  1200. {
  1201. m_currentIncomingClient = null;
  1202. }
  1203. }
  1204. else
  1205. {
  1206. m_log.DebugFormat(
  1207. "[LLUDPSERVER]: Dropped incoming {0} for dead client {1} in {2}",
  1208. packet.Type, client.Name, m_scene.RegionInfo.RegionName);
  1209. }
  1210. }
  1211. protected void LogoutHandler(IClientAPI client)
  1212. {
  1213. client.SendLogoutPacket();
  1214. if (!client.IsLoggingOut)
  1215. {
  1216. client.IsLoggingOut = true;
  1217. client.Close();
  1218. }
  1219. }
  1220. }
  1221. }