Gateway configuration in appsettings.json

This commit is contained in:
xeon 2024-02-10 22:11:16 +03:00
parent 7392b2d20a
commit 7921b2149e
5 changed files with 38 additions and 3 deletions

View file

@ -16,4 +16,10 @@
<ProjectReference Include="..\Protocol\Protocol.csproj" /> <ProjectReference Include="..\Protocol\Protocol.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View file

@ -2,8 +2,10 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using GameServer.Settings;
using KcpSharp; using KcpSharp;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace GameServer.Network.Kcp; namespace GameServer.Network.Kcp;
internal class KcpGateway internal class KcpGateway
@ -15,21 +17,22 @@ internal class KcpGateway
private const byte NetFlagAck = 0xEE; private const byte NetFlagAck = 0xEE;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOptions<GatewaySettings> _options;
private readonly SessionManager _sessionManager; private readonly SessionManager _sessionManager;
private IKcpTransport<IKcpMultiplexConnection>? _kcpTransport; private IKcpTransport<IKcpMultiplexConnection>? _kcpTransport;
private int _convCounter; private int _convCounter;
public KcpGateway(ILogger<KcpGateway> logger, SessionManager sessionManager) public KcpGateway(ILogger<KcpGateway> logger, IOptions<GatewaySettings> options, SessionManager sessionManager)
{ {
_logger = logger; _logger = logger;
_options = options;
_sessionManager = sessionManager; _sessionManager = sessionManager;
} }
public void Start() public void Start()
{ {
IPEndPoint endPoint = new(IPAddress.Any, 1337); IPEndPoint endPoint = _options.Value.EndPoint;
_kcpTransport = KcpSocketTransport.CreateMultiplexConnection(new(endPoint), 1400); _kcpTransport = KcpSocketTransport.CreateMultiplexConnection(new(endPoint), 1400);
_kcpTransport.SetHandshakeHandler(KcpSynPacketSize, HandleKcpSynPacket); _kcpTransport.SetHandshakeHandler(KcpSynPacketSize, HandleKcpSynPacket);
_kcpTransport.Start(); _kcpTransport.Start();

View file

@ -6,8 +6,10 @@ using GameServer.Network;
using GameServer.Network.Kcp; using GameServer.Network.Kcp;
using GameServer.Network.Messages; using GameServer.Network.Messages;
using GameServer.Network.Rpc; using GameServer.Network.Rpc;
using GameServer.Settings;
using GameServer.Systems.Entity; using GameServer.Systems.Entity;
using GameServer.Systems.Event; using GameServer.Systems.Event;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -21,6 +23,8 @@ internal static class Program
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(); builder.Logging.AddConsole();
builder.Services.Configure<GatewaySettings>(builder.Configuration.GetRequiredSection("Gateway"));
builder.Services.AddControllers() builder.Services.AddControllers()
.AddSingleton<KcpGateway>().AddScoped<PlayerSession>() .AddSingleton<KcpGateway>().AddScoped<PlayerSession>()
.AddScoped<MessageManager>().AddSingleton<EventHandlerFactory>() .AddScoped<MessageManager>().AddSingleton<EventHandlerFactory>()

View file

@ -0,0 +1,17 @@
using System.Net;
namespace GameServer.Settings;
internal class GatewaySettings
{
public required string Host { get; set; }
public IPEndPoint EndPoint
{
get
{
string[] split = Host.Split(':');
return new IPEndPoint(IPAddress.Parse(split[0]), int.Parse(split[1]));
}
}
}

View file

@ -0,0 +1,5 @@
{
"Gateway": {
"Host": "0.0.0.0:1337"
}
}