Una extensión que le permite hacer girar un servidor Enet en su aplicación ASP.NET Core. Está basado en el fantástico trabajo de Stanislav Denisov: ENet-CSharp.
Es fácil de configurar:
public void ConfigureServices ( IServiceCollection services )
{
// Your other code...
services . AddEnet ( ( configuration ) =>
{
configuration
/* Set the port your Enet Server will listen to. */
. SetPort ( 7001 )
/* Set the polling time, in milliseconds. */
. SetPollingTimeout ( 15 ) ;
/* Add your connection handlers to compose the pipeline. The order matters. */
configuration
. AddConnectionHandler < MyConnectionHandler1 > ( )
. AddConnectionHandler < MyConnectionHandlerN > ( ) ;
/* Add your disconnection handlers to compose the pipeline. The order matters. */
configuration
. AddDisconnectionHandler < MyDisconnectionHandler1 > ( )
. AddDisconnectionHandler < MyDisconnectionHandlerN > ( ) ;
/* Add your timeout handlers to compose the pipeline. The order matters. */
configuration
. AddTimeoutHandler < MyTimeoutHandler1 > ( )
. AddTimeoutHandler < MyTimeoutHandlerN > ( ) ;
/* Add your packet received handlers to compose the pipeline. The order matters. */
configuration
. AddPacketHandler < MyPacketHandler1 > ( )
. AddPacketHandler < MyPacketHandlerN > ( ) ;
} ) ;
// Your other code...
}
Puede encontrar el ejemplo de uso en la carpeta MHLab.AspNetCore.Enet.Sample
. Es la plantilla clásica de ASP.NET Core Web API con la adición del servidor Enet.
Un controlador de conexión se activa cuando se establece una nueva conexión de cliente. Puede crear sus controladores de conexión de esta manera:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class ConnectionHandler : IConnectionHandler
{
private readonly ILogger < ConnectionHandler > _logger ;
public ConnectionHandler ( ILogger < ConnectionHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnConnectedPeer ( Peer peer )
{
_logger . LogDebug ( $ "Client connected - ID: { peer . ID } , IP: { peer . IP } " ) ;
}
}
Un controlador de desconexión se activa cuando un cliente ya conectado se desconecta correctamente. Puede crear sus controladores de desconexión de esta manera:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class DisconnectionHandler : IDisconnectionHandler
{
private readonly ILogger < DisconnectionHandler > _logger ;
public DisconnectionHandler ( ILogger < DisconnectionHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnDisconnectedPeer ( Peer peer )
{
_logger . Log ( LogLevel . Debug , $ "Client disconnected - ID: { peer . ID } , IP: { peer . IP } " ) ;
}
}
Un controlador de tiempo de espera se activa cuando un cliente ya conectado se desconecta de forma desafortunada. Puede crear sus controladores de tiempo de espera de esta manera:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class TimeoutHandler : ITimeoutHandler
{
private readonly ILogger < TimeoutHandler > _logger ;
public TimeoutHandler ( ILogger < TimeoutHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnTimeoutPeer ( Peer peer )
{
_logger . Log ( LogLevel . Debug , $ "Client timeout - ID: { peer . ID } , IP: { peer . IP } " ) ;
}
}
Un controlador de paquetes se activa cuando se recibe un mensaje de un cliente conectado. Puede crear sus controladores de paquetes de esta manera:
using ENet ;
using MHLab . AspNetCore . Enet . Handlers ;
using Microsoft . Extensions . Logging ;
public class PacketHandler : IPacketHandler
{
private readonly ILogger < PacketHandler > _logger ;
public PacketHandler ( ILogger < PacketHandler > logger )
{
/* Here in the constructor you can take the full advantage of the
Microsoft Dependency Injection framework. */
_logger = logger ;
}
public void OnPacketReceived ( Peer peer , byte channelId , Packet packet )
{
_logger . Log ( LogLevel . Debug , $ "Packet received from - ID: { peer . ID } , IP: { peer . IP } , Channel ID: { channelId } , Data length: { packet . Length } " ) ;
}
}