Ejemplo n.º 1
0
        private async Task <(ReceiverLink receiverLink, string address)> CreateReceiverLink(CancellationToken cancellationToken)
        {
            var tcs          = TaskUtil.CreateTaskCompletionSource <string>(cancellationToken);
            var receiverLink = new ReceiverLink(_session, Guid.NewGuid().ToString(), new Source
            {
                Dynamic = true
            }, OnAttached);

            receiverLink.AddClosedCallback(OnClosed);
            var address = await tcs.Task.ConfigureAwait(false);

            receiverLink.Closed -= OnClosed;
            return(receiverLink, address);

            void OnAttached(ILink link, Attach attach)
            {
                if (attach != null && attach.Source is Source source)
                {
                    tcs.TrySetResult(source.Address);
                }
            }

            void OnClosed(IAmqpObject sender, Error error)
            {
                if (error != null)
                {
                    tcs.TrySetException(new CreateRpcClientException(error.Description, error.Condition));
                }
            }
        }
Ejemplo n.º 2
0
        private async Task <SenderLink> CreateSenderLink(string address, CancellationToken cancellationToken)
        {
            var tcs        = TaskUtil.CreateTaskCompletionSource <bool>(cancellationToken);
            var senderLink = new SenderLink(_session, Guid.NewGuid().ToString(), new Target
            {
                Address = address
            }, OnAttached);

            senderLink.AddClosedCallback(OnClosed);
            await tcs.Task.ConfigureAwait(false);

            senderLink.Closed -= OnClosed;
            return(senderLink);


            void OnAttached(ILink link, Attach attach)
            {
                if (attach != null)
                {
                    tcs.TrySetResult(true);
                }
            }

            void OnClosed(IAmqpObject sender, Error error)
            {
                if (error != null)
                {
                    tcs.TrySetException(new CreateRpcClientException(error.Description, error.Condition));
                }
            }
        }
Ejemplo n.º 3
0
        public Task <byte[]> DeclareAsync(CancellationToken cancellationToken)
        {
            var message = new Amqp.Message(new Declare());
            var tcs     = TaskUtil.CreateTaskCompletionSource <byte[]>(cancellationToken);

            _senderLink.Send(message, null, _onDeclareOutcome, tcs);
            return(tcs.Task);
        }
Ejemplo n.º 4
0
        public Task DischargeAsync(byte[] txnId, bool fail, CancellationToken cancellationToken)
        {
            var message = new Amqp.Message(new Discharge {
                TxnId = txnId, Fail = fail
            });
            var tcs = TaskUtil.CreateTaskCompletionSource <bool>(cancellationToken);

            _senderLink.Send(message, null, _onDischargeOutcome, tcs);
            return(tcs.Task);
        }
        protected async Task SendInternalAsync(string address, RoutingType?routingType, Message message, Transaction transaction, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var txnId = await _transactionsManager.GetTxnIdAsync(transaction, cancellationToken).ConfigureAwait(false);

            var transactionalState = txnId != null ? new TransactionalState {
                TxnId = txnId
            } : null;
            var tcs = TaskUtil.CreateTaskCompletionSource <bool>(cancellationToken);

            cancellationToken.Register(() => tcs.TrySetCanceled());
            message.DurabilityMode ??= _configuration.MessageDurabilityMode ?? DurabilityMode.Durable;
            Send(address, routingType, message, transactionalState, _onOutcome, tcs);
            await tcs.Task.ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        public async Task <Message> SendAsync(Message message, CancellationToken cancellationToken)
        {
            var correlationId = Guid.NewGuid();

            message.SetCorrelationId(correlationId);
            message.Properties.ReplyTo = _replyToAddress;

            var tcs = TaskUtil.CreateTaskCompletionSource <Message>(cancellationToken);

            try
            {
                _pendingRequests.TryAdd(correlationId, tcs);
                _senderLink.Send(message.InnerMessage, null, _onOutcome, tcs);
                return(await tcs.Task.ConfigureAwait(false));
            }
            finally
            {
                _pendingRequests.TryRemove(correlationId, out _);
            }
        }