コード例 #1
0
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
 {
     if (m_Position >= m_Offset + m_Size)
     {
         count = 0;
     }
     else if (m_Position + count > m_Offset + m_Size)
     {
         count = (int)(m_Offset + m_Size - m_Position);
     }
     return(WrappedStream.BeginRead(buffer, offset, count, callback, state));
 }
コード例 #2
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
        {
            try {
                if (Interlocked.Increment(ref m_ReadNesting) != 1)
                {
                    throw new NotSupportedException(SR.GetString(SR.net_io_invalidnestedcall, "BeginRead", "read"));
                }

                if (m_ReadCallback == null)
                {
                    m_ReadCallback = new AsyncCallback(ReadCallback);
                }

                if (m_HeadEOF)
                {
                    return(WrappedStream.BeginRead(buffer, offset, count, callback, state));
                }
                else
                {
                    InnerAsyncResult userResult = new InnerAsyncResult(state, callback, buffer, offset, count);
                    IAsyncResult     ar         = m_HeadStream.BeginRead(buffer, offset, count, m_ReadCallback, userResult);

                    if (!ar.CompletedSynchronously)
                    {
                        return(userResult);
                    }

                    int bytes = m_HeadStream.EndRead(ar);
                    m_HeadLength += bytes;

                    //check on EOF condition
                    if (bytes == 0 && userResult.Count != 0)
                    {
                        //Got a first stream EOF
                        m_HeadEOF = true;
                        m_HeadStream.Close();
                        return(WrappedStream.BeginRead(buffer, offset, count, callback, state));
                    }
                    else
                    {
                        // just complete user IO
                        userResult.Buffer = null;
                        userResult.InvokeCallback(count);
                        return(userResult);
                    }
                }
            }
            catch {
                Interlocked.Decrement(ref m_ReadNesting);
                throw;
            }
        }
コード例 #3
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);
            }
        }
コード例 #4
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
        {
            if (Interlocked.Increment(ref m_ReadNesting) != 1)
            {
                throw new NotSupportedException(SR.GetString(SR.net_io_invalidnestedcall, "BeginRead", "read"));
            }

            try {
                if (m_ReadCallback == null)
                {
                    m_ReadCallback = new AsyncCallback(ReadCallback);
                }

                if (m_ShadowStreamIsDead && m_BytesToSkip == 0L)
                {
                    return(WrappedStream.BeginRead(buffer, offset, count, callback, state));
                }
                else
                {
                    InnerAsyncResult userResult = new InnerAsyncResult(state, callback, buffer, offset, count);
                    if (m_BytesToSkip != 0L)
                    {
                        InnerAsyncResult temp = userResult;
                        userResult = new InnerAsyncResult(temp, null, new byte[4096],
                                                          0, m_BytesToSkip < (long)buffer.Length? (int)m_BytesToSkip: buffer.Length);
                    }
                    IAsyncResult result = WrappedStream.BeginRead(userResult.Buffer, userResult.Offset, userResult.Count, m_ReadCallback, userResult);
                    if (result.CompletedSynchronously)
                    {
                        ReadComplete(result);
                    }
                    return(userResult);
                }
            }
            catch {
                Interlocked.Decrement(ref m_ReadNesting);
                throw;
            }
        }
コード例 #5
0
ファイル: SnapBinaryPatcher.cs プロジェクト: hutterm/snapx
 /// <summary>
 /// Begins an asynchronous read operation.
 /// </summary>
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     ThrowIfDisposed();
     return(WrappedStream.BeginRead(buffer, offset, count, callback, state));
 }
コード例 #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);
            }
        }
コード例 #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;
                }
            }
        }