예제 #1
0
 protected override void Dispose(bool disposing)
 {
     if (disposing && !closedCancelSource.IsCancellationRequested)
     {
         writeBuffer.CloseWrite();
         readBuffer.CloseRead();
         try {
             Task.WhenAll(writeTask, readTask).Wait();
         }
         catch (AggregateException) {
         }
         closedCancelSource.Cancel();
         if (WriteStream != null)
         {
             WriteStream.Close();
         }
         if (ReadStream != null)
         {
             ReadStream.Close();
         }
         closedCancelSource.Dispose();
     }
     base.Dispose(disposing);
 }
예제 #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);
            }
        }