mirror of
https://github.com/raphaeIl/Novaria.git
synced 2025-12-12 14:34:38 +01:00
serverlist handler + basic crypto, rename project
This commit is contained in:
61
Novaria.GameServer/GameServer.cs
Normal file
61
Novaria.GameServer/GameServer.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using Serilog;
|
||||
using System.Reflection;
|
||||
using Novaria.GameServer.Protocol;
|
||||
|
||||
namespace Novaria.GameServer
|
||||
{
|
||||
public class GameServer
|
||||
{
|
||||
private readonly TcpListener listener;
|
||||
|
||||
private static GameServer _instance;
|
||||
public static readonly int GameServerPort = 6969;
|
||||
|
||||
public static GameServer Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return _instance ??= new GameServer();
|
||||
}
|
||||
}
|
||||
|
||||
public GameServer()
|
||||
{
|
||||
listener = new(IPAddress.Parse("0.0.0.0"), GameServerPort);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
listener.Start();
|
||||
|
||||
Log.Information($"{nameof(GameServer)} started and listening on port {GameServerPort}");
|
||||
|
||||
while (true)
|
||||
{
|
||||
TcpClient tcpClient = listener.AcceptTcpClient();
|
||||
string id = tcpClient.Client.RemoteEndPoint!.ToString()!;
|
||||
|
||||
Log.Information($"{id} connected");
|
||||
|
||||
Task.Run(() => HandleMessage(tcpClient));
|
||||
}
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Log.Information("TCP listener error: " + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void HandleMessage(TcpClient tcpClient)
|
||||
{
|
||||
Connection connection = Connection.CreateConnection(tcpClient);
|
||||
|
||||
connection.HandleMessage(tcpClient);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Novaria.GameServer/Novaria.GameServer.csproj
Normal file
14
Novaria.GameServer/Novaria.GameServer.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
45
Novaria.GameServer/Protocol/Connection.cs
Normal file
45
Novaria.GameServer/Protocol/Connection.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Serilog;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Novaria.GameServer.Protocol
|
||||
{
|
||||
public class Connection
|
||||
{
|
||||
private TcpClient tcpClient;
|
||||
|
||||
private BinaryReader reader;
|
||||
|
||||
private long sendSequence;
|
||||
|
||||
private int messageCount_;
|
||||
|
||||
private int sendingMessageCount_;
|
||||
|
||||
public static Connection CreateConnection(TcpClient tcpClient)
|
||||
{
|
||||
return new Connection(tcpClient);
|
||||
}
|
||||
|
||||
public Connection(TcpClient tcpClient) {
|
||||
this.tcpClient = tcpClient;
|
||||
this.reader = new BinaryReader(new StreamReader(tcpClient.GetStream()).BaseStream);
|
||||
}
|
||||
|
||||
public void HandleMessage(TcpClient tcpClient)
|
||||
{
|
||||
while (tcpClient.Connected)
|
||||
{
|
||||
if (tcpClient.GetStream().DataAvailable)
|
||||
{
|
||||
string message = reader.ReadString();
|
||||
Log.Information($"Received message: {message}");
|
||||
}
|
||||
}
|
||||
|
||||
tcpClient.Close();
|
||||
Log.Information("Client Disconnected!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user