Engine-Pong/Game/Network/NetworkServer.cs

40 lines
1.0 KiB
C#
Raw Normal View History

2024-02-01 23:38:39 +03:00
using LiteNetLib;
2024-02-02 12:04:53 +03:00
namespace Pong.Network;
2024-02-01 23:38:39 +03:00
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();
}