Ejemplo n.º 1
0
        IEnumerable <TMessageResponse> INetworkBus.PublishAndWaitForReplyEnumerable <TMessageRequest, TMessageResponse>(TMessageRequest message, Guid?referenceId, int timeout)
        {
            var observable = Connection.Observe(INetworkBus.GetSubject <TMessageResponse>(referenceId))
                             .Select(msg =>
            {
                var response = new TMessageResponse();
                response.SetData(msg.Data);
                return(response);
            })
                             .Timeout(TimeSpan.FromMilliseconds(timeout == -1 ? int.MaxValue : timeout))
                             .Catch <TMessageResponse, TimeoutException>(_ => Observable.Empty <TMessageResponse>());

            using var _ = observable.Subscribe();
            Publish(message, referenceId);

            foreach (var response in observable.ToEnumerable())
            {
                yield return(response);

                if (response.IsLastMessage)
                {
                    yield break;
                }
            }
        }
Ejemplo n.º 2
0
 IDisposable INetworkBus.Subscribe <TMessage>(Action <TMessage> func, Guid?referenceId) =>
 Connection.SubscribeAsync(INetworkBus.GetSubject <TMessage>(referenceId), (s, e) =>
 {
     var request = new TMessage();
     request.SetData(e.Message.Data);
     func(request);
 });
Ejemplo n.º 3
0
 Task <IDisposable> IAsyncNetworkBus.SubscribeAsync <TMessage>(Func <TMessage, Task> func, Guid?referenceId) =>
 Task.FromResult((IDisposable)Connection.SubscribeAsync(INetworkBus.GetSubject <TMessage>(referenceId), async(s, e) =>
 {
     var request = new TMessage();
     request.SetData(e.Message.Data);
     await func(request);
 }));
Ejemplo n.º 4
0
 Task <IDisposable> IAsyncNetworkBus.SubscribeAndReplyAsync <TMessageRequest>(Func <TMessageRequest, Task <IMessage> > func, Guid?referenceId) =>
 Task.FromResult((IDisposable)Connection.SubscribeAsync(INetworkBus.GetSubject <TMessageRequest>(referenceId), async(s, e) =>
 {
     var request = new TMessageRequest();
     request.SetData(e.Message.Data);
     var response = await func(request);
     await PublishAsync(response, referenceId);
 }));
Ejemplo n.º 5
0
        IDisposable INetworkBus.SubscribeAndReply <TMessageRequest>(Func <TMessageRequest, IMessage> func, Guid?referenceId) =>
        Connection.SubscribeAsync(INetworkBus.GetSubject <TMessageRequest>(referenceId), (s, e) =>
        {
            var request = new TMessageRequest();
            request.SetData(e.Message.Data);
            var response = func(request);

            Publish(response, referenceId);
        });
Ejemplo n.º 6
0
 IDisposable INetworkBus.SubscribeAndReplyEnumerable <TMessageRequest, TMessageResponse>(Func <TMessageRequest, IEnumerable <TMessageResponse> > func, Guid?referenceId) =>
 Connection.SubscribeAsync(INetworkBus.GetSubject <TMessageRequest>(), (s, e) =>
 {
     var request = new TMessageRequest();
     request.SetData(e.Message.Data);
     foreach (var response in func(request))
     {
         Publish(response, referenceId);
     }
 });
Ejemplo n.º 7
0
        TMessageResponse INetworkBus.PublishAndWaitForReply <TMessageRequest, TMessageResponse>(TMessageRequest message, Guid?referenceId, int timeout)
        {
            using var cancellationTokenSource = new CancellationTokenSource(timeout);
            var returnLock = new TaskCompletionSource <TMessageResponse>(TaskCreationOptions.RunContinuationsAsynchronously);

            using var sub = Connection.SubscribeAsync(INetworkBus.GetSubject <TMessageResponse>(referenceId), (s, e) =>
            {
                var response = new TMessageResponse();
                response.SetData(e.Message.Data);
                returnLock.SetResult(response);
            });
            Publish(message, referenceId);

            return(returnLock.Task.WaitAsync(cancellationTokenSource.Token).GetAwaiter().GetResult());
        }
Ejemplo n.º 8
0
        async Task <TMessageResponse> IAsyncNetworkBus.PublishAndWaitForReplyAsync <TMessageRequest, TMessageResponse>(TMessageRequest message, Guid?referenceId, int timeout)
        {
            using var cancellationTokenSource = new CancellationTokenSource(timeout);
            var returnLock = new TaskCompletionSource <TMessageResponse>(TaskCreationOptions.RunContinuationsAsynchronously);

            using var sub = Connection.SubscribeAsync(INetworkBus.GetSubject <TMessageResponse>(referenceId), (s, e) =>
            {
                // TODO:
                try
                {
                    var response = new TMessageResponse();
                    response.SetData(e.Message.Data);
                    returnLock.SetResult(response);
                }
                catch (Exception) { }
            });
            await PublishAsync(message, referenceId);

            return(await returnLock.Task.WaitAsync(cancellationTokenSource.Token));
        }
Ejemplo n.º 9
0
 public void Publish <TMessage>(TMessage message, Guid?referenceId = null) where TMessage : notnull, IMessage =>
 Connection.Publish(INetworkBus.GetSubject(message, referenceId), message.GetData().ToArray());
Ejemplo n.º 10
0
 public Task PublishAsync <TMessage>(TMessage message, Guid?referenceId) where TMessage : notnull, IMessage =>
 Task.Run(() => Connection.Publish(INetworkBus.GetSubject(message, referenceId), message.GetData().ToArray()));