Adds listening sockets to the configuration file (#172)

This commit is contained in:
Kamron Batman 2020-07-03 20:45:37 -07:00 committed by GitHub
parent e17195ca80
commit f868c34678
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 153 additions and 39 deletions

View file

@ -3,7 +3,7 @@
* Copyright (C) 2019-2020 - ModernUO Development Team * * Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com * * Email: hi@modernuo.com *
* File: ServerConfiguration.cs * * File: ServerConfiguration.cs *
* Created: 2019/10/04 - Updated: 2020/05/09 * * Created: 2019/10/04 - Updated: 2020/07/03 *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
@ -22,6 +22,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Server.Json; using Server.Json;
@ -36,6 +37,8 @@ namespace Server
public static List<string> DataDirectories => m_Settings.dataDirectories; public static List<string> DataDirectories => m_Settings.dataDirectories;
public static List<IPEndPoint> Listeners => m_Settings.listeners;
public static string GetSetting(string key, string defaultValue) public static string GetSetting(string key, string defaultValue)
{ {
m_Settings.settings.TryGetValue(key, out string value); m_Settings.settings.TryGetValue(key, out string value);
@ -167,8 +170,19 @@ namespace Server
if (m_Settings.dataDirectories.Count == 0) if (m_Settings.dataDirectories.Count == 0)
{ {
updated = true; updated = true;
Utility.PushColor(ConsoleColor.DarkYellow);
Console.WriteLine("Core: Server configuration is missing data directories."); Console.WriteLine("Core: Server configuration is missing data directories.");
m_Settings.dataDirectories.Add(GetDataDirectory()); Utility.PopColor();
m_Settings.dataDirectories.AddRange(GetDataDirectories());
}
if (m_Settings.listeners.Count == 0)
{
updated = true;
Utility.PushColor(ConsoleColor.DarkYellow);
Console.WriteLine("Core: Server is missing socket listener IP addresses.");
Utility.PopColor();
m_Settings.listeners.AddRange(GetListeners());
} }
if (updated) if (updated)
@ -185,6 +199,9 @@ namespace Server
[JsonPropertyName("dataDirectories")] [JsonPropertyName("dataDirectories")]
public List<string> dataDirectories { get; set; } = new List<string>(); public List<string> dataDirectories { get; set; } = new List<string>();
[JsonPropertyName("listeners")]
public List<IPEndPoint> listeners { get; set; } = new List<IPEndPoint>();
[JsonPropertyName("settings")] [JsonPropertyName("settings")]
public Dictionary<string, string> settings { get; set; } = new Dictionary<string, string>(); public Dictionary<string, string> settings { get; set; } = new Dictionary<string, string>();
@ -192,18 +209,63 @@ namespace Server
public Dictionary<string, object> metadata { get; set; } = new Dictionary<string, object>(); public Dictionary<string, object> metadata { get; set; } = new Dictionary<string, object>();
} }
private static string GetDataDirectory() private static List<string> GetDataDirectories()
{ {
Console.WriteLine("Please enter the Ultima Online directory:"); Console.WriteLine("Please enter the absolute path to the Ultima Online data:");
List<string> directories = new List<string>();
string directory;
do do
{ {
Console.Write("> "); Console.Write("{0}> ", directories.Count > 0 ? "[finish] " : " ");
directory = Console.ReadLine(); var directory = Console.ReadLine();
} while (!Directory.Exists(directory)); if (string.IsNullOrWhiteSpace(directory)) break;
return directory; if (Directory.Exists(directory))
{
directories.Add(directory);
Console.WriteLine("Core: Path {0} added.", directory);
}
else
Console.WriteLine("Core: Path does not exist. ({0})");
} while (true);
return directories;
}
private static List<IPEndPoint> GetListeners()
{
Console.WriteLine("Please enter the IP and ports to listen:");
Console.WriteLine(" - Only enter IP addresses directly bound to this machine");
Console.WriteLine(" - To listen to all IP addresses enter 0.0.0.0");
List<IPEndPoint> ips = new List<IPEndPoint>();
do
{ // IP:Port?
Console.Write("[{0}]> ", ips.Count > 0 ? "finish" : "0.0.0.0:2593");
var ipStr = Console.ReadLine();
if (string.IsNullOrWhiteSpace(ipStr)) break;
if (ipStr.IndexOf(":", StringComparison.Ordinal) == -1)
ipStr += ":2593";
if (IPEndPoint.TryParse(ipStr, out var ip))
{
ips.Add(ip);
Console.WriteLine("Core: {0} added.", ipStr);
}
else
{
Console.WriteLine("Core: {0} is not a valid IP or port");
}
} while (true);
if (ips.Count == 0)
ips.Add(new IPEndPoint(IPAddress.Any, 2593));
return ips;
} }
public static void Save() public static void Save()

View file

@ -0,0 +1,42 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IPEndPointConverter.cs *
* Created: 2020/07/03 - Updated: 2020/07/03 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class IPEndPointConverter : JsonConverter<IPEndPoint>
{
public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (IPEndPoint.TryParse(reader.GetString(), out var ipep))
return ipep;
throw new JsonException("IPEndPoint must be in the correct format");
}
public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}
}

View file

@ -0,0 +1,35 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IPEndPointConverterFactory.cs *
* Created: 2020/07/03 - Updated: 2020/07/03 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class IPEndPointConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(IPEndPoint);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => new IPEndPointConverter();
}
}

View file

@ -64,7 +64,7 @@ namespace Server.Json
break; break;
if (reader.TokenType != JsonTokenType.PropertyName) if (reader.TokenType != JsonTokenType.PropertyName)
throw new JsonException("Invalid Json structure for Point2D object"); throw new JsonException("Invalid json structure for Point2D object");
var key = reader.GetString(); var key = reader.GetString();

View file

@ -64,7 +64,7 @@ namespace Server.Json
break; break;
if (reader.TokenType != JsonTokenType.PropertyName) if (reader.TokenType != JsonTokenType.PropertyName)
throw new JsonException("Invalid Json structure for Point3D object"); throw new JsonException("Invalid json structure for Point3D object");
var key = reader.GetString(); var key = reader.GetString();

View file

@ -67,7 +67,7 @@ namespace Server.Json
break; break;
if (reader.TokenType != JsonTokenType.PropertyName) if (reader.TokenType != JsonTokenType.PropertyName)
throw new JsonException("Invalid Json structure for Rectangle3D object"); throw new JsonException("Invalid json structure for Rectangle3D object");
var key = reader.GetString(); var key = reader.GetString();

View file

@ -46,6 +46,7 @@ namespace Server.Json
options.Converters.Add(new Point3DConverterFactory()); options.Converters.Add(new Point3DConverterFactory());
options.Converters.Add(new Rectangle3DConverterFactory()); options.Converters.Add(new Rectangle3DConverterFactory());
options.Converters.Add(new TimeSpanConverterFactory()); options.Converters.Add(new TimeSpanConverterFactory());
options.Converters.Add(new IPEndPointConverterFactory());
for (int i = 0; i < converters.Length; i++) options.Converters.Add(converters[i]); for (int i = 0; i < converters.Length; i++) options.Converters.Add(converters[i]);

View file

@ -386,8 +386,6 @@ namespace Server
AssemblyHandler.Invoke("Initialize"); AssemblyHandler.Invoke("Initialize");
AssemblyHandler.Invoke("RegisterListeners");
timerThread.Start(); timerThread.Start();
foreach (var m in Map.AllMaps) foreach (var m in Map.AllMaps)

View file

@ -32,8 +32,6 @@ namespace Server.Network
{ {
public static class TcpServer public static class TcpServer
{ {
public static List<IPEndPoint> Listeners { get; } = new List<IPEndPoint>();
// Make this thread safe // Make this thread safe
public static List<NetState> Instances { get; } = new List<NetState>(); public static List<NetState> Instances { get; } = new List<NetState>();
@ -45,7 +43,7 @@ namespace Server.Network
.ConfigureServices(services => { services.AddSingleton<IMessagePumpService>(new MessagePumpService()); }) .ConfigureServices(services => { services.AddSingleton<IMessagePumpService>(new MessagePumpService()); })
.UseKestrel(options => .UseKestrel(options =>
{ {
foreach (var ipep in Listeners) foreach (var ipep in ServerConfiguration.Listeners)
{ {
options.Listen(ipep, builder => { builder.UseConnectionHandler<ServerConnectionHandler>(); }); options.Listen(ipep, builder => { builder.UseConnectionHandler<ServerConnectionHandler>(); });
m_ListeningAddresses = GetListeningAddresses(ipep); m_ListeningAddresses = GetListeningAddresses(ipep);

View file

@ -1,22 +0,0 @@
using System.Net;
using Server.Network;
namespace Server
{
public class SocketOptions
{
private static readonly IPEndPoint[] m_ListenerEndPoints =
{
new IPEndPoint(IPAddress.Any, 2593) // Default: Listen on port 2593 on all IP addresses
// Examples:
// new IPEndPoint( IPAddress.Any, 80 ), // Listen on port 80 on all IP addresses
// new IPEndPoint( IPAddress.Parse( "1.2.3.4" ), 2593 ), // Listen on port 2593 on IP address 1.2.3.4
};
public static void RegisterListeners()
{
TcpServer.Listeners.AddRange(m_ListenerEndPoints);
}
}
}