Compare commits

...

3 Commits

Author SHA1 Message Date
Syntriax de267a9d0f feat: NetworkServer 2024-02-01 23:38:39 +03:00
Syntriax 977a2abdd7 feat: NetworkClient 2024-02-01 23:38:35 +03:00
Syntriax 91e88bbff8 feat: Added LiteNetLib 2024-02-01 22:14:59 +03:00
3 changed files with 61 additions and 0 deletions

View File

@ -21,6 +21,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Apos.Shapes" Version="0.2.3" />
<PackageReference Include="LiteNetLib" Version="1.2.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
</ItemGroup>

View File

@ -0,0 +1,21 @@
using LiteNetLib;
namespace Game.Network;
public class NetworkClient
{
public readonly EventBasedNetListener Listener = null!;
public readonly NetManager Client = null!;
public NetworkClient()
{
Listener = new EventBasedNetListener();
Client = new NetManager(Listener);
}
public void Connect(string address, int port, string? password = null)
=> Client.Connect(address, port, password ?? string.Empty);
public void PollEvents() => Client.PollEvents();
public void Stop() => Client.Stop();
}

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();
}