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