wip
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
using ENet;
|
||||
|
||||
namespace Ragon.Server.ENet;
|
||||
|
||||
public sealed class ENetConnection: INetworkConnection
|
||||
{
|
||||
public ushort Id { get; }
|
||||
public INetworkChannel ReliableChannel { get; private set; }
|
||||
public INetworkChannel UnreliableChannel { get; private set; }
|
||||
|
||||
public ENetConnection(Peer peer)
|
||||
{
|
||||
Id = (ushort) peer.ID;
|
||||
ReliableChannel = new ENetReliableChannel(peer, 0);
|
||||
UnreliableChannel = new ENetUnreliableChannel(peer, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using ENet;
|
||||
|
||||
namespace Ragon.Server.ENet;
|
||||
|
||||
public sealed class ENetReliableChannel: INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public ENetReliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
var newPacket = new Packet();
|
||||
newPacket.Create(data, data.Length, PacketFlags.Reliable);
|
||||
|
||||
_peer.Send(_channelId, ref newPacket);
|
||||
}
|
||||
}
|
||||
Executable
+115
@@ -0,0 +1,115 @@
|
||||
using System.Diagnostics;
|
||||
using ENet;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
|
||||
namespace Ragon.Server.ENet
|
||||
{
|
||||
public sealed class ENetServer: INetworkServer
|
||||
{
|
||||
public ENetConnection[] Connections;
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
private INetworkListener _listener;
|
||||
private uint _protocol;
|
||||
private Host _host;
|
||||
private Event _event;
|
||||
private NetworkConfiguration _configuration;
|
||||
|
||||
public ENetServer()
|
||||
{
|
||||
_host = new Host();
|
||||
}
|
||||
|
||||
public void Start(INetworkListener listener, NetworkConfiguration configuration)
|
||||
{
|
||||
Library.Initialize();
|
||||
|
||||
_listener = listener;
|
||||
_protocol = configuration.Protocol;
|
||||
|
||||
Connections = new ENetConnection[configuration.LimitConnections];
|
||||
|
||||
var address = new Address { Port = (ushort) configuration.Port };
|
||||
_host.Create(address, Connections.Length, 2, 0, 0, 1024 * 1024);
|
||||
|
||||
var protocolDecoded = RagonVersion.Parse(_protocol);
|
||||
_logger.Info($"Network listening on {configuration.Port}");
|
||||
_logger.Info($"Protocol: {protocolDecoded}");
|
||||
}
|
||||
|
||||
public void Poll()
|
||||
{
|
||||
bool polled = false;
|
||||
while (!polled)
|
||||
{
|
||||
if (_host.CheckEvents(out _event) <= 0)
|
||||
{
|
||||
if (_host.Service(0, out _event) <= 0)
|
||||
break;
|
||||
|
||||
polled = true;
|
||||
}
|
||||
|
||||
switch (_event.Type)
|
||||
{
|
||||
case EventType.None:
|
||||
{
|
||||
_logger.Trace("None event");
|
||||
break;
|
||||
}
|
||||
case EventType.Connect:
|
||||
{
|
||||
if (IsValidProtocol(_event.Data))
|
||||
{
|
||||
_logger.Warn("Mismatched protocol, close connection");
|
||||
break;
|
||||
}
|
||||
|
||||
var connection = new ENetConnection(_event.Peer);
|
||||
Connections[_event.Peer.ID] = connection;
|
||||
|
||||
_listener.OnConnected(connection);
|
||||
break;
|
||||
}
|
||||
case EventType.Disconnect:
|
||||
{
|
||||
var connection = Connections[_event.Peer.ID];
|
||||
_listener.OnDisconnected(connection);
|
||||
break;
|
||||
}
|
||||
case EventType.Timeout:
|
||||
{
|
||||
var connection = Connections[_event.Peer.ID];
|
||||
_listener.OnTimeout(connection);
|
||||
break;
|
||||
}
|
||||
case EventType.Receive:
|
||||
{
|
||||
var peerId = (ushort) _event.Peer.ID;
|
||||
var connection = Connections[peerId];
|
||||
var dataRaw = new byte[_event.Packet.Length];
|
||||
|
||||
_event.Packet.CopyTo(dataRaw);
|
||||
_event.Packet.Dispose();
|
||||
|
||||
_listener.OnData(connection, dataRaw);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_host?.Dispose();
|
||||
|
||||
Library.Deinitialize();
|
||||
}
|
||||
|
||||
private bool IsValidProtocol(uint protocol)
|
||||
{
|
||||
return protocol == _configuration.Protocol;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using ENet;
|
||||
|
||||
namespace Ragon.Server.ENet;
|
||||
|
||||
public sealed class ENetUnreliableChannel: INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public ENetUnreliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
var newPacket = new Packet();
|
||||
newPacket.Create(data, data.Length, PacketFlags.None);
|
||||
|
||||
_peer.Send(_channelId, ref newPacket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Ragon.ENet</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
||||
<PackageReference Include="NLog" Version="5.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user