protected void Page_Load(object sender, EventArgs e)
        {
            IGameServerContainerDiagnostics gameContainerDiag = GameServerContainer.Instance;
            ContainerDiagInfo  gameContainerDiagInfo          = gameContainerDiag.GetContainerDiagInfo();
            GameServerDiagInfo gameServerDiagInfo             = gameContainerDiag.GetDiagInfo();

            BindGameServerDiagInfo(gameContainerDiagInfo, gameServerDiagInfo);

            DDLMatchNumber.DataSource = MatchServer.Containers.Select(kvp => kvp.Key).ToArray();
            DDLMatchNumber.DataBind();

            if (!string.IsNullOrEmpty(DDLMatchNumber.SelectedValue))
            {
                MatchServerPanel.Visible = true;

                IMatchServerContainerDiagnostics matchContainerDiag = MatchServer.Containers.Where(kvp => kvp.Key == new Guid(DDLMatchNumber.SelectedValue)).Select(kvp => kvp.Value).FirstOrDefault();
                ContainerDiagInfo   matchContainerDiagInfo          = matchContainerDiag.GetContainerDiagInfo();
                MatchServerDiagInfo matchServerDiagInfo             = matchContainerDiag.GetDiagInfo();

                BindMatchServerDiagInfo(matchContainerDiagInfo, matchServerDiagInfo);
            }
            else
            {
                MatchServerPanel.Visible = false;
            }
        }
 private void BindGameServerDiagInfo(ContainerDiagInfo diagInfo, GameServerDiagInfo gsDiagInfo)
 {
     GSConnections.Text             = diagInfo.ConnectionsCount.ToString();
     GSRegisteredClients.Text       = diagInfo.RegisteredClientsCount.ToString();
     GSIsMainThreadRunning.Text     = diagInfo.IsMainThreadRunning.ToString();
     GSISecondaryThreadRunning.Text = diagInfo.IsSecondaryThreadRunning.ToString();
     GSActiveReplays.Text           = gsDiagInfo.ActiveReplaysCount.ToString();
     GSClientsJoinedToRooms.Text    = gsDiagInfo.ClientsJoinedToRoomsCount.ToString();
     GSCreatedRooms.Text            = gsDiagInfo.CreatedRoomsCount.ToString();
     GSClientsWithPlayers.Text      = gsDiagInfo.ClinetsWithPlayersCount.ToString();
     GSLoggedInPlayers.Text         = gsDiagInfo.LoggedInPlayersCount.ToString();
     GSLoggedInBots.Text            = gsDiagInfo.LoggedInBotsCount.ToString();
     GSRunningMatches.Text          = gsDiagInfo.RunningMatchesCount.ToString();
 }
 private void BindMatchServerDiagInfo(ContainerDiagInfo diagInfo, MatchServerDiagInfo msDiagInfo)
 {
     MSConnections.Text = diagInfo.ConnectionsCount.ToString();
     MSContainerRegisteredClients.Text = diagInfo.RegisteredClientsCount.ToString();
     MSIsMainThreadRunning.Text        = diagInfo.IsMainThreadRunning.ToString();
     MSIsSecondaryThreadRunning.Text   = diagInfo.IsSecondaryThreadRunning.ToString();
     MSIsGCThreadRunning.Text          = MatchServer.IsGCThreadRunningDiag.ToString();
     MSIncomingMessagesFrequency.Text  = diagInfo.IncomingMessagesFrequency.ToString();
     MSOutgoingMessagesFrequency.Text  = diagInfo.OutgoingMessagesFrequency.ToString();
     MSIsInitializationStarted.Text    = msDiagInfo.IsInitializationStarted.ToString();
     MSIsInitialized.Text           = msDiagInfo.IsInitialized.ToString();
     MSIsEnabled.Text               = msDiagInfo.IsEnabled.ToString();
     MSIsMatchEngineCreated.Text    = msDiagInfo.IsMatchEngineCreated.ToString();
     MSIsReplay.Text                = msDiagInfo.IsReplay.ToString();
     MSServerRegisteredClients.Text = msDiagInfo.ServerRegisteredClientsCount.ToString();
     MSReadyToPlayCLients.Text      = msDiagInfo.ReadyToPlayClientsCount.ToString();
     MSClientsWithPlayers.Text      = msDiagInfo.ClientsWithPlayersCount.ToString();
     MSPlayers.Text = msDiagInfo.PlayersCount.ToString();
     MSBots.Text    = msDiagInfo.BotsCount.ToString();
 }
        private ContainerDiagInfo GetContainerDiagInfo()
        {
            ContainerDiagInfo diagInfo = new ContainerDiagInfo();

            lock (m_protocolToClientId)
            {
                diagInfo.ConnectionsCount       = m_protocolToClientId.Count;
                diagInfo.RegisteredClientsCount = m_clientIdToProtocol.Count;
            }

            lock (m_incomingMessages)
            {
                diagInfo.IsMainThreadRunning       = m_isMainThreadRunning;
                diagInfo.IncomingMessagesFrequency = m_incomingMessagesFrequency.Value;
            }

            lock (m_outgoingMessages)
            {
                diagInfo.IsSecondaryThreadRunning  = m_isSecondaryThreadRunning;
                diagInfo.OutgoingMessagesFrequency = m_outgoingMessagesFrequency.Value;
            }

            return(diagInfo);
        }
        private void MainThread()
        {
            m_stopwatch = Stopwatch.StartNew();
            OnBeforeRun();

            while (true)
            {
                QueuedMessage message = null;

                float iterationStart = Time;

                lock (m_incomingMessages)
                {
                    if (!m_isMainThreadRunning)
                    {
                        m_incomingMessages.Clear();
                        m_stopwatch.Stop();
                        OnAfterStop();
                        break;
                    }

                    //if (m_incomingMessages.Count == 0)
                    //{
                    //    /*The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. The caller is blocked while waiting to reacquire the lock. This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.*/
                    //    Monitor.Wait(m_incomingMessages);
                    //    continue;
                    //}
                    if (m_incomingMessages.Count > 0)
                    {
                        message = m_incomingMessages.Dequeue();
                    }
                }

                try
                {
                    if (message != null)
                    {
                        if (message.IsRequest)
                        {
                            OnRequest(message.Sender, message.Request);
                        }
                        else
                        {
                            OnMessage(message.Sender, message.Message);
                        }
                    }

                    OnTick();

                    m_incomingMessagesFrequency.Tick();
                    m_outgoingMessagesFrequency.Tick();

                    if (m_nextDiagInfoUpdateTime < Time)
                    {
                        m_nextDiagInfoUpdateTime = Time + 10;
                        m_diagInfo = GetContainerDiagInfo();
                        UpdateDiagInfo();
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message, e);
#if DEBUG
                    m_diagInfo = GetContainerDiagInfo();
                    throw;
#endif
                }

                int iterationDuration = (int)((Time - iterationStart) * 1000);
                Thread.Sleep(Math.Max(1, 35 - iterationDuration));
            }
        }