From 45a0b3898a3e16f39440ae18eaeeef367a9bc8f8 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 14 Feb 2024 15:20:55 +0000 Subject: [PATCH] feat: use async event handlers --- TcpDotNet.ClientIntegrationTest/Program.cs | 6 +++++- TcpDotNet.ListenerIntegrationTest/Program.cs | 12 ++++++++++-- TcpDotNet/AsyncEventHandler.cs | 16 ++++++++++++++++ TcpDotNet/ProtocolClient.cs | 2 +- TcpDotNet/ProtocolListener.cs | 10 +++++----- 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 TcpDotNet/AsyncEventHandler.cs diff --git a/TcpDotNet.ClientIntegrationTest/Program.cs b/TcpDotNet.ClientIntegrationTest/Program.cs index 9a732c5..f07e0ac 100644 --- a/TcpDotNet.ClientIntegrationTest/Program.cs +++ b/TcpDotNet.ClientIntegrationTest/Program.cs @@ -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.Empty); client.RegisterPacketHandler(new GoodbyePacketHandler()); diff --git a/TcpDotNet.ListenerIntegrationTest/Program.cs b/TcpDotNet.ListenerIntegrationTest/Program.cs index 9ef8c33..c1e4d18 100644 --- a/TcpDotNet.ListenerIntegrationTest/Program.cs +++ b/TcpDotNet.ListenerIntegrationTest/Program.cs @@ -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()); diff --git a/TcpDotNet/AsyncEventHandler.cs b/TcpDotNet/AsyncEventHandler.cs new file mode 100644 index 0000000..0d51db8 --- /dev/null +++ b/TcpDotNet/AsyncEventHandler.cs @@ -0,0 +1,16 @@ +namespace TcpDotNet; + +/// +/// Represents the method that will handle an asynchronous event that has no event data. +/// +/// The source of the event. +/// An object that contains no event data. +public delegate Task AsyncEventHandler(object? sender, EventArgs e); + +/// +/// Represents the method that will handle an asynchronous event when the event provides data. +/// +/// The type of the event data generated by the event. +/// The source of the event. +/// An object that contains event data. +public delegate Task AsyncEventHandler(object? sender, TEventArgs e); diff --git a/TcpDotNet/ProtocolClient.cs b/TcpDotNet/ProtocolClient.cs index 98547d5..e422719 100644 --- a/TcpDotNet/ProtocolClient.cs +++ b/TcpDotNet/ProtocolClient.cs @@ -30,7 +30,7 @@ public sealed class ProtocolClient : ClientNode /// /// Occurs when the client has been disconnected. /// - public event EventHandler? Disconnected; + public event AsyncEventHandler? Disconnected; /// /// Establishes a connection to a remote host. diff --git a/TcpDotNet/ProtocolListener.cs b/TcpDotNet/ProtocolListener.cs index 52c5c74..fce8f2d 100644 --- a/TcpDotNet/ProtocolListener.cs +++ b/TcpDotNet/ProtocolListener.cs @@ -27,27 +27,27 @@ public sealed partial class ProtocolListener : Node /// /// Occurs when a client connects to the listener. /// - public event EventHandler? ClientConnected; + public event AsyncEventHandler? ClientConnected; /// /// Occurs when a client disconnects from the listener. /// - public event EventHandler? ClientDisconnected; + public event AsyncEventHandler? ClientDisconnected; /// /// Occurs when a client sends a packet to the listener. /// - public event EventHandler? ClientPacketReceived; + public event AsyncEventHandler? ClientPacketReceived; /// /// Occurs when the server has started. /// - public event EventHandler? Started; + public event AsyncEventHandler? Started; /// /// Occurs when the server has started. /// - public event EventHandler? Stopped; + public event AsyncEventHandler? Stopped; /// /// Gets a read-only view of the clients connected to this listener.