Add config for server.

This commit is contained in:
Ray Koopa 2019-01-04 19:14:55 +01:00
parent 1d02d3834b
commit 1a5bbe714f
8 changed files with 90 additions and 33 deletions

View File

@ -35,9 +35,9 @@ namespace Syroot.Worms.OnlineWorms.Server
{
SendPacket(new ConnectReply
{
Unknown = _server.Name,
Unknown2 = _server.RegionName,
Version = _server.Version
Unknown = _server.Config.Name,
Unknown2 = _server.Config.Region,
Version = _server.Config.Version
});
}
@ -53,7 +53,6 @@ namespace Syroot.Worms.OnlineWorms.Server
}
SendPacket(new LoginReply
{
Unknown1 = 1,
Result = LoginResult.Success,
PlayerInfos = playerInfos
});
@ -72,7 +71,7 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo
{
Name = "Test Channel",
EndPoint = new IPEndPoint(IPAddress.Loopback, 1),
EndPoint = new IPEndPoint(_server.Config.IPAddress, 1),
Type = ChannelType.Normal,
Color = Color.LightGreen,
Load = ChannelLoad.Highest
@ -80,20 +79,20 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo
{
Name = "Real Channel",
EndPoint = new IPEndPoint(IPAddress.Loopback, 2),
EndPoint = new IPEndPoint(_server.Config.IPAddress, 2),
Type = ChannelType.Normal
},
new ChannelInfo
{
Name = "Boredom Time",
EndPoint = new IPEndPoint(IPAddress.Loopback, 3),
EndPoint = new IPEndPoint(_server.Config.IPAddress, 3),
Type = ChannelType.Normal,
Load = ChannelLoad.Medium
},
new ChannelInfo
{
Name = "Nothing Goes",
EndPoint = new IPEndPoint(IPAddress.Loopback, 4),
EndPoint = new IPEndPoint(_server.Config.IPAddress, 4),
Type = ChannelType.Roping,
Color = Color.Orange,
Coins = 2,
@ -102,7 +101,7 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo
{
Name = "Doper's Heaven",
EndPoint = new IPEndPoint(IPAddress.Loopback, 5),
EndPoint = new IPEndPoint(_server.Config.IPAddress, 5),
Type = ChannelType.Roping,
Color = Color.Orange,
Coins = 1,
@ -111,7 +110,7 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo
{
Name = "Unhelpful Channel",
EndPoint = new IPEndPoint(IPAddress.Loopback, 6),
EndPoint = new IPEndPoint(_server.Config.IPAddress, 6),
Type = ChannelType.Special
}
}
@ -123,7 +122,7 @@ namespace Syroot.Worms.OnlineWorms.Server
// Simply allow joining a channel running on the same server port.
SendPacket(new ChannelEnterReply
{
EndPoint = new IPEndPoint(IPAddress.Loopback, _server.Port)
EndPoint = _server.Config.EndPoint
});
}
@ -158,6 +157,8 @@ namespace Syroot.Worms.OnlineWorms.Server
});
}
SendPacket(reply);
SendPacket(new RawPacket(PacketType.Channel, 0x44, (byte)0x00));
}
#if DEBUG

View File

@ -0,0 +1,26 @@
using System.Net;
namespace Syroot.Worms.OnlineWorms.Server
{
internal class Config
{
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
/// <summary>
/// Gets or sets the external IP sent to clients to connect to.
/// </summary>
public string IP { get; set; }
/// <summary>
/// Gets or sets the port under which the server listens for new connections.
/// </summary>
public ushort Port { get; set; }
public string Name { get; set; }
public string Region { get; set; }
public ushort Version { get; set; }
internal IPAddress IPAddress => IPAddress.Parse(IP);
internal IPEndPoint EndPoint => new IPEndPoint(IPAddress, Port);
}
}

View File

@ -13,7 +13,11 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
internal LoginResult Result { get; set; }
internal ushort Unknown1 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the user can send any messages. Apparently also affects possibility
/// of even joining a channel.
/// </summary>
internal bool HasMessageRights { get; set; } = true;
internal LoginPlayerInfo[] PlayerInfos { get; set; }
@ -27,7 +31,7 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
stream.WriteBoolean(loginSuccessful);
if (loginSuccessful)
{
stream.WriteUInt16(Unknown1);
stream.WriteBoolean(!HasMessageRights, BooleanCoding.Word);
stream.WriteUInt16((ushort)PlayerInfos.Length);
foreach (LoginPlayerInfo playerInfo in PlayerInfos)
{

View File

@ -10,10 +10,11 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
{
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
internal RawPacket(PacketType type, int id)
internal RawPacket(PacketType type, int id, params byte[] data)
{
Type = type;
ID = id;
Data = data;
}
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
@ -22,12 +23,19 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
internal int ID { get; }
internal byte[] Data { get; set; }
internal byte[] Data { get; private set; }
// ---- METHODS (INTERNAL) -------------------------------------------------------------------------------------
internal override void Deserialize(PacketStream stream) => Data = stream.ReadBytes((int)stream.Length);
internal override void Serialize(PacketStream stream) => stream.WriteBytes(Data);
internal override void Deserialize(PacketStream stream)
{
Data = stream.ReadBytes((int)stream.Length);
}
internal override void Serialize(PacketStream stream)
{
stream.WriteBytes(Data);
}
}
}
#endif

View File

@ -0,0 +1,7 @@
{
"IP": "87.123.189.212", // external IP sent to clients to connect to
"Port": 17022,
"Name": "Online Worms Private Server",
"Region": "Global",
"Version": 114
}

View File

@ -13,7 +13,8 @@ namespace Syroot.Worms.OnlineWorms.Server
{
try
{
new Server().Listen(17022);
// Start a new server.
new Server().Listen();
}
catch (Exception ex)
{

View File

@ -2,6 +2,7 @@
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace Syroot.Worms.OnlineWorms.Server
{
@ -15,16 +16,20 @@ namespace Syroot.Worms.OnlineWorms.Server
private readonly List<Client> _clients = new List<Client>();
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
internal Server()
{
// Create and read the configuration.
Config = new ConfigurationBuilder()
.AddJsonFile("OWServerConfig.json", true)
.Build()
.Get<Config>();
}
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
internal string Name => "Online Worms Private Server";
internal string RegionName => "Global";
internal ushort Version => 114;
/// <summary>
/// Gets the port under which the server listens for new connections.
/// </summary>
internal int Port { get; private set; }
internal Config Config { get; }
internal Log Log { get; } = new Log();
@ -35,12 +40,11 @@ namespace Syroot.Worms.OnlineWorms.Server
/// dispatching them into asynchronous handling threads. This call is blocking.
/// </summary>
/// <param name="port">The port on which to listen for new client connections.</param>
internal void Listen(int port)
internal void Listen()
{
Port = port;
TcpListener tcpListener = new TcpListener(IPAddress.Any, Port);
TcpListener tcpListener = new TcpListener(IPAddress.Any, Config.Port);
tcpListener.Start();
Log.Write(LogCategory.Server, $"Listening on port {Port}...");
Log.Write(LogCategory.Server, $"Listening on {Config.EndPoint}...");
while (true)
{

View File

@ -1,13 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net452;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Syroot.BinaryData" Version="5.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<None Update="OWServerConfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>