protected virtual void OnReceiveFrameBuffer(ByteBuffer buffer)
        {
            string empty = string.Empty;

            try
            {
                empty = "UsageMeter";
                if (this.usageMeter != null)
                {
                    this.usageMeter.OnBytesRead(buffer.Length);
                }
                if (base.State > AmqpObjectState.OpenClosePipe)
                {
                    empty = "FrameBuffer";
                    this.OnFrameBuffer(buffer);
                }
                else
                {
                    empty = "ProtocolHeader";
                    ProtocolHeader protocolHeader = new ProtocolHeader();
                    protocolHeader.Decode(buffer);
                    this.OnProtocolHeader(protocolHeader);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                MessagingClientEtwProvider.TraceClient <AmqpConnectionBase, string, string>((AmqpConnectionBase a, string b, string c) => MessagingClientEtwProvider.Provider.EventWriteAmqpLogError(a, b, c), this, empty, exception.Message);
                base.SafeClose(exception);
            }
        }
Пример #2
0
        void OnReceiveFrameBuffer(ByteBuffer buffer)
        {
            int step = 0;

            try
            {
                if (this.State <= AmqpObjectState.OpenClosePipe)
                {
                    step = 1;
                    Fx.Assert(buffer.Length == AmqpConstants.ProtocolHeaderSize, "protocol header size is wrong");
                    ProtocolHeader header = new ProtocolHeader();
                    header.Decode(buffer);
                    this.OnProtocolHeader(header);
                }
                else
                {
                    step = 2;
                    this.OnFrameBuffer(buffer);
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }

                AmqpTrace.Provider.AmqpLogError(this, step.ToString(CultureInfo.InvariantCulture), exception.Message);

                this.SafeClose(exception);
            }
        }
Пример #3
0
        private void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
        {
            if (args.Exception != null)
            {
                CompleteOnException(args);
                return;
            }

            try
            {
                ProtocolHeader receivedHeader = new ProtocolHeader();
                receivedHeader.Decode(new ByteBuffer(args.Buffer, args.Offset, args.Count));
                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);
                _tcs.TrySetResult(transport);
            }
            catch (Exception ex)
            {
                args.Exception = ex;
                CompleteOnException(args);
            }
        }
Пример #4
0
        void OnReceiveFrameBuffer(ByteBuffer buffer)
        {
            if (this.State <= AmqpObjectState.OpenClosePipe)
            {
                Fx.Assert(buffer.Length == AmqpConstants.ProtocolHeaderSize, "protocol header size is wrong");
                try
                {
                    ProtocolHeader header = new ProtocolHeader();
                    header.Decode(buffer);
                    this.OnProtocolHeader(header);
                }
                catch (Exception exception) when(!Fx.IsFatal(exception))
                {
                    AmqpTrace.Provider.AmqpLogError(this, "OnProtocolHeader", exception);

                    this.TerminalException = exception;
                    this.Abort();
                }
            }
            else
            {
                try
                {
                    this.OnFrameBuffer(buffer);
                }
                catch (Exception exception) when(!Fx.IsFatal(exception))
                {
                    AmqpTrace.Provider.AmqpLogError(this, "OnFrame", exception);

                    this.SafeClose(exception);
                }
            }
        }
Пример #5
0
        public ProtocolHeader ExtractProtocolHeader(ByteBuffer buffer)
        {
            if (buffer.Length < ProtocolHeader.Size)
            {
                return(null);
            }

            return(ProtocolHeader.Decode(buffer));
        }
Пример #6
0
        public ProtocolHeader ExtractProtocolHeader(ByteBuffer buffer)
        {
            if (buffer.Length < 8)
            {
                return(null);
            }
            ProtocolHeader protocolHeader = new ProtocolHeader();

            protocolHeader.Decode(buffer);
            return(protocolHeader);
        }
Пример #7
0
            void OnProtocolHeader(ByteBuffer buffer)
            {
                ProtocolHeader header = new ProtocolHeader();

                header.Decode(buffer);
                AmqpTrace.Provider.AmqpLogOperationInformational(this, TraceOperation.Receive, header);

                // Protocol id negotiation
                TransportProvider provider = null;

                if (!this.parent.settings.TryGetTransportProvider(header, out provider))
                {
                    Fx.Assert(provider != null, "At least on provider should be configured.");
                    this.WriteReplyHeader(new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion), true);
                    return;
                }

                // Protocol version negotiation
                AmqpVersion version;

                if (!provider.TryGetVersion(header.Version, out version))
                {
                    this.WriteReplyHeader(new ProtocolHeader(provider.ProtocolId, version), true);
                    return;
                }

                TransportBase newTransport = provider.CreateTransport(this.args.Transport, false);

                if (object.ReferenceEquals(newTransport, this.args.Transport))
                {
                    if ((this.parent.settings.RequireSecureTransport && !newTransport.IsSecure) ||
                        (!this.parent.settings.AllowAnonymousConnection && !newTransport.IsAuthenticated))
                    {
                        AmqpTrace.Provider.AmqpInsecureTransport(this.parent, newTransport, newTransport.IsSecure, newTransport.IsAuthenticated);
                        this.WriteReplyHeader(this.parent.settings.GetDefaultHeader(), true);
                    }
                    else
                    {
                        this.args.UserToken = header;
                        this.parent.OnHandleTransportComplete(this.args);
                    }
                }
                else
                {
#if DEBUG
                    header.Trace(false);
#endif
                    AmqpTrace.Provider.AmqpUpgradeTransport(this, args.Transport, newTransport);
                    this.args.Transport = newTransport;
                    this.WriteReplyHeader(header, false);
                }
            }
Пример #8
0
        public ProtocolHeader ExtractProtocolHeader(ByteBuffer buffer)
        {
            if (buffer.Length < AmqpConstants.ProtocolHeaderSize)
            {
                return(null);
            }

            ProtocolHeader header = new ProtocolHeader();

            header.Decode(buffer);

            return(header);
        }
Пример #9
0
            void OnProtocolHeader(ByteBuffer buffer)
            {
                ProtocolHeader header = ProtocolHeader.Decode(buffer);
                Utils.Trace(TraceLevel.Info, "{0}: Received a protocol header {1}.", this, header);

                // Protocol id negotiation
                TransportProvider provider = null;
                if (!this.parent.settings.TryGetTransportProvider(header, out provider))
                {
                    Fx.Assert(provider != null, "At least on provider should be configured.");
                    this.WriteReplyHeader(new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion), true);
                    return;
                }

                // Protocol version negotiation
                AmqpVersion version;
                if (!provider.TryGetVersion(header.Version, out version))
                {
                    this.WriteReplyHeader(new ProtocolHeader(provider.ProtocolId, version), true);
                    return;
                }

                TransportBase newTransport = provider.CreateTransport(this.args.Transport, false);
                if (object.ReferenceEquals(newTransport, this.args.Transport))
                {
                    if ((this.parent.settings.RequireSecureTransport && !newTransport.IsSecure) ||
                        (!this.parent.settings.AllowAnonymousConnection && !newTransport.IsAuthenticated))
                    {
                        Utils.Trace(TraceLevel.Warning, 
                            "{0}: Transport {1} does not meet the security requirement (isSecure:{2}, isAuthenticated:{3}).", 
                            this.parent, 
                            newTransport,
                            newTransport.IsSecure,
                            newTransport.IsAuthenticated);
                        this.WriteReplyHeader(this.parent.settings.GetDefaultHeader(), true);
                    }
                    else
                    {
                        this.args.UserToken = header;
                        this.parent.OnHandleTransportComplete(this.args);
                    }
                }
                else
                {
                    Utils.Trace(TraceLevel.Frame, "RECV  {0}", header);
                    Utils.Trace(TraceLevel.Verbose, "{0}: Upgrade transport {1} -> {2}.", this, this.args.Transport, newTransport);
                    this.args.Transport = newTransport;
                    this.WriteReplyHeader(header, false);
                }
            }
Пример #10
0
        void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
        {
            if (args.Exception != null)
            {
                AmqpTrace.Provider.AmqpLogError(this, "ReadHeader", args.Exception.Message);
                this.Complete(args);
                return;
            }

            try
            {
                ProtocolHeader receivedHeader = new ProtocolHeader();
                receivedHeader.Decode(new ByteBuffer(args.Buffer, args.Offset, args.Count));
#if DEBUG
                receivedHeader.Trace(false);
                AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Receive, receivedHeader);
#endif

                if (!receivedHeader.Equals(this.sentHeader))
                {
                    // TODO: need to reconnect with the reply version if supported
                    throw new AmqpException(AmqpErrorCode.NotImplemented, AmqpResources.GetString(AmqpResources.AmqpProtocolVersionNotSupported, this.sentHeader, receivedHeader));
                }

                // upgrade transport
                TransportBase secureTransport = this.settings.TransportProviders[this.providerIndex].CreateTransport(args.Transport, true);
                AmqpTrace.Provider.AmqpUpgradeTransport(this, args.Transport, secureTransport);
                args.Transport = secureTransport;
                IAsyncResult result = args.Transport.BeginOpen(this.timeoutHelper.RemainingTime(), this.OnTransportOpenCompete, args);
                if (result.CompletedSynchronously)
                {
                    this.HandleTransportOpened(result);
                }
            }
            catch (Exception exp)
            {
                if (Fx.IsFatal(exp))
                {
                    throw;
                }

                AmqpTrace.Provider.AmqpLogError(this, "OnProtocolHeader", exp.Message);
                args.Exception = exp;
                this.Complete(args);
            }
        }
Пример #11
0
        private void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, $"{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(OnReadHeaderComplete)}: Received Protocol Header: {receivedHeader.ToString()}");
                }

                if (!receivedHeader.Equals(SentProtocolHeader))
                {
                    throw new AmqpException(AmqpErrorCode.NotImplemented, $"The requested protocol version {SentProtocolHeader} 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(OnReadHeaderComplete)}: Created SaslTransportHandler ");
                }
                TaskCompletionSource.TrySetResult(transport);
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, $"{nameof(OnReadHeaderComplete)}");
                }
            }
            catch (Exception ex)
            {
                args.Exception = ex;
                CompleteOnException(args);
            }
        }
Пример #12
0
        void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
        {
            if (args.Exception != null)
            {
                this.Complete(args);
                return;
            }

            try
            {
                ProtocolHeader receivedHeader = ProtocolHeader.Decode(ByteBuffer.Wrap(args.Buffer, args.Offset, args.Count));
                Utils.Trace(TraceLevel.Info, "{0}: Received header {1}", this, receivedHeader);
                Utils.Trace(TraceLevel.Frame, "RECV  {0}", receivedHeader);

                if (!receivedHeader.Equals(this.sentHeader))
                {
                    // TODO: need to reconnect with the reply version if supported
                    throw new AmqpException(AmqpError.NotImplemented, SRClient.ProtocolVersionNotSupported(this.sentHeader, receivedHeader));
                }

                // upgrade transport
                TransportBase secureTransport = this.settings.TransportProviders[this.providerIndex].CreateTransport(args.Transport, true);
                Utils.Trace(TraceLevel.Info, "{0}: Upgrade transport {1} -> {2}.", this, args.Transport, secureTransport);
                args.Transport = secureTransport;
                IAsyncResult result = args.Transport.BeginOpen(this.timeoutHelper.RemainingTime(), this.OnTransportOpenCompete, args);
                if (result.CompletedSynchronously)
                {
                    this.HandleTransportOpened(result);
                }
            }
            catch (Exception exp)
            {
                if (Fx.IsFatal(exp))
                {
                    throw;
                }

                Utils.Trace(TraceLevel.Info, "{0}: exception: {1}", this, exp.Message);
                args.Exception = exp;
                this.Complete(args);
            }
        }
Пример #13
0
            private void OnProtocolHeader(ByteBuffer buffer)
            {
                AmqpVersion    amqpVersion;
                ProtocolHeader protocolHeader = new ProtocolHeader();

                protocolHeader.Decode(buffer);
                MessagingClientEtwProvider.TraceClient <AmqpTransportListener.TransportHandler, ProtocolHeader>((AmqpTransportListener.TransportHandler source, ProtocolHeader detail) => MessagingClientEtwProvider.Provider.EventWriteAmqpLogOperation(source, TraceOperation.Receive, detail), this, protocolHeader);
                TransportProvider transportProvider = null;

                if (!this.parent.settings.TryGetTransportProvider(protocolHeader, out transportProvider))
                {
                    this.WriteReplyHeader(new ProtocolHeader(transportProvider.ProtocolId, transportProvider.DefaultVersion), true);
                    return;
                }
                if (!transportProvider.TryGetVersion(protocolHeader.Version, out amqpVersion))
                {
                    this.WriteReplyHeader(new ProtocolHeader(transportProvider.ProtocolId, amqpVersion), true);
                    return;
                }
                TransportBase transportBase = transportProvider.CreateTransport(this.args.Transport, false);

                if (!object.ReferenceEquals(transportBase, this.args.Transport))
                {
                    MessagingClientEtwProvider.TraceClient <AmqpTransportListener.TransportHandler, TransportBase, TransportBase>((AmqpTransportListener.TransportHandler source, TransportBase from, TransportBase to) => MessagingClientEtwProvider.Provider.EventWriteAmqpUpgradeTransport(source, from, to), this, this.args.Transport, transportBase);
                    this.args.Transport = transportBase;
                    this.WriteReplyHeader(protocolHeader, false);
                    return;
                }
                if ((!this.parent.settings.RequireSecureTransport || transportBase.IsSecure) && (this.parent.settings.AllowAnonymousConnection || transportBase.IsAuthenticated))
                {
                    this.args.UserToken = protocolHeader;
                    this.parent.OnHandleTransportComplete(this.args);
                    return;
                }
                MessagingClientEtwProvider.TraceClient <AmqpTransportListener, TransportBase>((AmqpTransportListener source, TransportBase transport) => MessagingClientEtwProvider.Provider.EventWriteAmqpInsecureTransport(source, transport, transport.IsSecure, transport.IsAuthenticated), this.parent, transportBase);
                this.WriteReplyHeader(this.parent.settings.GetDefaultHeader(), true);
            }
Пример #14
0
 private void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
 {
     if (args.Exception != null)
     {
         MessagingClientEtwProvider.TraceClient <AmqpTransportInitiator, Exception>((AmqpTransportInitiator source, Exception ex) => MessagingClientEtwProvider.Provider.EventWriteAmqpLogError(source, "ReadHeader", ex.Message), this, args.Exception);
         this.Complete(args);
         return;
     }
     try
     {
         ProtocolHeader protocolHeader = new ProtocolHeader();
         protocolHeader.Decode(new ByteBuffer(args.Buffer, args.Offset, args.Count));
         if (!protocolHeader.Equals(this.sentHeader))
         {
             throw new AmqpException(AmqpError.NotImplemented, SRAmqp.AmqpProtocolVersionNotSupported(this.sentHeader, protocolHeader));
         }
         TransportBase transportBase = this.settings.TransportProviders[this.providerIndex].CreateTransport(args.Transport, true);
         MessagingClientEtwProvider.TraceClient <AmqpTransportInitiator, TransportBase, TransportBase>((AmqpTransportInitiator source, TransportBase from, TransportBase to) => MessagingClientEtwProvider.Provider.EventWriteAmqpUpgradeTransport(source, from, to), this, args.Transport, transportBase);
         args.Transport = transportBase;
         IAsyncResult asyncResult = args.Transport.BeginOpen(this.timeoutHelper.RemainingTime(), new AsyncCallback(this.OnTransportOpenCompete), args);
         if (asyncResult.CompletedSynchronously)
         {
             this.HandleTransportOpened(asyncResult);
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         MessagingClientEtwProvider.TraceClient <AmqpTransportInitiator, Exception>((AmqpTransportInitiator source, Exception ex) => MessagingClientEtwProvider.Provider.EventWriteAmqpLogError(source, "OnProtocolHeader", ex.Message), this, exception);
         args.Exception = exception;
         this.Complete(args);
     }
 }