Exemple #1
0
        public override async Task <int> ReadAsync(byte[] buf, int offset, int length, CancellationToken cancel_token)
        {
            if (closedCancelSource.IsCancellationRequested)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
            CheckReadException();
            var len = await readBuffer.ReadAsync(buf, offset, length, cancel_token).ConfigureAwait(false);

            CheckReadException();
            readBytesCounter.Add(len);
            return(len);
        }
Exemple #2
0
        private async Task ProcessWrite(Stream s)
        {
            writeBuffer.ReadTimeout = Timeout.Infinite;
            var buf = new byte[64 * 1024];
            var ct  = CancellationToken.None;

            try {
                var len = await writeBuffer.ReadAsync(buf, 0, buf.Length, ct).ConfigureAwait(false);

                while (len > 0)
                {
                    await s.WriteAsync(buf, 0, len, ct).ConfigureAwait(false);

                    len = await writeBuffer.ReadAsync(buf, 0, buf.Length, ct).ConfigureAwait(false);
                }
                await s.FlushAsync(ct).ConfigureAwait(false);

                socket.Shutdown(SocketShutdown.Send);
            }
            catch (IOException) {
                if (!ct.IsCancellationRequested)
                {
                    throw;
                }
            }
            catch (ObjectDisposedException) {
                if (!ct.IsCancellationRequested)
                {
                    throw;
                }
            }
            finally {
                writeBuffer.CloseRead();
                closedCancelSource.CancelAfter(CloseTimeout);
            }
        }