diff options
Switched in NameSpaceChanges
Diffstat (limited to 'OpenSim/Region/ClientStack/UDPServer.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/UDPServer.cs | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/UDPServer.cs b/OpenSim/Region/ClientStack/UDPServer.cs new file mode 100644 index 0000000..259352c --- /dev/null +++ b/OpenSim/Region/ClientStack/UDPServer.cs | |||
@@ -0,0 +1,207 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.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.Text; | ||
30 | using System.IO; | ||
31 | using System.Threading; | ||
32 | using System.Net; | ||
33 | using System.Net.Sockets; | ||
34 | using System.Timers; | ||
35 | using System.Reflection; | ||
36 | using System.Collections; | ||
37 | using System.Collections.Generic; | ||
38 | using libsecondlife; | ||
39 | using libsecondlife.Packets; | ||
40 | using OpenSim.Region.Terrain; | ||
41 | using OpenSim.Framework.Interfaces; | ||
42 | using OpenSim.Framework.Types; | ||
43 | using OpenSim.Assets; | ||
44 | using OpenSim.Region.Caches; | ||
45 | using OpenSim.Framework.Console; | ||
46 | using OpenSim.Framework; | ||
47 | using Nwc.XmlRpc; | ||
48 | using OpenSim.Framework.Servers; | ||
49 | |||
50 | namespace OpenSim.Region.ClientStack | ||
51 | { | ||
52 | |||
53 | public class UDPServer : ClientStackNetworkHandler | ||
54 | { | ||
55 | protected Dictionary<EndPoint, uint> clientCircuits = new Dictionary<EndPoint, uint>(); | ||
56 | public Socket Server; | ||
57 | protected IPEndPoint ServerIncoming; | ||
58 | protected byte[] RecvBuffer = new byte[4096]; | ||
59 | protected byte[] ZeroBuffer = new byte[8192]; | ||
60 | protected IPEndPoint ipeSender; | ||
61 | protected EndPoint epSender; | ||
62 | protected AsyncCallback ReceivedData; | ||
63 | protected PacketServer _packetServer; | ||
64 | |||
65 | protected int listenPort; | ||
66 | protected IWorld m_localWorld; | ||
67 | protected AssetCache m_assetCache; | ||
68 | protected InventoryCache m_inventoryCache; | ||
69 | protected LogBase m_log; | ||
70 | protected AuthenticateSessionsBase m_authenticateSessionsClass; | ||
71 | |||
72 | public PacketServer PacketServer | ||
73 | { | ||
74 | get | ||
75 | { | ||
76 | return _packetServer; | ||
77 | } | ||
78 | set | ||
79 | { | ||
80 | _packetServer = value; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | public IWorld LocalWorld | ||
85 | { | ||
86 | set | ||
87 | { | ||
88 | this.m_localWorld = value; | ||
89 | this._packetServer.LocalWorld = this.m_localWorld; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | public UDPServer() | ||
94 | { | ||
95 | } | ||
96 | |||
97 | public UDPServer(int port, AssetCache assetCache, InventoryCache inventoryCache, LogBase console, AuthenticateSessionsBase authenticateClass) | ||
98 | { | ||
99 | listenPort = port; | ||
100 | this.m_assetCache = assetCache; | ||
101 | this.m_inventoryCache = inventoryCache; | ||
102 | this.m_log = console; | ||
103 | this.m_authenticateSessionsClass = authenticateClass; | ||
104 | this.CreatePacketServer(); | ||
105 | |||
106 | } | ||
107 | |||
108 | protected virtual void CreatePacketServer() | ||
109 | { | ||
110 | PacketServer packetServer = new PacketServer(this, (uint) listenPort); | ||
111 | } | ||
112 | |||
113 | protected virtual void OnReceivedData(IAsyncResult result) | ||
114 | { | ||
115 | ipeSender = new IPEndPoint(IPAddress.Any, 0); | ||
116 | epSender = (EndPoint)ipeSender; | ||
117 | Packet packet = null; | ||
118 | int numBytes = Server.EndReceiveFrom(result, ref epSender); | ||
119 | int packetEnd = numBytes - 1; | ||
120 | |||
121 | packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer); | ||
122 | |||
123 | // do we already have a circuit for this endpoint | ||
124 | if (this.clientCircuits.ContainsKey(epSender)) | ||
125 | { | ||
126 | //if so then send packet to the packetserver | ||
127 | this._packetServer.ClientInPacket(this.clientCircuits[epSender], packet); | ||
128 | } | ||
129 | else if (packet.Type == PacketType.UseCircuitCode) | ||
130 | { | ||
131 | // new client | ||
132 | this.AddNewClient(packet); | ||
133 | } | ||
134 | else | ||
135 | { // invalid client | ||
136 | m_log.Warn("UDPServer.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString()); | ||
137 | } | ||
138 | |||
139 | Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null); | ||
140 | } | ||
141 | |||
142 | protected virtual void AddNewClient(Packet packet) | ||
143 | { | ||
144 | UseCircuitCodePacket useCircuit = (UseCircuitCodePacket)packet; | ||
145 | this.clientCircuits.Add(epSender, useCircuit.CircuitCode.Code); | ||
146 | |||
147 | this.PacketServer.AddNewClient(epSender, useCircuit, m_assetCache, m_inventoryCache, m_authenticateSessionsClass); | ||
148 | } | ||
149 | |||
150 | public void ServerListener() | ||
151 | { | ||
152 | m_log.Status("UDPServer.cs:ServerListener() - Opening UDP socket on " + listenPort); | ||
153 | |||
154 | ServerIncoming = new IPEndPoint(IPAddress.Any, listenPort); | ||
155 | Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | ||
156 | Server.Bind(ServerIncoming); | ||
157 | |||
158 | m_log.Verbose("UDPServer.cs:ServerListener() - UDP socket bound, getting ready to listen"); | ||
159 | |||
160 | ipeSender = new IPEndPoint(IPAddress.Any, 0); | ||
161 | epSender = (EndPoint)ipeSender; | ||
162 | ReceivedData = new AsyncCallback(this.OnReceivedData); | ||
163 | Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null); | ||
164 | |||
165 | m_log.Verbose("UDPServer.cs:ServerListener() - Listening..."); | ||
166 | |||
167 | } | ||
168 | |||
169 | public virtual void RegisterPacketServer(PacketServer server) | ||
170 | { | ||
171 | this._packetServer = server; | ||
172 | } | ||
173 | |||
174 | public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)//EndPoint packetSender) | ||
175 | { | ||
176 | // find the endpoint for this circuit | ||
177 | EndPoint sendto = null; | ||
178 | foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits) | ||
179 | { | ||
180 | if (p.Value == circuitcode) | ||
181 | { | ||
182 | sendto = p.Key; | ||
183 | break; | ||
184 | } | ||
185 | } | ||
186 | if (sendto != null) | ||
187 | { | ||
188 | //we found the endpoint so send the packet to it | ||
189 | this.Server.SendTo(buffer, size, flags, sendto); | ||
190 | } | ||
191 | } | ||
192 | |||
193 | public virtual void RemoveClientCircuit(uint circuitcode) | ||
194 | { | ||
195 | foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits) | ||
196 | { | ||
197 | if (p.Value == circuitcode) | ||
198 | { | ||
199 | this.clientCircuits.Remove(p.Key); | ||
200 | break; | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | |||
205 | |||
206 | } | ||
207 | } \ No newline at end of file | ||