diff --git a/Game/Network/NetworkServer.cs b/Game/Network/NetworkServer.cs new file mode 100644 index 0000000..b09bdab --- /dev/null +++ b/Game/Network/NetworkServer.cs @@ -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(); +}