diff options
Merge branch 'master' into vehicles
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs b/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs new file mode 100644 index 0000000..195ca57 --- /dev/null +++ b/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs | |||
@@ -0,0 +1,160 @@ | |||
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 OpenMetaverse; | ||
32 | |||
33 | namespace OpenSim.Region.ClientStack.LindenUDP | ||
34 | { | ||
35 | /// <summary> | ||
36 | /// Special collection that is optimized for tracking unacknowledged packets | ||
37 | /// </summary> | ||
38 | public sealed class UnackedPacketCollection | ||
39 | { | ||
40 | /// <summary>Synchronization primitive. A lock must be acquired on this | ||
41 | /// object before calling any of the unsafe methods</summary> | ||
42 | public object SyncRoot = new object(); | ||
43 | |||
44 | /// <summary>Holds the actual unacked packet data, sorted by sequence number</summary> | ||
45 | private SortedDictionary<uint, OutgoingPacket> packets = new SortedDictionary<uint, OutgoingPacket>(); | ||
46 | |||
47 | /// <summary>Gets the total number of unacked packets</summary> | ||
48 | public int Count { get { return packets.Count; } } | ||
49 | |||
50 | /// <summary> | ||
51 | /// Default constructor | ||
52 | /// </summary> | ||
53 | public UnackedPacketCollection() | ||
54 | { | ||
55 | } | ||
56 | |||
57 | /// <summary> | ||
58 | /// Add an unacked packet to the collection | ||
59 | /// </summary> | ||
60 | /// <param name="packet">Packet that is awaiting acknowledgement</param> | ||
61 | /// <returns>True if the packet was successfully added, false if the | ||
62 | /// packet already existed in the collection</returns> | ||
63 | public bool Add(OutgoingPacket packet) | ||
64 | { | ||
65 | lock (SyncRoot) | ||
66 | { | ||
67 | if (!packets.ContainsKey(packet.SequenceNumber)) | ||
68 | { | ||
69 | packets.Add(packet.SequenceNumber, packet); | ||
70 | return true; | ||
71 | } | ||
72 | return false; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | /// <summary> | ||
77 | /// Removes a packet from the collection without attempting to obtain a | ||
78 | /// lock first | ||
79 | /// </summary> | ||
80 | /// <param name="sequenceNumber">Sequence number of the packet to remove</param> | ||
81 | /// <returns>True if the packet was found and removed, otherwise false</returns> | ||
82 | public bool RemoveUnsafe(uint sequenceNumber) | ||
83 | { | ||
84 | return packets.Remove(sequenceNumber); | ||
85 | } | ||
86 | |||
87 | /// <summary> | ||
88 | /// Removes a packet from the collection without attempting to obtain a | ||
89 | /// lock first | ||
90 | /// </summary> | ||
91 | /// <param name="sequenceNumber">Sequence number of the packet to remove</param> | ||
92 | /// <param name="packet">Returns the removed packet</param> | ||
93 | /// <returns>True if the packet was found and removed, otherwise false</returns> | ||
94 | public bool RemoveUnsafe(uint sequenceNumber, out OutgoingPacket packet) | ||
95 | { | ||
96 | if (packets.TryGetValue(sequenceNumber, out packet)) | ||
97 | { | ||
98 | packets.Remove(sequenceNumber); | ||
99 | return true; | ||
100 | } | ||
101 | |||
102 | return false; | ||
103 | } | ||
104 | |||
105 | /// <summary> | ||
106 | /// Gets the packet with the lowest sequence number | ||
107 | /// </summary> | ||
108 | /// <returns>The packet with the lowest sequence number, or null if the | ||
109 | /// collection is empty</returns> | ||
110 | public OutgoingPacket GetOldest() | ||
111 | { | ||
112 | lock (SyncRoot) | ||
113 | { | ||
114 | using (SortedDictionary<uint, OutgoingPacket>.ValueCollection.Enumerator e = packets.Values.GetEnumerator()) | ||
115 | { | ||
116 | if (e.MoveNext()) | ||
117 | return e.Current; | ||
118 | else | ||
119 | return null; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||
124 | /// <summary> | ||
125 | /// Returns a list of all of the packets with a TickCount older than | ||
126 | /// the specified timeout | ||
127 | /// </summary> | ||
128 | /// <param name="timeoutMS">Number of ticks (milliseconds) before a | ||
129 | /// packet is considered expired</param> | ||
130 | /// <returns>A list of all expired packets according to the given | ||
131 | /// expiration timeout</returns> | ||
132 | public List<OutgoingPacket> GetExpiredPackets(int timeoutMS) | ||
133 | { | ||
134 | List<OutgoingPacket> expiredPackets = null; | ||
135 | |||
136 | lock (SyncRoot) | ||
137 | { | ||
138 | int now = Environment.TickCount; | ||
139 | foreach (OutgoingPacket packet in packets.Values) | ||
140 | { | ||
141 | if (packet.TickCount == 0) | ||
142 | continue; | ||
143 | |||
144 | if (now - packet.TickCount >= timeoutMS) | ||
145 | { | ||
146 | if (expiredPackets == null) | ||
147 | expiredPackets = new List<OutgoingPacket>(); | ||
148 | expiredPackets.Add(packet); | ||
149 | } | ||
150 | else | ||
151 | { | ||
152 | break; | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | return expiredPackets; | ||
158 | } | ||
159 | } | ||
160 | } | ||