UnackedPacketCollection.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 OpenMetaverse;
  31. namespace OpenSim.Region.ClientStack.LindenUDP
  32. {
  33. /// <summary>
  34. /// Special collection that is optimized for tracking unacknowledged packets
  35. /// </summary>
  36. public sealed class UnackedPacketCollection
  37. {
  38. /// <summary>
  39. /// Holds information about a pending acknowledgement
  40. /// </summary>
  41. private struct PendingAck
  42. {
  43. /// <summary>Sequence number of the packet to remove</summary>
  44. public uint SequenceNumber;
  45. /// <summary>Environment.TickCount value when the remove was queued.
  46. /// This is used to update round-trip times for packets</summary>
  47. public int RemoveTime;
  48. /// <summary>Whether or not this acknowledgement was attached to a
  49. /// resent packet. If so, round-trip time will not be calculated</summary>
  50. public bool FromResend;
  51. public PendingAck(uint sequenceNumber, int currentTime, bool fromResend)
  52. {
  53. SequenceNumber = sequenceNumber;
  54. RemoveTime = currentTime;
  55. FromResend = fromResend;
  56. }
  57. }
  58. /// <summary>Holds the actual unacked packet data, sorted by sequence number</summary>
  59. private Dictionary<uint, OutgoingPacket> m_packets = new Dictionary<uint, OutgoingPacket>();
  60. /// <summary>Holds packets that need to be added to the unacknowledged list</summary>
  61. private LocklessQueue<OutgoingPacket> m_pendingAdds = new LocklessQueue<OutgoingPacket>();
  62. /// <summary>Holds information about pending acknowledgements</summary>
  63. private LocklessQueue<PendingAck> m_pendingRemoves = new LocklessQueue<PendingAck>();
  64. /// <summary>
  65. /// Add an unacked packet to the collection
  66. /// </summary>
  67. /// <param name="packet">Packet that is awaiting acknowledgement</param>
  68. /// <returns>True if the packet was successfully added, false if the
  69. /// packet already existed in the collection</returns>
  70. /// <remarks>This does not immediately add the ACK to the collection,
  71. /// it only queues it so it can be added in a thread-safe way later</remarks>
  72. public void Add(OutgoingPacket packet)
  73. {
  74. m_pendingAdds.Enqueue(packet);
  75. }
  76. /// <summary>
  77. /// Marks a packet as acknowledged
  78. /// </summary>
  79. /// <param name="sequenceNumber">Sequence number of the packet to
  80. /// acknowledge</param>
  81. /// <param name="currentTime">Current value of Environment.TickCount</param>
  82. /// <remarks>This does not immediately acknowledge the packet, it only
  83. /// queues the ack so it can be handled in a thread-safe way later</remarks>
  84. public void Remove(uint sequenceNumber, int currentTime, bool fromResend)
  85. {
  86. m_pendingRemoves.Enqueue(new PendingAck(sequenceNumber, currentTime, fromResend));
  87. }
  88. /// <summary>
  89. /// Returns a list of all of the packets with a TickCount older than
  90. /// the specified timeout
  91. /// </summary>
  92. /// <param name="timeoutMS">Number of ticks (milliseconds) before a
  93. /// packet is considered expired</param>
  94. /// <returns>A list of all expired packets according to the given
  95. /// expiration timeout</returns>
  96. /// <remarks>This function is not thread safe, and cannot be called
  97. /// multiple times concurrently</remarks>
  98. public List<OutgoingPacket> GetExpiredPackets(int timeoutMS)
  99. {
  100. ProcessQueues();
  101. List<OutgoingPacket> expiredPackets = null;
  102. if (m_packets.Count > 0)
  103. {
  104. int now = Environment.TickCount & Int32.MaxValue;
  105. foreach (OutgoingPacket packet in m_packets.Values)
  106. {
  107. // TickCount of zero means a packet is in the resend queue
  108. // but hasn't actually been sent over the wire yet
  109. if (packet.TickCount == 0)
  110. continue;
  111. if (now - packet.TickCount >= timeoutMS)
  112. {
  113. if (expiredPackets == null)
  114. expiredPackets = new List<OutgoingPacket>();
  115. // The TickCount will be set to the current time when the packet
  116. // is actually sent out again
  117. packet.TickCount = 0;
  118. expiredPackets.Add(packet);
  119. }
  120. }
  121. }
  122. return expiredPackets;
  123. }
  124. private void ProcessQueues()
  125. {
  126. // Process all the pending adds
  127. OutgoingPacket pendingAdd;
  128. while (m_pendingAdds.Dequeue(out pendingAdd))
  129. m_packets[pendingAdd.SequenceNumber] = pendingAdd;
  130. // Process all the pending removes, including updating statistics and round-trip times
  131. PendingAck pendingRemove;
  132. OutgoingPacket ackedPacket;
  133. while (m_pendingRemoves.Dequeue(out pendingRemove))
  134. {
  135. if (m_packets.TryGetValue(pendingRemove.SequenceNumber, out ackedPacket))
  136. {
  137. m_packets.Remove(pendingRemove.SequenceNumber);
  138. // Update stats
  139. System.Threading.Interlocked.Add(ref ackedPacket.Client.UnackedBytes, -ackedPacket.Buffer.DataLength);
  140. if (!pendingRemove.FromResend)
  141. {
  142. // Calculate the round-trip time for this packet and its ACK
  143. int rtt = pendingRemove.RemoveTime - ackedPacket.TickCount;
  144. if (rtt > 0)
  145. ackedPacket.Client.UpdateRoundTrip(rtt);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }