public static async Task FillAsync( ReadAsyncCallback read, byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (count != await TryFillAsync(read, buffer, offset, count, cancellationToken)) { throw new EndOfStreamException("Unexpected end of stream."); } }
public ReadBuffer(ReadAsyncCallback readAsync, int bufferSize) : base(bufferSize) { if (readAsync == null) { throw new ArgumentNullException(nameof(readAsync)); } this.readAsync = readAsync; this.read = (b, o, c) => ThrowInvalidSyncOperationException(); }
/// <summary>Initializes a new instance of the <see cref="TelnetStream"/> class.</summary> /// <exception cref="ArgumentNullException"><paramref name="readAsync"/>, <paramref name="writeAsync"/> and/or /// <paramref name="dataAvailable"/> equal <c>null</c>.</exception> public TelnetStream(ReadAsyncCallback readAsync, WriteAsyncCallback writeAsync, Func <bool> dataAvailable) : base( new ReadBuffer(readAsync, Defaults.PhysicalStreamBufferSize), new WriteBuffer(writeAsync, Defaults.PhysicalStreamBufferSize)) { if (readAsync == null) { throw new ArgumentNullException(nameof(readAsync)); } if (writeAsync == null) { throw new ArgumentNullException(nameof(writeAsync)); } this.dataAvailable = dataAvailable ?? throw new ArgumentNullException(nameof(dataAvailable)); }
public void ExceptionTest() { using (var dummy = new MemoryStream()) { ReadAsyncCallback fakeRead = (b, o, c, t) => Task.FromResult(0); WriteAsyncCallback fakeWrite = (b, o, c, t) => Task.FromResult(false); AssertThrow <NotSupportedException>(() => new S101Client(dummy, fakeRead, fakeWrite).Dispose()); AsyncPump.Run( async() => { using (var connection = new CompleteOnDispose()) using (var client = new S101Client(connection, (b, o, c, t) => connection.Task, fakeWrite)) { await AssertThrowAsync <InvalidOperationException>( () => Task.Run(() => client.SendMessageAsync(new S101Message(0x00, new KeepAliveRequest())))); } AssertThrow <ArgumentNullException>( () => new S101Client(null, fakeRead, fakeWrite).Dispose(), () => new S101Client(dummy, null, fakeWrite).Dispose(), () => new S101Client(dummy, fakeRead, null).Dispose()); AssertThrow <ArgumentOutOfRangeException>( () => new S101Client(dummy, fakeRead, fakeWrite, null, 3000, 0).Dispose(), () => new S101Client(dummy, fakeRead, fakeWrite, null, -2, 1).Dispose()); using (var connection = new CompleteOnDispose()) using (var client = new S101Client( connection, (b, o, c, t) => connection.Task, fakeWrite, null, 3000, 1)) { await AssertThrowAsync <ArgumentNullException>( () => client.SendMessageAsync(null)); await AssertThrowAsync <ArgumentException>(() => client.SendMessageAsync(EmberDataMessage)); await AssertThrowAsync <ArgumentException>(() => client.SendOutOfFrameByteAsync(0xFE)); client.Dispose(); await AssertThrowAsync <ObjectDisposedException>( () => client.SendMessageAsync(new S101Message(0x00, new KeepAliveRequest()))); } }); } }
public static async Task <int> TryFillAsync( ReadAsyncCallback read, byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (read == null) { throw new ArgumentNullException(nameof(read)); } int index = offset; int readCount; while ((readCount = await read(buffer, index, count, cancellationToken)) > 0) { index += readCount; count -= readCount; } return(index - offset); }
public static async Task<int> TryFillAsync( ReadAsyncCallback read, byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (read == null) { throw new ArgumentNullException(nameof(read)); } int index = offset; int readCount; while ((readCount = await read(buffer, index, count, cancellationToken)) > 0) { index += readCount; count -= readCount; } return index - offset; }
/// <summary>Gets a <see cref="S101Client"/> object, which receives the message contained in /// <paramref name="messageStream"/> as soon as a message has been sent by calling /// <see cref="S101Client.SendMessageAsync(S101Message)"/>.</summary> protected static S101Client GetFakeClient(Stream messageStream, IS101Logger logger) { var disposable = new CompleteOnDispose(); var requestReceived = new TaskCompletionSource <bool>(); ReadAsyncCallback read = async(b, o, c, t) => { await requestReceived.Task; return(await Read(messageStream, b, o, c, t, disposable.Task)); }; WriteAsyncCallback write = (b, o, c, t) => { requestReceived.SetResult(true); return(Task.FromResult(false)); }; return(new S101Client(disposable, read, write, logger, -1, 8192)); }
/// <summary>Initializes a new instance of the <see cref="TelnetStream"/> class.</summary> /// <exception cref="ArgumentNullException"><paramref name="readAsync"/>, <paramref name="writeAsync"/> and/or /// <paramref name="dataAvailable"/> equal <c>null</c>.</exception> public TelnetStream(ReadAsyncCallback readAsync, WriteAsyncCallback writeAsync, Func<bool> dataAvailable) : base( new ReadBuffer(readAsync, Defaults.PhysicalStreamBufferSize), new WriteBuffer(writeAsync, Defaults.PhysicalStreamBufferSize)) { if (readAsync == null) { throw new ArgumentNullException(nameof(readAsync)); } if (writeAsync == null) { throw new ArgumentNullException(nameof(writeAsync)); } if (dataAvailable == null) { throw new ArgumentNullException(nameof(dataAvailable)); } this.dataAvailable = dataAvailable; }
public S101Reader(ReadAsyncCallback readAsync, int bufferSize) { this.readBuffer = new ReadBuffer(readAsync, bufferSize); }
public S101Reader(ReadAsyncCallback readAsync) : this(readAsync, Constants.PhysicalStreamBufferSize) { }
public S101Client( IDisposable connection, ReadAsyncCallback readAsync, WriteAsyncCallback writeAsync, IS101Logger logger, int timeout, int bufferSize) { ReadAsyncCallback readAsyncWithLog; using (var connectionGuard = ScopeGuard.Create(connection)) using (var loggerGuard = ScopeGuard.Create(logger)) { if (SynchronizationContext.Current == null) { throw new NotSupportedException( "S101Client is not supported when SynchronizationContext.Current == null"); } if (connection == null) { throw new ArgumentNullException(nameof(connection)); } if (readAsync == null) { throw new ArgumentNullException(nameof(readAsync)); } if (writeAsync == null) { throw new ArgumentNullException(nameof(writeAsync)); } if (timeout < -1) { throw new ArgumentOutOfRangeException(nameof(timeout), "A number >= -1 is required."); } WriteAsyncCallback writeAsyncWithLog; if (logger == null) { readAsyncWithLog = readAsync; writeAsyncWithLog = writeAsync; } else { const string Type = "RawData"; readAsyncWithLog = async(b, o, c, t) => { var read = await readAsync(b, o, c, t); await this.logQueue.Enqueue(() => this.logger.LogData(Type, LogNames.Receive, b, o, read)); return(read); }; writeAsyncWithLog = async(b, o, c, t) => { await this.logQueue.Enqueue(() => this.logger.LogData(Type, LogNames.Send, b, o, c)); await writeAsync(b, o, c, t); }; } this.threadId = NativeMethods.GetCurrentThreadId(); this.writer = new S101Writer(writeAsyncWithLog, bufferSize); this.logger = logger; this.timeout = timeout; connectionGuard.Dismiss(); loggerGuard.Dismiss(); } this.ReadLoop(connection, new S101Reader(readAsyncWithLog, bufferSize)); }
public S101Client( IDisposable connection, ReadAsyncCallback readAsync, WriteAsyncCallback writeAsync, IS101Logger logger) : this(connection, readAsync, writeAsync, logger, 3000, 8192) { }
public S101Client(IDisposable connection, ReadAsyncCallback readAsync, WriteAsyncCallback writeAsync) : this(connection, readAsync, writeAsync, null) { }