Ejemplo n.º 1
0
        void OnReceiveAttach(Attach attach)
        {
            StateTransition stateTransition;

            this.TransitState("R:ATTACH", StateTransition.ReceiveOpen, out stateTransition);

            Error error = this.Negotiate(attach);

            if (error != null)
            {
                this.OnLinkOpenFailed(new AmqpException(error));
                return;
            }

            if (stateTransition.From == AmqpObjectState.OpenSent)
            {
                if (this.IsReceiver)
                {
                    Source source = this.settings.Source as Source;
                    if (source != null && source.Dynamic())
                    {
                        source.Address = ((Source)attach.Source).Address;
                    }
                }
                else
                {
                    Target target = this.settings.Target as Target;
                    if (target != null && target.Dynamic())
                    {
                        target.Address = ((Target)attach.Target).Address;
                    }
                }
            }

            if (stateTransition.To == AmqpObjectState.Opened)
            {
                if ((this.IsReceiver && attach.Source == null) ||
                    (!this.IsReceiver && attach.Target == null))
                {
                    // not linkendpoint was created on the remote side
                    // a detach should be sent immediately by peer with error
                    return;
                }

                if (this.IsReceiver)
                {
                    this.deliveryCount   = attach.InitialDeliveryCount.Value;
                    this.settings.Source = attach.Source;
                }
                else
                {
                    this.settings.Target = attach.Target;
                }

                this.CompleteOpen(false, null);
            }
            else if (stateTransition.To == AmqpObjectState.OpenReceived)
            {
                Utils.Trace(TraceLevel.Verbose, "{0}: opending.", this);
                try
                {
                    this.Session.Connection.AmqpSettings.RuntimeProvider.BeginOpenLink(this, this.DefaultOpenTimeout, this.OnProviderLinkOpened, null);
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }

                    this.OnLinkOpenFailed(exception);
                }
            }
        }