From 885079ae7000ede3eb9cdf75f17420967f35da1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Fri, 7 Feb 2025 17:05:23 +0100 Subject: [PATCH] Fixed wrong R/W timeout handling on tcp listener handling. v0.4.2 --- AMWD.Protocols.Modbus.Tcp/ModbusTcpProxy.cs | 25 ++++++++++++++------- CHANGELOG.md | 11 ++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/AMWD.Protocols.Modbus.Tcp/ModbusTcpProxy.cs b/AMWD.Protocols.Modbus.Tcp/ModbusTcpProxy.cs index 3e20e05..b45f551 100644 --- a/AMWD.Protocols.Modbus.Tcp/ModbusTcpProxy.cs +++ b/AMWD.Protocols.Modbus.Tcp/ModbusTcpProxy.cs @@ -213,23 +213,32 @@ namespace AMWD.Protocols.Modbus.Tcp { var requestBytes = new List(); + // Waiting for next request + byte[] headerBytes = await stream.ReadExpectedBytesAsync(6, cancellationToken).ConfigureAwait(continueOnCapturedContext: false); + requestBytes.AddRange(headerBytes); + + ushort length = headerBytes + .Skip(4).Take(2).ToArray() + .GetBigEndianUInt16(); + + // Waiting for the remaining required data using (var cts = new CancellationTokenSource(ReadWriteTimeout)) using (cancellationToken.Register(cts.Cancel)) { - byte[] headerBytes = await stream.ReadExpectedBytesAsync(6, cts.Token).ConfigureAwait(continueOnCapturedContext: false); - requestBytes.AddRange(headerBytes); - - ushort length = headerBytes - .Skip(4).Take(2).ToArray() - .GetBigEndianUInt16(); - byte[] bodyBytes = await stream.ReadExpectedBytesAsync(length, cts.Token).ConfigureAwait(continueOnCapturedContext: false); requestBytes.AddRange(bodyBytes); } byte[] responseBytes = await HandleRequestAsync([.. requestBytes], cancellationToken).ConfigureAwait(continueOnCapturedContext: false); if (responseBytes != null) - await stream.WriteAsync(responseBytes, 0, responseBytes.Length, cancellationToken).ConfigureAwait(continueOnCapturedContext: false); + { + // Write response when available + using (var cts = new CancellationTokenSource(ReadWriteTimeout)) + using (cancellationToken.Register(cts.Cancel)) + { + await stream.WriteAsync(responseBytes, 0, responseBytes.Length, cts.Token).ConfigureAwait(continueOnCapturedContext: false); + } + } } } catch diff --git a/CHANGELOG.md b/CHANGELOG.md index ad43ac2..0581b69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 _nothing changed yet_ + +## [v0.4.2] (2025-02-07) + +### Fixed + +- Fixing issue with R/W timeouts while processing client requests on the `ModbusTcpProxy`. + + ## [v0.4.1] (2025-02-06) ### Changed @@ -87,7 +95,8 @@ So this tag is only here for documentation purposes of the NuGet Gallery. -[Unreleased]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.1...HEAD +[Unreleased]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.2...HEAD +[v0.4.2]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.1...v0.4.2 [v0.4.1]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.0...v0.4.1 [v0.4.0]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.2...v0.4.0 [v0.3.2]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.1...v0.3.2