aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/ClientView.PacketQueue.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/ClientView.PacketQueue.cs')
-rw-r--r--OpenSim/Region/ClientStack/ClientView.PacketQueue.cs314
1 files changed, 314 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/ClientView.PacketQueue.cs b/OpenSim/Region/ClientStack/ClientView.PacketQueue.cs
new file mode 100644
index 0000000..05c2869
--- /dev/null
+++ b/OpenSim/Region/ClientStack/ClientView.PacketQueue.cs
@@ -0,0 +1,314 @@
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 OpenSim 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 System.Net.Sockets;
32using System.Timers;
33using libsecondlife;
34using libsecondlife.Packets;
35using OpenSim.Framework.Console;
36using OpenSim.Framework.Utilities;
37using OpenSim.Region.Environment;
38
39namespace OpenSim.Region.ClientStack
40{
41 public partial class ClientView
42 {
43 protected BlockingQueue<QueItem> PacketQueue;
44 protected Dictionary<uint, uint> PendingAcks = new Dictionary<uint, uint>();
45 protected Dictionary<uint, Packet> NeedAck = new Dictionary<uint, Packet>();
46
47 protected Timer AckTimer;
48 protected uint Sequence = 0;
49 protected object SequenceLock = new object();
50 protected const int MAX_APPENDED_ACKS = 10;
51 protected const int RESEND_TIMEOUT = 4000;
52 protected const int MAX_SEQUENCE = 0xFFFFFF;
53
54 public uint CircuitCode;
55 public EndPoint userEP;
56
57 protected PacketServer m_networkServer;
58
59 protected virtual void ProcessOutPacket(Packet Pack)
60 {
61 // Keep track of when this packet was sent out
62 Pack.TickCount = System.Environment.TickCount;
63
64 if (!Pack.Header.Resent)
65 {
66 // Set the sequence number
67 lock (SequenceLock)
68 {
69 if (Sequence >= MAX_SEQUENCE)
70 {
71 Sequence = 1;
72 }
73 else
74 {
75 Sequence++;
76 }
77
78 Pack.Header.Sequence = Sequence;
79 }
80
81 if (Pack.Header.Reliable) //DIRTY HACK
82 {
83 lock (NeedAck)
84 {
85 if (!NeedAck.ContainsKey(Pack.Header.Sequence))
86 {
87 try
88 {
89 NeedAck.Add(Pack.Header.Sequence, Pack);
90 }
91 catch (Exception e) // HACKY
92 {
93 e.ToString();
94 // Ignore
95 // Seems to throw a exception here occasionally
96 // of 'duplicate key' despite being locked.
97 // !?!?!?
98 }
99 }
100 else
101 {
102 // Client.Log("Attempted to add a duplicate sequence number (" +
103 // packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
104 // packet.Type.ToString(), Helpers.LogLevel.Warning);
105 }
106 }
107
108 // Don't append ACKs to resent packets, in case that's what was causing the
109 // delivery to fail
110 if (!Pack.Header.Resent)
111 {
112 // Append any ACKs that need to be sent out to this packet
113 lock (PendingAcks)
114 {
115 if (PendingAcks.Count > 0 && PendingAcks.Count < MAX_APPENDED_ACKS &&
116 Pack.Type != PacketType.PacketAck &&
117 Pack.Type != PacketType.LogoutRequest)
118 {
119 Pack.Header.AckList = new uint[PendingAcks.Count];
120 int i = 0;
121
122 foreach (uint ack in PendingAcks.Values)
123 {
124 Pack.Header.AckList[i] = ack;
125 i++;
126 }
127
128 PendingAcks.Clear();
129 Pack.Header.AppendedAcks = true;
130 }
131 }
132 }
133 }
134 }
135
136 byte[] ZeroOutBuffer = new byte[4096];
137 byte[] sendbuffer;
138 sendbuffer = Pack.ToBytes();
139
140 try
141 {
142 if (Pack.Header.Zerocoded)
143 {
144 int packetsize = Helpers.ZeroEncode(sendbuffer, sendbuffer.Length, ZeroOutBuffer);
145 m_networkServer.SendPacketTo(ZeroOutBuffer, packetsize, SocketFlags.None, CircuitCode);//userEP);
146 }
147 else
148 {
149 m_networkServer.SendPacketTo(sendbuffer, sendbuffer.Length, SocketFlags.None, CircuitCode); //userEP);
150 }
151 }
152 catch (Exception)
153 {
154 MainLog.Instance.Warn("client", "OpenSimClient.cs:ProcessOutPacket() - WARNING: Socket exception occurred on connection " + userEP.ToString() + " - killing thread");
155 this.KillThread();
156 }
157
158 }
159
160 public virtual void InPacket(Packet NewPack)
161 {
162 // Handle appended ACKs
163 if (NewPack.Header.AppendedAcks)
164 {
165 lock (NeedAck)
166 {
167 foreach (uint ack in NewPack.Header.AckList)
168 {
169 NeedAck.Remove(ack);
170 }
171 }
172 }
173
174 // Handle PacketAck packets
175 if (NewPack.Type == PacketType.PacketAck)
176 {
177 PacketAckPacket ackPacket = (PacketAckPacket)NewPack;
178
179 lock (NeedAck)
180 {
181 foreach (PacketAckPacket.PacketsBlock block in ackPacket.Packets)
182 {
183 NeedAck.Remove(block.ID);
184 }
185 }
186 }
187 else if ((NewPack.Type == PacketType.StartPingCheck))
188 {
189 //reply to pingcheck
190 StartPingCheckPacket startPing = (StartPingCheckPacket)NewPack;
191 CompletePingCheckPacket endPing = new CompletePingCheckPacket();
192 endPing.PingID.PingID = startPing.PingID.PingID;
193 OutPacket(endPing);
194 }
195 else
196 {
197 QueItem item = new QueItem();
198 item.Packet = NewPack;
199 item.Incoming = true;
200 this.PacketQueue.Enqueue(item);
201 }
202
203 }
204
205 public virtual void OutPacket(Packet NewPack)
206 {
207 QueItem item = new QueItem();
208 item.Packet = NewPack;
209 item.Incoming = false;
210 this.PacketQueue.Enqueue(item);
211 }
212
213 # region Low Level Packet Methods
214
215 protected void ack_pack(Packet Pack)
216 {
217 if (Pack.Header.Reliable)
218 {
219 PacketAckPacket ack_it = new PacketAckPacket();
220 ack_it.Packets = new PacketAckPacket.PacketsBlock[1];
221 ack_it.Packets[0] = new PacketAckPacket.PacketsBlock();
222 ack_it.Packets[0].ID = Pack.Header.Sequence;
223 ack_it.Header.Reliable = false;
224
225 OutPacket(ack_it);
226
227 }
228 /*
229 if (Pack.Header.Reliable)
230 {
231 lock (PendingAcks)
232 {
233 uint sequence = (uint)Pack.Header.Sequence;
234 if (!PendingAcks.ContainsKey(sequence)) { PendingAcks[sequence] = sequence; }
235 }
236 }*/
237 }
238
239 protected void ResendUnacked()
240 {
241 int now = System.Environment.TickCount;
242
243 lock (NeedAck)
244 {
245 foreach (Packet packet in NeedAck.Values)
246 {
247 if ((now - packet.TickCount > RESEND_TIMEOUT) && (!packet.Header.Resent))
248 {
249 MainLog.Instance.Verbose( "Resending " + packet.Type.ToString() + " packet, " +
250 (now - packet.TickCount) + "ms have passed");
251
252 packet.Header.Resent = true;
253 OutPacket(packet);
254 }
255 }
256 }
257 }
258
259 protected void SendAcks()
260 {
261 lock (PendingAcks)
262 {
263 if (PendingAcks.Count > 0)
264 {
265 if (PendingAcks.Count > 250)
266 {
267 // FIXME: Handle the odd case where we have too many pending ACKs queued up
268 MainLog.Instance.Verbose( "Too many ACKs queued up!");
269 return;
270 }
271
272 //OpenSim.Framework.Console.MainLog.Instance.WriteLine("Sending PacketAck");
273
274
275 int i = 0;
276 PacketAckPacket acks = new PacketAckPacket();
277 acks.Packets = new PacketAckPacket.PacketsBlock[PendingAcks.Count];
278
279 foreach (uint ack in PendingAcks.Values)
280 {
281 acks.Packets[i] = new PacketAckPacket.PacketsBlock();
282 acks.Packets[i].ID = ack;
283 i++;
284 }
285
286 acks.Header.Reliable = false;
287 OutPacket(acks);
288
289 PendingAcks.Clear();
290 }
291 }
292 }
293
294 protected void AckTimer_Elapsed(object sender, ElapsedEventArgs ea)
295 {
296 SendAcks();
297 ResendUnacked();
298 }
299 #endregion
300
301 #region Nested Classes
302
303 public class QueItem
304 {
305 public QueItem()
306 {
307 }
308
309 public Packet Packet;
310 public bool Incoming;
311 }
312 #endregion
313 }
314}