Beispiel #1
0
        internal void Init(IBufferManager bufferManager, AmqpSettings amqpSettings, IAsyncTransport transport, Open open)
        {
            transport.SetConnection(this);

            this.BufferManager      = bufferManager;
            this.channelMax         = GetChannelMax(amqpSettings, open);
            this.maxFrameSize       = GetMaxFrameSize(amqpSettings, open);
            this.MaxLinksPerSession = amqpSettings.MaxLinksPerSession;
            this.writer             = new TransportWriter(transport, this.OnIoException);

            // after getting the transport, move state to open pipe before starting the pump
            if (open == null)
            {
                open = new Open()
                {
                    ContainerId  = amqpSettings.ContainerId,
                    HostName     = amqpSettings.HostName ?? this.address.Host,
                    ChannelMax   = this.channelMax,
                    MaxFrameSize = this.maxFrameSize,
                    IdleTimeOut  = (uint)amqpSettings.IdleTimeout / 2
                };
            }

            if (open.IdleTimeOut > 0)
            {
                this.heartBeat = new HeartBeat(this, open.IdleTimeOut * 2);
            }

            this.SendHeader();
            this.SendOpen(open);
            this.state = ConnectionState.OpenPipe;
        }
Beispiel #2
0
 public Writer(TcpTransport owner, IAsyncTransport transport)
 {
     this.owner             = owner;
     this.transport         = transport;
     this.bufferQueue       = new Queue <ByteBuffer>();
     this.buffersInProgress = new List <ByteBuffer>();
 }
Beispiel #3
0
        internal Connection(IBufferManager bufferManager, AmqpSettings amqpSettings, Address address,
                            IAsyncTransport transport, Open open, OnOpened onOpened)
            : this((ushort)(amqpSettings.MaxSessionsPerConnection - 1), (uint)amqpSettings.MaxFrameSize)
        {
            this.BufferManager      = bufferManager;
            this.MaxLinksPerSession = amqpSettings.MaxLinksPerSession;
            this.address            = address;
            this.onOpened           = onOpened;
            this.transport          = transport;
            transport.SetConnection(this);

            // after getting the transport, move state to open pipe before starting the pump
            if (open == null)
            {
                open = new Open()
                {
                    ContainerId  = amqpSettings.ContainerId,
                    HostName     = amqpSettings.HostName ?? this.address.Host,
                    ChannelMax   = this.channelMax,
                    MaxFrameSize = this.maxFrameSize
                };
            }

            this.SendHeader();
            this.SendOpen(open);
            this.state = State.OpenPipe;
        }
        async Task HandleTransportAsync(IAsyncTransport transport)
        {
            if (this.saslSettings != null)
            {
                ListenerSaslProfile profile = new ListenerSaslProfile(this);
                transport = await profile.OpenAsync(null, this.BufferManager, transport);
            }

            Connection connection  = new ListenerConnection(this, this.address, transport);
            bool       shouldClose = false;

            lock (this.connections)
            {
                if (!this.closed)
                {
                    connection.Closed += this.OnConnectionClosed;
                    this.connections.Add(connection);
                }
                else
                {
                    shouldClose = true;
                }
            }

            if (shouldClose)
            {
                await connection.CloseAsync();
            }
            else
            {
                AsyncPump pump = new AsyncPump(this.BufferManager, transport);
                pump.Start(connection);
            }
        }
Beispiel #5
0
        internal Connection(IBufferManager bufferManager, AmqpSettings amqpSettings, Address address,
                            IAsyncTransport transport, Open open, OnOpened onOpened, IHandler handler)
            : this(address, (ushort)(amqpSettings.MaxSessionsPerConnection - 1), (uint)amqpSettings.MaxFrameSize)
        {
            transport.SetConnection(this);

            this.handler            = handler;
            this.BufferManager      = bufferManager;
            this.MaxLinksPerSession = amqpSettings.MaxLinksPerSession;
            this.onOpened           = onOpened;
            this.writer             = new TransportWriter(transport, this.OnIoException);

            // after getting the transport, move state to open pipe before starting the pump
            if (open == null)
            {
                open = new Open()
                {
                    ContainerId  = amqpSettings.ContainerId,
                    HostName     = amqpSettings.HostName ?? this.address.Host,
                    ChannelMax   = this.channelMax,
                    MaxFrameSize = this.maxFrameSize,
                    IdleTimeOut  = (uint)amqpSettings.IdleTimeout
                };
            }

            if (open.IdleTimeOut > 0)
            {
                this.heartBeat = new HeartBeat(this, open.IdleTimeOut);
            }

            this.SendHeader();
            this.SendOpen(open);
            this.state = ConnectionState.OpenPipe;
        }
Beispiel #6
0
            protected async Task HandleSocketAsync(Socket socket)
            {
                try
                {
                    if (this.Listener.tcpSettings != null)
                    {
                        this.Listener.tcpSettings.Configure(socket);
                    }

                    IHandler handler = this.Listener.HandlerFactory?.Invoke(this.Listener);
                    if (handler != null && handler.CanHandle(EventId.SocketAccept))
                    {
                        handler.Handle(Event.Create(EventId.SocketAccept, null, context: socket));
                    }

                    IAsyncTransport transport = await this.CreateTransportAsync(socket, handler).ConfigureAwait(false);

                    await this.Listener.HandleTransportAsync(transport, handler, socket).ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    Trace.WriteLine(TraceLevel.Error, exception.ToString());
                    socket.Dispose();
                }
            }
Beispiel #7
0
 internal Connection(IBufferManager bufferManager, AmqpSettings amqpSettings, Address address,
                     IAsyncTransport transport, Open open, OnOpened onOpened, IHandler handler)
     : this(address, DefaultMaxSessions, DefaultMaxFrameSize)
 {
     this.onOpened = onOpened;
     this.handler  = handler;
     this.Init(bufferManager, amqpSettings, transport, open);
 }
Beispiel #8
0
        public async Task ConnectAsync(Address address, ConnectionFactory factory)
        {
            Socket socket;

            IPAddress[] ipAddresses;
            IPAddress   ipAddress;

            if (IPAddress.TryParse(address.Host, out ipAddress))
            {
                socket      = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                ipAddresses = new IPAddress[] { ipAddress };
            }
            else
            {
                socket      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ipAddresses = Dns.GetHostEntry(address.Host).AddressList;
            }

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            await Task.Factory.FromAsync(
                (c, s) => ((Socket)s).BeginConnect(ipAddresses, address.Port, c, s),
                (r) => ((Socket)r.AsyncState).EndConnect(r),
                socket);

            IAsyncTransport transport;

            if (address.UseSsl)
            {
                SslStream sslStream;
                var       ssl = factory.sslSettings;
                if (ssl == null)
                {
                    sslStream = new SslStream(new NetworkStream(socket));
                    await sslStream.AuthenticateAsClientAsync(address.Host);
                }
                else
                {
                    sslStream = new SslStream(new NetworkStream(socket), false, ssl.RemoteCertificateValidationCallback);
                    await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                                                              ssl.Protocols, ssl.CheckCertificateRevocation);
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
            this.writer          = new Writer(this, this.socketTransport);
        }
Beispiel #9
0
        async Task HandleTransportAsync(IAsyncTransport transport, IHandler handler, object context)
        {
            IPrincipal principal = null;

            if (this.saslSettings != null)
            {
                ListenerSaslProfile profile = new ListenerSaslProfile(this);
                transport = await profile.NegotiateAsync(transport).ConfigureAwait(false);

                principal = profile.GetPrincipal();
            }

            var connection = new ListenerConnection(this, this.address, handler, transport);

            if (principal == null)
            {
                // SASL principal preferred. If not present, check transport.
                IAuthenticated authenticated = transport as IAuthenticated;
                if (authenticated != null)
                {
                    principal = authenticated.Principal;
                }
            }

            connection.Principal = principal;

            bool shouldClose = false;

            lock (this.connections)
            {
                if (!this.closed)
                {
                    connection.Closed += this.OnConnectionClosed;
                    this.connections.Add(connection);
                }
                else
                {
                    shouldClose = true;
                }
            }

            if (shouldClose)
            {
                await connection.CloseAsync().ConfigureAwait(false);
            }
            else
            {
                if (handler != null && handler.CanHandle(EventId.ConnectionAccept))
                {
                    handler.Handle(Event.Create(EventId.ConnectionAccept, connection, context: context));
                }

                AsyncPump pump = new AsyncPump(this.BufferManager, transport);
                pump.Start(connection);
            }
        }
        public TransportStream(ITransport transport, FileAccess accessMode, bool ownsTransport)
        {
            // parameters validation
            if (transport == null)
                throw new ArgumentNullException("transport", "Transport can not be null");
            if (!transport.IsConnected)
                throw new ArgumentException("Transport not connected", "transport");

            _accessMode = accessMode;
            _ownsTransport = ownsTransport;
            _transport = TransportAsyncWrapper.GetWrapper(transport);
        }
        async Task HandleTransportAsync(IAsyncTransport transport)
        {
            IPrincipal principal = null;

            if (this.saslSettings != null)
            {
                ListenerSaslProfile profile = new ListenerSaslProfile(this);
                transport = await profile.NegotiateAsync(transport);

                principal = profile.GetPrincipal();
            }

            IHandler handler    = this.HandlerFactory?.Invoke(this);
            var      connection = new ListenerConnection(this, this.address, handler, transport);

            if (principal == null)
            {
                // SASL principal preferred. If not present, check transport.
                IAuthenticated authenticated = transport as IAuthenticated;
                if (authenticated != null)
                {
                    principal = authenticated.Principal;
                }
            }

            connection.Principal = principal;

            bool shouldClose = false;

            lock (this.connections)
            {
                if (!this.closed)
                {
                    connection.Closed += this.OnConnectionClosed;
                    this.connections.Add(connection);
                }
                else
                {
                    shouldClose = true;
                }
            }

            if (shouldClose)
            {
                await connection.CloseAsync();
            }
            else
            {
                AsyncPump pump = new AsyncPump(this.BufferManager, transport);
                pump.Start(connection);
            }
        }
        public TransportStream(ITransport transport, FileAccess accessMode, bool ownsTransport)
        {
            // parameters validation
            if (transport == null)
            {
                throw new ArgumentNullException("pipe", "Transport can not be null");
            }
            if (!transport.IsConnected)
            {
                throw new ArgumentException("Transport not connected", "transport");
            }

            _accessMode    = accessMode;
            _ownsTransport = ownsTransport;
            _transport     = TransportAsyncWrapper.GetWrapper(transport);
        }
		new protected virtual void Dispose(bool disposing)
		{
			if (_disposed)
				return;

			if (_transport != null)
			{
				if (_ownsTransport)
				{
					// close transport connection
					_transport.Close();
				}

				_transport = null;
			}

			_disposed = true;
		}
Beispiel #14
0
            protected async Task HandleSocketAsync(Socket socket)
            {
                try
                {
                    if (this.Listener.tcpSettings != null)
                    {
                        this.Listener.tcpSettings.Configure(socket);
                    }

                    IAsyncTransport transport = await this.CreateTransportAsync(socket);

                    await this.Listener.HandleTransportAsync(transport);
                }
                catch (Exception exception)
                {
                    Trace.WriteLine(TraceLevel.Error, exception.ToString());
                    socket.Dispose();
                }
            }
Beispiel #15
0
        public async Task ConnectAsync(Address address, ConnectionFactory factory)
        {
            Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            await Task.Factory.FromAsync(
                (c, s) => ((Socket)s).BeginConnect(address.Host, address.Port, c, s),
                (r) => ((Socket)r.AsyncState).EndConnect(r),
                socket);

            IAsyncTransport transport;

            if (address.UseSsl)
            {
                SslStream sslStream;
                var       ssl = factory.SslInternal;
                if (ssl == null)
                {
                    sslStream = new SslStream(new NetworkStream(socket));
                    await sslStream.AuthenticateAsClientAsync(address.Host);
                }
                else
                {
                    sslStream = new SslStream(new NetworkStream(socket), false, ssl.RemoteCertificateValidationCallback, ssl.LocalCertificateSelectionCallback);
                    await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                                                              ssl.Protocols, ssl.CheckCertificateRevocation);
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
            this.writer          = new Writer(this, this.socketTransport);
        }
        new protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (_transport != null)
            {
                if (_ownsTransport)
                {
                    // close transport connection
                    _transport.Close();
                }

                _transport = null;
            }

            _disposed = true;
        }
        async Task HandleTransportAsync(IAsyncTransport transport)
        {
            if (this.saslMechanisms != null)
            {
                ListenerSasProfile profile = new ListenerSasProfile(this);
                transport = await profile.OpenAsync(null, transport);
            }

            Connection connection = new ListenerConnection(this, this.address, transport);

            connection.Closed += this.OnConnectionClosed;
            lock (this.connections)
            {
                this.connections.Add(connection);
            }

            AsyncPump pump = new AsyncPump(transport);

            pump.Start(connection);
        }
        internal async Task ConnectAsync(Address address, SaslProfile saslProfile, Open open, Connection connection)
        {
            if (saslProfile == null)
            {
                if (address.User != null)
                {
                    saslProfile = new SaslPlainProfile(address.User, address.Password);
                }
                else if (this.saslSettings != null && this.saslSettings.Profile != null)
                {
                    saslProfile = this.saslSettings.Profile;
                }
            }

            IAsyncTransport transport = await this.CreateTransportAsync(address, saslProfile, connection.Handler).ConfigureAwait(false);

            connection.Init(this.BufferManager, this.AMQP, transport, open);

            AsyncPump pump = new AsyncPump(this.BufferManager, transport);

            pump.Start(connection);
        }
        internal Connection(ConnectionFactory factory, Address address, IAsyncTransport transport, Open open, OnOpened onOpened)
            : this(factory.amqpSettings.MaxSessionsPerConnection)
        {
            this.address      = address;
            this.onOpened     = onOpened;
            this.maxFrameSize = (uint)factory.amqpSettings.MaxFrameSize;
            this.transport    = transport;
            transport.SetConnection(this);

            // after getting the transport, move state to open pipe before starting the pump
            if (open == null)
            {
                open = new Open()
                {
                    ContainerId = factory.amqpSettings.ContainerId,
                    HostName    = factory.amqpSettings.HostName ?? this.address.Host
                };
            }

            this.SendHeader();
            this.SendOpen(open);
            this.state = State.OpenPipe;
        }
        async Task <Connection> CreateAsync(Address address, Open open, OnOpened onOpened, IHandler handler)
        {
            SaslProfile saslProfile = null;

            if (address.User != null)
            {
                saslProfile = new SaslPlainProfile(address.User, address.Password);
            }
            else if (this.saslSettings != null && this.saslSettings.Profile != null)
            {
                saslProfile = this.saslSettings.Profile;
            }

            IAsyncTransport transport = await this.CreateTransportAsync(address, saslProfile, handler).ConfigureAwait(false);

            Connection connection = new Connection(this.BufferManager, this.AMQP, address, transport, open, onOpened, handler);

            AsyncPump pump = new AsyncPump(this.BufferManager, transport);

            pump.Start(connection);

            return(connection);
        }
Beispiel #21
0
        async Task HandleTransportAsync(IAsyncTransport transport)
        {
            IPrincipal principal = null;
            if (this.saslSettings != null)
            {
                ListenerSaslProfile profile = new ListenerSaslProfile(this);
                transport = await profile.OpenAsync(null, this.BufferManager, transport);
                principal = profile.GetPrincipal();
            }

            var connection = new ListenerConnection(this, this.address, transport);
            if (principal == null)
            {
                // SASL principal preferred. If not present, check transport.
                IAuthenticated authenticated = transport as IAuthenticated;
                if (authenticated != null)
                {
                    principal = authenticated.Principal;
                }
            }

            connection.Principal = principal;

            bool shouldClose = false;
            lock (this.connections)
            {
                if (!this.closed)
                {
                    connection.Closed += this.OnConnectionClosed;
                    this.connections.Add(connection);
                }
                else
                {
                    shouldClose = true;
                }
            }

            if (shouldClose)
            {
                await connection.CloseAsync();
            }
            else
            {
                AsyncPump pump = new AsyncPump(this.BufferManager, transport);
                pump.Start(connection);
            }
        }
Beispiel #22
0
 internal ListenerConnection(ConnectionListener listener, Address address, IAsyncTransport transport)
     : base(listener, address, transport, null, null)
 {
     this.listener = listener;
 }
Beispiel #23
0
        internal Connection(IBufferManager bufferManager, AmqpSettings amqpSettings, Address address,
            IAsyncTransport transport, Open open, OnOpened onOpened)
            : this((ushort)(amqpSettings.MaxSessionsPerConnection - 1), (uint)amqpSettings.MaxFrameSize)
        {
            this.BufferManager = bufferManager;
            this.address = address;
            this.onOpened = onOpened;
            this.maxFrameSize = (uint)amqpSettings.MaxFrameSize;
            this.transport = transport;
            transport.SetConnection(this);

            // after getting the transport, move state to open pipe before starting the pump
            if (open == null)
            {
                open = new Open()
                {
                    ContainerId = amqpSettings.ContainerId,
                    HostName = amqpSettings.HostName ?? this.address.Host,
                    ChannelMax = this.channelMax,
                    MaxFrameSize = this.maxFrameSize
                };
            }

            this.SendHeader();
            this.SendOpen(open);
            this.state = State.OpenPipe;
        }
        internal static async Task<IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname, IAsyncTransport transport)
        {
            ProtocolHeader header = saslProfile.Start(hostname, transport);

            AsyncPump pump = new AsyncPump(transport);

            await pump.PumpAsync(
                h => { saslProfile.OnHeader(header, h); return true; },
                b => { SaslCode code; return saslProfile.OnFrame(transport, b, out code); });

            return (IAsyncTransport)saslProfile.UpgradeTransportInternal(transport);
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(true);

            if (_disposed)
                return;

            if (disposing)
            {
                if (_transport != null)
                {
                    if (_ownsTransport)
                    {
                        // close transport connection
                        _transport.Close();
                    }

                    _transport = null;
                }
            }

            _disposed = true;
        }
Beispiel #26
0
 internal ListenerConnection(ConnectionListener listener, Address address, IAsyncTransport transport)
     : base(listener.BufferManager, listener.AMQP, address, transport, null, onOpened)
 {
     this.listener = listener;
 }
Beispiel #27
0
 public AsyncPump(IAsyncTransport transport)
 {
     this.transport = transport;
 }
Beispiel #28
0
 // called by listener
 public TcpTransport(SslStream sslStream, IBufferManager bufferManager)
     : this(bufferManager)
 {
     this.socketTransport = new SslSocket(this, sslStream);
     this.writer          = new Writer(this, this.socketTransport);
 }
Beispiel #29
0
        public async Task ConnectAsync(Address address, ConnectionFactory factory)
        {
            Socket socket;
            IPAddress[] ipAddresses;
            IPAddress ipAddress;
            if (IPAddress.TryParse(address.Host, out ipAddress))
            {
                socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                ipAddresses = new IPAddress[] { ipAddress };
            }
            else
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ipAddresses = Dns.GetHostEntry(address.Host).AddressList;
            }

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            await Task.Factory.FromAsync(
                (c, s) => ((Socket)s).BeginConnect(ipAddresses, address.Port, c, s),
                (r) => ((Socket)r.AsyncState).EndConnect(r),
                socket);

            IAsyncTransport transport;
            if (address.UseSsl)
            {
                SslStream sslStream;
                var ssl = factory.SslInternal;
                if (ssl == null)
                {
                    sslStream = new SslStream(new NetworkStream(socket));
                    await sslStream.AuthenticateAsClientAsync(address.Host);
                }
                else
                {
                    sslStream = new SslStream(new NetworkStream(socket), false, ssl.RemoteCertificateValidationCallback);
                    await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                        ssl.Protocols, ssl.CheckCertificateRevocation);
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
            this.writer = new Writer(this, this.socketTransport);
        }
Beispiel #30
0
 public Task <IAsyncTransport> NegotiateAsync(IAsyncTransport transport)
 {
     return(this.OpenAsync(null, this.listener.BufferManager, transport, this.GetStartCommand(null)));
 }
Beispiel #31
0
        internal static async Task <IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
                                                               IBufferManager bufferManager, IAsyncTransport transport)
        {
            ProtocolHeader header = saslProfile.Start(hostname, transport);

            AsyncPump pump = new AsyncPump(bufferManager, transport);

            await pump.PumpAsync(
                h => { saslProfile.OnHeader(header, h); return(true); },
                b => { SaslCode code; return(saslProfile.OnFrame(transport, b, out code)); });

            return((IAsyncTransport)saslProfile.UpgradeTransportInternal(transport));
        }
Beispiel #32
0
        public async Task ConnectAsync(Address address, ConnectionFactory factory)
        {
            IPAddress[] ipAddresses;
            IPAddress ip;
            if (IPAddress.TryParse(address.Host, out ip))
            {
                ipAddresses = new IPAddress[] { ip };
            }
            else
            {
                ipAddresses = await TaskExtensions.GetHostAddressesAsync(address.Host);
            }

            // need to handle both IPv4 and IPv6
            Socket socket = null;
            Exception exception = null;
            for (int i = 0; i < ipAddresses.Length; i++)
            {
                if (ipAddresses[i] == null ||
                    (ipAddresses[i].AddressFamily == AddressFamily.InterNetwork && !Socket.OSSupportsIPv4) ||
                    (ipAddresses[i].AddressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6))
                {
                    continue;
                }

                socket = new Socket(ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    await socket.ConnectAsync(ipAddresses[i], address.Port);
                    exception = null;
                    break;
                }
                catch (Exception e)
                {
                    exception = e;
                    socket.Dispose();
                    socket = null;
                }
            }

            if (socket == null)
            {
                throw exception ?? new SocketException((int)SocketError.AddressNotAvailable);
            }

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            IAsyncTransport transport;
            if (address.UseSsl)
            {
                SslStream sslStream;
                var ssl = factory.SslInternal;
                if (ssl == null)
                {
                    sslStream = new SslStream(new NetworkStream(socket));
                    await sslStream.AuthenticateAsClientAsync(address.Host);
                }
                else
                {
                    sslStream = new SslStream(new NetworkStream(socket), false, ssl.RemoteCertificateValidationCallback, ssl.LocalCertificateSelectionCallback);
                    await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                        ssl.Protocols, ssl.CheckCertificateRevocation);
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
            this.writer = new Writer(this, this.socketTransport);
        }
Beispiel #33
0
 // called by listener
 public TcpTransport(SslStream sslStream, IBufferManager bufferManager)
     : this(bufferManager)
 {
     this.socketTransport = new SslSocket(this, sslStream);
     this.writer = new Writer(this, this.socketTransport);
 }
Beispiel #34
0
 // called by listener
 public TcpTransport(Socket socket, IBufferManager bufferManager)
     : this(bufferManager)
 {
     this.socketTransport = new TcpSocket(this, socket);
     this.writer = new Writer(this, this.socketTransport);
 }
Beispiel #35
0
 // called by listener
 public TcpTransport(Socket socket, IBufferManager bufferManager)
     : this(bufferManager)
 {
     this.socketTransport = new TcpSocket(this, socket);
     this.writer          = new Writer(this, this.socketTransport);
 }
Beispiel #36
0
 // called by listener
 public TcpTransport(SslStream sslStream)
 {
     this.socketTransport = new SslSocket(this, sslStream);
     this.writer          = new Writer(this, this.socketTransport);
 }
        async Task HandleTransportAsync(IAsyncTransport transport)
        {
            if (this.saslSettings != null)
            {
                ListenerSaslProfile profile = new ListenerSaslProfile(this);
                transport = await profile.OpenAsync(null, transport);
            }

            Connection connection = new ListenerConnection(this, this.address, transport);
            bool shouldClose = false;
            lock (this.connections)
            {
                if (!this.closed)
                {
                    connection.Closed += this.OnConnectionClosed;
                    this.connections.Add(connection);
                }
                else
                {
                    shouldClose = true;
                }
            }

            if (shouldClose)
            {
                await connection.CloseAsync();
            }
            else
            {
                AsyncPump pump = new AsyncPump(transport);
                pump.Start(connection);
            }
        }
Beispiel #38
0
        public async Task ConnectAsync(Address address, ConnectionFactory factory)
        {
            IPAddress[] ipAddresses;
            IPAddress   ip;

            if (IPAddress.TryParse(address.Host, out ip))
            {
                ipAddresses = new IPAddress[] { ip };
            }
            else
            {
                ipAddresses = await TaskExtensions.GetHostAddressesAsync(address.Host);
            }

            // need to handle both IPv4 and IPv6
            Socket    socket    = null;
            Exception exception = null;

            for (int i = 0; i < ipAddresses.Length; i++)
            {
                if (ipAddresses[i] == null ||
                    (ipAddresses[i].AddressFamily == AddressFamily.InterNetwork && !Socket.OSSupportsIPv4) ||
                    (ipAddresses[i].AddressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6))
                {
                    continue;
                }

                socket = new Socket(ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    await socket.ConnectAsync(ipAddresses[i], address.Port);

                    exception = null;
                    break;
                }
                catch (Exception e)
                {
                    exception = e;
                    socket.Dispose();
                    socket = null;
                }
            }

            if (socket == null)
            {
                throw exception ?? new SocketException((int)SocketError.AddressNotAvailable);
            }

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            IAsyncTransport transport;

            if (address.UseSsl)
            {
                SslStream sslStream;
                var       ssl = factory.SslInternal;
                if (ssl == null)
                {
                    sslStream = new SslStream(new NetworkStream(socket));
                    await sslStream.AuthenticateAsClientAsync(address.Host);
                }
                else
                {
                    sslStream = new SslStream(new NetworkStream(socket), false, ssl.RemoteCertificateValidationCallback, ssl.LocalCertificateSelectionCallback);
                    await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                                                              ssl.Protocols, ssl.CheckCertificateRevocation);
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
            this.writer          = new Writer(this, this.socketTransport);
        }
Beispiel #39
0
 internal ListenerConnection(ConnectionListener listener, Address address, IAsyncTransport transport)
     : base(listener.BufferManager, listener.AMQP, address, transport, null, onOpened)
 {
     this.listener = listener;
 }
Beispiel #40
0
 public AsyncPump(IBufferManager bufferManager, IAsyncTransport transport)
 {
     this.bufferManager = bufferManager;
     this.transport     = transport;
 }
Beispiel #41
0
        internal static async Task <IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
                                                               IBufferManager bufferManager, IAsyncTransport transport, DescribedList command)
        {
            // if transport is closed, pump reader should throw exception
            TransportWriter writer = new TransportWriter(transport, e => { });

            ProtocolHeader myHeader = saslProfile.Start(writer, command);

            AsyncPump pump = new AsyncPump(bufferManager, transport);
            SaslCode  code = SaslCode.Auth;

            await pump.PumpAsync(
                SaslProfile.MaxFrameSize,
                header =>
            {
                saslProfile.OnHeader(myHeader, header);
                return(true);
            },
                buffer =>
            {
                return(saslProfile.OnFrame(hostname, writer, buffer, out code));
            }).ConfigureAwait(false);

            await writer.FlushAsync().ConfigureAwait(false);

            if (code != SaslCode.Ok)
            {
                throw new AmqpException(ErrorCode.UnauthorizedAccess,
                                        Fx.Format(SRAmqp.SaslNegoFailed, code));
            }

            return((IAsyncTransport)saslProfile.UpgradeTransportInternal(transport));
        }
Beispiel #42
0
        internal static async Task<IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
            IBufferManager bufferManager, IAsyncTransport transport)
        {
            // if transport is closed, pump reader should throw exception
            TransportWriter writer = new TransportWriter(transport, e => { });

            ProtocolHeader myHeader = saslProfile.Start(hostname, writer);

            AsyncPump pump = new AsyncPump(bufferManager, transport);
            SaslCode code = SaslCode.Auth;

            await pump.PumpAsync(
                header =>
                {
                    saslProfile.OnHeader(myHeader, header);
                    return true;
                },
                buffer =>
                {
                    return saslProfile.OnFrame(writer, buffer, out code);
                });

            await writer.FlushAsync();

            if (code != SaslCode.Ok)
            {
                throw new AmqpException(ErrorCode.UnauthorizedAccess,
                    Fx.Format(SRAmqp.SaslNegoFailed, code));
            }

            return (IAsyncTransport)saslProfile.UpgradeTransportInternal(transport);
        }
Beispiel #43
0
 // called by listener
 public TcpTransport(Socket socket)
 {
     this.socketTransport = new TcpSocket(this, socket);
     this.writer = new Writer(this, this.socketTransport);
 }
Beispiel #44
0
 public Writer(TcpTransport owner, IAsyncTransport transport)
 {
     this.owner = owner;
     this.transport = transport;
     this.bufferQueue = new Queue<ByteBuffer>();
     this.buffersInProgress = new List<ByteBuffer>();
 }
Beispiel #45
0
 // called by listener
 public TcpTransport(SslStream sslStream)
 {
     this.socketTransport = new SslSocket(this, sslStream);
     this.writer = new Writer(this, this.socketTransport);
 }
Beispiel #46
0
 public AsyncPump(IAsyncTransport transport)
 {
     this.transport = transport;
 }
Beispiel #47
0
        public async Task ConnectAsync(Address address, ConnectionFactory factory)
        {
            Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            await Task.Factory.FromAsync(
                (c, s) => ((Socket)s).BeginConnect(address.Host, address.Port, c, s),
                (r) => ((Socket)r.AsyncState).EndConnect(r),
                socket);

            IAsyncTransport transport;
            if (address.UseSsl)
            {
                SslStream sslStream;
                var ssl = factory.SslInternal;
                if (ssl == null)
                {
                    sslStream = new SslStream(new NetworkStream(socket));
                    await sslStream.AuthenticateAsClientAsync(address.Host);
                }
                else
                {
                    sslStream = new SslStream(new NetworkStream(socket), false, ssl.RemoteCertificateValidationCallback, ssl.LocalCertificateSelectionCallback);
                    await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                        ssl.Protocols, ssl.CheckCertificateRevocation);
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
            this.writer = new Writer(this, this.socketTransport);
        }
Beispiel #48
0
        internal static async Task<IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
            IBufferManager bufferManager, IAsyncTransport transport)
        {
            // if transport is closed, pump reader should throw exception
            TransportWriter writer = new TransportWriter(transport, e => { });

            ProtocolHeader myHeader = saslProfile.Start(hostname, writer);

            AsyncPump pump = new AsyncPump(bufferManager, transport);

            await pump.PumpAsync(
                header =>
                {
                    saslProfile.OnHeader(myHeader, header);
                    return true;
                },
                buffer =>
                {
                    SaslCode code;
                    return saslProfile.OnFrame(writer, buffer, out code);
                });

            await writer.FlushAsync();

            return (IAsyncTransport)saslProfile.UpgradeTransportInternal(transport);
        }
Beispiel #49
0
 public AsyncPump(IBufferManager bufferManager, IAsyncTransport transport)
 {
     this.bufferManager = bufferManager;
     this.transport = transport;
 }
Beispiel #50
0
 public TransportWriter(IAsyncTransport transport, Action<Exception> onException)
 {
     this.transport = transport;
     this.onException = onException;
     this.bufferQueue = new Queue<ByteBuffer>();
 }
        async Task HandleTransportAsync(IAsyncTransport transport)
        {
            if (this.saslSettings != null)
            {
                ListenerSaslProfile profile = new ListenerSaslProfile(this);
                transport = await profile.OpenAsync(null, transport);
            }

            Connection connection = new ListenerConnection(this, this.address, transport);
            connection.Closed += this.OnConnectionClosed;
            lock (this.connections)
            {
                this.connections.Add(connection);
            }

            AsyncPump pump = new AsyncPump(transport);
            pump.Start(connection);
        }
Beispiel #52
0
 // called by listener
 public TcpTransport(Socket socket)
 {
     this.socketTransport = new TcpSocket(this, socket);
     this.writer          = new Writer(this, this.socketTransport);
 }
 internal ListenerConnection(ConnectionListener listener, Address address, IAsyncTransport transport)
     : base(listener.AMQP, address, transport, null, null)
 {
     this.listener = listener;
 }