Engine-Pong/Game/Network/NetworkServer.cs

40 lines
1.0 KiB
C#

using LiteNetLib;
namespace Pong.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();
}