Esempio n. 1
0
 public void DisposeMessage(AmqpMessage message, DeliveryState state, bool settled, bool batchable)
 {
     this.ThrowIfClosed();
     message.Batchable = batchable;
     this.DisposeDelivery(message, settled, state);
 }
Esempio n. 2
0
        void OnReceiveMessage(AmqpMessage message)
        {
            if (this.messageListener != null)
            {
                this.messageListener(message);
            }
            else
            {
                ReceiveAsyncResult waiter = null;
                int  creditToIssue        = 0;
                bool releaseMessage       = false;
                lock (this.SyncRoot)
                {
                    if (this.waiterList != null && this.waiterList.Count > 0)
                    {
                        var firstWaiter = this.waiterList.First.Value;

                        if (this.messageQueue.IsPrefetchingBySize)
                        {
                            if (this.messageQueue.UpdateCreditToIssue(message))
                            {
                                this.SetTotalLinkCredit(this.messageQueue.BoundedTotalLinkCredit, true);
                            }
                        }

                        firstWaiter.Add(message);
                        if (firstWaiter.RequestedMessageCount == 1 || firstWaiter.MessageCount >= firstWaiter.RequestedMessageCount)
                        {
                            this.waiterList.RemoveFirst();
                            firstWaiter.OnRemoved();
                            creditToIssue = this.Settings.AutoSendFlow ? 0 : this.GetOnDemandReceiveCredit();
                            waiter        = firstWaiter;
                        }
                    }
                    else if (!this.Settings.AutoSendFlow && this.Settings.SettleType != SettleMode.SettleOnSend)
                    {
                        releaseMessage = true;
                    }
                    else if (this.messageQueue != null)
                    {
                        this.messageQueue.Enqueue(message);
                        AmqpTrace.Provider.AmqpCacheMessage(
                            this,
                            message.DeliveryId.Value,
                            this.messageQueue.Count,
                            this.messageQueue.IsPrefetchingBySize,
                            this.TotalCacheSizeInBytes ?? 0,
                            this.Settings == null ? 0 : this.Settings.TotalLinkCredit,
                            this.LinkCredit);
                    }
                }

                if (releaseMessage)
                {
                    this.ReleaseMessage(message);
                    message.Dispose();
                }

                if (creditToIssue > 0)
                {
                    this.IssueCredit((uint)creditToIssue, false, AmqpConstants.NullBinary);
                }

                if (waiter != null)
                {
                    // Schedule the completion on another thread so we don't block the I/O thread
                    ActionItem.Schedule(o => { var w = (ReceiveAsyncResult)o; w.Signal(false); }, waiter);
                }
            }
        }
Esempio n. 3
0
 public void AcceptMessage(AmqpMessage message, bool settled, bool batchable)
 {
     this.DisposeMessage(message, AmqpConstants.AcceptedOutcome, settled, batchable);
 }
Esempio n. 4
0
 public void ReleaseMessage(AmqpMessage message)
 {
     this.DisposeMessage(message, AmqpConstants.ReleasedOutcome, true, false);
 }
 public void Done(bool completedSynchronously, AmqpMessage response)
 {
     this.response = response;
     this.CompleteSelf(completedSynchronously);
 }
Esempio n. 6
0
        public void AcceptMessage(AmqpMessage message, bool batchable)
        {
            bool settled = this.Settings.SettleType != SettleMode.SettleOnDispose;

            this.AcceptMessage(message, settled, batchable);
        }
 public IAsyncResult BeginRequest(AmqpMessage request, TimeSpan timeout, AsyncCallback callback, object state)
 {
     return(new RequestAsyncResult(this, request, timeout, callback, state));
 }
 public void Start()
 {
     this.SetTimer();
     this.parent.sender.SendMessageNoWait(this.request, AmqpConstants.EmptyBinary, AmqpConstants.NullBinary);
     this.request = null;
 }
Esempio n. 9
0
 public AmqpClonedMessage(AmqpMessage source, bool deepCopy)
 {
     this.deepCopy = deepCopy;
     this.CopyFrom(source, deepCopy, true);
     this.source = source.buffer?.AddReference();
 }
Esempio n. 10
0
        /// <summary>
        /// Sends a security token for a resource for later access.
        /// </summary>
        /// <param name="tokenProvider">The provider for issuing security tokens.</param>
        /// <param name="namespaceAddress">The namespace (or tenant) name.</param>
        /// <param name="audience">The audience. In most cases it is the same as resource.</param>
        /// <param name="resource">The resource to access.</param>
        /// <param name="requiredClaims">The required claims to access the resource.</param>
        /// <param name="timeout">The operation timeout.</param>
        /// <returns></returns>
        public async Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout)
        {
            if (this.connection.IsClosing())
            {
                throw new OperationCanceledException("Connection is closing or closed.");
            }

            CbsToken token = await tokenProvider.GetTokenAsync(namespaceAddress, resource, requiredClaims);

            string tokenType = token.TokenType;

            if (tokenType == null)
            {
                throw new NotSupportedException(AmqpResources.AmqpUnsupportedTokenType);
            }

            RequestResponseAmqpLink requestResponseLink;

            if (!this.linkFactory.TryGetOpenedObject(out requestResponseLink))
            {
                requestResponseLink = await this.linkFactory.GetOrCreateAsync(timeout);
            }

            AmqpValue value = new AmqpValue();

            value.Value = token.TokenValue;
            AmqpMessage putTokenRequest = AmqpMessage.Create(value);

            putTokenRequest.ApplicationProperties.Map[CbsConstants.Operation]           = CbsConstants.PutToken.OperationValue;
            putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Type]       = tokenType;
            putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Audience]   = audience;
            putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Expiration] = token.ExpiresAtUtc;

            AmqpMessage putTokenResponse = await requestResponseLink.RequestAsync(putTokenRequest, timeout);

            int    statusCode        = (int)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusCode];
            string statusDescription = (string)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusDescription];

            if (statusCode == (int)AmqpResponseStatusCode.Accepted || statusCode == (int)AmqpResponseStatusCode.OK)
            {
                return(token.ExpiresAtUtc);
            }

            Exception exception;
            AmqpResponseStatusCode amqpResponseStatusCode = (AmqpResponseStatusCode)statusCode;

            switch (amqpResponseStatusCode)
            {
            case AmqpResponseStatusCode.BadRequest:
                exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription));
                break;

            case AmqpResponseStatusCode.NotFound:
                exception = new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription));
                break;

            case AmqpResponseStatusCode.Forbidden:
                exception = new AmqpException(AmqpErrorCode.TransferLimitExceeded, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription));
                break;

            case AmqpResponseStatusCode.Unauthorized:
                exception = new AmqpException(AmqpErrorCode.UnauthorizedAccess, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription));
                break;

            default:
                exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription));
                break;
            }

            throw exception;
        }
Esempio n. 11
0
 public void DisposeMessage(AmqpMessage message, DeliveryState deliveryState, bool settled, bool batchable)
 {
     this.receiver.DisposeMessage(message, deliveryState, settled, batchable);
 }
Esempio n. 12
0
 public Task <Outcome> SendMessageAsync(AmqpMessage message, ArraySegment <byte> deliveryTag, ArraySegment <byte> txnId, TimeSpan timeout)
 {
     return(this.sender.SendMessageAsync(message, deliveryTag, txnId, timeout));
 }
Esempio n. 13
0
            protected override IEnumerator <AsyncStep> GetAsyncSteps()
            {
                Task <CbsToken> getTokenTask = null;

                yield return(this.CallTask(
                                 (thisPtr, t) => getTokenTask = thisPtr.tokenProvider.GetTokenAsync(thisPtr.namespaceAddress, thisPtr.resource, thisPtr.requiredClaims),
                                 ExceptionPolicy.Transfer));

                this.token = getTokenTask.Result;

                string tokenType = this.token.TokenType;

                if (tokenType == null)
                {
                    this.Complete(new InvalidOperationException(AmqpResources.AmqpUnsupportedTokenType));
                    yield break;
                }

                RequestResponseAmqpLink requestResponseLink;

                if (this.cbsLink.FaultTolerantLink.TryGetOpenedObject(out requestResponseLink))
                {
                    this.requestResponseLinkTask = Task.FromResult(requestResponseLink);
                }
                else
                {
                    yield return(this.CallTask(
                                     (thisPtr, t) => thisPtr.requestResponseLinkTask = thisPtr.cbsLink.FaultTolerantLink.GetOrCreateAsync(t),
                                     ExceptionPolicy.Transfer));
                }

                AmqpValue value = new AmqpValue();

                value.Value = this.token.TokenValue;
                AmqpMessage putTokenRequest = AmqpMessage.Create(value);

                putTokenRequest.ApplicationProperties = new ApplicationProperties();
                putTokenRequest.ApplicationProperties.Map[CbsConstants.Operation]           = CbsConstants.PutToken.OperationValue;
                putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Type]       = tokenType;
                putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Audience]   = this.audience;
                putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Expiration] = this.token.ExpiresAtUtc;

                AmqpMessage putTokenResponse = null;

                Fx.AssertIsNotNull(this.requestResponseLinkTask.Result, "requestResponseLink cannot be null without exception");
                yield return(this.CallAsync(
                                 (thisPtr, t, c, s) => thisPtr.requestResponseLinkTask.Result.BeginRequest(putTokenRequest, t, c, s),
                                 (thisPtr, r) => putTokenResponse = thisPtr.requestResponseLinkTask.Result.EndRequest(r),
                                 ExceptionPolicy.Transfer));

                int    statusCode        = (int)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusCode];
                string statusDescription = (string)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusDescription];

                if (statusCode == (int)AmqpResponseStatusCode.Accepted || statusCode == (int)AmqpResponseStatusCode.OK)
                {
                    this.ExpiresAtUtc = this.token.ExpiresAtUtc;
                }
                else
                {
                    this.Complete(ConvertToException(statusCode, statusDescription));
                }
            }