aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2012-10-05 01:12:56 +0100
committerJustin Clark-Casey (justincc)2012-10-05 01:12:56 +0100
commitf0178a6a413e35a45efcb0f7f0eeffc0daed15fe (patch)
tree82e1d79e5dfd6091bcffda3013cba8be7e61505f /OpenSim/Framework
parentMake the asset retrieval concurrency a config switch. The current value (diff)
downloadopensim-SC-f0178a6a413e35a45efcb0f7f0eeffc0daed15fe.zip
opensim-SC-f0178a6a413e35a45efcb0f7f0eeffc0daed15fe.tar.gz
opensim-SC-f0178a6a413e35a45efcb0f7f0eeffc0daed15fe.tar.bz2
opensim-SC-f0178a6a413e35a45efcb0f7f0eeffc0daed15fe.tar.xz
refactor: Move OpenSim.Framework.PacketPool to OpenSim.Region.Clientstack.Linden.UDP
This is to allow it to use OpenSim.Framework.Monitoring in the future. This is also a better location since the packet pool is linden udp specific
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r--OpenSim/Framework/PacketPool.cs247
1 files changed, 0 insertions, 247 deletions
diff --git a/OpenSim/Framework/PacketPool.cs b/OpenSim/Framework/PacketPool.cs
deleted file mode 100644
index 41d17c5..0000000
--- a/OpenSim/Framework/PacketPool.cs
+++ /dev/null
@@ -1,247 +0,0 @@
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.Reflection;
31using OpenMetaverse;
32using OpenMetaverse.Packets;
33using log4net;
34
35namespace OpenSim.Framework
36{
37
38 public sealed class PacketPool
39 {
40 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
41
42 private static readonly PacketPool instance = new PacketPool();
43
44 private bool packetPoolEnabled = true;
45 private bool dataBlockPoolEnabled = true;
46
47 private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>();
48
49 private static Dictionary<Type, Stack<Object>> DataBlocks =
50 new Dictionary<Type, Stack<Object>>();
51
52 static PacketPool()
53 {
54 }
55
56 public static PacketPool Instance
57 {
58 get { return instance; }
59 }
60
61 public bool RecyclePackets
62 {
63 set { packetPoolEnabled = value; }
64 get { return packetPoolEnabled; }
65 }
66
67 public bool RecycleDataBlocks
68 {
69 set { dataBlockPoolEnabled = value; }
70 get { return dataBlockPoolEnabled; }
71 }
72
73 public Packet GetPacket(PacketType type)
74 {
75 Packet packet;
76
77 if (!packetPoolEnabled)
78 return Packet.BuildPacket(type);
79
80 lock (pool)
81 {
82 if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0)
83 {
84 // Creating a new packet if we cannot reuse an old package
85 packet = Packet.BuildPacket(type);
86 }
87 else
88 {
89 // Recycle old packages
90 packet = (pool[type]).Pop();
91 }
92 }
93
94 return packet;
95 }
96
97 // private byte[] decoded_header = new byte[10];
98 private static PacketType GetType(byte[] bytes)
99 {
100 byte[] decoded_header = new byte[10 + 8];
101 ushort id;
102 PacketFrequency freq;
103
104 if ((bytes[0] & Helpers.MSG_ZEROCODED) != 0)
105 {
106 Helpers.ZeroDecode(bytes, 16, decoded_header);
107 }
108 else
109 {
110 Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
111 }
112
113 if (decoded_header[6] == 0xFF)
114 {
115 if (decoded_header[7] == 0xFF)
116 {
117 id = (ushort) ((decoded_header[8] << 8) + decoded_header[9]);
118 freq = PacketFrequency.Low;
119 }
120 else
121 {
122 id = decoded_header[7];
123 freq = PacketFrequency.Medium;
124 }
125 }
126 else
127 {
128 id = decoded_header[6];
129 freq = PacketFrequency.High;
130 }
131
132 return Packet.GetType(id, freq);
133 }
134
135 public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer)
136 {
137 PacketType type = GetType(bytes);
138
139 Array.Clear(zeroBuffer, 0, zeroBuffer.Length);
140
141 int i = 0;
142 Packet packet = GetPacket(type);
143 if (packet == null)
144 m_log.WarnFormat("[PACKETPOOL]: Failed to get packet of type {0}", type);
145 else
146 packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
147
148 return packet;
149 }
150
151 /// <summary>
152 /// Return a packet to the packet pool
153 /// </summary>
154 /// <param name="packet"></param>
155 public void ReturnPacket(Packet packet)
156 {
157 if (dataBlockPoolEnabled)
158 {
159 switch (packet.Type)
160 {
161 case PacketType.ObjectUpdate:
162 ObjectUpdatePacket oup = (ObjectUpdatePacket)packet;
163
164 foreach (ObjectUpdatePacket.ObjectDataBlock oupod in oup.ObjectData)
165 ReturnDataBlock<ObjectUpdatePacket.ObjectDataBlock>(oupod);
166
167 oup.ObjectData = null;
168 break;
169
170 case PacketType.ImprovedTerseObjectUpdate:
171 ImprovedTerseObjectUpdatePacket itoup = (ImprovedTerseObjectUpdatePacket)packet;
172
173 foreach (ImprovedTerseObjectUpdatePacket.ObjectDataBlock itoupod in itoup.ObjectData)
174 ReturnDataBlock<ImprovedTerseObjectUpdatePacket.ObjectDataBlock>(itoupod);
175
176 itoup.ObjectData = null;
177 break;
178 }
179 }
180
181 if (packetPoolEnabled)
182 {
183 switch (packet.Type)
184 {
185 // List pooling packets here
186 case PacketType.PacketAck:
187 case PacketType.ObjectUpdate:
188 case PacketType.ImprovedTerseObjectUpdate:
189 lock (pool)
190 {
191 PacketType type = packet.Type;
192
193 if (!pool.ContainsKey(type))
194 {
195 pool[type] = new Stack<Packet>();
196 }
197
198 if ((pool[type]).Count < 50)
199 {
200 (pool[type]).Push(packet);
201 }
202 }
203 break;
204
205 // Other packets wont pool
206 default:
207 return;
208 }
209 }
210 }
211
212 public static T GetDataBlock<T>() where T: new()
213 {
214 lock (DataBlocks)
215 {
216 Stack<Object> s;
217
218 if (DataBlocks.TryGetValue(typeof(T), out s))
219 {
220 if (s.Count > 0)
221 return (T)s.Pop();
222 }
223 else
224 {
225 DataBlocks[typeof(T)] = new Stack<Object>();
226 }
227
228 return new T();
229 }
230 }
231
232 public static void ReturnDataBlock<T>(T block) where T: new()
233 {
234 if (block == null)
235 return;
236
237 lock (DataBlocks)
238 {
239 if (!DataBlocks.ContainsKey(typeof(T)))
240 DataBlocks[typeof(T)] = new Stack<Object>();
241
242 if (DataBlocks[typeof(T)].Count < 50)
243 DataBlocks[typeof(T)].Push(block);
244 }
245 }
246 }
247}