mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-13 06:54:51 +01:00
Add project files.
This commit is contained in:
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user