Compare commits
3 Commits
257e414c2a
...
de267a9d0f
Author | SHA1 | Date |
---|---|---|
Syntriax | de267a9d0f | |
Syntriax | 977a2abdd7 | |
Syntriax | 91e88bbff8 |
|
@ -21,6 +21,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Apos.Shapes" Version="0.2.3" />
|
<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.Framework.DesktopGL" Version="3.8.1.303" />
|
||||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -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