mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-12 14:34:39 +01:00
Add project files.
This commit is contained in:
9
BLHX.Server.Common/BLHX.Server.Common.csproj
Normal file
9
BLHX.Server.Common/BLHX.Server.Common.csproj
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
99
BLHX.Server.Common/Utils/Logger.cs
Normal file
99
BLHX.Server.Common/Utils/Logger.cs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace BLHX.Server.Common.Utils
|
||||||
|
{
|
||||||
|
public class Logger
|
||||||
|
{
|
||||||
|
public static readonly Logger c = new("BLHX.Server", ConsoleColor.Blue);
|
||||||
|
private readonly string _name;
|
||||||
|
private readonly bool TraceOnError;
|
||||||
|
private readonly ConsoleColor _color;
|
||||||
|
|
||||||
|
public Logger(string name, ConsoleColor color = ConsoleColor.Cyan, bool traceOnError = true)
|
||||||
|
{
|
||||||
|
_name = name;
|
||||||
|
_color = color;
|
||||||
|
TraceOnError = traceOnError;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Log(params string[] message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = _color;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Warn(params string[] message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Trail(params string[] msg)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.DarkGray;
|
||||||
|
Console.WriteLine($"\t└── {string.Join(' ', msg)}");
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(params string[] message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
if (TraceOnError)
|
||||||
|
Console.BackgroundColor = ConsoleColor.DarkRed;
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
#if DEBUG
|
||||||
|
StackTrace trace = new(true);
|
||||||
|
if (TraceOnError)
|
||||||
|
Trail(trace.ToString());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Debug(params string[] message)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.BackgroundColor = ConsoleColor.Black;
|
||||||
|
|
||||||
|
StackTrace trace = new(true);
|
||||||
|
if (TraceOnError)
|
||||||
|
Trail(trace.ToString());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
BLHX.Server.Game/BLHX.Server.Game.csproj
Normal file
13
BLHX.Server.Game/BLHX.Server.Game.csproj
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\BLHX.Server.Common\BLHX.Server.Common.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
49
BLHX.Server.Game/Connection.cs
Normal file
49
BLHX.Server.Game/Connection.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using BLHX.Server.Common.Utils;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
|
namespace BLHX.Server.Game
|
||||||
|
{
|
||||||
|
public class Connection
|
||||||
|
{
|
||||||
|
readonly TcpClient tcpClient;
|
||||||
|
readonly Logger c;
|
||||||
|
readonly CancellationTokenSource cts = new();
|
||||||
|
readonly Task loopTask;
|
||||||
|
public IPEndPoint EndPoint => (IPEndPoint)tcpClient.Client.RemoteEndPoint!;
|
||||||
|
|
||||||
|
public Connection(TcpClient tcpClient)
|
||||||
|
{
|
||||||
|
this.tcpClient = tcpClient;
|
||||||
|
c = new(EndPoint.ToString());
|
||||||
|
loopTask = Task.Run(ClientLoop, cts.Token);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ClientLoop()
|
||||||
|
{
|
||||||
|
var ns = tcpClient.GetStream();
|
||||||
|
var buf = GC.AllocateUninitializedArray<byte>(8 << 13);
|
||||||
|
var pos = 0;
|
||||||
|
|
||||||
|
while (!cts.Token.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
await Task.Delay(1);
|
||||||
|
int len = ns.Read(buf, pos, buf.Length - pos);
|
||||||
|
len += pos;
|
||||||
|
if (len == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
c.Debug(BitConverter.ToString(buf[..len]).Replace("-", ""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void EndProtocol()
|
||||||
|
{
|
||||||
|
cts.Cancel();
|
||||||
|
loopTask.Wait();
|
||||||
|
loopTask.Dispose();
|
||||||
|
|
||||||
|
tcpClient.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
47
BLHX.Server.Game/GameServer.cs
Normal file
47
BLHX.Server.Game/GameServer.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using BLHX.Server.Common.Utils;
|
||||||
|
|
||||||
|
namespace BLHX.Server.Game
|
||||||
|
{
|
||||||
|
public static class GameServer
|
||||||
|
{
|
||||||
|
static readonly TcpListener listener;
|
||||||
|
public static readonly Dictionary<IPEndPoint, Connection> connections = new();
|
||||||
|
public static readonly Logger c = new(nameof(GameServer), ConsoleColor.Magenta);
|
||||||
|
public static IPEndPoint EndPoint { get; }
|
||||||
|
|
||||||
|
static GameServer()
|
||||||
|
{
|
||||||
|
EndPoint = new(IPAddress.Any, 20000);
|
||||||
|
listener = new TcpListener(EndPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task Start()
|
||||||
|
{
|
||||||
|
listener.Start();
|
||||||
|
c.Log($"{nameof(GameServer)} started on {EndPoint}");
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TcpClient client = await listener.AcceptTcpClientAsync();
|
||||||
|
if (client.Client.RemoteEndPoint is not null and IPEndPoint)
|
||||||
|
{
|
||||||
|
if (connections.ContainsKey((IPEndPoint)client.Client.RemoteEndPoint))
|
||||||
|
connections[(IPEndPoint)client.Client.RemoteEndPoint].EndProtocol();
|
||||||
|
|
||||||
|
connections[(IPEndPoint)client.Client.RemoteEndPoint] = new Connection(client);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
client.Dispose();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
c.Error($"{nameof(GameServer)} listener error {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
BLHX.Server.sln
Normal file
37
BLHX.Server.sln
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.8.34309.116
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server", "BLHX.Server\BLHX.Server.csproj", "{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server.Game", "BLHX.Server.Game\BLHX.Server.Game.csproj", "{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLHX.Server.Common", "BLHX.Server.Common\BLHX.Server.Common.csproj", "{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C67D2B44-EFF0-4325-9C90-9D8BA4799E02}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{33059688-36C1-43D1-BEE6-3A5C5C7DA4E6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{BA896C61-5025-4CBB-B56C-18A59D3D0D7D}.Debug|Any CPU.ActiveCfg = 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.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {23C7FC14-F3E9-4AA5-8FE0-0E08FF97E4BA}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
16
BLHX.Server/BLHX.Server.csproj
Normal file
16
BLHX.Server/BLHX.Server.csproj
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<PublishAot>true</PublishAot>
|
||||||
|
<InvariantGlobalization>true</InvariantGlobalization>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\BLHX.Server.Game\BLHX.Server.Game.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
14
BLHX.Server/Program.cs
Normal file
14
BLHX.Server/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using BLHX.Server.Common.Utils;
|
||||||
|
using BLHX.Server.Game;
|
||||||
|
|
||||||
|
namespace BLHX.Server
|
||||||
|
{
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main()
|
||||||
|
{
|
||||||
|
Logger.c.Log("Starting...");
|
||||||
|
Task.Run(GameServer.Start).Wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user