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