Esempio n. 1
0
        internal bool OnHeader(ProtocolHeader header)
        {
            Trace.WriteLine(TraceLevel.Frame, "RECV AMQP {0}", header);
            lock (this.ThisLock)
            {
                if (this.state == State.OpenPipe)
                {
                    this.state = State.OpenSent;
                }
                else if (this.state == State.OpenClosePipe)
                {
                    this.state = State.ClosePipe;
                }
                else
                {
                    throw new AmqpException(ErrorCode.IllegalState,
                                            Fx.Format(SRAmqp.AmqpIllegalOperationState, "OnHeader", this.state));
                }

                if (header.Major != 1 || header.Minor != 0 || header.Revision != 0)
                {
                    throw new AmqpException(ErrorCode.NotImplemented, header.ToString());
                }
            }

            return(true);
        }
Esempio n. 2
0
        internal static void Trace(this ProtocolHeader header, bool send, AmqpConnection connection)
        {
            if (!AmqpTrace.AmqpDebug)
            {
                return;
            }

            string message = string.Format(
                "{0} [{1:X3}.{2:X3} {3:HH:mm:ss.fff}] {4} {5}",
                AppDomain.CurrentDomain.FriendlyName,
                Process.GetCurrentProcess().Id,
                Environment.CurrentManagedThreadId,
                DateTime.UtcNow,
                send ? "SEND" : "RECV",
                header.ToString());

            if (AmqpTrace.TraceCallback != null)
            {
                AmqpTrace.TraceCallback(message);
            }
            else
            {
                System.Diagnostics.Trace.WriteLine(message);
            }
        }
Esempio n. 3
0
            private void WriteReplyHeader(ProtocolHeader header, bool fail)
            {
                Action <TransportAsyncCallbackArgs> action;

                MessagingClientEtwProvider.TraceClient <AmqpTransportListener.TransportHandler, ProtocolHeader>((AmqpTransportListener.TransportHandler source, ProtocolHeader detail) => MessagingClientEtwProvider.Provider.EventWriteAmqpLogOperation(source, TraceOperation.Send, detail), this, header);
                header.Encode(new ByteBuffer(this.buffer));
                this.args.SetBuffer(this.buffer, 0, (int)this.buffer.Length);
                TransportAsyncCallbackArgs transportAsyncCallbackArg = this.args;

                if (fail)
                {
                    action = null;
                }
                else
                {
                    action = AmqpTransportListener.TransportHandler.writeCompleteCallback;
                }
                transportAsyncCallbackArg.CompletedCallback = action;
                this.bufferWriter.WriteBuffer(this.args);
                if (fail)
                {
                    this.args.Exception = new NotSupportedException(header.ToString());
                    this.parent.OnHandleTransportComplete(this.args);
                }
            }
Esempio n. 4
0
 internal void OnHeader(ProtocolHeader myHeader, ProtocolHeader theirHeader)
 {
     if (theirHeader.Id != myHeader.Id || theirHeader.Major != myHeader.Major ||
         theirHeader.Minor != myHeader.Minor || theirHeader.Revision != myHeader.Revision)
     {
         throw new AmqpException(ErrorCode.NotImplemented, theirHeader.ToString());
     }
 }
            void WriteReplyHeader(ProtocolHeader header, bool fail)
            {
                Utils.Trace(TraceLevel.Verbose, "{0}: Write reply protocol header {1}", this, header);
                Utils.Trace(TraceLevel.Frame, "SEND  {0}", header);
                this.args.SetBuffer(header.Buffer);
                this.args.CompletedCallback = fail ? null : this.writeCompleteCallback;
                this.bufferWriter.WriteBuffer(this.args);

                if (fail)
                {
                    this.args.Exception = new NotSupportedException(header.ToString());
                    this.parent.OnHandleTransportComplete(this.args);
                }
            }
Esempio n. 6
0
            void WriteReplyHeader(ProtocolHeader header, bool fail)
            {
                AmqpTrace.Provider.AmqpLogOperationInformational(this, TraceOperation.Send, header);
#if DEBUG
                header.Trace(true);
#endif

                ByteBuffer byteBuffer = new ByteBuffer(this.buffer);
                header.Encode(byteBuffer);
                this.args.SetBuffer(this.buffer, 0, this.buffer.Length);
                this.args.CompletedCallback = fail ? null : TransportHandler.writeCompleteCallback;
                this.bufferWriter.WriteBuffer(this.args);

                if (fail)
                {
                    this.args.Exception = new NotSupportedException(header.ToString());
                    this.parent.OnHandleTransportComplete(this.args);
                }
            }
Esempio n. 7
0
        protected override void OnProtocolHeader(ProtocolHeader header)
        {
            base.TransitState("R:HDR", StateTransition.ReceiveHeader);
            Exception amqpException = null;

            if (!this.isInitiator)
            {
                ProtocolHeader supportedHeader = this.amqpSettings.GetSupportedHeader(header);
                this.SendProtocolHeader(supportedHeader);
                if (!supportedHeader.Equals(header))
                {
                    amqpException = new AmqpException(AmqpError.NotImplemented, SRAmqp.AmqpProtocolVersionNotSupported(this.initialHeader.ToString(), header.ToString()));
                }
            }
            else if (!this.initialHeader.Equals(header))
            {
                amqpException = new AmqpException(AmqpError.NotImplemented, SRAmqp.AmqpProtocolVersionNotSupported(this.initialHeader.ToString(), header.ToString()));
            }
            if (amqpException != null)
            {
                base.CompleteOpen(false, amqpException);
            }
        }
        public async Task OpenAsync(TimeSpan timeout, bool useWebSocket, X509Certificate2 clientCert, IWebProxy proxy, RemoteCertificateValidationCallback remoteCerificateValidationCallback)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}");
            }

            var hostName = _uri.Host;

            var tcpSettings = new TcpTransportSettings {
                Host = hostName, Port = _uri.Port != -1 ? _uri.Port : AmqpConstants.DefaultSecurePort
            };

            TransportSettings = new TlsTransportSettings(tcpSettings)
            {
                TargetHost  = hostName,
                Certificate = clientCert,
                CertificateValidationCallback = remoteCerificateValidationCallback,
            };

            TransportBase transport;

            if (useWebSocket)
            {
                transport = await CreateClientWebSocketTransportAsync(timeout, proxy).ConfigureAwait(false);

                SaslTransportProvider provider = _amqpSettings.GetTransportProvider <SaslTransportProvider>();
                if (provider != null)
                {
                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}: Using SaslTransport");
                    }

                    _sentHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion);
                    ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]);
                    _sentHeader.Encode(buffer);

                    _tcs = new TaskCompletionSource <TransportBase>();

                    var args = new TransportAsyncCallbackArgs();
                    args.SetBuffer(buffer.Buffer, buffer.Offset, buffer.Length);
                    args.CompletedCallback = OnWriteHeaderComplete;
                    args.Transport         = transport;
                    bool operationPending = transport.WriteAsync(args);

                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}: Sent Protocol Header: {_sentHeader.ToString()} operationPending: {operationPending} completedSynchronously: {args.CompletedSynchronously}");
                    }

                    if (!operationPending)
                    {
                        args.CompletedCallback(args);
                    }

                    transport = await _tcs.Task.ConfigureAwait(false);

                    await transport.OpenAsync(timeout).ConfigureAwait(false);
                }
            }
            else
            {
                var tcpInitiator = new AmqpTransportInitiator(_amqpSettings, TransportSettings);
                transport = await tcpInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false);
            }

            AmqpConnection         = new AmqpConnection(transport, _amqpSettings, AmqpConnectionSettings);
            AmqpConnection.Closed += OnConnectionClosed;
            await AmqpConnection.OpenAsync(timeout).ConfigureAwait(false);

            _isConnectionClosed = false;
        }
        private void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, $"{nameof(AmqpClientConnection)}.{nameof(OnReadHeaderComplete)}");
            }

            if (args.Exception != null)
            {
                CompleteOnException(args);
                return;
            }

            try
            {
                ProtocolHeader receivedHeader = new ProtocolHeader();
                receivedHeader.Decode(new ByteBuffer(args.Buffer, args.Offset, args.Count));

                if (Logging.IsEnabled)
                {
                    Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OnReadHeaderComplete)}: Received Protocol Header: {receivedHeader.ToString()}");
                }

                if (!receivedHeader.Equals(_sentHeader))
                {
                    throw new AmqpException(AmqpErrorCode.NotImplemented, $"The requested protocol version {_sentHeader} is not supported. The supported version is {receivedHeader}");
                }

                SaslTransportProvider provider = _amqpSettings.GetTransportProvider <SaslTransportProvider>();
                var transport = provider.CreateTransport(args.Transport, true);
                if (Logging.IsEnabled)
                {
                    Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OnReadHeaderComplete)}: Created SaslTransportHandler ");
                }

                _tcs.TrySetResult(transport);
            }
            catch (Exception ex)
            {
                args.Exception = ex;
                CompleteOnException(args);
            }
        }
Esempio n. 10
0
        protected override void OnProtocolHeader(ProtocolHeader header)
        {
#if DEBUG
            header.Trace(false);
            AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Receive, header);
#endif
            this.heartBeat.OnReceive();
            if (this.UsageMeter != null)
            {
                this.UsageMeter.OnRead(this, 0, header.EncodeSize);
            }

            this.TransitState("R:HDR", StateTransition.ReceiveHeader);
            Exception exception = null;

            if (this.isInitiator)
            {
                if (!this.initialHeader.Equals(header))
                {
                    exception = new AmqpException(AmqpErrorCode.NotImplemented, AmqpResources.GetString(AmqpResources.AmqpProtocolVersionNotSupported, this.initialHeader.ToString(), header.ToString()));
                }
            }
            else
            {
                ProtocolHeader supportedHeader = this.amqpSettings.GetSupportedHeader(header);
                this.SendProtocolHeader(supportedHeader);
                if (!supportedHeader.Equals(header))
                {
                    exception = new AmqpException(AmqpErrorCode.NotImplemented, AmqpResources.GetString(AmqpResources.AmqpProtocolVersionNotSupported, this.initialHeader.ToString(), header.ToString()));
                }
            }

            if (exception != null)
            {
                this.CompleteOpen(false, exception);
            }
        }
Esempio n. 11
0
        private async Task <TransportBase> InitializeTransport(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(InitializeTransport)}");
            }
            TransportBase transport;

            switch (AmqpTransportSettings.GetTransportType())
            {
            case TransportType.Amqp_WebSocket_Only:
                transport = await CreateClientWebSocketTransportAsync(timeout).ConfigureAwait(false);

                SaslTransportProvider provider = AmqpSettings.GetTransportProvider <SaslTransportProvider>();
                if (provider != null)
                {
                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(InitializeTransport)}: Using SaslTransport");
                    }
                    SentProtocolHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion);
                    ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]);
                    SentProtocolHeader.Encode(buffer);

                    TaskCompletionSource = new TaskCompletionSource <TransportBase>();

                    var args = new TransportAsyncCallbackArgs();
                    args.SetBuffer(buffer.Buffer, buffer.Offset, buffer.Length);
                    args.CompletedCallback = OnWriteHeaderComplete;
                    args.Transport         = transport;
                    bool operationPending = transport.WriteAsync(args);

                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(InitializeTransport)}: Sent Protocol Header: {SentProtocolHeader.ToString()} operationPending: {operationPending} completedSynchronously: {args.CompletedSynchronously}");
                    }

                    if (!operationPending)
                    {
                        args.CompletedCallback(args);
                    }

                    transport = await TaskCompletionSource.Task.ConfigureAwait(false);

                    await transport.OpenAsync(timeout).ConfigureAwait(false);
                }
                break;

            case TransportType.Amqp_Tcp_Only:
                var amqpTransportInitiator = new AmqpTransportInitiator(AmqpSettings, TlsTransportSettings);
                transport = await amqpTransportInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false);

                break;

            default:
                throw new InvalidOperationException("AmqpTransportSettings must specify WebSocketOnly or TcpOnly");
            }
            if (Logging.IsEnabled)
            {
                Logging.Exit(this, timeout, $"{nameof(InitializeTransport)}");
            }
            return(transport);
        }
Esempio n. 12
0
        protected override void OnProtocolHeader(ProtocolHeader header)
        {
            Utils.Trace(TraceLevel.Frame, "RECV  {0}", header);
            this.TransitState("R:HDR", StateTransition.ReceiveHeader);
            Exception exception = null;

            if (this.isInitiator)
            {
                if (!this.initialHeader.Equals(header))
                {
                    exception = new AmqpException(AmqpError.NotImplemented, SRClient.ProtocolVersionNotSupported(this.initialHeader.ToString(), header.ToString()));
                }
            }
            else
            {
                ProtocolHeader supportedHeader = this.amqpSettings.GetSupportedHeader(header);
                this.SendProtocolHeader(supportedHeader);
                if (!supportedHeader.Equals(header))
                {
                    exception = new AmqpException(AmqpError.NotImplemented, SRClient.ProtocolVersionNotSupported(this.initialHeader.ToString(), header.ToString()));
                }
            }

            if (exception != null)
            {
                this.CompleteOpen(false, exception);
            }
        }