Ejemplo n.º 1
0
        private void UDPMessageReceivedHandler(NetInMessage message)
        {
            MessageType.Command command = (MessageType.Command)message.ReadInt32();
            if (command != MessageType.Command.FingerData)
            {
                return;
            }

            Dictionary <Finger, double> data = new Dictionary <Finger, double>();

            for (int i = 0; i < 5; i++)
            {
                int    fingerIndex = message.ReadInt32();
                Finger finger      = (Finger)fingerIndex;

                double force = message.ReadDouble();

                data.Add(finger, force);
            }

            if (OnFingerDataReceived != null)   //処理が入れられていたらdataをinputとしてその処理を実行する
            {
                OnFingerDataReceived(data);
            }
        }
Ejemplo n.º 2
0
        private void HandleUDPMessageReceived(NetInMessage message)
        {
            message.ResetIndex();

            MessageType.Command command = (MessageType.Command)message.ReadInt32();
            if (command == MessageType.Command.ModeSet)
            {
                MessageType.Mode mode = (MessageType.Mode)message.ReadInt32();

                dynamicForceUI.SetActive(mode == MessageType.Mode.Dynamic);
                staticForceUI.SetActive(mode == MessageType.Mode.Static);
                guitarHeroObj.SetActive(mode == MessageType.Mode.GuitarHero);
                flappyBirdObj.SetActive(mode == MessageType.Mode.FlappyBird);
                musicplayObj.SetActive(mode == MessageType.Mode.MusicPlay);
            }
        }
Ejemplo n.º 3
0
        private void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            DataReader networkDataReader = args.GetDataReader();

            int len = networkDataReader.ReadInt32();

            byte[] data = new byte[len];
            networkDataReader.ReadBytes(data);

            NetInMessage message = new NetInMessage(data);

            lock (lockObj)
            {
                m_incomingMessageQueue.Enqueue(message);
            }
        }
Ejemplo n.º 4
0
        private void ReceiveData()
        {
            while (true)
            {
                try
                {
                    IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, int.Parse(listenPort));
                    if (m_udpClient.Available > 0)
                    {
                        byte[] data = m_udpClient.Receive(ref anyIP);

                        lock (lockObj)
                        {
                            // Our convention for each UDP packet is to first write the
                            // size of the data in bytes. In the receiving end, we then use this
                            // size value to initialize the byte array. However, when reading via System.Net,
                            // it automatically detects the size of the packet, so we don't use the size value.
                            // In this case, we just skip it.
                            byte[] modifiedData = new byte[data.Length - 4];
                            Array.Copy(data, 4, modifiedData, 0, data.Length - 4);

                            NetInMessage receivedMessage = new NetInMessage(modifiedData);

                            /**
                             * Because polling for data from the UDP connection is done by
                             * a different thread (and not the Unity thread), we cannot simply
                             * call the registered delegate functions that will handle how the data
                             * will be processed. To overcome this, we put them in a queue, and then
                             * in the Update() function of Unity, we go over the queue, and see if there
                             * are UDP data that needs to be processed.
                             */
                            m_incomingMessageQueue.Enqueue(receivedMessage);
                        }
                    }

                    Thread.Sleep(1);
                }
                catch (Exception e)
                {
                    Debug.LogError(e.ToString());
                }
            }
        }
Ejemplo n.º 5
0
        private void UDPMessageHandler(NetInMessage message)
        {
            int commandInt = message.ReadInt32();

            MessageType.Command command = (MessageType.Command)commandInt;
            if (command == MessageType.Command.Control)
            {
                MessageType.ControlType controlType = (MessageType.ControlType)message.ReadInt32();
                if ((controlType == MessageType.ControlType.Start) || (controlType == MessageType.ControlType.Next))
                {
                    int activeFingerIndex = message.ReadInt32();
                    for (int i = 0; i < fingerIndicators.Count; i++)
                    {
                        fingerIndicators[i].SetIsOn(i == activeFingerIndex);
                        fingerIndicators[i].IsVisible = (i == activeFingerIndex);
                    }

                    for (int i = 0; i < fingerBarGraphs.Count; i++)
                    {
                        int    fingerIndex = message.ReadInt32();
                        double targetForce = message.ReadDouble();

                        fingerBarGraphs[fingerIndex].targetValue = targetForce;
                    }
                }
                else if (controlType == MessageType.ControlType.Stop)
                {
                    ResetIndicators();
                    ResetGraphValues();
                }
            }
            else if (command == MessageType.Command.Data)
            {
                for (int i = 0; i < fingerBarGraphs.Count; i++)
                {
                    int   fingerIndex = message.ReadInt32();
                    float plotValue   = message.ReadFloat();

                    fingerBarGraphs[fingerIndex].value = plotValue;
                }
            }
        }
Ejemplo n.º 6
0
        private void Update()
        {
            lock (lockObj)
            {
                while (m_incomingMessageQueue.Count > 0)
                {
                    NetInMessage receivedMessage = m_incomingMessageQueue.Dequeue();
                    if (MessageReceived != null)
                    {
                        Delegate[] invocationList = MessageReceived.GetInvocationList();
                        if (invocationList != null)
                        {
                            for (int i = 0; i < invocationList.Length; i++)
                            {
                                receivedMessage.ResetIndex();

                                invocationList[i].DynamicInvoke(receivedMessage);
                            }
                        }
                    }
                }
            }
        }
        private void UDPMessageReceived(NetInMessage message)
        {
            MessageType.Command commandType = (MessageType.Command)message.ReadInt32();
            if (commandType == MessageType.Command.Data)
            {
                if (m_isRunning)
                {
                    float timeX       = message.ReadFloat();
                    float valueToPlot = message.ReadFloat();
                    lineGraph.Plot(timeX, valueToPlot, Constants.FORCE_PLOT_SERIES_NAME);
                }
            }
            else if (commandType == MessageType.Command.Control)
            {
                MessageType.ControlType controlType = (MessageType.ControlType)message.ReadInt32();
                if (controlType == MessageType.ControlType.Start)
                {
                    m_isRunning = true;
                    m_time      = 0.0f;

                    ClearIndicators();
                    HideIndicators();

                    int numFingersToPress = message.ReadInt32();
                    for (int i = 0; i < numFingersToPress; i++)
                    {
                        int       fingerIndex = message.ReadInt32();
                        Indicator indicator   = GetFingerIndicator((Finger)fingerIndex);
                        if (indicator != null)
                        {
                            indicator.IsVisible = true;
                            indicator.SetIsOn(true);
                        }
                    }

                    double maximumPlotValue        = message.ReadDouble();
                    float  staticPhaseDuration     = message.ReadFloat();
                    float  increasingPhaseDuration = message.ReadFloat();
                    float  plateauPhaseDuration    = message.ReadFloat();
                    float  decreasingPhaseDuration = message.ReadFloat();
                    float  staticEndPhaseDuration  = message.ReadFloat();

                    double plateauPhaseValue = message.ReadDouble();

                    float measurementDuration = staticPhaseDuration +
                                                increasingPhaseDuration +
                                                plateauPhaseDuration +
                                                decreasingPhaseDuration +
                                                staticEndPhaseDuration;

                    lineGraph.maxX = measurementDuration;
                    lineGraph.maxY = (float)maximumPlotValue;
                    lineGraph.Refresh();

                    lineGraph.ClearPlot(Constants.TARGET_FORCE_PLOT_SERIES_NAME);
                    lineGraph.ClearPlot(Constants.FORCE_PLOT_SERIES_NAME);

                    float timeStamp = 0.0f;
                    lineGraph.Plot(0.0f, 0.0f, Constants.TARGET_FORCE_PLOT_SERIES_NAME);

                    timeStamp += staticPhaseDuration;
                    lineGraph.Plot(timeStamp, 0.0f, Constants.TARGET_FORCE_PLOT_SERIES_NAME);

                    timeStamp += increasingPhaseDuration;
                    lineGraph.Plot(timeStamp, plateauPhaseValue, Constants.TARGET_FORCE_PLOT_SERIES_NAME);

                    timeStamp += plateauPhaseDuration;
                    lineGraph.Plot(timeStamp, plateauPhaseValue, Constants.TARGET_FORCE_PLOT_SERIES_NAME);

                    timeStamp += decreasingPhaseDuration;
                    lineGraph.Plot(timeStamp, 0.0f, Constants.TARGET_FORCE_PLOT_SERIES_NAME);

                    timeStamp += staticEndPhaseDuration;
                    lineGraph.Plot(timeStamp, 0.0f, Constants.TARGET_FORCE_PLOT_SERIES_NAME);

                    m_time = 0.0f;
                }
                else if (controlType == MessageType.ControlType.Stop)
                {
                    m_isRunning = false;

                    ClearIndicators();
                    HideIndicators();
                }
            }
        }