Пример #1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            try
            {
                var receiveTask = m_socket.ReceiveAsync(buffer, offset, count, SocketFlags.None);
                receiveTask.Wait();
                return(receiveTask.Result);
            }
            catch (AggregateException aggregateException)
            {
                Exception ex = aggregateException.Flatten().InnerException;

                if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }
                else
                {
                    throw new IOException("failure read from socket", ex);
                }
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }
                else
                {
                    throw new IOException("failure read from socket", ex);
                }
            }
        }