aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/Main.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Main.cs')
-rw-r--r--src/Main.cs138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/Main.cs b/src/Main.cs
new file mode 100644
index 0000000..da926db
--- /dev/null
+++ b/src/Main.cs
@@ -0,0 +1,138 @@
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.Collections;
37using System.Collections.Generic;
38using libsecondlife;
39using libsecondlife.Packets;
40using OpenSim.world;
41
42namespace OpenSim
43{
44 /// <summary>
45 /// Description of MainForm.
46 /// </summary>
47 public class OpenSim_Main
48 {
49 private static OpenSim_Main sim;
50 public static SimConfig cfg;
51 public static World local_world;
52 private static Thread MainListener;
53 private static Thread PingRespponder;
54 public static Socket Server;
55 private static IPEndPoint ServerIncoming;
56 private static byte[] RecvBuffer = new byte[4096];
57 private byte[] ZeroBuffer = new byte[8192];
58 private static IPEndPoint ipeSender;
59 private static EndPoint epSender;
60 private static AsyncCallback ReceivedData;
61 public Dictionary<EndPoint, OpenSimClient> ClientThreads = new Dictionary<EndPoint, OpenSimClient>();
62
63 [STAThread]
64 public static void Main( string[] args )
65 {
66 Console.WriteLine("OpenSim " + VersionInfo.Version + "\n");
67 Console.WriteLine("Starting...\n");
68 sim = new OpenSim_Main();
69 sim.Startup();
70 while(true) {
71 Thread.Sleep(1000);
72 }
73 }
74
75 private OpenSim_Main() {
76 }
77
78 private void Startup() {
79 // We check our local database first, then the grid for config options
80 Console.WriteLine("Main.cs:Startup() - Loading configuration");
81 cfg = new SimConfig();
82 cfg.InitConfig();
83 Console.WriteLine("Main.cs:Startup() - Contacting gridserver");
84 cfg.LoadFromGrid();
85
86 Console.WriteLine("Main.cs:Startup() - We are " + cfg.RegionName + " at " + cfg.RegionLocX.ToString() + "," + cfg.RegionLocY.ToString());
87 Console.WriteLine("Initialising world");
88 local_world = cfg.LoadWorld();
89
90 Console.WriteLine("Main.cs:Startup() - Starting up messaging system");
91 MainListener = new Thread(new ThreadStart(MainServerListener));
92 MainListener.Start();
93
94 }
95
96 private void OnReceivedData(IAsyncResult result) {
97 ipeSender = new IPEndPoint(IPAddress.Any, 0);
98 epSender = (EndPoint)ipeSender;
99 Packet packet = null;
100 int numBytes = Server.EndReceiveFrom(result, ref epSender);
101 int packetEnd = numBytes - 1;
102 packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
103 Console.Error.WriteLine(packet.ToString());
104
105 // This is either a new client or a packet to send to an old one
106 if(ClientThreads.ContainsKey(epSender)) {
107 ClientThreads[epSender].InPacket(packet);
108 } else if( packet.Type == PacketType.UseCircuitCode ) { // new client
109 OpenSimClient newuser = new OpenSimClient(epSender,(UseCircuitCodePacket)packet);
110 ClientThreads.Add(epSender, newuser);
111 } else { // invalid client
112 Console.Error.WriteLine("Main.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString());
113 }
114 Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
115 }
116
117 private void MainServerListener() {
118 Console.WriteLine("Main.cs:MainServerListener() - New thread started");
119 Console.WriteLine("Main.cs:MainServerListener() - Opening UDP socket on " + cfg.IPListenAddr + ":" + cfg.IPListenPort);
120
121 ServerIncoming = new IPEndPoint(IPAddress.Parse(cfg.IPListenAddr),cfg.IPListenPort);
122 Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
123 Server.Bind(ServerIncoming);
124
125 Console.WriteLine("Main.cs:MainServerListener() - UDP socket bound, getting ready to listen");
126
127 ipeSender = new IPEndPoint(IPAddress.Any, 0);
128 epSender = (EndPoint) ipeSender;
129 ReceivedData = new AsyncCallback(this.OnReceivedData);
130 Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
131
132 Console.WriteLine("Main.cs:MainServerListener() - Listening...");
133 while(true) {
134 Thread.Sleep(1000);
135 }
136 }
137 }
138}