Added implementation of Modbus TCP Server
This commit is contained in:
26
AMWD.Protocols.Modbus.Tcp/Extensions/StreamExtensions.cs
Normal file
26
AMWD.Protocols.Modbus.Tcp/Extensions/StreamExtensions.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.IO
|
||||
{
|
||||
internal static class StreamExtensions
|
||||
{
|
||||
public static async Task<byte[]> ReadExpectedBytesAsync(this Stream stream, int expectedBytes, CancellationToken cancellationToken = default)
|
||||
{
|
||||
byte[] buffer = new byte[expectedBytes];
|
||||
int offset = 0;
|
||||
do
|
||||
{
|
||||
int count = await stream.ReadAsync(buffer, offset, expectedBytes - offset, cancellationToken).ConfigureAwait(false);
|
||||
if (count < 1)
|
||||
throw new EndOfStreamException();
|
||||
|
||||
offset += count;
|
||||
}
|
||||
while (offset < expectedBytes && !cancellationToken.IsCancellationRequested);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user