Пример #1
0
        private void SendPacketTo(NetworkStream clientStream)
        {
            if (IsShuttingDown)
            {
                return;
            }

            KinectPacket packet = new KinectPacket();

            lock (_kinect.Joints)   ///Kinect code can drop joints from array so make sure to lock
            {
                foreach (KeyValuePair <string, KinectPoint> kvp in _kinect.Joints)
                {
                    //Console.WriteLine(kvp.Key + " " + kvp.Value.ToString());
                    packet.Messages.Add(kvp.Key, kvp.Value);
                }
            }
            byte[] data = SerializationUtils.SerializeToByteArray(packet);

            try
            {
                clientStream.Write(data, 0, data.Length);
                clientStream.Flush();
            }
            catch
            {
                //Do nothing
            }
        }
Пример #2
0
 public void ListenForServer()
 {
     while (!IsShuttingDown)
     {
         while (_clientStream.DataAvailable)
         {
             try
             {
                 byte[]       message   = new byte[4096];
                 int          bytesRead = _clientStream.Read(message, 0, 4096);
                 KinectPacket packet    = SerializationUtils.DeserializeFromByteArray <KinectPacket>(message);
                 foreach (KeyValuePair <string, KinectPoint> kvp in packet.Messages)
                 {
                     Console.WriteLine(kvp.Key + " " + kvp.Value.ToString());
                 }
             }
             catch
             {
             }
         }
     }
 }
Пример #3
0
    // Update is called once per frame
    void Update()
    {
        TestArm();
        if (_clientStream == null)
        {
            return;
        }
        {               //Handle sampling threshold
            _elapsedTime += Time.deltaTime;
            if (_elapsedTime < SampleRate)
            {
                return;
            }
            _elapsedTime = 0.0f;
        }

        //Out simple system has zero back end protection to packets are only sent by request.  W/O this request the TCP stack fills and the problem lags, badly.
        RequestJointUpdate();

        //Make sure to parse all packets in client stream or they will build up.
        while (_clientStream.DataAvailable)
        {
            try
            {
                byte[]       message   = new byte[4096];
                int          bytesRead = _clientStream.Read(message, 0, 4096);
                KinectPacket packet    = DeserializeFromByteArray <KinectPacket>(message);

                foreach (KeyValuePair <string, KinectPoint> kvp in packet.Messages)
                {
                    //Debug.Log(kvp.Key + "found");
                    string [] tokens = kvp.Value.ToString().Split(' ');
                    string    key    = kvp.Key;
                    if (tokens.Length == 3)
                    {
                        //Debug.Log(kvp.Key + " " + kvp.Value.ToString());
                        GameObject go = null;
                        if (Points.ContainsKey(key))
                        {
                            go = Points[key];
                        }
                        else
                        {
                            go          = (GameObject)Instantiate(JointPrefab, Vector3.zero, Quaternion.identity);
                            go.name     = key;
                            Points[key] = go;
                        }

                        //Normalize data to [0...1] on all axes.  Ranges are specified manually but they should be taken from the Kinect specs which I didn't have handy.
                        Vector3 pos = new Vector3(float.Parse(tokens[0]), float.Parse(tokens[1]), float.Parse(tokens[2]));
                        pos.x = (pos.x - Min.x) / (Max.x - Min.x);
                        pos.y = (pos.y - Min.y) / (Max.y - Min.y);
                        pos.z = (pos.z - Min.z) / (Max.z - Min.z);


                        if (go)
                        {
                            go.transform.position = pos;
                        }
                    }
                }
                CenterModel();
                //TestArm();
            }
            catch (SystemException ex)
            {
                Debug.Log("Problem w/ client: " + ex.Message + "," + ex.StackTrace);
            }
        }
    }
Пример #4
0
        private void SendPacketTo(NetworkStream clientStream)
        {
            if (IsShuttingDown) return;

            KinectPacket packet = new KinectPacket();
            lock (_kinect.Joints)   ///Kinect code can drop joints from array so make sure to lock
            {
                foreach (KeyValuePair<string, KinectPoint> kvp in _kinect.Joints)
                {
                    //Console.WriteLine(kvp.Key + " " + kvp.Value.ToString());
                    packet.Messages.Add(kvp.Key, kvp.Value);
                }
            }
            byte[] data = SerializationUtils.SerializeToByteArray(packet);

            try
            {
                clientStream.Write(data, 0, data.Length);
                clientStream.Flush();
            }
            catch
            {
                //Do nothing
            }
        }