LLUDPServer.cs 47 KB

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