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.cs114
1 files changed, 114 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..16c0035
--- /dev/null
+++ b/OpenSim/Region/ClientStack/LindenUDP/UnackedPacketCollection.cs
@@ -0,0 +1,114 @@
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
28using System;
29using System.Collections.Generic;
30using System.Net;
31using OpenMetaverse;
32
33namespace OpenSim.Region.ClientStack.LindenUDP
34{
35 public sealed class UnackedPacketCollection
36 {
37 public object SyncRoot = new object();
38
39 SortedDictionary<uint, OutgoingPacket> packets;
40
41 public int Count { get { return packets.Count; } }
42
43 public UnackedPacketCollection()
44 {
45 packets = new SortedDictionary<uint, OutgoingPacket>();
46 }
47
48 public bool Add(OutgoingPacket packet)
49 {
50 lock (SyncRoot)
51 {
52 if (!packets.ContainsKey(packet.SequenceNumber))
53 {
54 packets.Add(packet.SequenceNumber, packet);
55 return true;
56 }
57 return false;
58 }
59 }
60
61 public bool RemoveUnsafe(uint sequenceNumber)
62 {
63 return packets.Remove(sequenceNumber);
64 }
65
66 public bool RemoveUnsafe(uint sequenceNumber, out OutgoingPacket packet)
67 {
68 if (packets.TryGetValue(sequenceNumber, out packet))
69 {
70 packets.Remove(sequenceNumber);
71 return true;
72 }
73
74 return false;
75 }
76
77 public OutgoingPacket GetOldest()
78 {
79 lock (SyncRoot)
80 {
81 using (SortedDictionary<uint, OutgoingPacket>.ValueCollection.Enumerator e = packets.Values.GetEnumerator())
82 return e.Current;
83 }
84 }
85
86 public List<OutgoingPacket> GetExpiredPackets(int timeout)
87 {
88 List<OutgoingPacket> expiredPackets = null;
89
90 lock (SyncRoot)
91 {
92 int now = Environment.TickCount;
93 foreach (OutgoingPacket packet in packets.Values)
94 {
95 if (packet.TickCount == 0)
96 continue;
97
98 if (now - packet.TickCount >= timeout)
99 {
100 if (expiredPackets == null)
101 expiredPackets = new List<OutgoingPacket>();
102 expiredPackets.Add(packet);
103 }
104 else
105 {
106 break;
107 }
108 }
109 }
110
111 return expiredPackets;
112 }
113 }
114}