示例#1
0
        // returns true is the stream could be reused.
        internal bool FlushInput()
        {
            if (!HasEntityBody)
            {
                return(true);
            }

            int length = 2048;

            if (content_length > 0)
            {
                length = (int)Math.Min(content_length, (long)length);
            }

            byte [] bytes = new byte [length];
            while (true)
            {
                // TODO: test if MS has a timeout when doing this
                try {
                    IAsyncResult ares = InputStream.BeginRead(bytes, 0, length, null, null);
                    if (!ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne(1000))
                    {
                        return(false);
                    }
                    if (InputStream.EndRead(ares) <= 0)
                    {
                        return(true);
                    }
                } catch {
                    return(false);
                }
            }
        }
        // Returns true is the stream could be reused.
        internal bool FlushInput()
        {
            if (!HasEntityBody)
            {
                return(true);
            }

            var length = 2048;

            if (_contentLength > 0)
            {
                length = (int)Math.Min(_contentLength, (long)length);
            }

            var buffer = new byte [length];

            while (true)
            {
                // TODO: Test if MS has a timeout when doing this.
                try {
                    var ares = InputStream.BeginRead(buffer, 0, length, null, null);
                    if (!ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne(100))
                    {
                        return(false);
                    }

                    if (InputStream.EndRead(ares) <= 0)
                    {
                        return(true);
                    }
                } catch {
                    return(false);
                }
            }
        }
        internal bool FlushInput()
        {
            //Discarded unreachable code: IL_0091
            if (!HasEntityBody)
            {
                return(true);
            }
            int num = 2048;

            if (_contentLength > 0)
            {
                num = (int)Math.Min(_contentLength, num);
            }
            byte[] buffer = new byte[num];
            while (true)
            {
                try
                {
                    IAsyncResult asyncResult = InputStream.BeginRead(buffer, 0, num, null, null);
                    if (!asyncResult.IsCompleted && !asyncResult.AsyncWaitHandle.WaitOne(100))
                    {
                        return(false);
                    }
                    if (InputStream.EndRead(asyncResult) <= 0)
                    {
                        return(true);
                    }
                }
                catch
                {
                    return(false);
                }
            }
        }
 protected virtual void Cleanup()
 {
     if (recvResult != null && recvResult.IsCompleted)
     {
         try {
             int bytes = InputStream.EndRead(recvResult);
             if (bytes < 0)
             {
                 OnError();
             }
             else
             {
                 recvBytesCounter.Add(bytes);
             }
         }
         catch (ObjectDisposedException) {}
         catch (IOException) {
             OnError();
         }
     }
     if (sendResult != null)
     {
         try {
             OutputStream.EndWrite(sendResult);
             sendBytesCounter.Add((int)sendResult.AsyncState);
         }
         catch (ObjectDisposedException) {}
         catch (IOException) {
             OnError();
         }
     }
     if (!HasError && sendStream.Length > 0)
     {
         var buf = sendStream.ToArray();
         try {
             OutputStream.Write(buf, 0, buf.Length);
         }
         catch (ObjectDisposedException) {}
         catch (IOException) {
             OnError();
         }
     }
     recvResult = null;
     sendResult = null;
     sendStream.SetLength(0);
     sendStream.Position = 0;
     recvStream.SetLength(0);
     recvStream.Position = 0;
     this.InputStream.Close();
     this.OutputStream.Close();
 }
示例#5
0
        // returns true is the stream could be reused.
        internal bool FlushInput()
        {
            if (!HasEntityBody)
            {
                return(true);
            }

            var length = 2048;

            if (ContentLength64 > 0)
            {
                length = (int)Math.Min(ContentLength64, length);
            }

            var bytes = new byte[length];

            while (true)
            {
                // TODO: test if MS has a timeout when doing this
                try
                {
                    var ares = InputStream.BeginRead(bytes, 0, length, null, null);
                    if (!ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne(1000))
                    {
                        return(false);
                    }
                    if (InputStream.EndRead(ares) <= 0)
                    {
                        return(true);
                    }
                }
                catch (ObjectDisposedException)
                {
                    _inputStream = null;
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
        }
示例#6
0
        internal bool FlushInput()
        {
            if (!HasEntityBody)
            {
                return(true);
            }

            var len = 2048;

            if (_contentLength > 0 && _contentLength < len)
            {
                len = (int)_contentLength;
            }

            var buff = new byte[len];

            while (true)
            {
                try
                {
                    var ares = InputStream.BeginRead(buff, 0, len, null, null);
                    if (!ares.IsCompleted)
                    {
                        var timeout = 100;
                        if (!ares.AsyncWaitHandle.WaitOne(timeout))
                        {
                            return(false);
                        }
                    }

                    if (InputStream.EndRead(ares) <= 0)
                    {
                        return(true);
                    }
                }
                catch
                {
                    return(false);
                }
            }
        }
        // returns true is the stream could be reused.
        internal bool FlushInput()
        {
            if (!HasEntityBody)
            {
                return(true);
            }

            int length = 2048;

            if (_contentLength > 0)
            {
                length = (int)Math.Min(_contentLength, (long)length);
            }

            byte[] bytes = new byte[length];
            while (true)
            {
                try
                {
                    IAsyncResult ares = InputStream.BeginRead(bytes, 0, length, null, null);
                    if (!ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne(1000))
                    {
                        return(false);
                    }
                    if (InputStream.EndRead(ares) <= 0)
                    {
                        return(true);
                    }
                }
                catch (ObjectDisposedException)
                {
                    _inputStream = null;
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
        }
示例#8
0
        public void OnCompleteRead(IAsyncResult asyncResult)
        {
            //异步读取一个快,接收数据
            int bytesRead = InputStream.EndRead(asyncResult);

            //如果没有任何字节,则流已达文件结尾
            if (bytesRead > 0)
            {
                //暂停以对模拟对数据块的处理
                Debug.WriteLine("异步线程:已读取一块内容");
                var datastr = Encoding.GetEncoding("gb2312").GetString(buffer, 0, buffer.Length);
                ReadValue.Append(datastr);
                //
                Thread.Sleep(TimeSpan.FromMilliseconds(20));
                //开始读取下一块
                InputStream.BeginRead(buffer, 0, buffer.Length, OnCompleteRead, null);
            }
            else
            {
                //操作结束
                Debug.WriteLine("   异步线程:读取文件结束");
                OnIsReturnEvent();
            }
        }
 private void ProcessRecv()
 {
     if (recvResult != null && recvResult.IsCompleted)
     {
         try {
             int bytes = InputStream.EndRead(recvResult);
             if (bytes > 0)
             {
                 recvBytesCounter.Add(bytes);
                 recvStream.Seek(0, SeekOrigin.End);
                 recvStream.Write(recvBuffer, 0, bytes);
                 recvStream.Seek(0, SeekOrigin.Begin);
             }
             else if (bytes < 0)
             {
                 OnError();
             }
         }
         catch (ObjectDisposedException) {}
         catch (IOException) {
             OnError();
         }
         recvResult = null;
     }
     if (!HasError && recvResult == null)
     {
         try {
             recvResult = InputStream.BeginRead(recvBuffer, 0, recvBuffer.Length, null, null);
         }
         catch (ObjectDisposedException) {
         }
         catch (IOException) {
             OnError();
         }
     }
 }
示例#10
0
 /// <summary>等待挂起的异步读完成</summary>
 /// <param name="asyncResult"></param>
 /// <returns></returns>
 public override int EndRead(IAsyncResult asyncResult)
 {
     return(InputStream.EndRead(asyncResult));
 }