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