Main.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright (c) OpenSim project, http://osgrid.org/
  3. * All rights reserved.
  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 <organization> 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 <copyright holder> ``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 <copyright holder> 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. using System;
  28. using System.Text;
  29. using System.IO;
  30. using System.Threading;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Timers;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using libsecondlife;
  37. using libsecondlife.Packets;
  38. using OpenSim.world;
  39. using OpenSim.GridServers;
  40. using OpenSim.Assets;
  41. using PhysicsSystem;
  42. namespace OpenSim
  43. {
  44. /// <summary>
  45. /// Description of MainForm.
  46. /// </summary>
  47. public class OpenSim_Main
  48. {
  49. public static OpenSim_Main sim;
  50. public static SimConfig cfg;
  51. public static World local_world;
  52. public static Grid gridServers;
  53. public static AssetCache assetCache;
  54. //private static Thread MainListener;
  55. //private static Thread PingRespponder;
  56. public static Socket Server;
  57. private static IPEndPoint ServerIncoming;
  58. private static byte[] RecvBuffer = new byte[4096];
  59. private byte[] ZeroBuffer = new byte[8192];
  60. private static IPEndPoint ipeSender;
  61. private static EndPoint epSender;
  62. private static AsyncCallback ReceivedData;
  63. public Dictionary<EndPoint, OpenSimClient> ClientThreads = new Dictionary<EndPoint, OpenSimClient>();
  64. private PhysicsManager physManager;
  65. private System.Timers.Timer timer1 = new System.Timers.Timer();
  66. [STAThread]
  67. public static void Main( string[] args )
  68. {
  69. //Console.WriteLine("OpenSim " + VersionInfo.Version + "\n");
  70. Console.WriteLine("Starting...\n");
  71. sim = new OpenSim_Main();
  72. bool sandbox = false;
  73. bool loginserver = false;
  74. for (int i = 0; i < args.Length; i++)
  75. {
  76. if(args[i] == "-sandbox")
  77. {
  78. sandbox = true;
  79. }
  80. if(args[i] == "-loginserver")
  81. {
  82. loginserver = true;
  83. }
  84. }
  85. if(sandbox)
  86. {
  87. OpenSim_Main.gridServers = new Grid(true);
  88. Console.WriteLine("Starting in Sandbox mode");
  89. }
  90. else
  91. {
  92. OpenSim_Main.gridServers = new Grid(false);
  93. Console.WriteLine("Starting in Grid mode");
  94. }
  95. if(loginserver && sandbox)
  96. {
  97. LoginServer loginServer = new LoginServer(OpenSim_Main.gridServers.GridServer);
  98. loginServer.Startup();
  99. }
  100. assetCache = new AssetCache(OpenSim_Main.gridServers.AssetServer);
  101. sim.Startup();
  102. while(true) {
  103. Thread.Sleep(1000);
  104. }
  105. }
  106. private OpenSim_Main() {
  107. }
  108. private void Startup() {
  109. timer1.Enabled = true;
  110. timer1.Interval = 100;
  111. timer1.Elapsed +=new ElapsedEventHandler( this.Timer1Tick );
  112. // We check our local database first, then the grid for config options
  113. Console.WriteLine("Main.cs:Startup() - Loading configuration");
  114. cfg = new SimConfig();
  115. cfg.InitConfig();
  116. Console.WriteLine("Main.cs:Startup() - Contacting gridserver");
  117. cfg.LoadFromGrid();
  118. Console.WriteLine("Main.cs:Startup() - We are " + cfg.RegionName + " at " + cfg.RegionLocX.ToString() + "," + cfg.RegionLocY.ToString());
  119. Console.WriteLine("Initialising world");
  120. local_world = cfg.LoadWorld();
  121. this.physManager = new PhysicsSystem.PhysicsManager();
  122. this.physManager.LoadPlugins();
  123. Console.WriteLine("Main.cs:Startup() - Starting up messaging system");
  124. local_world.PhysScene = this.physManager.GetPhysicsScene("PhysX"); //should be reading from the config file what physics engine to use
  125. MainServerListener();
  126. }
  127. private void OnReceivedData(IAsyncResult result) {
  128. ipeSender = new IPEndPoint(IPAddress.Any, 0);
  129. epSender = (EndPoint)ipeSender;
  130. Packet packet = null;
  131. int numBytes = Server.EndReceiveFrom(result, ref epSender);
  132. int packetEnd = numBytes - 1;
  133. packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
  134. Console.Error.WriteLine(packet.ToString());
  135. // This is either a new client or a packet to send to an old one
  136. if(ClientThreads.ContainsKey(epSender)) {
  137. ClientThreads[epSender].InPacket(packet);
  138. } else if( packet.Type == PacketType.UseCircuitCode ) { // new client
  139. OpenSimClient newuser = new OpenSimClient(epSender,(UseCircuitCodePacket)packet);
  140. ClientThreads.Add(epSender, newuser);
  141. } else { // invalid client
  142. Console.Error.WriteLine("Main.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString());
  143. }
  144. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  145. }
  146. private void MainServerListener() {
  147. Console.WriteLine("Main.cs:MainServerListener() - New thread started");
  148. Console.WriteLine("Main.cs:MainServerListener() - Opening UDP socket on " + cfg.IPListenAddr + ":" + cfg.IPListenPort);
  149. ServerIncoming = new IPEndPoint(IPAddress.Any, cfg.IPListenPort);
  150. Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  151. Server.Bind(ServerIncoming);
  152. Console.WriteLine("Main.cs:MainServerListener() - UDP socket bound, getting ready to listen");
  153. ipeSender = new IPEndPoint(IPAddress.Any, 0);
  154. epSender = (EndPoint) ipeSender;
  155. ReceivedData = new AsyncCallback(this.OnReceivedData);
  156. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  157. Console.WriteLine("Main.cs:MainServerListener() - Listening...");
  158. }
  159. void Timer1Tick( object sender, System.EventArgs e )
  160. {
  161. local_world.Update();
  162. }
  163. }
  164. public class Grid
  165. {
  166. public IAssetServer AssetServer;
  167. public IGridServer GridServer;
  168. public Grid(bool sandbox)
  169. {
  170. if(sandbox)
  171. {
  172. this.AssetServer =(IAssetServer) new LocalAssetServer(); //assets not implemented yet
  173. this.GridServer =(IGridServer) new LocalGridServer();
  174. }
  175. else
  176. {
  177. this.AssetServer =(IAssetServer) new RemoteAssetServer(); //assets not implemented yet
  178. this.GridServer =(IGridServer) new RemoteGridServer();
  179. }
  180. }
  181. }
  182. }