private void HandleRequests()
        {
            while (true)
            {
                var request = requestQueue.Dequeue();
                if (request != null)
                {
//                    Console.WriteLine("Found Request Processing..");
//                    Console.WriteLine("command --> " + request.command);

                    var response = Commands.ProcessRequest(request);
                    responseQueue.Enqueue(new Response()
                    {
                        client = request.client,
                        reply  = response
                    });

//                    Console.WriteLine("Added Response to queue");
//                    Console.WriteLine("response --> " + response);
                }
                else
                {
                    Thread.Sleep(1);
                }
            }
        }
Exemple #2
0
 public void FlushQueue()
 {
     while (RequestQueue.Count > 0)
     {
         var reqContext = RequestQueue.Dequeue();
         Flush(reqContext);
     }
 }
        public void Test_RequestQueue_Dequeuing()
        {
            queue.Enqueue(new object());
            queue.Enqueue(new object());
            object obj = queue.Dequeue();

            Assert.AreEqual(queue.Count, 1);
            Assert.IsNotNull(obj);
        }
Exemple #4
0
        public async Task <T> RequestAsync <T>(string eventName, object data, int timeout)
        {
            if (_socket == null || _socket.disconnected)
            {
                throw new Exception("websocket not connected");
            }

            if (_requestQueue.Contains(eventName))
            {
                throw new Exception(string.Format("RequestQueue already contains request {0}", eventName));
            }

            _requestQueue.Add(eventName);
            _socket.emit(eventName, data);

            var timeoutSpan = timeout == 0
                ? TimeSpan.FromMilliseconds(DefaultRequestTimeout)
                : TimeSpan.FromMilliseconds(timeout);
            var timer = new Stopwatch();

            timer.Start();

            while (!_requestQueue.IsDone(eventName) && timer.Elapsed < timeoutSpan)
            {
                await Task.Delay(100);
            }

            var responseData = _requestQueue.Dequeue(eventName);

            timer.Stop();
            if (timer.Elapsed >= timeoutSpan)
            {
                throw new TimeoutException(string.Format("elapsed time = {0} millis", timer.Elapsed.TotalMilliseconds));
            }

            return(responseData.ToObject <T>());
        }
 /// <summary>
 /// Serves the request in the current index.
 /// </summary>
 private async Task ServeNextRequest()
 {
     try
     {
         while (RequestQueue.Count != 0)
         {
             Request request = RequestQueue.Dequeue();
             await Resolve(request);
         }
     }
     finally
     {
         Cancel();
     }
 }
Exemple #6
0
 protected virtual void TriggerRequest(Event @event = null)
 {
     while (RequestQueue.Count > 0)
     {
         var request = RequestQueue.Peek();
         DoRequest(request);
         if (request.IsTriggered)
         {
             RequestQueue.Dequeue();
         }
         else
         {
             break;
         }
     }
 }
Exemple #7
0
 protected override void TriggerRequest(Event @event = null)
 {
     while (RequestQueue.Count > 0)
     {
         var request = RequestQueue.Peek();
         DoRequest(request as ResourceGroupRequest);
         if (request.IsTriggered)
         {
             RequestQueue.Dequeue();
         }
         else
         {
             break;
         }
     }
 }
Exemple #8
0
        public static async Task Start()
        {
            using (var client = await OrleansClient.StartWithRetries())
            {
                while (true)
                {
                    var message = RequestQueue.Dequeue();
                    if (message == null)
                    {
                        await Task.Delay(1_000);

                        continue;
                    }
                    Console.WriteLine($"-- {message}");
                    Task.Run(async() => await client.Process(message));
                }
            }
        }
 protected virtual void TriggerRequest(Event @event = null)
 {
     while (RequestQueue.Count > 0)
     {
         var request = RequestQueue.First;
         DoRequest(request);
         if (request.IsTriggered)
         {
             RequestQueue.Dequeue();
             TriggerWhenEmpty();
             TriggerWhenChange();
         }
         else
         {
             break;
         }
     }
 }
    private IEnumerator ProcessRequestLoop()
    {
        bool   _foundRequest = false;
        string _rType        = "";

        if (RequestQueue.Count > 0)
        {
            _foundRequest = true;
            Request r = RequestQueue.Dequeue();
            Debug.Log("Starting new request: " + r.type.ToString());
            _rType = r.type.ToString();
            switch (r.type)
            {
            case RequestType.Add:
                yield return(StartCoroutine(Instance.AddEntryToDatabase(r.word, r.emotionIdeal, r.boolResult)));

                yield return(new WaitForSeconds(IN_BETWEEN_WAIT));

                break;

            case RequestType.Contains:
                yield return(StartCoroutine(Instance.DatabaseContains(r.word, r.boolResult)));

                yield return(new WaitForSeconds(IN_BETWEEN_WAIT));

                break;

            case RequestType.Get:
                yield return(StartCoroutine(Instance.GetWord(r.word, r.wordEmoIdealResult)));

                yield return(new WaitForSeconds(IN_BETWEEN_WAIT));

                break;
            }
        }

        if (_foundRequest)
        {
            Debug.Log("Finished request: " + _rType);
        }
        yield return(null);

        StartCoroutine("ProcessRequestLoop");
    }
        public Request TakeRequest()
        {
            lock (this)
            {
                while (!HasRequest())
                {
                    try
                    {
                        Monitor.Wait(this);
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }
                var req = RequestQueue.Dequeue();
                Monitor.PulseAll(this);

                return(req);
            }
        }
Exemple #12
0
 protected virtual void TriggerRequest(Event @event = null)
 {
     while (RequestQueue.Count > 0)
     {
         var request = RequestQueue.First;
         DoRequest(request);
         if (request.IsTriggered)
         {
             RequestQueue.Dequeue();
             TriggerWhenEmpty();
             TriggerWhenChange();
         }
         else
         {
             break;
         }
     }
     Utilization?.UpdateTo(InUse / (double)Capacity);
     WIP?.UpdateTo(InUse + RequestQueue.Count);
     QueueLength?.UpdateTo(RequestQueue.Count);
 }
Exemple #13
0
        /// <summary>
        /// 出队
        /// </summary>
        /// <returns></returns>
        public ServiceData Dequeue()
        {
            ServiceData command = null;

            try
            {
                if (!IsEmpty())
                {
                    for (int i = 1; i <= 5; i++)
                    {
                        RequestQueue <ServiceData> queue = null;
                        if (m_PriorityQueue.TryGetValue(i, out queue))
                        {
                            if (!queue.IsEmpty())
                            {
                                command = queue.Dequeue();
                            }

                            if (command != null)
                            {
                                m_Logger.Debug(string.Format("服务出队。\r\nGuid:{0}。\r\n服务名称:{1}"
                                                             , command.Command.Guid, command.Command.ServiceFullName));
                                return(command);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                m_Logger.Debug("服务出队异常。");
                m_Logger.Fatal(string.Concat("服务出队: ", ex.Message));
                m_Logger.Fatal(string.Concat("服务出队: ", ex.StackTrace));
            }

            return(command);
        }