Exemplo n.º 1
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (Interlocked.Decrement(ref m_ReadNesting) != 0)
            {
                Interlocked.Increment(ref m_ReadNesting);
                throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndRead"));
            }

            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            InnerAsyncResult myResult = asyncResult as InnerAsyncResult;

            if (myResult == null)
            {
                // We are just passing IO down, although m_HeadEOF should be always true here.
                GlobalLog.Assert(m_HeadEOF, "CombinedReadStream::EndRead|m_HeadEOF is false and asyncResult is not of InnerAsyncResult type {0).", asyncResult.GetType().FullName);
                return(m_HeadEOF? WrappedStream.EndRead(asyncResult): m_HeadStream.EndRead(asyncResult));
            }

            // this is our wrapped AsyncResult
            myResult.InternalWaitForCompletion();

            // Exception?
            if (myResult.Result is Exception)
            {
                throw (Exception)(myResult.Result);
            }

            // Report the count read
            return((int)myResult.Result);
        }
Exemplo n.º 2
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            int result = WrappedStream.EndRead(asyncResult);

            m_Position += result;
            return(result);
        }
Exemplo n.º 3
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (Interlocked.Decrement(ref m_ReadNesting) != 0)
            {
                Interlocked.Increment(ref m_ReadNesting);
                throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndRead"));
            }

            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            InnerAsyncResult myResult = asyncResult as InnerAsyncResult;

            if (myResult == null)
            {
                // We are just passing IO down, although the shadow stream should be dead for now.
                GlobalLog.Assert(m_ShadowStreamIsDead, "ForwardingReadStream::EndRead|m_ShadowStreamIsDead is false and asyncResult is not of InnerAsyncResult type {0}.", asyncResult.GetType().FullName);
                int bytes = WrappedStream.EndRead(asyncResult);
                if (bytes == 0)
                {
                    m_SeenReadEOF = true;
                }
            }

            // this is our wrapped AsyncResult
            bool suceess = false;

            try {
                myResult.InternalWaitForCompletion();
                // Exception?
                if (myResult.Result is Exception)
                {
                    throw (Exception)(myResult.Result);
                }
                suceess = true;
            }
            finally {
                if (!suceess && !m_ShadowStreamIsDead)
                {
                    m_ShadowStreamIsDead = true;
                    if (m_ShadowStream is ICloseEx)
                    {
                        ((ICloseEx)m_ShadowStream).CloseEx(CloseExState.Abort | CloseExState.Silent);
                    }
                    else
                    {
                        m_ShadowStream.Close();
                    }
                }
            }

            // Report the read count
            return((int)myResult.Result);
        }
Exemplo n.º 4
0
        private void ReadCallback(IAsyncResult transportResult)
        {
            GlobalLog.Assert(transportResult.AsyncState is InnerAsyncResult, "InnerAsyncResult::ReadCallback|The state expected to be of type InnerAsyncResult, received {0}.", transportResult.GetType().FullName);
            if (transportResult.CompletedSynchronously)
            {
                return;
            }

            InnerAsyncResult userResult = transportResult.AsyncState as InnerAsyncResult;

            try {
                // Complete transport IO, in this callback that is always the head stream
                int count;
                if (!m_HeadEOF)
                {
                    count         = m_HeadStream.EndRead(transportResult);
                    m_HeadLength += count;
                }
                else
                {
                    count = WrappedStream.EndRead(transportResult);
                }


                //check on EOF condition
                if (!m_HeadEOF && count == 0 && userResult.Count != 0)
                {
                    //Got a first stream EOF
                    m_HeadEOF = true;
                    m_HeadStream.Close();
                    IAsyncResult ar = WrappedStream.BeginRead(userResult.Buffer, userResult.Offset, userResult.Count, m_ReadCallback, userResult);
                    if (!ar.CompletedSynchronously)
                    {
                        return;
                    }
                    count = WrappedStream.EndRead(ar);
                }
                // just complete user IO
                userResult.Buffer = null;
                userResult.InvokeCallback(count);
            }
            catch (Exception e) {
                //ASYNC: try to ignore even serious exceptions (nothing to loose?)
                if (userResult.InternalPeekCompleted)
                {
                    throw;
                }

                userResult.InvokeCallback(e);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Waits for the pending asynchronous read to complete.
 /// </summary>
 public override int EndRead(IAsyncResult asyncResult)
 {
     ThrowIfDisposed();
     return(WrappedStream.EndRead(asyncResult));
 }
Exemplo n.º 6
0
        public async Task Parameterized_Methods_Are_Properly_Wrapped()
        {
            var strm = Substitute.For <Stream>();

            using (var wrprstrm = new WrappedStream(strm, false))
            {
                wrprstrm.Seek(0, SeekOrigin.Current);
                strm.Received(1).Seek(0, SeekOrigin.Current);

                wrprstrm.SetLength(10);
                strm.Received(1).SetLength(10);

                var arrByte = new byte[5];
                wrprstrm.Read(arrByte, 0, arrByte.Length);
                strm.Received(1).Read(arrByte, 0, arrByte.Length);

                wrprstrm.Write(arrByte, 0, arrByte.Length);
                strm.Received(1).Write(arrByte, 0, arrByte.Length);

                var anotherStrm = new MemoryStream();
                await wrprstrm.CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                await wrprstrm.ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                var cb     = new AsyncCallback(ar => { });
                var result = wrprstrm.BeginRead(arrByte, 0, arrByte.Length, cb, null);
                strm.Received(1).BeginRead(arrByte, 0, arrByte.Length, cb, null);

                wrprstrm.BeginWrite(arrByte, 0, arrByte.Length, cb, null);
                strm.Received(1).BeginWrite(arrByte, 0, arrByte.Length, cb, null);

#if FEATURE_REMOTING
                wrprstrm.CreateObjRef(null);
                strm.Received(1).CreateObjRef(null);

                wrprstrm.InitializeLifetimeService();
                strm.Received(1).InitializeLifetimeService();
#else
                Assert.True(Assert.Throws <RemotingException>(() => wrprstrm.CreateObjRef(null))
                            .Message.Equals(WrappedStream.RemotingErrorTxt));
                Assert.True(Assert.Throws <RemotingException>(() => wrprstrm.InitializeLifetimeService())
                            .Message.Equals(WrappedStream.RemotingErrorTxt));
#endif
                wrprstrm.EndRead(result);
                strm.Received(1).EndRead(result);

                wrprstrm.EndWrite(result);
                strm.Received(1).EndWrite(result);

                wrprstrm.WriteByte(20);
                strm.Received(1).WriteByte(20);

                wrprstrm.Equals(null);
                strm.Received(1).Equals(null);
            }
        }
Exemplo n.º 7
0
        private void ReadComplete(IAsyncResult transportResult)
        {
            while (true)
            {
                // Recover our asyncResult
                InnerAsyncResult userResult = transportResult.AsyncState as InnerAsyncResult;

                try
                {
                    if (!userResult.IsWriteCompletion)
                    {
                        userResult.Count = WrappedStream.EndRead(transportResult);
                        if (userResult.Count == 0)
                        {
                            m_SeenReadEOF = true;
                        }


                        if (!m_ShadowStreamIsDead)
                        {
                            userResult.IsWriteCompletion = true;
                            //Optionally charge notification write IO
                            transportResult = m_ShadowStream.BeginWrite(userResult.Buffer, userResult.Offset, userResult.Count, m_ReadCallback, userResult);
                            if (transportResult.CompletedSynchronously)
                            {
                                continue;
                            }
                            return;
                        }
                    }
                    else
                    {
                        GlobalLog.Assert(!m_ShadowStreamIsDead, "ForwardingReadStream::ReadComplete|ERROR: IsWriteCompletion && m_ShadowStreamIsDead");

                        m_ShadowStream.EndWrite(transportResult);
                        userResult.IsWriteCompletion = false;
                    }
                }
                catch (Exception e)
                {
                    //ASYNC: try to ignore even serious exceptions (nothing to loose?)
                    if (userResult.InternalPeekCompleted)
                    {
                        GlobalLog.Print("ShadowReadStream::ReadComplete() Rethrowing Exception (end), userResult.IsCompleted, stack trace = " + e.ToString());
                        throw;
                    }

                    try
                    {
                        m_ShadowStreamIsDead = true;
                        if (m_ShadowStream is ICloseEx)
                        {
                            ((ICloseEx)m_ShadowStream).CloseEx(CloseExState.Abort | CloseExState.Silent);
                        }
                        else
                        {
                            m_ShadowStream.Close();
                        }
                    }
                    catch (Exception ee)
                    {
                        //ASYNC: Again try to ignore even serious exceptions
                        GlobalLog.Print("ShadowReadStream::ReadComplete() Got (ignoring) Exception, on shadow stream.Close, stack trace = " + ee.ToString());
                    }

                    if (!userResult.IsWriteCompletion || m_ThrowOnWriteError)
                    {
                        if (transportResult.CompletedSynchronously)
                        {
                            throw;
                        }

                        userResult.InvokeCallback(e);
                        return;
                    }
                }

                // Need to process, re-issue the read.
                try
                {
                    if (m_BytesToSkip != 0L)
                    {
                        m_BytesToSkip   -= userResult.Count;
                        userResult.Count = m_BytesToSkip < (long)userResult.Buffer.Length? (int)m_BytesToSkip: userResult.Buffer.Length;
                        if (m_BytesToSkip == 0L)
                        {
                            // we did hide the original IO request in the outer iaresult state.
                            // charge the real user operation now
                            transportResult = userResult;
                            userResult      = userResult.AsyncState as InnerAsyncResult;
                            GlobalLog.Assert(userResult != null, "ForwardingReadStream::ReadComplete|ERROR: Inner IAResult is null after stream FastForwarding.");
                        }
                        transportResult = WrappedStream.BeginRead(userResult.Buffer, userResult.Offset, userResult.Count, m_ReadCallback, userResult);
                        if (transportResult.CompletedSynchronously)
                        {
                            continue;
                        }
                        return;
                    }
                    //if came to here, complete original user IO
                    userResult.InvokeCallback(userResult.Count);
                    return;
                }
                catch (Exception e)
                {
                    //ASYNC: try to ignore even serious exceptions (nothing to loose?)
                    if (userResult.InternalPeekCompleted)
                    {
                        GlobalLog.Print("ShadowReadStream::ReadComplete() Rethrowing Exception (begin), userResult.IsCompleted, stack trace = " + e.ToString());
                        throw;
                    }

                    try
                    {
                        m_ShadowStreamIsDead = true;
                        if (m_ShadowStream is ICloseEx)
                        {
                            ((ICloseEx)m_ShadowStream).CloseEx(CloseExState.Abort | CloseExState.Silent);
                        }
                        else
                        {
                            m_ShadowStream.Close();
                        }
                    }
                    catch (Exception ee)
                    {
                        //ASYNC: Again try to ignore even serious exceptions
                        GlobalLog.Print("ShadowReadStream::ReadComplete() Got (ignoring) Exception, on shadow stream.Close (after begin), stack trace = " + ee.ToString());
                    }

                    if (transportResult.CompletedSynchronously)
                    {
                        throw;
                    }

                    // This will set the exception result first then try to execute a user callback
                    userResult.InvokeCallback(e);
                    return;
                }
            }
        }
Exemplo n.º 8
0
 public override int EndRead(IAsyncResult asyncResult)
 {
     return(WrappedStream.EndRead(asyncResult));
 }