PacketPool.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. */
  28. using System;
  29. using System.Collections;
  30. using libsecondlife.Packets;
  31. namespace OpenSim.Framework
  32. {
  33. public sealed class PacketPool
  34. {
  35. // Set up a thread-safe singleton pattern
  36. static PacketPool()
  37. {
  38. }
  39. private static readonly PacketPool instance = new PacketPool();
  40. public static PacketPool Instance
  41. {
  42. get { return instance; }
  43. }
  44. private Hashtable pool = new Hashtable();
  45. public Packet GetPacket(PacketType type)
  46. {
  47. return Packet.BuildPacket(type);
  48. /* Skip until PacketPool performance problems have been resolved (mantis 281)
  49. Packet packet = null;
  50. lock (pool)
  51. {
  52. if (pool[type] == null || ((Stack) 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 = (Packet) ((Stack) pool[type]).Pop();
  61. }
  62. }
  63. return packet;
  64. */
  65. }
  66. // Copied from LibSL, and added a check to avoid overwriting the
  67. // buffer
  68. private void ZeroDecodeCommand(byte[] src, byte[] dest)
  69. {
  70. for (int srcPos = 6, destPos = 6; destPos < 10; ++srcPos)
  71. {
  72. if (src[srcPos] == 0x00)
  73. {
  74. for (byte j = 0; j < src[srcPos + 1] && destPos < 10; ++j)
  75. {
  76. dest[destPos++] = 0x00;
  77. }
  78. ++srcPos;
  79. }
  80. else
  81. {
  82. dest[destPos++] = src[srcPos];
  83. }
  84. }
  85. }
  86. private PacketType GetType(byte[] bytes)
  87. {
  88. byte[] decoded_header = new byte[10];
  89. ushort id;
  90. libsecondlife.PacketFrequency freq;
  91. Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
  92. if((bytes[0] & libsecondlife.Helpers.MSG_ZEROCODED)!=0)
  93. {
  94. ZeroDecodeCommand(bytes, decoded_header);
  95. }
  96. if (decoded_header[6] == 0xFF)
  97. {
  98. if (decoded_header[7] == 0xFF)
  99. {
  100. id = (ushort)((decoded_header[8] << 8) + decoded_header[9]);
  101. freq = libsecondlife.PacketFrequency.Low;
  102. }
  103. else
  104. {
  105. id = (ushort)decoded_header[7];
  106. freq = libsecondlife.PacketFrequency.Medium;
  107. }
  108. }
  109. else
  110. {
  111. id = (ushort)decoded_header[6];
  112. freq = libsecondlife.PacketFrequency.High;
  113. }
  114. return Packet.GetType(id, freq);
  115. }
  116. public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer)
  117. {
  118. PacketType type = GetType(bytes);
  119. int i = 0;
  120. Packet packet = GetPacket(type);
  121. packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
  122. return packet;
  123. }
  124. public void ReturnPacket(Packet packet)
  125. {
  126. /* Skip until PacketPool performance problems have been resolved (mantis 281)
  127. lock (pool)
  128. {
  129. PacketType type = packet.Type;
  130. if (pool[type] == null)
  131. {
  132. pool[type] = new Stack();
  133. }
  134. ((Stack) pool[type]).Push(packet);
  135. }
  136. */
  137. }
  138. }
  139. }