diff options
author | MW | 2007-05-16 12:44:58 +0000 |
---|---|---|
committer | MW | 2007-05-16 12:44:58 +0000 |
commit | 3d93d390485075345442b9b5ea35eed88a9cab49 (patch) | |
tree | a51cdb72cd310ae3f579650886836ddc0211d986 /OpenSim.RegionServer/UDPServer.cs | |
parent | made QueItem a nested class in SimClient. (diff) | |
download | opensim-SC-3d93d390485075345442b9b5ea35eed88a9cab49.zip opensim-SC-3d93d390485075345442b9b5ea35eed88a9cab49.tar.gz opensim-SC-3d93d390485075345442b9b5ea35eed88a9cab49.tar.bz2 opensim-SC-3d93d390485075345442b9b5ea35eed88a9cab49.tar.xz |
started to refactor startup code and to move udp server code out to its own class (currently not being used though) so that a single instance can handle multiple regions (each will need to be listening on a separate udp port)
Diffstat (limited to '')
-rw-r--r-- | OpenSim.RegionServer/UDPServer.cs | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/OpenSim.RegionServer/UDPServer.cs b/OpenSim.RegionServer/UDPServer.cs new file mode 100644 index 0000000..e70ae78 --- /dev/null +++ b/OpenSim.RegionServer/UDPServer.cs | |||
@@ -0,0 +1,164 @@ | |||
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 class UDPServer : OpenSimNetworkHandler | ||
28 | { | ||
29 | private Dictionary<EndPoint, uint> clientCircuits = new Dictionary<EndPoint, uint>(); | ||
30 | public Socket Server; | ||
31 | private IPEndPoint ServerIncoming; | ||
32 | private byte[] RecvBuffer = new byte[4096]; | ||
33 | private byte[] ZeroBuffer = new byte[8192]; | ||
34 | private IPEndPoint ipeSender; | ||
35 | private EndPoint epSender; | ||
36 | private AsyncCallback ReceivedData; | ||
37 | private PacketServer _packetServer; | ||
38 | |||
39 | private int listenPort; | ||
40 | private Grid m_gridServers; | ||
41 | private World m_localWorld; | ||
42 | private AssetCache m_assetCache; | ||
43 | private InventoryCache m_inventoryCache; | ||
44 | private RegionInfo m_regionData; | ||
45 | private bool m_sandbox = false; | ||
46 | private bool user_accounts = false; | ||
47 | private ConsoleBase m_console; | ||
48 | |||
49 | public UDPServer(int port, Grid gridServers, World localWorld, AssetCache assetCache, InventoryCache inventoryCache, RegionInfo _regionData, bool sandbox, bool accounts, ConsoleBase console) | ||
50 | { | ||
51 | listenPort = port; | ||
52 | this.m_gridServers = gridServers; | ||
53 | this.m_localWorld = localWorld; | ||
54 | this.m_assetCache = assetCache; | ||
55 | this.m_inventoryCache = inventoryCache; | ||
56 | this.m_regionData = _regionData; | ||
57 | this.m_sandbox = sandbox; | ||
58 | this.user_accounts = accounts; | ||
59 | this.m_console = console; | ||
60 | PacketServer packetServer = new PacketServer(this); | ||
61 | this._packetServer.LocalWorld = m_localWorld; | ||
62 | this._packetServer.RegisterClientPacketHandlers(); | ||
63 | } | ||
64 | |||
65 | protected virtual void OnReceivedData(IAsyncResult result) | ||
66 | { | ||
67 | ipeSender = new IPEndPoint(IPAddress.Any, 0); | ||
68 | epSender = (EndPoint)ipeSender; | ||
69 | Packet packet = null; | ||
70 | int numBytes = Server.EndReceiveFrom(result, ref epSender); | ||
71 | int packetEnd = numBytes - 1; | ||
72 | |||
73 | packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer); | ||
74 | |||
75 | // do we already have a circuit for this endpoint | ||
76 | if (this.clientCircuits.ContainsKey(epSender)) | ||
77 | { | ||
78 | //if so then send packet to the packetserver | ||
79 | this._packetServer.ClientInPacket(this.clientCircuits[epSender], packet); | ||
80 | } | ||
81 | else if (packet.Type == PacketType.UseCircuitCode) | ||
82 | { | ||
83 | // new client | ||
84 | this.AddNewClient(packet); | ||
85 | } | ||
86 | else | ||
87 | { // invalid client | ||
88 | Console.Error.WriteLine("Main.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString()); | ||
89 | } | ||
90 | |||
91 | Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null); | ||
92 | } | ||
93 | |||
94 | protected virtual void AddNewClient(Packet packet) | ||
95 | { | ||
96 | UseCircuitCodePacket useCircuit = (UseCircuitCodePacket)packet; | ||
97 | this.clientCircuits.Add(epSender, useCircuit.CircuitCode.Code); | ||
98 | bool isChildAgent = false; | ||
99 | |||
100 | SimClient newuser = new SimClient(epSender, useCircuit, m_localWorld, _packetServer.ClientThreads, m_assetCache, m_gridServers.GridServer, this, m_inventoryCache, m_sandbox, isChildAgent, this.m_regionData); | ||
101 | if ((this.m_gridServers.UserServer != null) && (user_accounts)) | ||
102 | { | ||
103 | newuser.UserServer = this.m_gridServers.UserServer; | ||
104 | } | ||
105 | //OpenSimRoot.Instance.ClientThreads.Add(epSender, newuser); | ||
106 | this._packetServer.ClientThreads.Add(useCircuit.CircuitCode.Code, newuser); | ||
107 | } | ||
108 | |||
109 | private void ServerListener() | ||
110 | { | ||
111 | m_console.WriteLine("Main.cs:MainServerListener() - Opening UDP socket on " + listenPort); | ||
112 | |||
113 | ServerIncoming = new IPEndPoint(IPAddress.Any, listenPort); | ||
114 | Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | ||
115 | Server.Bind(ServerIncoming); | ||
116 | |||
117 | m_console.WriteLine("Main.cs:MainServerListener() - UDP socket bound, getting ready to listen"); | ||
118 | |||
119 | ipeSender = new IPEndPoint(IPAddress.Any, 0); | ||
120 | epSender = (EndPoint)ipeSender; | ||
121 | ReceivedData = new AsyncCallback(this.OnReceivedData); | ||
122 | Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null); | ||
123 | |||
124 | m_console.WriteLine("Main.cs:MainServerListener() - Listening..."); | ||
125 | |||
126 | } | ||
127 | |||
128 | public void RegisterPacketServer(PacketServer server) | ||
129 | { | ||
130 | this._packetServer = server; | ||
131 | } | ||
132 | |||
133 | public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)//EndPoint packetSender) | ||
134 | { | ||
135 | // find the endpoint for this circuit | ||
136 | EndPoint sendto = null; | ||
137 | foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits) | ||
138 | { | ||
139 | if (p.Value == circuitcode) | ||
140 | { | ||
141 | sendto = p.Key; | ||
142 | break; | ||
143 | } | ||
144 | } | ||
145 | if (sendto != null) | ||
146 | { | ||
147 | //we found the endpoint so send the packet to it | ||
148 | this.Server.SendTo(buffer, size, flags, sendto); | ||
149 | } | ||
150 | } | ||
151 | |||
152 | public virtual void RemoveClientCircuit(uint circuitcode) | ||
153 | { | ||
154 | foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits) | ||
155 | { | ||
156 | if (p.Value == circuitcode) | ||
157 | { | ||
158 | this.clientCircuits.Remove(p.Key); | ||
159 | break; | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | } \ No newline at end of file | ||