/*
     * 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]
        }
    }
Esempio n. 2
0
        public void ReceiveMessageWithTimeout()
        {
            {
                var pubSync = new AutoResetEvent(false);
                var payload = new byte[300];
                const int waitTime = 500;

                var t1 = new Task(() =>
                {
                    using (var pubSocket = new PublisherSocket())
                    {
                        pubSocket.Bind("tcp://127.0.0.1:12345");
                        pubSync.WaitOne();
                        Thread.Sleep(waitTime);
                        pubSocket.SendFrame(payload);
                        pubSync.WaitOne();
                    }
                }, TaskCreationOptions.LongRunning);

                var t2 = new Task(() =>
                {
                    using (var subSocket = new SubscriberSocket())
                    {
                        subSocket.Connect("tcp://127.0.0.1:12345");
                        subSocket.Subscribe("");
                        Thread.Sleep(100);
                        pubSync.Set();

                        NetMQMessage msg = null;
                        Assert.IsFalse(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(100), ref msg));

                        Assert.IsTrue(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(waitTime), ref msg));
                        Assert.NotNull(msg);
                        Assert.AreEqual(1, msg.FrameCount);
                        Assert.AreEqual(300, msg.First.MessageSize);
                        pubSync.Set();
                    }
                }, TaskCreationOptions.LongRunning);

                t1.Start();
                t2.Start();

                Task.WaitAll(t1, t2);
            }
        }
    /*
     * 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]
        }
    }