PacketPool.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using OpenMetaverse.Packets;
  32. using log4net;
  33. namespace OpenSim.Framework
  34. {
  35. public sealed class PacketPool
  36. {
  37. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  38. private static readonly PacketPool instance = new PacketPool();
  39. private bool packetPoolEnabled = true;
  40. private bool dataBlockPoolEnabled = true;
  41. private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>();
  42. private static Dictionary<Type, Stack<Object>> DataBlocks =
  43. new Dictionary<Type, Stack<Object>>();
  44. static PacketPool()
  45. {
  46. }
  47. public static PacketPool Instance
  48. {
  49. get { return instance; }
  50. }
  51. public bool RecyclePackets
  52. {
  53. set { packetPoolEnabled = value; }
  54. get { return packetPoolEnabled; }
  55. }
  56. public bool RecycleDataBlocks
  57. {
  58. set { dataBlockPoolEnabled = value; }
  59. get { return dataBlockPoolEnabled; }
  60. }
  61. public Packet GetPacket(PacketType type)
  62. {
  63. Packet packet;
  64. if (!packetPoolEnabled)
  65. return Packet.BuildPacket(type);
  66. lock (pool)
  67. {
  68. if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0)
  69. {
  70. // Creating a new packet if we cannot reuse an old package
  71. packet = Packet.BuildPacket(type);
  72. }
  73. else
  74. {
  75. // Recycle old packages
  76. packet = (pool[type]).Pop();
  77. }
  78. }
  79. return packet;
  80. }
  81. // private byte[] decoded_header = new byte[10];
  82. private static PacketType GetType(byte[] bytes)
  83. {
  84. byte[] decoded_header = new byte[10 + 8];
  85. ushort id;
  86. PacketFrequency freq;
  87. if ((bytes[0] & Helpers.MSG_ZEROCODED) != 0)
  88. {
  89. Helpers.ZeroDecode(bytes, 16, decoded_header);
  90. }
  91. else
  92. {
  93. Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
  94. }
  95. if (decoded_header[6] == 0xFF)
  96. {
  97. if (decoded_header[7] == 0xFF)
  98. {
  99. id = (ushort) ((decoded_header[8] << 8) + decoded_header[9]);
  100. freq = PacketFrequency.Low;
  101. }
  102. else
  103. {
  104. id = decoded_header[7];
  105. freq = PacketFrequency.Medium;
  106. }
  107. }
  108. else
  109. {
  110. id = decoded_header[6];
  111. freq = PacketFrequency.High;
  112. }
  113. return Packet.GetType(id, freq);
  114. }
  115. public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer)
  116. {
  117. PacketType type = GetType(bytes);
  118. Array.Clear(zeroBuffer, 0, zeroBuffer.Length);
  119. int i = 0;
  120. Packet packet = GetPacket(type);
  121. if (packet == null)
  122. m_log.WarnFormat("[PACKETPOOL]: Failed to get packet of type {0}", type);
  123. else
  124. packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
  125. return packet;
  126. }
  127. /// <summary>
  128. /// Return a packet to the packet pool
  129. /// </summary>
  130. /// <param name="packet"></param>
  131. public void ReturnPacket(Packet packet)
  132. {
  133. if (dataBlockPoolEnabled)
  134. {
  135. switch (packet.Type)
  136. {
  137. case PacketType.ObjectUpdate:
  138. ObjectUpdatePacket oup = (ObjectUpdatePacket)packet;
  139. foreach (ObjectUpdatePacket.ObjectDataBlock oupod in
  140. oup.ObjectData)
  141. ReturnDataBlock<ObjectUpdatePacket.ObjectDataBlock>(oupod);
  142. oup.ObjectData = null;
  143. break;
  144. case PacketType.ImprovedTerseObjectUpdate:
  145. ImprovedTerseObjectUpdatePacket itoup =
  146. (ImprovedTerseObjectUpdatePacket)packet;
  147. foreach (ImprovedTerseObjectUpdatePacket.ObjectDataBlock
  148. itoupod in itoup.ObjectData)
  149. ReturnDataBlock<ImprovedTerseObjectUpdatePacket.ObjectDataBlock>(itoupod);
  150. itoup.ObjectData = null;
  151. break;
  152. }
  153. }
  154. if (packetPoolEnabled)
  155. {
  156. switch (packet.Type)
  157. {
  158. // List pooling packets here
  159. case PacketType.PacketAck:
  160. case PacketType.ObjectUpdate:
  161. case PacketType.ImprovedTerseObjectUpdate:
  162. lock (pool)
  163. {
  164. PacketType type = packet.Type;
  165. if (!pool.ContainsKey(type))
  166. {
  167. pool[type] = new Stack<Packet>();
  168. }
  169. if ((pool[type]).Count < 50)
  170. {
  171. (pool[type]).Push(packet);
  172. }
  173. }
  174. break;
  175. // Other packets wont pool
  176. default:
  177. return;
  178. }
  179. }
  180. }
  181. public static T GetDataBlock<T>() where T: new()
  182. {
  183. lock (DataBlocks)
  184. {
  185. Stack<Object> s;
  186. if (DataBlocks.TryGetValue(typeof(T), out s))
  187. {
  188. if (s.Count > 0)
  189. return (T)s.Pop();
  190. }
  191. else
  192. {
  193. DataBlocks[typeof(T)] = new Stack<Object>();
  194. }
  195. return new T();
  196. }
  197. }
  198. public static void ReturnDataBlock<T>(T block) where T: new()
  199. {
  200. if (block == null)
  201. return;
  202. lock (DataBlocks)
  203. {
  204. DataBlocks[typeof(T)].Push(block);
  205. }
  206. }
  207. }
  208. }