PacketPool.cs 5.9 KB

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