Skip to content

Commit 885079a

Browse files
Fixed wrong R/W timeout handling on tcp listener handling. v0.4.2
1 parent 17fc216 commit 885079a

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

AMWD.Protocols.Modbus.Tcp/ModbusTcpProxy.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,32 @@ private async Task HandleClientAsync(TcpClientWrapper client, CancellationToken
213213
{
214214
var requestBytes = new List<byte>();
215215

216+
// Waiting for next request
217+
byte[] headerBytes = await stream.ReadExpectedBytesAsync(6, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
218+
requestBytes.AddRange(headerBytes);
219+
220+
ushort length = headerBytes
221+
.Skip(4).Take(2).ToArray()
222+
.GetBigEndianUInt16();
223+
224+
// Waiting for the remaining required data
216225
using (var cts = new CancellationTokenSource(ReadWriteTimeout))
217226
using (cancellationToken.Register(cts.Cancel))
218227
{
219-
byte[] headerBytes = await stream.ReadExpectedBytesAsync(6, cts.Token).ConfigureAwait(continueOnCapturedContext: false);
220-
requestBytes.AddRange(headerBytes);
221-
222-
ushort length = headerBytes
223-
.Skip(4).Take(2).ToArray()
224-
.GetBigEndianUInt16();
225-
226228
byte[] bodyBytes = await stream.ReadExpectedBytesAsync(length, cts.Token).ConfigureAwait(continueOnCapturedContext: false);
227229
requestBytes.AddRange(bodyBytes);
228230
}
229231

230232
byte[] responseBytes = await HandleRequestAsync([.. requestBytes], cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
231233
if (responseBytes != null)
232-
await stream.WriteAsync(responseBytes, 0, responseBytes.Length, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
234+
{
235+
// Write response when available
236+
using (var cts = new CancellationTokenSource(ReadWriteTimeout))
237+
using (cancellationToken.Register(cts.Cancel))
238+
{
239+
await stream.WriteAsync(responseBytes, 0, responseBytes.Length, cts.Token).ConfigureAwait(continueOnCapturedContext: false);
240+
}
241+
}
233242
}
234243
}
235244
catch

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
_nothing changed yet_
1111

12+
13+
## [v0.4.2] (2025-02-07)
14+
15+
### Fixed
16+
17+
- Fixing issue with R/W timeouts while processing client requests on the `ModbusTcpProxy`.
18+
19+
1220
## [v0.4.1] (2025-02-06)
1321

1422
### Changed
@@ -87,7 +95,8 @@ So this tag is only here for documentation purposes of the NuGet Gallery.
8795

8896

8997

90-
[Unreleased]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.1...HEAD
98+
[Unreleased]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.2...HEAD
99+
[v0.4.2]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.1...v0.4.2
91100
[v0.4.1]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.4.0...v0.4.1
92101
[v0.4.0]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.2...v0.4.0
93102
[v0.3.2]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.1...v0.3.2

0 commit comments

Comments
 (0)