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