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; } } }
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); });
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); }));
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); }));
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); });
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); } });
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()); }
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)); }
public void Publish <TMessage>(TMessage message, Guid?referenceId = null) where TMessage : notnull, IMessage => Connection.Publish(INetworkBus.GetSubject(message, referenceId), message.GetData().ToArray());
public Task PublishAsync <TMessage>(TMessage message, Guid?referenceId) where TMessage : notnull, IMessage => Task.Run(() => Connection.Publish(INetworkBus.GetSubject(message, referenceId), message.GetData().ToArray()));