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);
                }
            }
Esempio n. 2
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);
            }