/*
     * Update is called once per frame
     * Take the most recent ZMQ message and use it to position the cameras.
     * If there has not been a recent message, the renderer should probably pause rendering until a new request is received.
     */

    void Update()
    {
        if (pull_socket.HasIn || socket_initialized)
        {
            // Receive most recent message
            var msg     = new NetMQMessage();
            var new_msg = new NetMQMessage();
            // Blocking receive for a message
            msg = pull_socket.ReceiveMultipartMessage();
            // Make sure that splashscreen is disabled
            splashScreen.SetActive(false);

            // Check if this is the latest message
            while (pull_socket.TryReceiveMultipartMessage(ref new_msg))
            {
                ;
            }

            // Check that we got the whole message
            if (new_msg.FrameCount >= msg.FrameCount)
            {
                msg = new_msg;
            }

            if (msg.FrameCount == 0)
            {
                return;
            }

            // Get scene state from LCM
            state = JsonConvert.DeserializeObject <StateMessage_t>(msg[1].ConvertToString());

            // Make sure that all objects are initialized properly
            initializeObjects();
            // Ensure that dynamic object settings such as depth-scaling and color are set correctly.
            updateDynamicObjectSettings();
            // Update position of game objects.
            updateObjectPositions();

            // Mark socket as initialized
            socket_initialized = true;
        }
        else
        {
            // Throttle to 10hz when idle
            Thread.Sleep(100); // [ms]
        }
    }
    /*
     * Update is called once per frame
     * Take the most recent ZMQ message and use it to position the cameras.
     * If there has not been a recent message, the renderer should probably pause rendering until a new request is received.
     */

    void Update()
    {
        if (pull_socket.HasIn || socket_initialized)
        {
            // Receive most recent message
            var msg     = new NetMQMessage();
            var new_msg = new NetMQMessage();

            // Wait for a message from the client.
            bool received_new_packet = pull_socket.TryReceiveMultipartMessage(new TimeSpan(0, 0, connection_timeout_seconds), ref new_msg);

            if (!received_new_packet && socket_initialized)
            {
                // Close ZMQ sockets
                pull_socket.Close();
                push_socket.Close();
                Debug.Log("Terminated ZMQ sockets.");
                NetMQConfig.Cleanup();
                Thread.Sleep(100); // [ms]
                // Restart FlightGoggles and wait for a new connection.
                SceneManager.LoadScene(topLevelSceneName);
                // Kill this gameobject/controller script.
                Destroy(this.gameObject);
                // Don't bother with the rest of the script.
                return;
            }

            // Check if this is the latest message
            //while (pull_socket.TryReceiveMultipartMessage(ref new_msg)) ;

            // Check that we got the whole message
            if (new_msg.FrameCount >= msg.FrameCount)
            {
                msg = new_msg;
            }

            if (msg.FrameCount != 2)
            {
                return;
            }

            if (msg[1].MessageSize < 10)
            {
                return;
            }

            // Get scene state from LCM
            state = JsonConvert.DeserializeObject <StateMessage_t>(msg[1].ConvertToString());

            // Make sure that all objects are initialized properly
            initializeObjects();
            // Ensure that dynamic object settings such as depth-scaling and color are set correctly.
            updateDynamicObjectSettings();
            // Update position of game objects.
            updateObjectPositions();

            // Do collision detection
            updateCameraCollisions();
            // Run landmark visibility checks
            updateLandmarkVisibility();
            // Compute sensor data
            updateLidarData();

            // Mark socket as initialized
            socket_initialized = true;
        }
        else
        {
            // Throttle to 10hz when idle
            Thread.Sleep(3); // [ms]
        }
    }