forked from sharpot/sharpot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cs
161 lines (138 loc) · 5.3 KB
/
Server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using SharpOT.Util;
using SharpOT.Scripting;
using System.Diagnostics;
namespace SharpOT
{
public class Server
{
static void Main(string[] args)
{
new Server().Run();
}
Game game;
TcpListener clientLoginListener = new TcpListener(IPAddress.Any, 7171);
TcpListener clientGameListener = new TcpListener(IPAddress.Any, 7172);
List<Connection> connections = new List<Connection>();
static int startTimeInMillis = 0;
static int startTextLength = 0;
void Run()
{
game = new Game();
try
{
LogStart("Initializing database");
Database.Initialize(SharpOT.Properties.Settings.Default.DatabaseFile);
LogDone();
LogStart("Loading items");
ItemInfo.LoadItemsOtb(SharpOT.Properties.Settings.Default.ItemsOtbFile);
ItemInfo.LoadItemsXml(SharpOT.Properties.Settings.Default.ItemsXmlFile);
LogDone();
LogStart("Loading map");
game.Map.Load();
LogDone();
LogStart("Loading scripts");
string errors = Scripting.ScriptManager.LoadAllScripts(game);
LogDone();
if (errors.Length > 0)
{
Log("There were errors when compiling scripts:\n\n" + errors);
}
LogStart("Listening for clients");
clientLoginListener.Start();
clientLoginListener.BeginAcceptSocket(new AsyncCallback(LoginListenerCallback), clientLoginListener);
clientGameListener.Start();
clientGameListener.BeginAcceptSocket(new AsyncCallback(GameListenerCallback), clientGameListener);
LogDone();
}
catch (Exception e)
{
LogError(e.ToString());
if (Debugger.IsAttached)
{
throw;
}
}
while (true)
{
bool exit = false;
string line = Console.ReadLine();
switch (line.ToLower())
{
case "exit":
exit = true;
break;
case "reloadscripts":
ScriptManager.ReloadAllScripts(game);
break;
}
if (exit) break;
}
connections.ForEach(c => c.Close());
clientGameListener.Stop();
clientLoginListener.Stop();
}
public static void LogStart(string text)
{
string s = String.Format("{0}: {1}...", DateTime.Now, text);
Console.Write(s);
startTextLength = s.Length;
startTimeInMillis = System.Environment.TickCount;
}
public static void LogDone()
{
int elapsed = System.Environment.TickCount - startTimeInMillis;
string done = "Done";
string doneTime = "";
if (elapsed < 1000)
{
doneTime = String.Format("({0} ms)", elapsed);
}
else
{
doneTime = String.Format("({0:0.00} s)", elapsed / 1000.0);
}
Console.Write(".".Repeat(Console.WindowWidth - startTextLength - done.Length - 12));
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(done);
Console.ResetColor();
Console.Write(" ".Repeat(11 - doneTime.Length));
Console.Write(doneTime);
Console.WriteLine();
}
public static void LogError(string errorText)
{
string error = "Error";
Console.Write(".".Repeat(Console.WindowWidth - startTextLength - error.Length - 12));
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(error);
Console.ResetColor();
Console.WriteLine(errorText);
}
public static void Log(string text, params object[] args)
{
Console.WriteLine("{0}: {1}", DateTime.Now, String.Format(text, args));
}
private void LoginListenerCallback(IAsyncResult ar)
{
Connection connection = new Connection(game);
connection.LoginListenerCallback(ar);
connections.Add(connection);
clientLoginListener.BeginAcceptSocket(new AsyncCallback(LoginListenerCallback), clientLoginListener);
Log("New client connected to login server.");
}
private void GameListenerCallback(IAsyncResult ar)
{
Connection connection = new Connection(game);
connection.GameListenerCallback(ar);
connections.Add(connection);
clientGameListener.BeginAcceptSocket(new AsyncCallback(GameListenerCallback), clientGameListener);
Log("New client connected to game server.");
}
}
}