PacketPool.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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;
  29. using System.Net;
  30. using libsecondlife;
  31. using libsecondlife.Packets;
  32. namespace OpenSim.Framework
  33. {
  34. public sealed class PacketPool
  35. {
  36. private static readonly PacketPool instance = new PacketPool();
  37. private Hashtable pool = new Hashtable();
  38. static PacketPool()
  39. {
  40. }
  41. public static PacketPool Instance
  42. {
  43. get { return instance; }
  44. }
  45. public static void EncodeProxyMessage(byte[] bytes, ref int numBytes, EndPoint trueEP)
  46. {
  47. if (numBytes > 4090) // max UPD size = 4096
  48. {
  49. throw new Exception("ERROR: No space to encode the proxy EP");
  50. }
  51. ushort port = (ushort) ((IPEndPoint) trueEP).Port;
  52. bytes[numBytes++] = (byte) (port % 256);
  53. bytes[numBytes++] = (byte) (port / 256);
  54. foreach (byte b in ((IPEndPoint) trueEP).Address.GetAddressBytes())
  55. {
  56. bytes[numBytes++] = b;
  57. }
  58. int x = numBytes;
  59. DecodeProxyMessage(bytes, ref numBytes);
  60. numBytes = x;
  61. }
  62. public static EndPoint DecodeProxyMessage(byte[] bytes, ref int numBytes)
  63. {
  64. // IPv4 Only
  65. byte[] addr = new byte[4];
  66. addr[3] = bytes[--numBytes];
  67. addr[2] = bytes[--numBytes];
  68. addr[1] = bytes[--numBytes];
  69. addr[0] = bytes[--numBytes];
  70. ushort port = (ushort) (bytes[--numBytes] * 256);
  71. port += (ushort) bytes[--numBytes];
  72. return (EndPoint) new IPEndPoint(new IPAddress(addr), (int) port);
  73. }
  74. public Packet GetPacket(PacketType type)
  75. {
  76. Packet packet;
  77. lock (pool)
  78. {
  79. if (pool[type] == null || ((Stack) pool[type]).Count == 0)
  80. {
  81. // Creating a new packet if we cannot reuse an old package
  82. packet = Packet.BuildPacket(type);
  83. }
  84. else
  85. {
  86. // Recycle old packages
  87. packet = (Packet) ((Stack) pool[type]).Pop();
  88. }
  89. }
  90. return packet;
  91. }
  92. // private byte[] decoded_header = new byte[10];
  93. private static PacketType GetType(byte[] bytes)
  94. {
  95. byte[] decoded_header = new byte[10 + 8];
  96. ushort id;
  97. PacketFrequency freq;
  98. Buffer.BlockCopy(bytes, 0, decoded_header, 0, 10);
  99. if ((bytes[0] & Helpers.MSG_ZEROCODED) != 0)
  100. {
  101. Helpers.ZeroDecodeCommand(bytes, decoded_header);
  102. }
  103. if (decoded_header[6] == 0xFF)
  104. {
  105. if (decoded_header[7] == 0xFF)
  106. {
  107. id = (ushort) ((decoded_header[8] << 8) + decoded_header[9]);
  108. freq = PacketFrequency.Low;
  109. }
  110. else
  111. {
  112. id = (ushort) decoded_header[7];
  113. freq = PacketFrequency.Medium;
  114. }
  115. }
  116. else
  117. {
  118. id = (ushort) decoded_header[6];
  119. freq = PacketFrequency.High;
  120. }
  121. return Packet.GetType(id, freq);
  122. }
  123. public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer)
  124. {
  125. PacketType type = GetType(bytes);
  126. int i = 0;
  127. Packet packet = GetPacket(type);
  128. packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer);
  129. return packet;
  130. }
  131. public void ReturnPacket(Packet packet)
  132. {
  133. return; // packet pool disabled
  134. /* // Commented out to remove a compiler warning. :)
  135. lock (pool)
  136. {
  137. PacketType type=packet.Type;
  138. if (pool[type] == null)
  139. {
  140. pool[type] = new Stack();
  141. }
  142. if (((Stack)pool[type]).Count < 50)
  143. {
  144. ((Stack)pool[type]).Push(packet);
  145. }
  146. }
  147. */
  148. }
  149. }
  150. }