mirror of
https://github.com/oliverbooth/TcpDotNet
synced 2024-11-23 00:28:48 +00:00
feat: use async event handlers
This commit is contained in:
parent
3ac93daf4f
commit
45a0b3898a
@ -7,7 +7,11 @@ using TcpDotNet.Protocol.Packets.ClientBound;
|
||||
using TcpDotNet.Protocol.Packets.ServerBound;
|
||||
|
||||
using var client = new ProtocolClient();
|
||||
client.Disconnected += (_, e) => Console.WriteLine($"Disconnected: {e.DisconnectReason}");
|
||||
client.Disconnected += (_, e) =>
|
||||
{
|
||||
Console.WriteLine($"Disconnected: {e.DisconnectReason}");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
client.RegisterPacketHandler(PacketHandler<PongPacket>.Empty);
|
||||
client.RegisterPacketHandler(new GoodbyePacketHandler());
|
||||
|
@ -5,8 +5,16 @@ using TcpDotNet.Protocol.Packets.ClientBound;
|
||||
using TcpDotNet.Protocol.Packets.ServerBound;
|
||||
|
||||
var listener = new ProtocolListener();
|
||||
listener.ClientConnected += (_, e) => Console.WriteLine($"Client connected from {e.Client.RemoteEndPoint} with session {e.Client.SessionId}");
|
||||
listener.ClientDisconnected += (_, e) => Console.WriteLine($"Client {e.Client.SessionId} disconnected ({e.DisconnectReason})");
|
||||
listener.ClientConnected += (_, e) =>
|
||||
{
|
||||
Console.WriteLine($"Client connected from {e.Client.RemoteEndPoint} with session {e.Client.SessionId}");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
listener.ClientDisconnected += (_, e) =>
|
||||
{
|
||||
Console.WriteLine($"Client {e.Client.SessionId} disconnected ({e.DisconnectReason})");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
listener.RegisterPacketHandler(new HelloPacketHandler());
|
||||
listener.RegisterPacketHandler(new PingPacketHandler());
|
||||
|
16
TcpDotNet/AsyncEventHandler.cs
Normal file
16
TcpDotNet/AsyncEventHandler.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace TcpDotNet;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the method that will handle an asynchronous event that has no event data.
|
||||
/// </summary>
|
||||
/// <param name="sender">The source of the event.</param>
|
||||
/// <param name="e">An object that contains no event data.</param>
|
||||
public delegate Task AsyncEventHandler(object? sender, EventArgs e);
|
||||
|
||||
/// <summary>
|
||||
/// Represents the method that will handle an asynchronous event when the event provides data.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
|
||||
/// <param name="sender">The source of the event.</param>
|
||||
/// <param name="e">An object that contains event data.</param>
|
||||
public delegate Task AsyncEventHandler<in TEventArgs>(object? sender, TEventArgs e);
|
@ -30,7 +30,7 @@ public sealed class ProtocolClient : ClientNode
|
||||
/// <summary>
|
||||
/// Occurs when the client has been disconnected.
|
||||
/// </summary>
|
||||
public event EventHandler<DisconnectedEventArgs>? Disconnected;
|
||||
public event AsyncEventHandler<DisconnectedEventArgs>? Disconnected;
|
||||
|
||||
/// <summary>
|
||||
/// Establishes a connection to a remote host.
|
||||
|
@ -27,27 +27,27 @@ public sealed partial class ProtocolListener : Node
|
||||
/// <summary>
|
||||
/// Occurs when a client connects to the listener.
|
||||
/// </summary>
|
||||
public event EventHandler<ClientConnectedEventArgs>? ClientConnected;
|
||||
public event AsyncEventHandler<ClientConnectedEventArgs>? ClientConnected;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a client disconnects from the listener.
|
||||
/// </summary>
|
||||
public event EventHandler<ClientDisconnectedEventArgs>? ClientDisconnected;
|
||||
public event AsyncEventHandler<ClientDisconnectedEventArgs>? ClientDisconnected;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a client sends a packet to the listener.
|
||||
/// </summary>
|
||||
public event EventHandler<ClientPacketReceivedEventArgs>? ClientPacketReceived;
|
||||
public event AsyncEventHandler<ClientPacketReceivedEventArgs>? ClientPacketReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the server has started.
|
||||
/// </summary>
|
||||
public event EventHandler? Started;
|
||||
public event AsyncEventHandler? Started;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the server has started.
|
||||
/// </summary>
|
||||
public event EventHandler? Stopped;
|
||||
public event AsyncEventHandler? Stopped;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read-only view of the clients connected to this listener.
|
||||
|
Loading…
Reference in New Issue
Block a user