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