LLUDPServer.cs 45 KB

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