aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/Main.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Main.cs')
-rw-r--r--src/Main.cs317
1 files changed, 317 insertions, 0 deletions
diff --git a/src/Main.cs b/src/Main.cs
new file mode 100644
index 0000000..4fc7d54
--- /dev/null
+++ b/src/Main.cs
@@ -0,0 +1,317 @@
1/*
2Copyright (c) OpenSim project, http://osgrid.org/
3
4
5* All rights reserved.
6*
7* Redistribution and use in source and binary forms, with or without
8* modification, are permitted provided that the following conditions are met:
9* * Redistributions of source code must retain the above copyright
10* notice, this list of conditions and the following disclaimer.
11* * Redistributions in binary form must reproduce the above copyright
12* notice, this list of conditions and the following disclaimer in the
13* documentation and/or other materials provided with the distribution.
14* * Neither the name of the <organization> nor the
15* names of its contributors may be used to endorse or promote products
16* derived from this software without specific prior written permission.
17*
18* THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
19* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
22* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30using System;
31using System.Text;
32using System.IO;
33using System.Threading;
34using System.Net;
35using System.Net.Sockets;
36using System.Timers;
37using System.Reflection;
38using System.Collections;
39using System.Collections.Generic;
40using libsecondlife;
41using libsecondlife.Packets;
42using OpenSim.world;
43using OpenSim.GridServers;
44using OpenSim.Assets;
45using ServerConsole;
46using PhysicsSystem;
47
48namespace OpenSim
49{
50 /// <summary>
51 /// Description of MainForm.
52 /// </summary>
53 public class OpenSim_Main
54 {
55 public static OpenSim_Main sim;
56 public static SimConfig cfg;
57 public static World local_world;
58 public static Grid gridServers;
59
60 public static Socket Server;
61 private static IPEndPoint ServerIncoming;
62 private static byte[] RecvBuffer = new byte[4096];
63 private byte[] ZeroBuffer = new byte[8192];
64 private static IPEndPoint ipeSender;
65 private static EndPoint epSender;
66 private static AsyncCallback ReceivedData;
67
68 public AssetCache assetCache;
69 public DateTime startuptime;
70 public Dictionary<EndPoint, OpenSimClient> ClientThreads = new Dictionary<EndPoint, OpenSimClient>();
71 private PhysicsManager physManager;
72 private System.Timers.Timer timer1 = new System.Timers.Timer();
73 private string ConfigDll = "SimConfig.dll";
74 public bool sandbox = false;
75 public bool loginserver = false;
76
77 [STAThread]
78 public static void Main( string[] args )
79 {
80 Console.WriteLine("OpenSim " + VersionInfo.Version + "\n");
81 Console.WriteLine("Starting...\n");
82 ServerConsole.MainConsole.Instance = new MServerConsole(ServerConsole.ConsoleBase.ConsoleType.Local,"",0);
83
84 sim = new OpenSim_Main();
85
86 for (int i = 0; i < args.Length; i++)
87 {
88 if(args[i] == "-sandbox")
89 {
90 sim.sandbox = true;
91 }
92 if(args[i] == "-loginserver")
93 {
94 sim.loginserver = true;
95 }
96 }
97
98 OpenSim_Main.gridServers = new Grid();
99 if(sim.sandbox)
100 {
101 OpenSim_Main.gridServers.AssetDll = "LocalGridServers.dll";
102 OpenSim_Main.gridServers.GridDll = "LocalGridServers.dll";
103 OpenSim_Main.gridServers.LoadPlugins();
104 ServerConsole.MainConsole.Instance.WriteLine("Starting in Sandbox mode");
105 }
106 else
107 {
108 OpenSim_Main.gridServers.AssetDll = "RemoteGridServers.dll";
109 OpenSim_Main.gridServers.GridDll = "RemoteGridServers.dll";
110 OpenSim_Main.gridServers.LoadPlugins();
111 ServerConsole.MainConsole.Instance.WriteLine("Starting in Grid mode");
112 }
113
114 if(sim.loginserver && sim.sandbox)
115 {
116 LoginServer loginServer = new LoginServer(OpenSim_Main.gridServers.GridServer);
117 loginServer.Startup();
118 }
119 sim.assetCache = new AssetCache(OpenSim_Main.gridServers.AssetServer);
120
121 sim.Startup();
122
123 while(true) {
124 ServerConsole.MainConsole.Instance.MainConsolePrompt();
125 }
126 }
127
128 private OpenSim_Main() {
129 }
130
131 private void Startup() {
132 startuptime=DateTime.Now;
133 timer1.Enabled = true;
134 timer1.Interval = 100;
135 timer1.Elapsed +=new ElapsedEventHandler( this.Timer1Tick );
136
137 // We check our local database first, then the grid for config options
138 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Startup() - Loading configuration");
139 cfg = this.LoadConfigDll(this.ConfigDll);
140 cfg.InitConfig();
141 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Startup() - Contacting gridserver");
142 cfg.LoadFromGrid();
143
144 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Startup() - We are " + cfg.RegionName + " at " + cfg.RegionLocX.ToString() + "," + cfg.RegionLocY.ToString());
145 ServerConsole.MainConsole.Instance.WriteLine("Initialising world");
146 local_world = cfg.LoadWorld();
147
148 this.physManager = new PhysicsSystem.PhysicsManager();
149 this.physManager.LoadPlugins();
150 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Startup() - Starting up messaging system");
151 local_world.PhysScene = this.physManager.GetPhysicsScene("PhysX"); //should be reading from the config file what physics engine to use
152 local_world.PhysScene.SetTerrain(local_world.LandMap);
153 OpenSim_Main.gridServers.AssetServer.SetServerInfo(OpenSim_Main.cfg.AssetURL, OpenSim_Main.cfg.AssetSendKey);
154 OpenSim_Main.gridServers.GridServer.SetServerInfo(OpenSim_Main.cfg.GridURL, OpenSim_Main.cfg.GridSendKey);
155
156 MainServerListener();
157
158 }
159
160 private SimConfig LoadConfigDll(string dllName)
161 {
162 Assembly pluginAssembly = Assembly.LoadFrom(dllName);
163 SimConfig config = null;
164
165 foreach (Type pluginType in pluginAssembly.GetTypes())
166 {
167 if (pluginType.IsPublic)
168 {
169 if (!pluginType.IsAbstract)
170 {
171 Type typeInterface = pluginType.GetInterface("ISimConfig", true);
172
173 if (typeInterface != null)
174 {
175 ISimConfig plug = (ISimConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
176 config = plug.GetConfigObject();
177 break;
178 }
179
180 typeInterface = null;
181 }
182 }
183 }
184 pluginAssembly = null;
185 return config;
186 }
187
188 private void OnReceivedData(IAsyncResult result) {
189 ipeSender = new IPEndPoint(IPAddress.Any, 0);
190 epSender = (EndPoint)ipeSender;
191 Packet packet = null;
192 int numBytes = Server.EndReceiveFrom(result, ref epSender);
193 int packetEnd = numBytes - 1;
194 packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
195
196 // This is either a new client or a packet to send to an old one
197 if(ClientThreads.ContainsKey(epSender)) {
198 ClientThreads[epSender].InPacket(packet);
199 } else if( packet.Type == PacketType.UseCircuitCode ) { // new client
200 OpenSimClient newuser = new OpenSimClient(epSender,(UseCircuitCodePacket)packet);
201 ClientThreads.Add(epSender, newuser);
202 } else { // invalid client
203 Console.Error.WriteLine("Main.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString());
204 }
205 Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
206 }
207
208 private void MainServerListener() {
209 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:MainServerListener() - New thread started");
210 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:MainServerListener() - Opening UDP socket on " + cfg.IPListenAddr + ":" + cfg.IPListenPort);
211
212 ServerIncoming = new IPEndPoint(IPAddress.Any, cfg.IPListenPort);
213 Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
214 Server.Bind(ServerIncoming);
215
216 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:MainServerListener() - UDP socket bound, getting ready to listen");
217
218 ipeSender = new IPEndPoint(IPAddress.Any, 0);
219 epSender = (EndPoint) ipeSender;
220 ReceivedData = new AsyncCallback(this.OnReceivedData);
221 Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
222
223 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:MainServerListener() - Listening...");
224
225 }
226
227 public static void Shutdown() {
228 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Shutdown() - Closing all threads");
229 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Shutdown() - Killing listener thread");
230 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Shutdown() - Killing clients");
231 // IMPLEMENT THIS
232 ServerConsole.MainConsole.Instance.WriteLine("Main.cs:Shutdown() - Closing console and terminating");
233 ServerConsole.MainConsole.Instance.Close();
234 Environment.Exit(0);
235 }
236
237 void Timer1Tick( object sender, System.EventArgs e )
238 {
239
240 local_world.Update();
241 }
242 }
243
244 public class Grid
245 {
246 public IAssetServer AssetServer;
247 public IGridServer GridServer;
248 public string AssetDll = "";
249 public string GridDll = "";
250
251 public Grid()
252 {
253 }
254
255 public void LoadPlugins()
256 {
257 this.AssetServer = this.LoadAssetDll(this.AssetDll);
258 this.GridServer = this.LoadGridDll(this.GridDll);
259 }
260
261 private IAssetServer LoadAssetDll(string dllName)
262 {
263 Assembly pluginAssembly = Assembly.LoadFrom(dllName);
264 IAssetServer server = null;
265
266 foreach (Type pluginType in pluginAssembly.GetTypes())
267 {
268 if (pluginType.IsPublic)
269 {
270 if (!pluginType.IsAbstract)
271 {
272 Type typeInterface = pluginType.GetInterface("IAssetPlugin", true);
273
274 if (typeInterface != null)
275 {
276 IAssetPlugin plug = (IAssetPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
277 server = plug.GetAssetServer();
278 break;
279 }
280
281 typeInterface = null;
282 }
283 }
284 }
285 pluginAssembly = null;
286 return server;
287 }
288
289 private IGridServer LoadGridDll(string dllName)
290 {
291 Assembly pluginAssembly = Assembly.LoadFrom(dllName);
292 IGridServer server = null;
293
294 foreach (Type pluginType in pluginAssembly.GetTypes())
295 {
296 if (pluginType.IsPublic)
297 {
298 if (!pluginType.IsAbstract)
299 {
300 Type typeInterface = pluginType.GetInterface("IGridPlugin", true);
301
302 if (typeInterface != null)
303 {
304 IGridPlugin plug = (IGridPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
305 server = plug.GetGridServer();
306 break;
307 }
308
309 typeInterface = null;
310 }
311 }
312 }
313 pluginAssembly = null;
314 return server;
315 }
316 }
317}