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