aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/PacketPool.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/PacketPool.cs131
1 files changed, 131 insertions, 0 deletions
diff --git a/OpenSim/Framework/PacketPool.cs b/OpenSim/Framework/PacketPool.cs
new file mode 100644
index 0000000..2b33d0b
--- /dev/null
+++ b/OpenSim/Framework/PacketPool.cs
@@ -0,0 +1,131 @@
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;
30using libsecondlife.Packets;
31
32namespace OpenSim.Framework
33{
34 public sealed class PacketPool
35 {
36 // Set up a thread-safe singleton pattern
37 static PacketPool()
38 {
39 }
40
41 private static readonly PacketPool instance = new PacketPool();
42
43 public static PacketPool Instance
44 {
45 get { return instance; }
46 }
47
48 private Hashtable pool = new Hashtable();
49
50 public Packet GetPacket(PacketType type)
51 {
52 Packet packet = null;
53
54 lock (pool)
55 {
56 if (pool[type] == null || ((Stack) pool[type]).Count == 0)
57 {
58 // Creating a new packet if we cannot reuse an old package
59 packet = Packet.BuildPacket(type);
60 }
61 else
62 {
63 // Recycle old packages
64 packet = (Packet) ((Stack) pool[type]).Pop();
65 }
66 }
67
68 return packet;
69 }
70
71 private byte[] decoded_header = new byte[10];
72 private PacketType GetType(byte[] bytes)
73 {
74 ushort id;
75 libsecondlife.PacketFrequency freq;
76
77 Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
78
79 if((bytes[0] & libsecondlife.Helpers.MSG_ZEROCODED)!=0)
80 {
81 libsecondlife.Helpers.ZeroDecodeCommand(bytes, decoded_header);
82 }
83
84 if (decoded_header[6] == 0xFF)
85 {
86 if (decoded_header[7] == 0xFF)
87 {
88 id = (ushort)((decoded_header[8] << 8) + decoded_header[9]);
89 freq = libsecondlife.PacketFrequency.Low;
90 }
91 else
92 {
93 id = (ushort)decoded_header[7];
94 freq = libsecondlife.PacketFrequency.Medium;
95 }
96 }
97 else
98 {
99 id = (ushort)decoded_header[6];
100 freq = libsecondlife.PacketFrequency.High;
101 }
102
103 return Packet.GetType(id, freq);
104 }
105
106 public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer)
107 {
108 PacketType type = GetType(bytes);
109
110 int i = 0;
111 Packet packet = GetPacket(type);
112 packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
113 return packet;
114 }
115
116 public void ReturnPacket(Packet packet)
117 {
118 lock (pool)
119 {
120 PacketType type = packet.Type;
121
122 if (pool[type] == null)
123 {
124 pool[type] = new Stack();
125 }
126
127 ((Stack) pool[type]).Push(packet);
128 }
129 }
130 }
131}