Esempio n. 1
0
        private void SendResponsesProc()
        {
            const float timeToCall = 1000 / WORLD_DELAY_BETWEEN_FRAMES_MS - 1;

            HRTimer  timer  = new HRTimer();
            DateTime opTime = DateTime.UtcNow;

            while (!_terminating)
            {
                if (_unsentResponsesAvailable)
                {
                    timer.StartWatch();
                    Pair <int, BaseResponse>[] responses;
                    lock (_responsesPool)
                    {
                        responses = _responses.ToArray();
                        _responses.Clear();
                        _unsentResponsesAvailable = false;
                    }
                    int cnt = responses.Length;
                    for (int i = 0; i < cnt; i++)
//                    Parallel.For(0, cnt, i => //!!!
                    {
                        int clientId;
                        Pair <int, BaseResponse> response = responses[i];
                        if (_playerClients.TryGetValue(response.Key, out clientId))
                        {
                            _netServer.Send(clientId, response.Value);
                        }
                        _responsesPool.Push(response);
//                    });
                    }
                    WriteLog(LogEventType.TCPResponsesSend, timer.StopWatch().ToString("F"));
                }

                DateTime curTime    = DateTime.UtcNow;
                TimeSpan elapsed    = curTime - opTime;
                int      timeToIdle = (int)(timeToCall - elapsed.TotalMilliseconds);
                while (timeToIdle > 0)
                {
                    Thread.Sleep(timeToIdle / 2);
                    elapsed    = (curTime = DateTime.UtcNow) - opTime;
                    timeToIdle = (int)(timeToCall - elapsed.TotalMilliseconds);
                }

                elapsed = curTime - opTime;
                opTime  = curTime;

                WriteLog(LogEventType.TCPResponsesProc, elapsed.TotalMilliseconds.ToString("F"));
            }
        }