コード例 #1
0
        private async Task <MsgOp> DoRequestAsync(string subject, byte[] body, int?timeoutMs)
        {
            var requestReplyAddress = $"{Guid.NewGuid():N}";
            var pubCmd = PubCmd.Generate(subject, body, requestReplyAddress);

            var taskComp            = new TaskCompletionSource <MsgOp>();
            var requestSubscription = MsgOpStream.Where(msg => msg.Subject == requestReplyAddress).Subscribe(
                msg => taskComp.SetResult(msg),
                ex => taskComp.SetException(ex));

            var subscriptionInfo = new SubscriptionInfo(requestReplyAddress, maxMessages: 1);
            var subCmd           = SubCmd.Generate(subscriptionInfo.Subject, subscriptionInfo.Id);
            var unsubCmd         = UnsubCmd.Generate(subscriptionInfo.Id, subscriptionInfo.MaxMessages);

            await _connection.WithWriteLockAsync(async writer =>
            {
                await writer.WriteAsync(subCmd).ConfigureAwait(false);
                await writer.WriteAsync(unsubCmd).ConfigureAwait(false);
                await writer.FlushAsync().ConfigureAwait(false);
                await writer.WriteAsync(pubCmd).ConfigureAwait(false);
                await writer.FlushAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);

            Task.WaitAny(new[] { Task.Delay(timeoutMs ?? _connectionInfo.RequestTimeoutMs), taskComp.Task }, _cancellation.Token);
            if (!taskComp.Task.IsCompleted)
            {
                taskComp.SetException(NatsException.RequestTimedOut());
            }

            return(await taskComp.Task
                   .ContinueWith(t =>
            {
                requestSubscription?.Dispose();

                if (!t.IsFaulted)
                {
                    return t.Result;
                }

                var ex = t.Exception?.GetBaseException() ?? t.Exception;
                if (ex == null)
                {
                    return t.Result;
                }

                _logger.Error("Exception while performing request.", ex);

                throw ex;
            })
                   .ConfigureAwait(false));
        }
コード例 #2
0
        public async Task PubAsync(string subject, string body, string replyTo = null)
        {
            ThrowIfDisposed();

            var cmdPayload = PubCmd.Generate(subject, body, replyTo);

            await _connection.WithWriteLockAsync(async writer =>
            {
                await writer.WriteAsync(cmdPayload).ConfigureAwait(false);
                if (ShouldAutoFlush)
                {
                    await writer.FlushAsync().ConfigureAwait(false);
                }
            }).ConfigureAwait(false);
        }
コード例 #3
0
        public void Pub(string subject, IPayload body, string replyTo = null)
        {
            ThrowIfDisposed();

            var cmdPayload = PubCmd.Generate(subject, body, replyTo);

            _connection.WithWriteLock(writer =>
            {
                writer.Write(cmdPayload);
                if (ShouldAutoFlush)
                {
                    writer.Flush();
                }
            });
        }
コード例 #4
0
ファイル: Publisher.cs プロジェクト: sujitn/mynatsclient
 public async Task PubAsync(string subject, IPayload body, string replyTo = null)
 => await _pubPayloadAsync(PubCmd.Generate(subject, body, replyTo)).ConfigureAwait(false);
コード例 #5
0
ファイル: Publisher.cs プロジェクト: sujitn/mynatsclient
 public async Task PubAsync(string subject, byte[] body, string replyTo = null)
 => await _pubBytesAsync(PubCmd.Generate(subject, body, replyTo)).ConfigureAwait(false);
コード例 #6
0
ファイル: Publisher.cs プロジェクト: sujitn/mynatsclient
 public void Pub(string subject, IPayload body, string replyTo = null)
 => _pubPayloadSync(PubCmd.Generate(subject, body, replyTo));
コード例 #7
0
ファイル: Publisher.cs プロジェクト: sujitn/mynatsclient
 public void Pub(string subject, byte[] body, string replyTo = null)
 => _pubBytesSync(PubCmd.Generate(subject, body, replyTo));