LLUDPServer.cs 53 KB

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