/// <summary>
        /// Create a new NatNet client, which manages all communication with the NatNet server (e.g. Motive)
        /// </summary>
        /// <param name="iConnectionType">0 = Multicast, 1 = Unicast</param>
        /// <returns></returns>
        private int CreateClient(int iConnectionType)
        {
            // release any previous instance
            if (m_NatNet != null)
            {
                m_NatNet.Uninitialize();
            }

            // [NatNet] create a new NatNet instance
            m_NatNet = new NatNetML.NatNetClientML(iConnectionType);

            // [NatNet] set a "Frame Ready" callback function (event handler) handler that will be
            // called by NatNet when NatNet receives a frame of data from the server application
            m_NatNet.OnFrameReady += new NatNetML.FrameReadyEventHandler(m_NatNet_OnFrameReady);

            /*
             * // [NatNet] for testing only - event signature format required by some types of .NET applications (e.g. MatLab)
             * m_NatNet.OnFrameReady2 += new FrameReadyEventHandler2(m_NatNet_OnFrameReady2);
             */

            // [NatNet] print version info
            int[] ver = new int[4];
            ver = m_NatNet.NatNetVersion();
            String strVersion = String.Format("NatNet Version : {0}.{1}.{2}.{3}", ver[0], ver[1], ver[2], ver[3]);

            OutputMessage(strVersion);

            return(0);
        }
Пример #2
0
        private int CreateClient(int iConnectionType)
        {
            if (m_NatNet != null)
            {
                m_NatNet.Uninitialize();
            }
            m_NatNet = new NatNetML.NatNetClientML(iConnectionType);

            // [NatNet] set a "Frame Ready" callback function (event handler) handler that will be
            // called by NatNet when NatNet receives a frame of data from the server application
            m_NatNet.OnFrameReady += new NatNetML.FrameReadyEventHandler(m_NatNet_OnFrameReady);

            // [NatNet] print version info
            int[] ver = new int[4];
            ver = m_NatNet.NatNetVersion();
            String strVersion = String.Format("NatNet Version : {0}.{1}.{2}.{3}", ver[0], ver[1], ver[2], ver[3]);

            OutputMessage(strVersion);

            return(0);
        }
Пример #3
0
        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            int returnCode = 0;

            if (FEnabled[0])
            {
                //If not connected already, try to connect
                if (!FConnected[0])
                {
                    // [NatNet] connect to a NatNet server
                    returnCode = mNatNet.Initialize(FLocal[0], FServer[0], FPort[0]);
                    FLogger.Log(LogType.Debug, "Trying to connect...");

                    if (returnCode == 0)
                    {
                        FLogger.Log(LogType.Debug, "Connected!");

                        // [NatNet] validate the connection
                        returnCode = mNatNet.GetServerDescription(mDesc);

                        if (returnCode == 0)
                        {
                            FLogger.Log(LogType.Debug, "Server App Name: " + mDesc.HostApp);


                            // [NatNet] [optional] send a test/response message
                            FLogger.Log(LogType.Debug, "Sending TestRequest");
                            int    nBytes   = 0;
                            byte[] response = new byte[10000];
                            int    rc       = mNatNet.SendMessageAndWait("TestRequest", out response, out nBytes);
                            if (rc == 0)
                            {
                                string str = "   Server: " + System.Text.Encoding.ASCII.GetString(response, 0, nBytes);
                                FLogger.Log(LogType.Debug, str);
                            }
                            else
                            {
                                FLogger.Log(LogType.Debug, "   Server: No Response.");
                            }

                            rc = mNatNet.SendMessageAndWait("FrameRate", out response, out nBytes);
                            if (rc == 0)
                            {
                                try
                                {
                                    double serverFramerate = BitConverter.ToSingle(response, 0);
                                    FLogger.Log(LogType.Debug, String.Format("   Server Framerate: {0}", serverFramerate));
                                }
                                catch (System.Exception ex)
                                {
                                    FLogger.Log(LogType.Debug, ex.Message);
                                }
                            }


                            //Connected
                            FConnected[0] = true;
                            FEnabled[0]   = FConnected[0];
                        }
                    }

                    //Get the rigid body names
                    this.RequestDataDescription();
                }
                else
                {
                    // Already connected, update output

                    // Upon update, query server for data description
                    if (FUpdate.IsChanged)
                    {
                        this.RequestDataDescription();
                    }
                    lock (syncLock) {
                        //FLogger.Log(LogType.Debug, "" + m_FrameQueue.Count);
                        while (m_FrameQueue.Count > 0)
                        {
                            mFrameOfData = m_FrameQueue.Dequeue();

                            if (m_FrameQueue.Count > 0)
                            {
                                continue;
                            }
                            FFrame.SliceCount = 0;
                            FFrame.Add(mFrameOfData);
                        }
                    }


                    // Update the rigid bodies names
                    FRigidBody.SliceCount = mRBNames.Count;
                    for (int i = 0; i < mRBNames.Count; ++i)
                    {
                        FRigidBody[i] = mRBNames[i];
                    }
                }
            }
            else
            {
                //If connected already, disconnect, else, do nothing
                if (FConnected[0])
                {
                    mRBNames.Clear();
                    returnCode    = mNatNet.Uninitialize();
                    FConnected[0] = (0 == returnCode);
                }
            }
        }