forked from BeardedManStudios/ForgeNetworkingRemastered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
147 lines (136 loc) · 3.7 KB
/
Program.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
using BeardedManStudios;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace MasterServer
{
class Program
{
private static void Main(string[] args)
{
string host = "0.0.0.0";
ushort port = 15940;
string read = string.Empty;
int eloRange = 0;
Dictionary<string, string> arguments = ArgumentParser.Parse(args);
if (args.Length > 0)
{
if (arguments.ContainsKey("host"))
host = arguments["host"];
if (arguments.ContainsKey("port"))
ushort.TryParse(arguments["port"], out port);
if (arguments.ContainsKey("elorange"))
int.TryParse(arguments["elorange"], out eloRange);
}
else
{
Console.WriteLine("Entering nothing will choose defaults.");
Console.WriteLine("Enter Host IP (Default: "+ GetLocalIPAddress() + "):");
read = Console.ReadLine();
if (string.IsNullOrEmpty(read))
host = GetLocalIPAddress();
else
host = read;
Console.WriteLine("Enter Port (Default: 15940):");
read = Console.ReadLine();
if (string.IsNullOrEmpty(read))
port = 15940;
else
{
ushort.TryParse(read, out port);
}
}
Console.WriteLine(string.Format("Hosting ip [{0}] on port [{1}]", host, port));
PrintHelp();
MasterServer server = new MasterServer(host, port);
server.EloRange = eloRange;
server.ToggleLogging();
while (true)
{
read = Console.ReadLine().ToLower();
if (read == "s" || read == "stop")
{
lock (server)
{
Console.WriteLine("Server stopped.");
server.Dispose();
}
}
else if (read == "l" || read == "log")
{
if (server.ToggleLogging())
Console.WriteLine("Logging has been enabled");
else
Console.WriteLine("Logging has been disabled");
}
else if (read == "r" || read == "restart")
{
lock (server)
{
if (server.IsRunning)
{
Console.WriteLine("Server stopped.");
server.Dispose();
}
}
Console.WriteLine("Restarting...");
Console.WriteLine(string.Format("Hosting ip [{0}] on port [{1}]", host, port));
server = new MasterServer(host, port);
}
else if (read == "q" || read == "quit")
{
lock (server)
{
Console.WriteLine("Quitting...");
server.Dispose();
}
break;
}
else if (read == "h" || read == "help")
PrintHelp();
else if (read.StartsWith("elo"))
{
int index = read.IndexOf("=");
string val = read.Substring(index + 1, read.Length - (index + 1));
if (int.TryParse(val.Replace(" ", string.Empty), out index))
{
Console.WriteLine(string.Format("Elo range set to {0}", index));
if (index == 0)
Console.WriteLine("Elo turned off");
server.EloRange = index;
}
else
Console.WriteLine("Invalid elo range provided (Must be an integer)\n");
}
else
Console.WriteLine("Command not recognized, please try again");
}
}
private static void PrintHelp()
{
Console.WriteLine(@"Commands Available
(s)top - Stops hosting
(r)estart - Restarts the hosting service even when stopped
(l)og - Toggles logging (starts enabled)
(q)uit - Quits the application
(h)elp - Get a full list of commands");
}
/// <summary>
/// Return the Local IP-Address
/// </summary>
/// <returns></returns>
private static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
}
}