PacketPool.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 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 = false;
  40. private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>();
  41. static PacketPool()
  42. {
  43. }
  44. public static PacketPool Instance
  45. {
  46. get { return instance; }
  47. }
  48. public Packet GetPacket(PacketType type)
  49. {
  50. Packet packet;
  51. if (!packetPoolEnabled)
  52. return Packet.BuildPacket(type);
  53. lock (pool)
  54. {
  55. if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0)
  56. {
  57. // Creating a new packet if we cannot reuse an old package
  58. packet = Packet.BuildPacket(type);
  59. }
  60. else
  61. {
  62. // Recycle old packages
  63. packet = (pool[type]).Pop();
  64. }
  65. }
  66. return packet;
  67. }
  68. // private byte[] decoded_header = new byte[10];
  69. private static PacketType GetType(byte[] bytes)
  70. {
  71. byte[] decoded_header = new byte[10 + 8];
  72. ushort id;
  73. PacketFrequency freq;
  74. if ((bytes[0] & Helpers.MSG_ZEROCODED) != 0)
  75. {
  76. Helpers.ZeroDecode(bytes, 16, decoded_header);
  77. }
  78. else
  79. {
  80. Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
  81. }
  82. if (decoded_header[6] == 0xFF)
  83. {
  84. if (decoded_header[7] == 0xFF)
  85. {
  86. id = (ushort) ((decoded_header[8] << 8) + decoded_header[9]);
  87. freq = PacketFrequency.Low;
  88. }
  89. else
  90. {
  91. id = decoded_header[7];
  92. freq = PacketFrequency.Medium;
  93. }
  94. }
  95. else
  96. {
  97. id = decoded_header[6];
  98. freq = PacketFrequency.High;
  99. }
  100. return Packet.GetType(id, freq);
  101. }
  102. public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer)
  103. {
  104. PacketType type = GetType(bytes);
  105. Array.Clear(zeroBuffer, 0, zeroBuffer.Length);
  106. int i = 0;
  107. Packet packet = GetPacket(type);
  108. if (packet == null)
  109. m_log.WarnFormat("[PACKETPOOL]: Failed to get packet of type {0}", type);
  110. else
  111. packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
  112. return packet;
  113. }
  114. /// <summary>
  115. /// Return a packet to the packet pool
  116. /// </summary>
  117. /// <param name="packet"></param>
  118. public void ReturnPacket(Packet packet)
  119. {
  120. if (!packetPoolEnabled)
  121. return;
  122. switch (packet.Type)
  123. {
  124. // List pooling packets here
  125. case PacketType.PacketAck:
  126. lock (pool)
  127. {
  128. PacketType type = packet.Type;
  129. if (!pool.ContainsKey(type))
  130. {
  131. pool[type] = new Stack<Packet>();
  132. }
  133. if ((pool[type]).Count < 50)
  134. {
  135. (pool[type]).Push(packet);
  136. }
  137. }
  138. break;
  139. // Other packets wont pool
  140. default:
  141. return;
  142. }
  143. }
  144. }
  145. }