RegionHandle.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Net;
  30. using System.Net.Sockets;
  31. namespace OpenSim.Framework
  32. {
  33. /// <summary>
  34. /// A class for manipulating RegionHandle coordinates
  35. /// </summary>
  36. internal class RegionHandle
  37. {
  38. private UInt64 handle;
  39. /// <summary>
  40. /// Initialises a new grid-aware RegionHandle
  41. /// </summary>
  42. /// <param name="ip">IP Address of the Grid Server for this region</param>
  43. /// <param name="x">Grid X Coordinate</param>
  44. /// <param name="y">Grid Y Coordinate</param>
  45. public RegionHandle(string ip, short x, short y)
  46. {
  47. IPAddress addr = IPAddress.Parse(ip);
  48. if (addr.AddressFamily != AddressFamily.InterNetwork)
  49. throw new Exception("Bad RegionHandle Parameter - must be an IPv4 address");
  50. uint baseHandle = BitConverter.ToUInt32(addr.GetAddressBytes(), 0);
  51. // Split the IP address in half
  52. short a = (short) ((baseHandle << 16) & 0xFFFF);
  53. short b = (short) ((baseHandle << 0) & 0xFFFF);
  54. // Raise the bounds a little
  55. uint nx = (uint) x;
  56. uint ny = (uint) y;
  57. // Multiply grid coords to get region coords
  58. nx *= 256;
  59. ny *= 256;
  60. // Stuff the IP address in too
  61. nx = (uint) a << 16;
  62. ny = (uint) b << 16;
  63. handle = ((UInt64) nx << 32) | (uint) ny;
  64. }
  65. /// <summary>
  66. /// Initialises a new RegionHandle that is not inter-grid aware
  67. /// </summary>
  68. /// <param name="x">Grid X Coordinate</param>
  69. /// <param name="y">Grid Y Coordinate</param>
  70. public RegionHandle(uint x, uint y)
  71. {
  72. handle = ((x*256) << 32) | (y*256);
  73. }
  74. /// <summary>
  75. /// Initialises a new RegionHandle from an existing value
  76. /// </summary>
  77. /// <param name="Region">A U64 RegionHandle</param>
  78. public RegionHandle(UInt64 Region)
  79. {
  80. handle = Region;
  81. }
  82. /// <summary>
  83. /// Returns the Grid Masked RegionHandle - For use in Teleport packets and other packets where sending the grid IP address may be handy.
  84. /// </summary>
  85. /// <remarks>Do not use for SimulatorEnable packets. The client will choke.</remarks>
  86. /// <returns>Region Handle including IP Address encoding</returns>
  87. public UInt64 getTeleportHandle()
  88. {
  89. return handle;
  90. }
  91. /// <summary>
  92. /// Returns a RegionHandle which may be used for SimulatorEnable packets. Removes the IP address encoding and returns the lower bounds.
  93. /// </summary>
  94. /// <returns>A U64 RegionHandle for use in SimulatorEnable packets.</returns>
  95. public UInt64 getNeighbourHandle()
  96. {
  97. UInt64 mask = 0x0000FFFF0000FFFF;
  98. return handle | mask;
  99. }
  100. /// <summary>
  101. /// Returns the IP Address of the GridServer from a Grid-Encoded RegionHandle
  102. /// </summary>
  103. /// <returns>Grid Server IP Address</returns>
  104. public IPAddress getGridIP()
  105. {
  106. uint a = (uint) ((handle >> 16) & 0xFFFF);
  107. uint b = (uint) ((handle >> 48) & 0xFFFF);
  108. return new IPAddress((long) (a << 16) | (long) b);
  109. }
  110. /// <summary>
  111. /// Returns the X Coordinate from a Grid-Encoded RegionHandle
  112. /// </summary>
  113. /// <returns>X Coordinate</returns>
  114. public uint getGridX()
  115. {
  116. uint x = (uint) ((handle >> 32) & 0xFFFF);
  117. return x;
  118. }
  119. /// <summary>
  120. /// Returns the Y Coordinate from a Grid-Encoded RegionHandle
  121. /// </summary>
  122. /// <returns>Y Coordinate</returns>
  123. public uint getGridY()
  124. {
  125. uint y = (uint) ((handle >> 0) & 0xFFFF);
  126. return y;
  127. }
  128. }
  129. }