Exemplo n.º 1
0
 private void InitializeClients()
 {
     for (var i = 0; i < ClientPoolSize; i++)
     {
         var client = new SirenaClient(clientSettings);
         clients.Enqueue(client);
     }
 }
Exemplo n.º 2
0
        private Boolean TryGetFreeClient(out SirenaClient client)
        {
            var result = default(Boolean);

            client = default(SirenaClient);

            result = clients.TryDequeue(out client);
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously queues a request in the SirenaClientPool instance.
        /// </summary>
        /// <typeparam name="TDtoRequest">The dto request type.</typeparam>
        /// <typeparam name="TDtoResponse">The dto response type.</typeparam>
        /// <param name="dtoRequest">The dto request to send.</param>
        /// <param name="connectionMode">The connection mode used for sending the request.</param>
        /// <returns>Returns the task object representing the asynchronous operation.</returns>
        public async Task <TDtoResponse> QueueRequestAsync <TDtoRequest, TDtoResponse>(TDtoRequest dtoRequest, ConnectionMode connectionMode = ConnectionMode.Plain)
            where TDtoRequest : DtoRequest
            where TDtoResponse : DtoResponse
        {
            var client       = default(SirenaClient);
            var attemptCount = 3;

            try
            {
                var task = Task.Factory.StartNew <SirenaClient>(() =>
                {
                    var result = default(SirenaClient);
                    while (true)
                    {
                        if (TryGetFreeClient(out result))
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(PollingDelay);
                        }
                    }

                    return(result);
                });

                client = await task;

                // Try to send the request to the sirena server.
                // If any exception occurred resend the request
                // while the attempt count more than 0, otherwise
                // rethrow an exception gotten during the request
                // sending.
                while (true)
                {
                    try
                    {
                        if (!client.IsConnected)
                        {
                            await client.ConnectAsync();
                        }
                        var dtoResponse = await client.SendRequestAsync <TDtoRequest, TDtoResponse>(dtoRequest, connectionMode);

                        return(dtoResponse);
                    }
                    catch (IOException e)
                    {
                        if (attemptCount == 0)
                        {
                            throw;
                        }

                        client = new SirenaClient(clientSettings);
                        attemptCount--;
                    }
                }
            }
            finally
            {
                //Try to put back client.
                if (client != null && client.IsConnected)
                {
                    PutBackClient(client);
                }
                else
                {
                    PutBackClient(new SirenaClient(clientSettings));
                }
            }
        }
Exemplo n.º 4
0
 private void PutBackClient(SirenaClient client)
 {
     clients.Enqueue(client);
 }