opensim – Blame information for rev 1

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