aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r--OpenSim/Framework/Console/ConsoleUtil.cs114
-rw-r--r--OpenSim/Framework/GridInstantMessage.cs9
-rw-r--r--OpenSim/Framework/PacketPool.cs247
-rw-r--r--OpenSim/Framework/Util.cs13
4 files changed, 132 insertions, 251 deletions
diff --git a/OpenSim/Framework/Console/ConsoleUtil.cs b/OpenSim/Framework/Console/ConsoleUtil.cs
new file mode 100644
index 0000000..2612a50
--- /dev/null
+++ b/OpenSim/Framework/Console/ConsoleUtil.cs
@@ -0,0 +1,114 @@
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.Linq;
31using System.Reflection;
32using log4net;
33using OpenMetaverse;
34
35public class ConsoleUtil
36{
37 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
38
39 public const string MinRawConsoleVectorValue = "-~";
40 public const string MaxRawConsoleVectorValue = "~";
41
42 public const string VectorSeparator = ",";
43 public static char[] VectorSeparatorChars = VectorSeparator.ToCharArray();
44
45 /// <summary>
46 /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3
47 /// </summary>
48 /// <param name='rawConsoleVector'>/param>
49 /// <param name='vector'></param>
50 /// <returns></returns>
51 public static bool TryParseConsoleMinVector(string rawConsoleVector, out Vector3 vector)
52 {
53 return TryParseConsoleVector(rawConsoleVector, c => float.MinValue.ToString(), out vector);
54 }
55
56 /// <summary>
57 /// Convert a maximum vector input from the console to an OpenMetaverse.Vector3
58 /// </summary>
59 /// <param name='rawConsoleVector'>/param>
60 /// <param name='vector'></param>
61 /// <returns></returns>
62 public static bool TryParseConsoleMaxVector(string rawConsoleVector, out Vector3 vector)
63 {
64 return TryParseConsoleVector(rawConsoleVector, c => float.MaxValue.ToString(), out vector);
65 }
66
67 /// <summary>
68 /// Convert a vector input from the console to an OpenMetaverse.Vector3
69 /// </summary>
70 /// <param name='rawConsoleVector'>
71 /// A string in the form <x>,<y>,<z> where there is no space between values.
72 /// Any component can be missing (e.g. ,,40). blankComponentFunc is invoked to replace the blank with a suitable value
73 /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40,30 or 40)
74 /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue
75 /// Other than that, component values must be numeric.
76 /// </param>
77 /// <param name='blankComponentFunc'></param>
78 /// <param name='vector'></param>
79 /// <returns></returns>
80 public static bool TryParseConsoleVector(
81 string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector)
82 {
83 List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList();
84
85 if (components.Count < 1 || components.Count > 3)
86 {
87 vector = Vector3.Zero;
88 return false;
89 }
90
91 for (int i = components.Count; i < 3; i++)
92 components.Add("");
93
94 List<string> semiDigestedComponents
95 = components.ConvertAll<string>(
96 c =>
97 {
98 if (c == "")
99 return blankComponentFunc.Invoke(c);
100 else if (c == MaxRawConsoleVectorValue)
101 return float.MaxValue.ToString();
102 else if (c == MinRawConsoleVectorValue)
103 return float.MinValue.ToString();
104 else
105 return c;
106 });
107
108 string semiDigestedConsoleVector = string.Join(VectorSeparator, semiDigestedComponents.ToArray());
109
110 m_log.DebugFormat("[CONSOLE UTIL]: Parsing {0} into OpenMetaverse.Vector3", semiDigestedConsoleVector);
111
112 return Vector3.TryParse(semiDigestedConsoleVector, out vector);
113 }
114} \ No newline at end of file
diff --git a/OpenSim/Framework/GridInstantMessage.cs b/OpenSim/Framework/GridInstantMessage.cs
index a6bf6e3..6ae0488 100644
--- a/OpenSim/Framework/GridInstantMessage.cs
+++ b/OpenSim/Framework/GridInstantMessage.cs
@@ -44,7 +44,6 @@ namespace OpenSim.Framework
44 public Vector3 Position; 44 public Vector3 Position;
45 public byte[] binaryBucket; 45 public byte[] binaryBucket;
46 46
47
48 public uint ParentEstateID; 47 public uint ParentEstateID;
49 public Guid RegionID; 48 public Guid RegionID;
50 public uint timestamp; 49 public uint timestamp;
@@ -58,7 +57,7 @@ namespace OpenSim.Framework
58 string _fromAgentName, UUID _toAgentID, 57 string _fromAgentName, UUID _toAgentID,
59 byte _dialog, bool _fromGroup, string _message, 58 byte _dialog, bool _fromGroup, string _message,
60 UUID _imSessionID, bool _offline, Vector3 _position, 59 UUID _imSessionID, bool _offline, Vector3 _position,
61 byte[] _binaryBucket) 60 byte[] _binaryBucket, bool addTimestamp)
62 { 61 {
63 fromAgentID = _fromAgentID.Guid; 62 fromAgentID = _fromAgentID.Guid;
64 fromAgentName = _fromAgentName; 63 fromAgentName = _fromAgentName;
@@ -79,7 +78,9 @@ namespace OpenSim.Framework
79 ParentEstateID = scene.RegionInfo.EstateSettings.ParentEstateID; 78 ParentEstateID = scene.RegionInfo.EstateSettings.ParentEstateID;
80 RegionID = scene.RegionInfo.RegionSettings.RegionUUID.Guid; 79 RegionID = scene.RegionInfo.RegionSettings.RegionUUID.Guid;
81 } 80 }
82 timestamp = (uint)Util.UnixTimeSinceEpoch(); 81
82 if (addTimestamp)
83 timestamp = (uint)Util.UnixTimeSinceEpoch();
83 } 84 }
84 85
85 public GridInstantMessage(IScene scene, UUID _fromAgentID, 86 public GridInstantMessage(IScene scene, UUID _fromAgentID,
@@ -87,7 +88,7 @@ namespace OpenSim.Framework
87 string _message, bool _offline, 88 string _message, bool _offline,
88 Vector3 _position) : this(scene, _fromAgentID, _fromAgentName, 89 Vector3 _position) : this(scene, _fromAgentID, _fromAgentName,
89 _toAgentID, _dialog, false, _message, 90 _toAgentID, _dialog, false, _message,
90 _fromAgentID ^ _toAgentID, _offline, _position, new byte[0]) 91 _fromAgentID ^ _toAgentID, _offline, _position, new byte[0], true)
91 { 92 {
92 } 93 }
93 } 94 }
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}
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index a26e930..74551ea 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -546,6 +546,19 @@ namespace OpenSim.Framework
546 } 546 }
547 547
548 /// <summary> 548 /// <summary>
549 /// Determines whether a point is inside a bounding box.
550 /// </summary>
551 /// <param name='v'>/param>
552 /// <param name='min'></param>
553 /// <param name='max'></param>
554 /// <returns></returns>
555 public static bool IsInsideBox(Vector3 v, Vector3 min, Vector3 max)
556 {
557 return v.X >= min.X & v.Y >= min.Y && v.Z >= min.Z
558 && v.X <= max.X && v.Y <= max.Y && v.Z <= max.Z;
559 }
560
561 /// <summary>
549 /// Are the co-ordinates of the new region visible from the old region? 562 /// Are the co-ordinates of the new region visible from the old region?
550 /// </summary> 563 /// </summary>
551 /// <param name="oldx">Old region x-coord</param> 564 /// <param name="oldx">Old region x-coord</param>