LLUDPClient.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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.Net;
  30. using System.Threading;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Packets;
  35. using TokenBucket = OpenSim.Region.ClientStack.LindenUDP.TokenBucket;
  36. namespace OpenSim.Region.ClientStack.LindenUDP
  37. {
  38. #region Delegates
  39. /// <summary>
  40. /// Fired when updated networking stats are produced for this client
  41. /// </summary>
  42. /// <param name="inPackets">Number of incoming packets received since this
  43. /// event was last fired</param>
  44. /// <param name="outPackets">Number of outgoing packets sent since this
  45. /// event was last fired</param>
  46. /// <param name="unAckedBytes">Current total number of bytes in packets we
  47. /// are waiting on ACKs for</param>
  48. public delegate void PacketStats(int inPackets, int outPackets, int unAckedBytes);
  49. /// <summary>
  50. /// Fired when the queue for one or more packet categories is empty. This
  51. /// event can be hooked to put more data on the empty queues
  52. /// </summary>
  53. /// <param name="category">Categories of the packet queues that are empty</param>
  54. public delegate void QueueEmpty(ThrottleOutPacketTypeFlags categories);
  55. #endregion Delegates
  56. /// <summary>
  57. /// Tracks state for a client UDP connection and provides client-specific methods
  58. /// </summary>
  59. public sealed class LLUDPClient
  60. {
  61. // TODO: Make this a config setting
  62. /// <summary>Percentage of the task throttle category that is allocated to avatar and prim
  63. /// state updates</summary>
  64. const float STATE_TASK_PERCENTAGE = 0.8f;
  65. private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  66. /// <summary>The number of packet categories to throttle on. If a throttle category is added
  67. /// or removed, this number must also change</summary>
  68. const int THROTTLE_CATEGORY_COUNT = 8;
  69. /// <summary>Fired when updated networking stats are produced for this client</summary>
  70. public event PacketStats OnPacketStats;
  71. /// <summary>Fired when the queue for a packet category is empty. This event can be
  72. /// hooked to put more data on the empty queue</summary>
  73. public event QueueEmpty OnQueueEmpty;
  74. /// <summary>AgentID for this client</summary>
  75. public readonly UUID AgentID;
  76. /// <summary>The remote address of the connected client</summary>
  77. public readonly IPEndPoint RemoteEndPoint;
  78. /// <summary>Circuit code that this client is connected on</summary>
  79. public readonly uint CircuitCode;
  80. /// <summary>Sequence numbers of packets we've received (for duplicate checking)</summary>
  81. public readonly IncomingPacketHistoryCollection PacketArchive = new IncomingPacketHistoryCollection(200);
  82. /// <summary>Packets we have sent that need to be ACKed by the client</summary>
  83. public readonly UnackedPacketCollection NeedAcks = new UnackedPacketCollection();
  84. /// <summary>ACKs that are queued up, waiting to be sent to the client</summary>
  85. public readonly OpenSim.Framework.LocklessQueue<uint> PendingAcks = new OpenSim.Framework.LocklessQueue<uint>();
  86. /// <summary>Current packet sequence number</summary>
  87. public int CurrentSequence;
  88. /// <summary>Current ping sequence number</summary>
  89. public byte CurrentPingSequence;
  90. /// <summary>True when this connection is alive, otherwise false</summary>
  91. public bool IsConnected = true;
  92. /// <summary>True when this connection is paused, otherwise false</summary>
  93. public bool IsPaused;
  94. /// <summary>Environment.TickCount when the last packet was received for this client</summary>
  95. public int TickLastPacketReceived;
  96. /// <summary>Smoothed round-trip time. A smoothed average of the round-trip time for sending a
  97. /// reliable packet to the client and receiving an ACK</summary>
  98. public float SRTT;
  99. /// <summary>Round-trip time variance. Measures the consistency of round-trip times</summary>
  100. public float RTTVAR;
  101. /// <summary>Retransmission timeout. Packets that have not been acknowledged in this number of
  102. /// milliseconds or longer will be resent</summary>
  103. /// <remarks>Calculated from <seealso cref="SRTT"/> and <seealso cref="RTTVAR"/> using the
  104. /// guidelines in RFC 2988</remarks>
  105. public int RTO;
  106. /// <summary>Number of bytes received since the last acknowledgement was sent out. This is used
  107. /// to loosely follow the TCP delayed ACK algorithm in RFC 1122 (4.2.3.2)</summary>
  108. public int BytesSinceLastACK;
  109. /// <summary>Number of packets received from this client</summary>
  110. public int PacketsReceived;
  111. /// <summary>Number of packets sent to this client</summary>
  112. public int PacketsSent;
  113. /// <summary>Number of packets resent to this client</summary>
  114. public int PacketsResent;
  115. /// <summary>Total byte count of unacked packets sent to this client</summary>
  116. public int UnackedBytes;
  117. /// <summary>Total number of received packets that we have reported to the OnPacketStats event(s)</summary>
  118. private int m_packetsReceivedReported;
  119. /// <summary>Total number of sent packets that we have reported to the OnPacketStats event(s)</summary>
  120. private int m_packetsSentReported;
  121. /// <summary>Holds the Environment.TickCount value of when the next OnQueueEmpty can be fired</summary>
  122. private int m_nextOnQueueEmpty = 1;
  123. /// <summary>Throttle bucket for this agent's connection</summary>
  124. private readonly AdaptiveTokenBucket m_throttleClient;
  125. public AdaptiveTokenBucket FlowThrottle
  126. {
  127. get { return m_throttleClient; }
  128. }
  129. /// <summary>Throttle bucket for this agent's connection</summary>
  130. private readonly TokenBucket m_throttleCategory;
  131. /// <summary>Throttle buckets for each packet category</summary>
  132. private readonly TokenBucket[] m_throttleCategories;
  133. /// <summary>Outgoing queues for throttled packets</summary>
  134. private readonly OpenSim.Framework.LocklessQueue<OutgoingPacket>[] m_packetOutboxes = new OpenSim.Framework.LocklessQueue<OutgoingPacket>[THROTTLE_CATEGORY_COUNT];
  135. /// <summary>A container that can hold one packet for each outbox, used to store
  136. /// dequeued packets that are being held for throttling</summary>
  137. private readonly OutgoingPacket[] m_nextPackets = new OutgoingPacket[THROTTLE_CATEGORY_COUNT];
  138. /// <summary>A reference to the LLUDPServer that is managing this client</summary>
  139. private readonly LLUDPServer m_udpServer;
  140. /// <summary>Caches packed throttle information</summary>
  141. private byte[] m_packedThrottles;
  142. private int m_defaultRTO = 1000; // 1sec is the recommendation in the RFC
  143. private int m_maxRTO = 60000;
  144. /// <summary>
  145. /// Default constructor
  146. /// </summary>
  147. /// <param name="server">Reference to the UDP server this client is connected to</param>
  148. /// <param name="rates">Default throttling rates and maximum throttle limits</param>
  149. /// <param name="parentThrottle">Parent HTB (hierarchical token bucket)
  150. /// that the child throttles will be governed by</param>
  151. /// <param name="circuitCode">Circuit code for this connection</param>
  152. /// <param name="agentID">AgentID for the connected agent</param>
  153. /// <param name="remoteEndPoint">Remote endpoint for this connection</param>
  154. /// <param name="defaultRTO">
  155. /// Default retransmission timeout for unacked packets. The RTO will never drop
  156. /// beyond this number.
  157. /// </param>
  158. /// <param name="maxRTO">
  159. /// The maximum retransmission timeout for unacked packets. The RTO will never exceed this number.
  160. /// </param>
  161. public LLUDPClient(
  162. LLUDPServer server, ThrottleRates rates, TokenBucket parentThrottle, uint circuitCode, UUID agentID,
  163. IPEndPoint remoteEndPoint, int defaultRTO, int maxRTO)
  164. {
  165. AgentID = agentID;
  166. RemoteEndPoint = remoteEndPoint;
  167. CircuitCode = circuitCode;
  168. m_udpServer = server;
  169. if (defaultRTO != 0)
  170. m_defaultRTO = defaultRTO;
  171. if (maxRTO != 0)
  172. m_maxRTO = maxRTO;
  173. // Create a token bucket throttle for this client that has the scene token bucket as a parent
  174. m_throttleClient = new AdaptiveTokenBucket(parentThrottle, rates.Total, rates.AdaptiveThrottlesEnabled);
  175. // Create a token bucket throttle for the total categary with the client bucket as a throttle
  176. m_throttleCategory = new TokenBucket(m_throttleClient, 0);
  177. // Create an array of token buckets for this clients different throttle categories
  178. m_throttleCategories = new TokenBucket[THROTTLE_CATEGORY_COUNT];
  179. for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
  180. {
  181. ThrottleOutPacketType type = (ThrottleOutPacketType)i;
  182. // Initialize the packet outboxes, where packets sit while they are waiting for tokens
  183. m_packetOutboxes[i] = new OpenSim.Framework.LocklessQueue<OutgoingPacket>();
  184. // Initialize the token buckets that control the throttling for each category
  185. m_throttleCategories[i] = new TokenBucket(m_throttleCategory, rates.GetRate(type));
  186. }
  187. // Default the retransmission timeout to one second
  188. RTO = m_defaultRTO;
  189. // Initialize this to a sane value to prevent early disconnects
  190. TickLastPacketReceived = Environment.TickCount & Int32.MaxValue;
  191. }
  192. /// <summary>
  193. /// Shuts down this client connection
  194. /// </summary>
  195. public void Shutdown()
  196. {
  197. IsConnected = false;
  198. for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
  199. {
  200. m_packetOutboxes[i].Clear();
  201. m_nextPackets[i] = null;
  202. }
  203. // pull the throttle out of the scene throttle
  204. m_throttleClient.Parent.UnregisterRequest(m_throttleClient);
  205. OnPacketStats = null;
  206. OnQueueEmpty = null;
  207. }
  208. /// <summary>
  209. /// Gets information about this client connection
  210. /// </summary>
  211. /// <returns>Information about the client connection</returns>
  212. public ClientInfo GetClientInfo()
  213. {
  214. // TODO: This data structure is wrong in so many ways. Locking and copying the entire lists
  215. // of pending and needed ACKs for every client every time some method wants information about
  216. // this connection is a recipe for poor performance
  217. ClientInfo info = new ClientInfo();
  218. info.pendingAcks = new Dictionary<uint, uint>();
  219. info.needAck = new Dictionary<uint, byte[]>();
  220. info.resendThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Resend].DripRate;
  221. info.landThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Land].DripRate;
  222. info.windThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Wind].DripRate;
  223. info.cloudThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Cloud].DripRate;
  224. info.taskThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Task].DripRate;
  225. info.assetThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Asset].DripRate;
  226. info.textureThrottle = (int)m_throttleCategories[(int)ThrottleOutPacketType.Texture].DripRate;
  227. info.totalThrottle = (int)m_throttleCategory.DripRate;
  228. return info;
  229. }
  230. /// <summary>
  231. /// Modifies the UDP throttles
  232. /// </summary>
  233. /// <param name="info">New throttling values</param>
  234. public void SetClientInfo(ClientInfo info)
  235. {
  236. // TODO: Allowing throttles to be manually set from this function seems like a reasonable
  237. // idea. On the other hand, letting external code manipulate our ACK accounting is not
  238. // going to happen
  239. throw new NotImplementedException();
  240. }
  241. /// <summary>
  242. /// Return statistics information about client packet queues.
  243. /// </summary>
  244. /// <remarks>
  245. /// FIXME: This should really be done in a more sensible manner rather than sending back a formatted string.
  246. /// </remarks>
  247. /// <returns></returns>
  248. public string GetStats()
  249. {
  250. return string.Format(
  251. "{0,7} {1,7} {2,7} {3,9} {4,7} {5,7} {6,7} {7,7} {8,7} {9,8} {10,7} {11,7} {12,7}",
  252. Util.EnvironmentTickCountSubtract(TickLastPacketReceived),
  253. PacketsReceived,
  254. PacketsSent,
  255. PacketsResent,
  256. UnackedBytes,
  257. m_packetOutboxes[(int)ThrottleOutPacketType.Resend].Count,
  258. m_packetOutboxes[(int)ThrottleOutPacketType.Land].Count,
  259. m_packetOutboxes[(int)ThrottleOutPacketType.Wind].Count,
  260. m_packetOutboxes[(int)ThrottleOutPacketType.Cloud].Count,
  261. m_packetOutboxes[(int)ThrottleOutPacketType.Task].Count,
  262. m_packetOutboxes[(int)ThrottleOutPacketType.Texture].Count,
  263. m_packetOutboxes[(int)ThrottleOutPacketType.Asset].Count,
  264. m_packetOutboxes[(int)ThrottleOutPacketType.State].Count);
  265. }
  266. public void SendPacketStats()
  267. {
  268. PacketStats callback = OnPacketStats;
  269. if (callback != null)
  270. {
  271. int newPacketsReceived = PacketsReceived - m_packetsReceivedReported;
  272. int newPacketsSent = PacketsSent - m_packetsSentReported;
  273. callback(newPacketsReceived, newPacketsSent, UnackedBytes);
  274. m_packetsReceivedReported += newPacketsReceived;
  275. m_packetsSentReported += newPacketsSent;
  276. }
  277. }
  278. public void SetThrottles(byte[] throttleData)
  279. {
  280. byte[] adjData;
  281. int pos = 0;
  282. if (!BitConverter.IsLittleEndian)
  283. {
  284. byte[] newData = new byte[7 * 4];
  285. Buffer.BlockCopy(throttleData, 0, newData, 0, 7 * 4);
  286. for (int i = 0; i < 7; i++)
  287. Array.Reverse(newData, i * 4, 4);
  288. adjData = newData;
  289. }
  290. else
  291. {
  292. adjData = throttleData;
  293. }
  294. // 0.125f converts from bits to bytes
  295. int resend = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  296. int land = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  297. int wind = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  298. int cloud = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  299. int task = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  300. int texture = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  301. int asset = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
  302. // State is a subcategory of task that we allocate a percentage to
  303. int state = 0;
  304. // Make sure none of the throttles are set below our packet MTU,
  305. // otherwise a throttle could become permanently clogged
  306. resend = Math.Max(resend, LLUDPServer.MTU);
  307. land = Math.Max(land, LLUDPServer.MTU);
  308. wind = Math.Max(wind, LLUDPServer.MTU);
  309. cloud = Math.Max(cloud, LLUDPServer.MTU);
  310. task = Math.Max(task, LLUDPServer.MTU);
  311. texture = Math.Max(texture, LLUDPServer.MTU);
  312. asset = Math.Max(asset, LLUDPServer.MTU);
  313. //int total = resend + land + wind + cloud + task + texture + asset;
  314. //m_log.DebugFormat("[LLUDPCLIENT]: {0} is setting throttles. Resend={1}, Land={2}, Wind={3}, Cloud={4}, Task={5}, Texture={6}, Asset={7}, Total={8}",
  315. // AgentID, resend, land, wind, cloud, task, texture, asset, total);
  316. // Update the token buckets with new throttle values
  317. TokenBucket bucket;
  318. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Resend];
  319. bucket.RequestedDripRate = resend;
  320. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Land];
  321. bucket.RequestedDripRate = land;
  322. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Wind];
  323. bucket.RequestedDripRate = wind;
  324. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Cloud];
  325. bucket.RequestedDripRate = cloud;
  326. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Asset];
  327. bucket.RequestedDripRate = asset;
  328. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Task];
  329. bucket.RequestedDripRate = task;
  330. bucket = m_throttleCategories[(int)ThrottleOutPacketType.State];
  331. bucket.RequestedDripRate = state;
  332. bucket = m_throttleCategories[(int)ThrottleOutPacketType.Texture];
  333. bucket.RequestedDripRate = texture;
  334. // Reset the packed throttles cached data
  335. m_packedThrottles = null;
  336. }
  337. public byte[] GetThrottlesPacked(float multiplier)
  338. {
  339. byte[] data = m_packedThrottles;
  340. if (data == null)
  341. {
  342. float rate;
  343. data = new byte[7 * 4];
  344. int i = 0;
  345. // multiply by 8 to convert bytes back to bits
  346. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Resend].RequestedDripRate * 8 * multiplier;
  347. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  348. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Land].RequestedDripRate * 8 * multiplier;
  349. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  350. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Wind].RequestedDripRate * 8 * multiplier;
  351. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  352. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Cloud].RequestedDripRate * 8 * multiplier;
  353. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  354. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Task].RequestedDripRate * 8 * multiplier;
  355. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  356. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Texture].RequestedDripRate * 8 * multiplier;
  357. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  358. rate = (float)m_throttleCategories[(int)ThrottleOutPacketType.Asset].RequestedDripRate * 8 * multiplier;
  359. Buffer.BlockCopy(Utils.FloatToBytes(rate), 0, data, i, 4); i += 4;
  360. m_packedThrottles = data;
  361. }
  362. return data;
  363. }
  364. /// <summary>
  365. /// Queue an outgoing packet if appropriate.
  366. /// </summary>
  367. /// <param name="packet"></param>
  368. /// <param name="forceQueue">Always queue the packet if at all possible.</param>
  369. /// <returns>
  370. /// true if the packet has been queued,
  371. /// false if the packet has not been queued and should be sent immediately.
  372. /// </returns>
  373. public bool EnqueueOutgoing(OutgoingPacket packet, bool forceQueue)
  374. {
  375. int category = (int)packet.Category;
  376. if (category >= 0 && category < m_packetOutboxes.Length)
  377. {
  378. OpenSim.Framework.LocklessQueue<OutgoingPacket> queue = m_packetOutboxes[category];
  379. TokenBucket bucket = m_throttleCategories[category];
  380. // Don't send this packet if there is already a packet waiting in the queue
  381. // even if we have the tokens to send it, tokens should go to the already
  382. // queued packets
  383. if (queue.Count > 0)
  384. {
  385. queue.Enqueue(packet);
  386. return true;
  387. }
  388. if (!forceQueue && bucket.RemoveTokens(packet.Buffer.DataLength))
  389. {
  390. // Enough tokens were removed from the bucket, the packet will not be queued
  391. return false;
  392. }
  393. else
  394. {
  395. // Force queue specified or not enough tokens in the bucket, queue this packet
  396. queue.Enqueue(packet);
  397. return true;
  398. }
  399. }
  400. else
  401. {
  402. // We don't have a token bucket for this category, so it will not be queued
  403. return false;
  404. }
  405. }
  406. /// <summary>
  407. /// Loops through all of the packet queues for this client and tries to send
  408. /// an outgoing packet from each, obeying the throttling bucket limits
  409. /// </summary>
  410. ///
  411. /// <remarks>
  412. /// Packet queues are inspected in ascending numerical order starting from 0. Therefore, queues with a lower
  413. /// ThrottleOutPacketType number will see their packet get sent first (e.g. if both Land and Wind queues have
  414. /// packets, then the packet at the front of the Land queue will be sent before the packet at the front of the
  415. /// wind queue).
  416. ///
  417. /// This function is only called from a synchronous loop in the
  418. /// UDPServer so we don't need to bother making this thread safe
  419. /// </remarks>
  420. ///
  421. /// <returns>True if any packets were sent, otherwise false</returns>
  422. public bool DequeueOutgoing()
  423. {
  424. OutgoingPacket packet;
  425. OpenSim.Framework.LocklessQueue<OutgoingPacket> queue;
  426. TokenBucket bucket;
  427. bool packetSent = false;
  428. ThrottleOutPacketTypeFlags emptyCategories = 0;
  429. //string queueDebugOutput = String.Empty; // Serious debug business
  430. for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
  431. {
  432. bucket = m_throttleCategories[i];
  433. //queueDebugOutput += m_packetOutboxes[i].Count + " "; // Serious debug business
  434. if (m_nextPackets[i] != null)
  435. {
  436. // This bucket was empty the last time we tried to send a packet,
  437. // leaving a dequeued packet still waiting to be sent out. Try to
  438. // send it again
  439. OutgoingPacket nextPacket = m_nextPackets[i];
  440. if (bucket.RemoveTokens(nextPacket.Buffer.DataLength))
  441. {
  442. // Send the packet
  443. m_udpServer.SendPacketFinal(nextPacket);
  444. m_nextPackets[i] = null;
  445. packetSent = true;
  446. }
  447. }
  448. else
  449. {
  450. // No dequeued packet waiting to be sent, try to pull one off
  451. // this queue
  452. queue = m_packetOutboxes[i];
  453. if (queue.Dequeue(out packet))
  454. {
  455. // A packet was pulled off the queue. See if we have
  456. // enough tokens in the bucket to send it out
  457. if (bucket.RemoveTokens(packet.Buffer.DataLength))
  458. {
  459. // Send the packet
  460. m_udpServer.SendPacketFinal(packet);
  461. packetSent = true;
  462. }
  463. else
  464. {
  465. // Save the dequeued packet for the next iteration
  466. m_nextPackets[i] = packet;
  467. }
  468. // If the queue is empty after this dequeue, fire the queue
  469. // empty callback now so it has a chance to fill before we
  470. // get back here
  471. if (queue.Count == 0)
  472. emptyCategories |= CategoryToFlag(i);
  473. }
  474. else
  475. {
  476. // No packets in this queue. Fire the queue empty callback
  477. // if it has not been called recently
  478. emptyCategories |= CategoryToFlag(i);
  479. }
  480. }
  481. }
  482. if (emptyCategories != 0)
  483. BeginFireQueueEmpty(emptyCategories);
  484. //m_log.Info("[LLUDPCLIENT]: Queues: " + queueDebugOutput); // Serious debug business
  485. return packetSent;
  486. }
  487. /// <summary>
  488. /// Called when an ACK packet is received and a round-trip time for a
  489. /// packet is calculated. This is used to calculate the smoothed
  490. /// round-trip time, round trip time variance, and finally the
  491. /// retransmission timeout
  492. /// </summary>
  493. /// <param name="r">Round-trip time of a single packet and its
  494. /// acknowledgement</param>
  495. public void UpdateRoundTrip(float r)
  496. {
  497. const float ALPHA = 0.125f;
  498. const float BETA = 0.25f;
  499. const float K = 4.0f;
  500. if (RTTVAR == 0.0f)
  501. {
  502. // First RTT measurement
  503. SRTT = r;
  504. RTTVAR = r * 0.5f;
  505. }
  506. else
  507. {
  508. // Subsequence RTT measurement
  509. RTTVAR = (1.0f - BETA) * RTTVAR + BETA * Math.Abs(SRTT - r);
  510. SRTT = (1.0f - ALPHA) * SRTT + ALPHA * r;
  511. }
  512. int rto = (int)(SRTT + Math.Max(m_udpServer.TickCountResolution, K * RTTVAR));
  513. // Clamp the retransmission timeout to manageable values
  514. rto = Utils.Clamp(rto, m_defaultRTO, m_maxRTO);
  515. RTO = rto;
  516. //if (RTO != rto)
  517. // m_log.Debug("[LLUDPCLIENT]: Setting RTO to " + RTO + "ms from " + rto + "ms with an RTTVAR of " +
  518. //RTTVAR + " based on new RTT of " + r + "ms");
  519. }
  520. /// <summary>
  521. /// Exponential backoff of the retransmission timeout, per section 5.5
  522. /// of RFC 2988
  523. /// </summary>
  524. public void BackoffRTO()
  525. {
  526. // Reset SRTT and RTTVAR, we assume they are bogus since things
  527. // didn't work out and we're backing off the timeout
  528. SRTT = 0.0f;
  529. RTTVAR = 0.0f;
  530. // Double the retransmission timeout
  531. RTO = Math.Min(RTO * 2, m_maxRTO);
  532. }
  533. /// <summary>
  534. /// Does an early check to see if this queue empty callback is already
  535. /// running, then asynchronously firing the event
  536. /// </summary>
  537. /// <param name="categories">Throttle categories to fire the callback for</param>
  538. private void BeginFireQueueEmpty(ThrottleOutPacketTypeFlags categories)
  539. {
  540. if (m_nextOnQueueEmpty != 0 && (Environment.TickCount & Int32.MaxValue) >= m_nextOnQueueEmpty)
  541. {
  542. // Use a value of 0 to signal that FireQueueEmpty is running
  543. m_nextOnQueueEmpty = 0;
  544. // Asynchronously run the callback
  545. Util.FireAndForget(FireQueueEmpty, categories);
  546. }
  547. }
  548. /// <summary>
  549. /// Fires the OnQueueEmpty callback and sets the minimum time that it
  550. /// can be called again
  551. /// </summary>
  552. /// <param name="o">Throttle categories to fire the callback for,
  553. /// stored as an object to match the WaitCallback delegate
  554. /// signature</param>
  555. private void FireQueueEmpty(object o)
  556. {
  557. const int MIN_CALLBACK_MS = 30;
  558. ThrottleOutPacketTypeFlags categories = (ThrottleOutPacketTypeFlags)o;
  559. QueueEmpty callback = OnQueueEmpty;
  560. int start = Environment.TickCount & Int32.MaxValue;
  561. if (callback != null)
  562. {
  563. try { callback(categories); }
  564. catch (Exception e) { m_log.Error("[LLUDPCLIENT]: OnQueueEmpty(" + categories + ") threw an exception: " + e.Message, e); }
  565. }
  566. m_nextOnQueueEmpty = start + MIN_CALLBACK_MS;
  567. if (m_nextOnQueueEmpty == 0)
  568. m_nextOnQueueEmpty = 1;
  569. }
  570. /// <summary>
  571. /// Converts a <seealso cref="ThrottleOutPacketType"/> integer to a
  572. /// flag value
  573. /// </summary>
  574. /// <param name="i">Throttle category to convert</param>
  575. /// <returns>Flag representation of the throttle category</returns>
  576. private static ThrottleOutPacketTypeFlags CategoryToFlag(int i)
  577. {
  578. ThrottleOutPacketType category = (ThrottleOutPacketType)i;
  579. /*
  580. * Land = 1,
  581. /// <summary>Wind data</summary>
  582. Wind = 2,
  583. /// <summary>Cloud data</summary>
  584. Cloud = 3,
  585. /// <summary>Any packets that do not fit into the other throttles</summary>
  586. Task = 4,
  587. /// <summary>Texture assets</summary>
  588. Texture = 5,
  589. /// <summary>Non-texture assets</summary>
  590. Asset = 6,
  591. /// <summary>Avatar and primitive data</summary>
  592. /// <remarks>This is a sub-category of Task</remarks>
  593. State = 7,
  594. */
  595. switch (category)
  596. {
  597. case ThrottleOutPacketType.Land:
  598. return ThrottleOutPacketTypeFlags.Land;
  599. case ThrottleOutPacketType.Wind:
  600. return ThrottleOutPacketTypeFlags.Wind;
  601. case ThrottleOutPacketType.Cloud:
  602. return ThrottleOutPacketTypeFlags.Cloud;
  603. case ThrottleOutPacketType.Task:
  604. return ThrottleOutPacketTypeFlags.Task;
  605. case ThrottleOutPacketType.Texture:
  606. return ThrottleOutPacketTypeFlags.Texture;
  607. case ThrottleOutPacketType.Asset:
  608. return ThrottleOutPacketTypeFlags.Asset;
  609. case ThrottleOutPacketType.State:
  610. return ThrottleOutPacketTypeFlags.State;
  611. default:
  612. return 0;
  613. }
  614. }
  615. }
  616. }