LLUDPClient.cs 33 KB

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