feat: NetworkServer

This commit is contained in:
Syntriax 2024-02-01 23:38:39 +03:00
parent 977a2abdd7
commit de267a9d0f
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
using LiteNetLib;
namespace Game.Network;
public class NetworkServer
{
public string Password { get; private set; } = string.Empty;
public int MaxConnectionCount { get; private set; } = 0;
public int Port { get; private set; } = 8888;
public readonly EventBasedNetListener Listener = null!;
public readonly NetManager Server = null!;
public NetworkServer()
{
Listener = new EventBasedNetListener();
Server = new NetManager(Listener);
Listener.ConnectionRequestEvent += request =>
{
if (Server.ConnectedPeersCount < MaxConnectionCount)
request.AcceptIfKey(Password);
else
request.Reject();
};
}
public void Start(int port, int maxConnectionCount, string? password = null)
{
Password = password ?? string.Empty;
MaxConnectionCount = maxConnectionCount;
Port = port;
Server.Start(port);
}
public void PollEvents() => Server.PollEvents();
public void Stop() => Server.Stop();
}