Exemplo n.º 1
0
        public Task <ResponseData> Request(RequestData request, CancellationToken token)
        {
            this.EnsureIsRunning();

            var requestId = this.NextId();

            var callback = new TaskCompletionSource <ResponseData>();

            this.requestCallbacks[requestId] = callback;

            Task.Run(() =>
            {
                var zmqRequest      = new ZmqRequest(requestId, request);
                var zmqRequestBytes = zmqRequest.ToBinary();

                this.requestsQueue.TryAdd(zmqRequestBytes);
            });

            if (token != CancellationToken.None)
            {
                token.Register(() =>
                {
                    TaskCompletionSource <ResponseData> _;
                    this.requestCallbacks.TryRemove(requestId, out _);
                });
            }

            return(callback.Task);
        }
Exemplo n.º 2
0
        public Task<ResponseData> Request(RequestData request, CancellationToken token)
        {
            this.EnsureIsRunning();

            var requestId = this.NextId();

            var callback = new TaskCompletionSource<ResponseData>();
            this.requestCallbacks[requestId] = callback;

            Task.Run(() =>
            {
                var zmqRequest = new ZmqRequest(requestId, request);
                var zmqRequestBytes = zmqRequest.ToBinary();

                this.requestsQueue.TryAdd(zmqRequestBytes);
            });

            if (token != CancellationToken.None)
            {
                token.Register(() =>
                {
                    TaskCompletionSource<ResponseData> _;
                    this.requestCallbacks.TryRemove(requestId, out _);
                });
            }

            return callback.Task;
        }
Exemplo n.º 3
0
        private void OnRequest(byte[] callerId, byte[] requestBytes)
        {
            Task.Run(() =>
            {
                var zmqRequest = ZmqRequest.FromBinary(requestBytes);

                var service = this.serviceFactory.CreateService(zmqRequest.Request.Service);

                service.Process(zmqRequest.Request)
                .ContinueWith(t =>
                {
                    var response = t.Result;

                    var zmqResponse      = new ZmqResponse(zmqRequest.Id, response);
                    var zmqResponseBytes = zmqResponse.ToBinary();

                    this.responsesQueue.TryAdd(new Tuple <byte[], byte[]>(callerId, zmqResponseBytes));
                });
            });
        }