Пример #1
0
    //Broadcast the game state to all players at fixed rate
    private void BroadcastGameStateLoop()
    {
        float  maxFrameRate = (1f / TickRate * 1000f);
        string state        = "";

        while (IsOpen)
        {
            int startTime = Environment.TickCount;

            lock (gameState)
            {
                if (gameState != "")
                {
                    state    += gameState;
                    gameState = "";
                }
            }

            //Get all the players last positions and angles
            foreach (Player player in players)
            {
                state += player.GetMovementStatus();
            }

            //Remove the last : and send the data
            if (state.Length > 0)
            {
                state = state.Remove(state.Length - 1);
                players.Broadcast(state);
                state = "";
            }

            //Sleep the thread if the frame rate is higher than the max frame rate
            int frameTime = Environment.TickCount - startTime;
            if (frameTime < maxFrameRate)
            {
                Thread.Sleep((int)(maxFrameRate - frameTime));
            }
        }
    }