feat: NetworkServer
This commit is contained in:
parent
977a2abdd7
commit
de267a9d0f
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue