LLUDPServer.cs 56 KB

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