1
0

RegionHandle.cs 5.5 KB

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