PacketPool.cs 5.2 KB

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