RegionHandle.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Net;
  3. namespace OpenSim.Framework.Types
  4. {
  5. /// <summary>
  6. /// A class for manipulating RegionHandle coordinates
  7. /// </summary>
  8. class RegionHandle
  9. {
  10. private UInt64 handle;
  11. /// <summary>
  12. /// Initialises a new grid-aware RegionHandle
  13. /// </summary>
  14. /// <param name="ip">IP Address of the Grid Server for this region</param>
  15. /// <param name="x">Grid X Coordinate</param>
  16. /// <param name="y">Grid Y Coordinate</param>
  17. public RegionHandle(string ip, short x, short y)
  18. {
  19. IPAddress addr = IPAddress.Parse(ip);
  20. if (addr.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
  21. throw new Exception("Bad RegionHandle Parameter - must be an IPv4 address");
  22. uint baseHandle = BitConverter.ToUInt32(addr.GetAddressBytes(), 0);
  23. // Split the IP address in half
  24. short a = (short)((baseHandle << 16) & 0xFFFF);
  25. short b = (short)((baseHandle << 0) & 0xFFFF);
  26. // Raise the bounds a little
  27. uint nx = (uint)x;
  28. uint ny = (uint)y;
  29. // Multiply grid coords to get region coords
  30. nx *= 256;
  31. ny *= 256;
  32. // Stuff the IP address in too
  33. nx = (uint)a << 16;
  34. ny = (uint)b << 16;
  35. handle = ((UInt64)nx << 32) | (uint)ny;
  36. }
  37. /// <summary>
  38. /// Initialises a new RegionHandle that is not inter-grid aware
  39. /// </summary>
  40. /// <param name="x">Grid X Coordinate</param>
  41. /// <param name="y">Grid Y Coordinate</param>
  42. public RegionHandle(uint x, uint y)
  43. {
  44. handle = ((x * 256) << 32) | (y * 256);
  45. }
  46. /// <summary>
  47. /// Initialises a new RegionHandle from an existing value
  48. /// </summary>
  49. /// <param name="Region">A U64 RegionHandle</param>
  50. public RegionHandle(UInt64 Region)
  51. {
  52. handle = Region;
  53. }
  54. /// <summary>
  55. /// Returns the Grid Masked RegionHandle - For use in Teleport packets and other packets where sending the grid IP address may be handy.
  56. /// </summary>
  57. /// <remarks>Do not use for SimulatorEnable packets. The client will choke.</remarks>
  58. /// <returns>Region Handle including IP Address encoding</returns>
  59. public UInt64 getTeleportHandle()
  60. {
  61. return handle;
  62. }
  63. /// <summary>
  64. /// Returns a RegionHandle which may be used for SimulatorEnable packets. Removes the IP address encoding and returns the lower bounds.
  65. /// </summary>
  66. /// <returns>A U64 RegionHandle for use in SimulatorEnable packets.</returns>
  67. public UInt64 getNeighbourHandle()
  68. {
  69. UInt64 mask = 0x0000FFFF0000FFFF;
  70. return handle | mask;
  71. }
  72. /// <summary>
  73. /// Returns the IP Address of the GridServer from a Grid-Encoded RegionHandle
  74. /// </summary>
  75. /// <returns>Grid Server IP Address</returns>
  76. public IPAddress getGridIP()
  77. {
  78. uint a = (uint)((handle >> 16) & 0xFFFF);
  79. uint b = (uint)((handle >> 48) & 0xFFFF);
  80. return new IPAddress((long)(a << 16) | (long)b);
  81. }
  82. /// <summary>
  83. /// Returns the X Coordinate from a Grid-Encoded RegionHandle
  84. /// </summary>
  85. /// <returns>X Coordinate</returns>
  86. public uint getGridX()
  87. {
  88. uint x = (uint)((handle >> 32) & 0xFFFF);
  89. return x;
  90. }
  91. /// <summary>
  92. /// Returns the Y Coordinate from a Grid-Encoded RegionHandle
  93. /// </summary>
  94. /// <returns>Y Coordinate</returns>
  95. public uint getGridY()
  96. {
  97. uint y = (uint)((handle >> 0) & 0xFFFF);
  98. return y;
  99. }
  100. }
  101. }