Exemplo n.º 1
0
        /// <summary>
        /// Send kafka payload to server and receive a task event when response is received.
        /// </summary>
        /// <typeparam name="T">A Kafka response object return by decode function.</typeparam>
        /// <param name="request">The IKafkaRequest to send to the kafka servers.</param>
        /// <returns></returns>
        public Task <List <T> > SendAsync <T>(IKafkaRequest <T> request)
        {
            //assign unique correlationId
            request.CorrelationId = NextCorrelationId();

            var tcs         = new TaskCompletionSource <List <T> >();
            var asynRequest = new AsyncRequestItem(request.CorrelationId);

            asynRequest.ReceiveTask.Task.ContinueWith(data =>
            {
                try
                {
                    var response = request.Decode(data.Result);
                    tcs.SetResult(response.ToList());

                    //TODO should we check for errors type here and throw?
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            });

            if (_requestIndex.TryAdd(request.CorrelationId, asynRequest) == false)
            {
                throw new ApplicationException("Failed to register request for async response.");
            }

            SendAsync(request.Encode());

            return(tcs.Task);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send kafka payload to server and receive a task event when response is received.
        /// </summary>
        /// <typeparam name="T">A Kafka response object return by decode function.</typeparam>
        /// <param name="request">The IKafkaRequest to send to the kafka servers.</param>
        /// <returns></returns>
        public async Task <List <T> > SendAsync <T>(IKafkaRequest <T> request)
        {
            //assign unique correlationId
            request.CorrelationId = NextCorrelationId();

            //if response is expected, register a receive data task and send request
            if (request.ExpectResponse)
            {
                var asyncRequest = new AsyncRequestItem(request.CorrelationId);

                if (_requestIndex.TryAdd(request.CorrelationId, asyncRequest) == false)
                {
                    throw new ApplicationException("Failed to register request for async response.");
                }

                SendAsync(request.Encode());

                var response = await asyncRequest.ReceiveTask.Task.ConfigureAwait(false);

                return(request.Decode(response).ToList());
            }


            //no response needed, just send
            await SendAsync(request.Encode()).ConfigureAwait(false);

            //TODO should this return a response of success for request?
            return(new List <T>());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Send kafka payload to server and receive a task event when response is received.
        /// </summary>
        /// <typeparam name="T">A Kafka response object return by decode function.</typeparam>
        /// <param name="request">The IKafkaRequest to send to the kafka servers.</param>
        /// <returns></returns>
        public async Task <List <T> > SendAsync <T>(IKafkaRequest <T> request)
        {
            //assign unique correlationId
            request.CorrelationId = NextCorrelationId();

            //if response is expected, register a receive data task and send request
            if (request.ExpectResponse)
            {
                var asyncRequest = new AsyncRequestItem(request.CorrelationId);

                try
                {
                    AddAsyncRequestItemToResponseQueue(asyncRequest);

                    await SendAsync(request.Encode()).ConfigureAwait(false);
                }
                catch (OperationCanceledException ex)
                {
                    TriggerMessageTimeout(asyncRequest);
                }

                var response = await asyncRequest.ReceiveTask.Task.ConfigureAwait(false);

                return(request.Decode(response).ToList());
            }


            //no response needed, just send
            await SendAsync(request.Encode()).ConfigureAwait(false);

            //TODO should this return a response of success for request?
            return(new List <T>());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Send kafka payload to server and receive a task event when response is received.
        /// </summary>
        /// <typeparam name="T">A Kafka response object return by decode function.</typeparam>
        /// <param name="request">The IKafkaRequest to send to the kafka servers.</param>
        /// <returns></returns>
        public async Task <List <T> > SendAsync <T>(IKafkaRequest <T> request)
        {
            //assign unique correlationId
            request.CorrelationId = NextCorrelationId();

            var asyncRequest = new AsyncRequestItem(request.CorrelationId);

            if (_requestIndex.TryAdd(request.CorrelationId, asyncRequest) == false)
            {
                throw new ApplicationException("Failed to register request for async response.");
            }

            await SendAsync(request.Encode());

            var response = await asyncRequest.ReceiveTask.Task;

            return(request.Decode(response).ToList());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Send kafka payload to server and receive a task event when response is received.
        /// </summary>
        /// <typeparam name="T">A Kafka response object return by decode function.</typeparam>
        /// <param name="request">The IKafkaRequest to send to the kafka servers.</param>
        /// <returns></returns>

        public async Task <List <T> > SendAsync <T>(IKafkaRequest <T> request)
        {
            //assign unique correlationId
            request.CorrelationId = NextCorrelationId();

            _log.DebugFormat("Entered SendAsync for CorrelationId:{0} Connection:{1} ", request.CorrelationId, Endpoint);
            //if response is expected, register a receive data task and send request
            if (request.ExpectResponse)
            {
                using (var asyncRequest = new AsyncRequestItem(request.CorrelationId))
                {
                    try
                    {
                        AddAsyncRequestItemToResponseQueue(asyncRequest);
                        ExceptionDispatchInfo exceptionDispatchInfo = null;

                        try
                        {
                            await _client.WriteAsync(request.Encode()).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
                        }

                        asyncRequest.MarkRequestAsSent(exceptionDispatchInfo, _responseTimeoutMs, TriggerMessageTimeout);
                    }
                    catch (OperationCanceledException)
                    {
                        TriggerMessageTimeout(asyncRequest);
                    }

                    var response = await asyncRequest.ReceiveTask.Task.ConfigureAwait(false);

                    return(request.Decode(response).ToList());
                }
            }

            //no response needed, just send
            await _client.WriteAsync(request.Encode()).ConfigureAwait(false);

            //TODO should this return a response of success for request?
            return(new List <T>());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Send kafka payload to server and receive a task event when response is received.
        /// </summary>
        /// <typeparam name="T">A Kafka response object return by decode function.</typeparam>
        /// <param name="request">The IKafkaRequest to send to the kafka servers.</param>
        /// <returns></returns>
        public async Task <List <T> > SendAsync <T>(IKafkaRequest <T> request)
        {
            //assign unique correlationId
            request.Correlation = NextCorrelationId();

            var asyncRequest = new AsyncRequestItem(request.Correlation);

            if (_requestIndex.TryAdd(request.Correlation, asyncRequest) == false)
            {
                throw new ApplicationException("Failed to register request for async response.");
            }

            // Encode the request into a stream
            var stream = new BinaryStream();

            request.Encode(stream);

            // Write to the socket
            await _client.WriteAsync(stream);

            var response = await asyncRequest.ReceiveTask.Task;

            return(request.Decode(response).ToList());
        }