/// <summary> /// Ends an async read operation on the stream. /// </summary> /// <param name="asyncResult">The async operation created with BeginRead call.</param> public override int EndRead(IAsyncResult asyncResult) { ExceptionUtilities.Assert(!this.FailOnRead, "A read operation should never be called on this stream."); TestAsyncResult testAsyncResult = asyncResult as TestAsyncResult; if (testAsyncResult == null) { throw new ArgumentException("Unexpected IAsyncResult implementation."); } return(((Func <int>)testAsyncResult.CustomState)()); }
/// <summary> /// Ends an async write operation on the stream. /// </summary> /// <param name="asyncResult">The async operation created with BeginWrite call.</param> public override void EndWrite(IAsyncResult asyncResult) { ExceptionUtilities.Assert(!this.FailOnWrite, "A write operation should never be called on this stream."); TestAsyncResult testAsyncResult = asyncResult as TestAsyncResult; if (testAsyncResult == null) { throw new ArgumentException("Unexpected IAsyncResult implementation."); } ((Action)testAsyncResult.CustomState)(); }
/// <summary> /// Begins an async read operation on the stream. /// </summary> /// <param name="buffer">The buffer to write the data to.</param> /// <param name="offset">Offset in the buffer to start writing at.</param> /// <param name="count">Number of bytes to read.</param> /// <param name="callback">Async callback.</param> /// <param name="state">Async callback state.</param> /// <returns>An async operation representing the read.</returns> public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { ExceptionUtilities.Assert(!this.FailOnRead, "A read operation should never be called on this stream."); if (this.AsyncMethodBehaviorsEnumerator != null) { this.AsyncMethodBehavior = this.AsyncMethodBehaviorsEnumerator.MoveNext() ? this.AsyncMethodBehaviorsEnumerator.Current : default(AsyncMethodBehavior); } TestAsyncResult asyncResult = TestAsyncResult.Create(this.AsyncMethodBehavior, callback, state); asyncResult.CustomState = (Func <int>)(() => { return(this.InnerStream.Read(buffer, offset, count)); }); asyncResult.Start(); return(asyncResult); }