Example #1
0
        protected override async Task <TTransport> AcceptImplementationAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(await Task.FromCanceled <TTransport>(cancellationToken));
            }

            if (_server == null)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
            }

            try
            {
                TTransport tSocketTransport = null;
                var        tcpClient        = await _server.AcceptTcpClientAsync();

                try
                {
                    tSocketTransport = new TSocketTransport(tcpClient)
                    {
                        Timeout = _clientTimeout
                    };

                    switch (_buffering)
                    {
                    case Buffering.BufferedTransport:
                        tSocketTransport = new TBufferedTransport(tSocketTransport);
                        break;

                    case Buffering.FramedTransport:
                        tSocketTransport = new TFramedTransport(tSocketTransport);
                        break;

                    default:
                        Debug.Assert(_buffering == Buffering.None);
                        break;
                    }

                    return(tSocketTransport);
                }
                catch (Exception)
                {
                    if (tSocketTransport != null)
                    {
                        tSocketTransport.Dispose();
                    }
                    else //  Otherwise, clean it up ourselves.
                    {
                        ((IDisposable)tcpClient).Dispose();
                    }

                    throw;
                }
            }
            catch (Exception ex)
            {
                throw new TTransportException(ex.ToString());
            }
        }
Example #2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _transport.Close();
                    _transport.Dispose();
                    _transport = null;
                }
            }

            _disposed = true;
        }