incomplete sdk server + inferred server address

This commit is contained in:
rfi
2024-02-19 11:31:35 +07:00
parent 7445a1f5d8
commit 5e8f90b2e9
12 changed files with 193 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
public class Config : Singleton<Config> public class Config : Singleton<Config>
{ {
public string Address { get; set; } = "192.168.1.4"; public string Address { get; set; } = "127.0.0.1";
public uint Port { get; set; } = 20000; public uint Port { get; set; } = 20000;
public static void Load() public static void Load()

View File

@@ -18,7 +18,6 @@ namespace BLHX.Server.Game
readonly CancellationTokenSource cts = new(); readonly CancellationTokenSource cts = new();
readonly Task loopTask; readonly Task loopTask;
ushort packetIdx = 0; ushort packetIdx = 0;
public static JsonSerializerOptions jsonSerializerOptions = new() { IncludeFields = true };
private ushort NextPacketIdx => packetIdx++; private ushort NextPacketIdx => packetIdx++;
public IPEndPoint EndPoint => (IPEndPoint)tcpClient.Client.RemoteEndPoint!; public IPEndPoint EndPoint => (IPEndPoint)tcpClient.Client.RemoteEndPoint!;

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<OutputType>Library</OutputType>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BLHX.Server.Common\BLHX.Server.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using BLHX.Server.Sdk;
namespace BLHX.Server.SDK.Controllers
{
public class AccountController : IRegisterable
{
public static void Register(WebApplication app)
{
}
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:55758",
"sslPort": 44326
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5048",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "https://localhost:7187;http://localhost:5048",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,66 @@
using BLHX.Server.Common.Utils;
using System.Reflection;
namespace BLHX.Server.Sdk
{
public class SDKServer
{
static readonly Logger c = new(nameof(SDKServer), ConsoleColor.Green);
static Task? runTask = null;
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Disables default logger
builder.Logging.ClearProviders();
var app = builder.Build();
app.Urls.Add("http://*:80");
app.Urls.Add("https://*:443");
app.UseMiddleware<RequestLoggingMiddleware>();
foreach (Type controller in Assembly.GetExecutingAssembly().GetTypes().Where(p => typeof(IRegisterable).IsAssignableFrom(p) && !p.IsInterface))
{
controller.GetMethod(nameof(IRegisterable.Register))!.Invoke(null, new object[] { app });
#if DEBUG
c.Log($"Registered HTTP controller '{controller.Name}'");
#endif
}
runTask = app.RunAsync();
c.Log($"{nameof(SDKServer)} started in port {string.Join(", ", app.Urls.Select(x => x.Split(':').Last()))}!");
}
private class RequestLoggingMiddleware(RequestDelegate next)
{
private readonly RequestDelegate _next = next;
private static readonly string[] SurpressedRoutes = [];
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
#if DEBUG
c.Error($"{ex} Request below:");
#else
c.Error($"{ex.Message} Request below:");
#endif
}
finally
{
c.Log($"{context.Response.StatusCode} {context.Request.Method.ToUpper()} {context.Request.Path + context.Request.QueryString}");
}
}
}
}
public interface IRegisterable
{
public abstract static void Register(WebApplication app);
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116 VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server", "BLHX.Server\BLHX.Server.csproj", "{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BLHX.Server", "BLHX.Server\BLHX.Server.csproj", "{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server.Game", "BLHX.Server.Game\BLHX.Server.Game.csproj", "{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BLHX.Server.Game", "BLHX.Server.Game\BLHX.Server.Game.csproj", "{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server.Common", "BLHX.Server.Common\BLHX.Server.Common.csproj", "{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BLHX.Server.Common", "BLHX.Server.Common\BLHX.Server.Common.csproj", "{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server.SDK", "BLHX.Server.SDK\BLHX.Server.SDK.csproj", "{DBEE7F5B-FA2F-43BE-9B80-7E3D9A6267F2}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Debug|Any CPU.Build.0 = Debug|Any CPU {BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Release|Any CPU.ActiveCfg = Release|Any CPU {BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Release|Any CPU.Build.0 = Release|Any CPU {BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Release|Any CPU.Build.0 = Release|Any CPU
{DBEE7F5B-FA2F-43BE-9B80-7E3D9A6267F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBEE7F5B-FA2F-43BE-9B80-7E3D9A6267F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBEE7F5B-FA2F-43BE-9B80-7E3D9A6267F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBEE7F5B-FA2F-43BE-9B80-7E3D9A6267F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -10,6 +10,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BLHX.Server.Game\BLHX.Server.Game.csproj" /> <ProjectReference Include="..\BLHX.Server.Game\BLHX.Server.Game.csproj" />
<ProjectReference Include="..\BLHX.Server.SDK\BLHX.Server.SDK.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,17 +1,25 @@
using BLHX.Server.Common.Utils; using BLHX.Server.Common.Utils;
using BLHX.Server.Game; using BLHX.Server.Game;
using BLHX.Server.Sdk;
using System.Net.NetworkInformation;
namespace BLHX.Server; namespace BLHX.Server;
internal class Program internal class Program
{ {
static void Main() static void Main(string[] args)
{ {
Logger.c.Log("Starting..."); Logger.c.Log("Starting...");
Config.Load(); Config.Load();
if (Config.Instance.Address == "127.0.0.1")
{
Config.Instance.Address = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.OperationalStatus == OperationalStatus.Up).First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().Address.ToString();
Config.Save();
}
Task.Run(GameServer.Start); Task.Run(GameServer.Start);
SDKServer.Main(args);
Task.Run(InputSystem.Start).Wait(); Task.Run(InputSystem.Start).Wait();
} }
} }