public Task <TResult> Send <TCommand, TResult>(TCommand command) where TCommand : ICommand where TResult : ICommandResult { if (!_isConnected.Value) { throw new Exception("lost connection to gateway"); } var message = new TransportMessage() { CommandId = Guid.NewGuid(), Message = command.Serialize(), CommandType = typeof(TCommand) }; var task = new TaskCompletionSource <ICommandResult>(); var cancel = new CancellationTokenSource(_configuration.CommandTimeout); cancel.Token.Register(() => task.TrySetCanceled(), false); _commandResults.Add(message.CommandId, task); _client.SendFrame(message.Serialize()); return(task.Task.ContinueWith(result => (TResult)result.Result, cancel.Token)); }
private void OnWorkAdded(TransportMessage work) { if (TryGetWorker(work, out WorkerDescriptor worker)) { work.WorkerId = worker.WorkerId; _backend.SendMoreFrame(worker.WorkerId.ToByteArray()) .SendMoreFrameEmpty() .SendFrame(work.Serialize()); } else { var queue = _workQueues.GetOrAdd(work.CommandType, (type) => { return(new NetMQQueue <TransportMessage>()); }); queue.Enqueue(work); } }