コード例 #1
0
        public async Task <RpcMessage> RpcExec(string queueName, string message)
        {
            var taskCompletion = new TaskCompletionSource <RpcMessage>();

            var id          = Guid.NewGuid().ToString();
            var returnQueue = ReturnQueueName(queueName, id);

            _connection.recieveChannel.QueueDeclare(returnQueue, false, false, true, null);

            var consumer = new EventingBasicConsumer(_connection.recieveChannel);

            _connection.recieveChannel.BasicConsume(returnQueue, false, "", false, true, null, consumer);

            var expiration = UnixTime.To(DateTime.Now.AddSeconds(TimeoutSeconds));
            var request    = new RpcRequestMessage(message, returnQueue, expiration);

            var r = RpcMessages.ToByte(request);

            Thread.Sleep(TimeSpan.FromTicks(1));
            var rpcQueueName = RpcQueueName(queueName);

            var properties = _connection.sendChannel.CreateBasicProperties();

            properties.ContentType = "application/json";
            properties.Expiration  = expiration.ToString();

            _connection.sendChannel.BasicPublish("", rpcQueueName, false, properties, r);

            consumer.Received += (model, ea) =>
            {
                _connection.recieveChannel.QueueDelete(returnQueue, false, true);
                if (DateTime.Now > (UnixTime.From(expiration)))
                {
                    //TODO: Error RPC timeout
                    taskCompletion.SetException(new Exception());
                }
                else
                {
                    var m = RpcMessages.FromByte(ea.Body);
                    _connection.recieveChannel.BasicAck(ea.DeliveryTag, false);

                    taskCompletion.SetResult(m);
                }
            };

            return(await taskCompletion.Task);
        }
コード例 #2
0
 public Task <RpcMessage> Handle(byte[] body)
 {
     Response = RpcMessages.FromByte(body);
     return(Task.FromResult(Response));
 }