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