LLUDPServer.cs 55 KB

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