From 376eb8aba750c66ef8472e31de00bf3e84762f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Tue, 10 Dec 2024 16:16:12 +0100 Subject: [PATCH] Update the OpenNettyConnection static APIs to make the cancellation token mandatory --- README.md | 9 ++++++--- src/OpenNetty/OpenNettyConnection.cs | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 97f86bf..057b987 100644 --- a/README.md +++ b/README.md @@ -151,8 +151,11 @@ await Task.Delay(-1); > parity : Parity.None, > dataBits: 8, > stopBits: StopBits.One); +> +> using var source = new CancellationTokenSource(); +> source.CancelAfter(TimeSpan.FromSeconds(10)); > -> await using var connection = await OpenNettyConnection.CreateSerialConnectionAsync(port); +> await using var connection = await OpenNettyConnection.CreateSerialConnectionAsync(port, source.Token); > > var message = OpenNettyMessage.CreateCommand( > protocol: OpenNettyProtocol.Zigbee, @@ -161,9 +164,9 @@ await Task.Delay(-1); > media : OpenNettyMedia.Radio, > mode : OpenNettyMode.Unicast); > -> await connection.SendAsync(message.Frame, CancellationToken.None); +> await connection.SendAsync(message.Frame, source.Token); > -> if (await connection.ReceiveAsync(CancellationToken.None) != OpenNettyFrames.Acknowledgement) +> if (await connection.ReceiveAsync(source.Token) != OpenNettyFrames.Acknowledgement) > { > throw new ApplicationException("The frame was not acknowledged by the gateway."); > } diff --git a/src/OpenNetty/OpenNettyConnection.cs b/src/OpenNetty/OpenNettyConnection.cs index 8f1720d..914ca52 100644 --- a/src/OpenNetty/OpenNettyConnection.cs +++ b/src/OpenNetty/OpenNettyConnection.cs @@ -62,7 +62,7 @@ public abstract class OpenNettyConnection : IAsyncDisposable /// A that can be used to monitor the asynchronous /// operation and whose result returns the created connection. /// - public static ValueTask CreateAsync(OpenNettyGateway gateway, CancellationToken cancellationToken = default) + public static ValueTask CreateAsync(OpenNettyGateway gateway, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(gateway); @@ -87,7 +87,7 @@ public static ValueTask CreateAsync(OpenNettyGateway gatewa /// A that can be used to monitor the asynchronous /// operation and whose result returns the created serial connection. /// - public static async ValueTask CreateSerialConnectionAsync(SerialPort port, CancellationToken cancellationToken = default) + public static async ValueTask CreateSerialConnectionAsync(SerialPort port, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(port); @@ -110,7 +110,7 @@ public static async ValueTask CreateSerialConnectionAsync(S /// A that can be used to monitor the asynchronous /// operation and whose result returns the created TCP connection. /// - public static async ValueTask CreateTcpConnectionAsync(IPEndPoint endpoint, CancellationToken cancellationToken = default) + public static async ValueTask CreateTcpConnectionAsync(IPEndPoint endpoint, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(endpoint);