aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs')
-rw-r--r--OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs353
1 files changed, 184 insertions, 169 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs b/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs
index e43f7cf..c120a12 100644
--- a/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs
+++ b/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs
@@ -1,169 +1,184 @@
1/* 1/*
2 * Copyright (c) Contributors, http://opensimulator.org/ 2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders. 3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met: 6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright 7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright 9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the 12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products 13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission. 14 * derived from this software without specific prior written permission.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY 16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY 19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 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 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 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. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28using System; 28using System;
29using System.Collections.Generic; 29using System.Collections.Generic;
30using System.Net; 30using System.Net;
31using OpenMetaverse; 31using OpenMetaverse;
32 32
33namespace OpenSim.Region.ClientStack.LindenUDP 33namespace OpenSim.Region.ClientStack.LindenUDP
34{ 34{
35 /// <summary> 35 /// <summary>
36 /// Special collection that is optimized for tracking unacknowledged packets 36 /// Special collection that is optimized for tracking unacknowledged packets
37 /// </summary> 37 /// </summary>
38 public sealed class UnackedPacketCollection 38 public sealed class UnackedPacketCollection
39 { 39 {
40 /// <summary> 40 /// <summary>
41 /// Holds information about a pending acknowledgement 41 /// Holds information about a pending acknowledgement
42 /// </summary> 42 /// </summary>
43 private struct PendingAck 43 private struct PendingAck
44 { 44 {
45 /// <summary>Sequence number of the packet to remove</summary> 45 /// <summary>Sequence number of the packet to remove</summary>
46 public uint SequenceNumber; 46 public uint SequenceNumber;
47 /// <summary>Environment.TickCount value when the remove was queued. 47 /// <summary>Environment.TickCount value when the remove was queued.
48 /// This is used to update round-trip times for packets</summary> 48 /// This is used to update round-trip times for packets</summary>
49 public int RemoveTime; 49 public int RemoveTime;
50 /// <summary>Whether or not this acknowledgement was attached to a 50 /// <summary>Whether or not this acknowledgement was attached to a
51 /// resent packet. If so, round-trip time will not be calculated</summary> 51 /// resent packet. If so, round-trip time will not be calculated</summary>
52 public bool FromResend; 52 public bool FromResend;
53 53
54 public PendingAck(uint sequenceNumber, int currentTime, bool fromResend) 54 public PendingAck(uint sequenceNumber, int currentTime, bool fromResend)
55 { 55 {
56 SequenceNumber = sequenceNumber; 56 SequenceNumber = sequenceNumber;
57 RemoveTime = currentTime; 57 RemoveTime = currentTime;
58 FromResend = fromResend; 58 FromResend = fromResend;
59 } 59 }
60 } 60 }
61 61
62 /// <summary>Holds the actual unacked packet data, sorted by sequence number</summary> 62 /// <summary>Holds the actual unacked packet data, sorted by sequence number</summary>
63 private Dictionary<uint, OutgoingPacket> m_packets = new Dictionary<uint, OutgoingPacket>(); 63 private Dictionary<uint, OutgoingPacket> m_packets = new Dictionary<uint, OutgoingPacket>();
64 /// <summary>Holds packets that need to be added to the unacknowledged list</summary> 64 /// <summary>Holds packets that need to be added to the unacknowledged list</summary>
65 private LocklessQueue<OutgoingPacket> m_pendingAdds = new LocklessQueue<OutgoingPacket>(); 65 private LocklessQueue<OutgoingPacket> m_pendingAdds = new LocklessQueue<OutgoingPacket>();
66 /// <summary>Holds information about pending acknowledgements</summary> 66 /// <summary>Holds information about pending acknowledgements</summary>
67 private LocklessQueue<PendingAck> m_pendingRemoves = new LocklessQueue<PendingAck>(); 67 private LocklessQueue<PendingAck> m_pendingRemoves = new LocklessQueue<PendingAck>();
68 68
69 /// <summary> 69 /// <summary>
70 /// Add an unacked packet to the collection 70 /// Add an unacked packet to the collection
71 /// </summary> 71 /// </summary>
72 /// <param name="packet">Packet that is awaiting acknowledgement</param> 72 /// <param name="packet">Packet that is awaiting acknowledgement</param>
73 /// <returns>True if the packet was successfully added, false if the 73 /// <returns>True if the packet was successfully added, false if the
74 /// packet already existed in the collection</returns> 74 /// packet already existed in the collection</returns>
75 /// <remarks>This does not immediately add the ACK to the collection, 75 /// <remarks>This does not immediately add the ACK to the collection,
76 /// it only queues it so it can be added in a thread-safe way later</remarks> 76 /// it only queues it so it can be added in a thread-safe way later</remarks>
77 public void Add(OutgoingPacket packet) 77 public void Add(OutgoingPacket packet)
78 { 78 {
79 m_pendingAdds.Enqueue(packet); 79 m_pendingAdds.Enqueue(packet);
80 } 80 }
81 81
82 /// <summary> 82 /// <summary>
83 /// Marks a packet as acknowledged 83 /// Marks a packet as acknowledged
84 /// </summary> 84 /// </summary>
85 /// <param name="sequenceNumber">Sequence number of the packet to 85 /// <param name="sequenceNumber">Sequence number of the packet to
86 /// acknowledge</param> 86 /// acknowledge</param>
87 /// <param name="currentTime">Current value of Environment.TickCount</param> 87 /// <param name="currentTime">Current value of Environment.TickCount</param>
88 /// <remarks>This does not immediately acknowledge the packet, it only 88 /// <remarks>This does not immediately acknowledge the packet, it only
89 /// queues the ack so it can be handled in a thread-safe way later</remarks> 89 /// queues the ack so it can be handled in a thread-safe way later</remarks>
90 public void Remove(uint sequenceNumber, int currentTime, bool fromResend) 90 public void Remove(uint sequenceNumber, int currentTime, bool fromResend)
91 { 91 {
92 m_pendingRemoves.Enqueue(new PendingAck(sequenceNumber, currentTime, fromResend)); 92 m_pendingRemoves.Enqueue(new PendingAck(sequenceNumber, currentTime, fromResend));
93 } 93 }
94 94
95 /// <summary> 95 /// <summary>
96 /// Returns a list of all of the packets with a TickCount older than 96 /// Returns a list of all of the packets with a TickCount older than
97 /// the specified timeout 97 /// the specified timeout
98 /// </summary> 98 /// </summary>
99 /// <param name="timeoutMS">Number of ticks (milliseconds) before a 99 /// <param name="timeoutMS">Number of ticks (milliseconds) before a
100 /// packet is considered expired</param> 100 /// packet is considered expired</param>
101 /// <returns>A list of all expired packets according to the given 101 /// <returns>A list of all expired packets according to the given
102 /// expiration timeout</returns> 102 /// expiration timeout</returns>
103 /// <remarks>This function is not thread safe, and cannot be called 103 /// <remarks>This function is not thread safe, and cannot be called
104 /// multiple times concurrently</remarks> 104 /// multiple times concurrently</remarks>
105 public List<OutgoingPacket> GetExpiredPackets(int timeoutMS) 105 public List<OutgoingPacket> GetExpiredPackets(int timeoutMS)
106 { 106 {
107 ProcessQueues(); 107 ProcessQueues();
108 108
109 List<OutgoingPacket> expiredPackets = null; 109 List<OutgoingPacket> expiredPackets = null;
110 110
111 if (m_packets.Count > 0) 111 if (m_packets.Count > 0)
112 { 112 {
113 int now = Environment.TickCount & Int32.MaxValue; 113 int now = Environment.TickCount & Int32.MaxValue;
114 114
115 foreach (OutgoingPacket packet in m_packets.Values) 115 foreach (OutgoingPacket packet in m_packets.Values)
116 { 116 {
117 // TickCount of zero means a packet is in the resend queue 117 // TickCount of zero means a packet is in the resend queue
118 // but hasn't actually been sent over the wire yet 118 // but hasn't actually been sent over the wire yet
119 if (packet.TickCount == 0) 119 if (packet.TickCount == 0)
120 continue; 120 continue;
121 121
122 if (now - packet.TickCount >= timeoutMS) 122 if (now - packet.TickCount >= timeoutMS)
123 { 123 {
124 if (expiredPackets == null) 124 if (expiredPackets == null)
125 expiredPackets = new List<OutgoingPacket>(); 125 expiredPackets = new List<OutgoingPacket>();
126 126
127 // The TickCount will be set to the current time when the packet 127 // The TickCount will be set to the current time when the packet
128 // is actually sent out again 128 // is actually sent out again
129 packet.TickCount = 0; 129 packet.TickCount = 0;
130 130
131 expiredPackets.Add(packet); 131 expiredPackets.Add(packet);
132 } 132 }
133 } 133 }
134 } 134 }
135 135
136 return expiredPackets; 136 return expiredPackets;
137 } 137 }
138 138
139 private void ProcessQueues() 139 private void ProcessQueues()
140 { 140 {
141 // Process all the pending adds 141 // Process all the pending adds
142 OutgoingPacket pendingAdd; 142
143 while (m_pendingAdds.Dequeue(out pendingAdd)) 143 OutgoingPacket pendingAdd;
144 m_packets[pendingAdd.SequenceNumber] = pendingAdd; 144 if (m_pendingAdds != null)
145 145 {
146 // Process all the pending removes, including updating statistics and round-trip times 146 while (m_pendingAdds.Dequeue(out pendingAdd))
147 PendingAck pendingRemove; 147 {
148 OutgoingPacket ackedPacket; 148 if (pendingAdd != null && m_packets != null)
149 while (m_pendingRemoves.Dequeue(out pendingRemove)) 149 {
150 { 150 m_packets[pendingAdd.SequenceNumber] = pendingAdd;
151 if (m_packets.TryGetValue(pendingRemove.SequenceNumber, out ackedPacket)) 151 }
152 { 152 }
153 m_packets.Remove(pendingRemove.SequenceNumber); 153 }
154 154
155 // Update stats 155 // Process all the pending removes, including updating statistics and round-trip times
156 System.Threading.Interlocked.Add(ref ackedPacket.Client.UnackedBytes, -ackedPacket.Buffer.DataLength); 156 PendingAck pendingRemove;
157 157 OutgoingPacket ackedPacket;
158 if (!pendingRemove.FromResend) 158 if (m_pendingRemoves != null)
159 { 159 {
160 // Calculate the round-trip time for this packet and its ACK 160 while (m_pendingRemoves.Dequeue(out pendingRemove))
161 int rtt = pendingRemove.RemoveTime - ackedPacket.TickCount; 161 {
162 if (rtt > 0) 162 if (m_pendingRemoves != null && m_packets != null)
163 ackedPacket.Client.UpdateRoundTrip(rtt); 163 {
164 } 164 if (m_packets.TryGetValue(pendingRemove.SequenceNumber, out ackedPacket))
165 } 165 {
166 } 166 m_packets.Remove(pendingRemove.SequenceNumber);
167 } 167
168 } 168 // Update stats
169} 169 System.Threading.Interlocked.Add(ref ackedPacket.Client.UnackedBytes, -ackedPacket.Buffer.DataLength);
170
171 if (!pendingRemove.FromResend)
172 {
173 // Calculate the round-trip time for this packet and its ACK
174 int rtt = pendingRemove.RemoveTime - ackedPacket.TickCount;
175 if (rtt > 0)
176 ackedPacket.Client.UpdateRoundTrip(rtt);
177 }
178 }
179 }
180 }
181 }
182 }
183 }
184}