Example #1
0
        // This thread handles incoming NatNet packets.
        private void ThreadRun()
        {
            stopReceive = false;
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            ipEndPoint = new IPEndPoint(IPAddress.Any, BLACK_BOX_CLIENT_PORT);
            //Debug.Log("prebind");
            socket.Bind(ipEndPoint);
            //Debug.Log("bind");
            MulticastOption mo = new MulticastOption(IPAddress.Parse("224.1.1.1"));

            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mo);

            nBytesReceived     = 0;
            lastLoadedBuffer   = b1;
            lastLoadedBufferMS = b1ms;
            lastLoadedFrame    = 0;

            byte[]       newPacketBuffer   = b2;
            MemoryStream newPacketBufferMS = b2ms;
            long         newPacketFrame    = 0;

            byte[]       tempBuffer;
            MemoryStream tempBufferMS;

            while (true)
            {
                //Debug.Log("preRECV");
                nBytesReceived = socket.Receive(newPacketBuffer);
                //Debug.Log("RECV");
                nPackets++;
                newPacketBufferMS.Position = 0;
                //Debug.Log ("Deserializing data...");
                update_protocol_v3.Update update = Serializer.Deserialize <update_protocol_v3.Update>(new MemoryStream(newPacketBuffer, 0, nBytesReceived));
                //Debug.Log ("Data deserialized. Received update of type " + update.label);

                newPacketFrame = update.mod_version;
                if (newPacketFrame > lastLoadedFrame)
                {
                    // Swap the buffers and reset the positions.
                    lastLoadedBufferMS.Position = 0;
                    newPacketBufferMS.Position  = 0;
                    tempBuffer         = lastLoadedBuffer;
                    tempBufferMS       = lastLoadedBufferMS;
                    lastLoadedBuffer   = newPacketBuffer;
                    lastLoadedBufferMS = newPacketBufferMS;
                    newPacketBuffer    = tempBuffer;
                    newPacketBufferMS  = tempBufferMS;
                    lastLoadedFrame    = newPacketFrame;



                    for (int j = 0; j < update.live_objects.Count; j++)
                    {
                        LiveObject or    = update.live_objects[j];
                        string     label = or.label;
                        if (label == "marker")
                        {
                            Debug.Log("marker at " + or.x + ", " + or.y + ", " + or.z);
                        }
                        LiveObjectStorage ow;
                        lock (lock_object) {
                            if (!labelToLiveObject.TryGetValue(label, out ow))
                            {
                                ow = new LiveObjectStorage();
                                labelToLiveObject[label] = ow;
                            }
                            else
                            {
                                ow = labelToLiveObject[label];
                            }
                            if (update.lhs_frame)
                            {
                                ow.pos = new Vector3(-(float)or.x, (float)or.y, (float)or.z);
                                ow.rot = new Quaternion(-(float)or.qx, (float)or.qy, (float)or.qz, -(float)or.qw);
                            }
                            else
                            {
                                ow.pos = new Vector3((float)or.x, (float)or.y, (float)or.z);
                                ow.rot = new Quaternion((float)or.qx, (float)or.qy, (float)or.qz, (float)or.qw);
                            }
                            ow.button_bits = or.button_bits;
                        }
                    }
                }

                if (stopReceive)
                {
                    break;
                }
            }
        }
Example #2
0
        // This thread handles incoming NatNet packets.
        private void ThreadRun()
        {
            stopReceive = false;
            Socket socket =new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            ipEndPoint = new IPEndPoint (IPAddress.Any, BLACK_BOX_CLIENT_PORT);
            //Debug.Log("prebind");
            socket.Bind (ipEndPoint);
            //Debug.Log("bind");
            MulticastOption mo = new MulticastOption (IPAddress.Parse ("224.1.1.1"));
            socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, mo);

            nBytesReceived = 0;
            lastLoadedBuffer = b1;
            lastLoadedBufferMS = b1ms;
            lastLoadedFrame = 0;

            byte[] newPacketBuffer = b2;
            MemoryStream newPacketBufferMS = b2ms;
            long newPacketFrame = 0;

            byte[] tempBuffer;
            MemoryStream tempBufferMS;

            while (true) {
                //Debug.Log("preRECV");
                nBytesReceived = socket.Receive(newPacketBuffer);
                //Debug.Log("RECV");
                nPackets++;
                newPacketBufferMS.Position = 0;
                //Debug.Log ("Deserializing data...");
                update_protocol_v3.Update update = Serializer.Deserialize<update_protocol_v3.Update>(new MemoryStream(newPacketBuffer, 0, nBytesReceived));
                //Debug.Log ("Data deserialized. Received update of type " + update.label);

                newPacketFrame = update.mod_version;
                if(newPacketFrame > lastLoadedFrame) {
                    // Swap the buffers and reset the positions.
                    lastLoadedBufferMS.Position = 0;
                    newPacketBufferMS.Position = 0;
                    tempBuffer = lastLoadedBuffer;
                    tempBufferMS = lastLoadedBufferMS;
                    lastLoadedBuffer = newPacketBuffer;
                    lastLoadedBufferMS = newPacketBufferMS;
                    newPacketBuffer = tempBuffer;
                    newPacketBufferMS = tempBufferMS;
                    lastLoadedFrame = newPacketFrame;

                    for (int j = 0; j < update.live_objects.Count; j++) {
                        LiveObject or = update.live_objects[j];
                        string label = or.label;
                        LiveObjectStorage ow;
                        lock (lock_object) {
                            if (!labelToLiveObject.TryGetValue(label, out ow)) {
                                ow = new LiveObjectStorage();
                                labelToLiveObject[label] = ow;
                            } else {
                                ow = labelToLiveObject[label];
                            }
                            if (update.lhs_frame) {
                                ow.pos = new Vector3(-(float)or.x, (float)or.y, (float)or.z);
                                ow.rot = new Quaternion(-(float)or.qx, (float)or.qy, (float)or.qz, -(float)or.qw);
                            } else {
                                ow.pos = new Vector3((float)or.x, (float)or.y, (float)or.z);
                                ow.rot = new Quaternion((float)or.qx, (float)or.qy, (float)or.qz, (float)or.qw);
                            }
                            if (or.button_bits > 0) {
                                ow.button_bits = or.button_bits;
                            }
                        }
                    }
                }

                if (stopReceive) {
                    break;
                }
            }
        }
        ///////////////////////////////////////////////////////////////////////////
        //
        // Thread methods
        //

        // This thread handles incoming NatNet packets.
        private void run()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Bind(new IPEndPoint(IPAddress.Any, BLACK_BOX_CLIENT_PORT));
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.1.1.1")));

            int nBytesReceived = 0;

            while (receivingPackets)
            {
                nBytesReceived = socket.Receive(currentPacket.bytes);
                currentPacket.stream.Position = 0;

                update = Serializer.Deserialize <update_protocol_v3.Update>(new MemoryStream(currentPacket.bytes, 0, nBytesReceived));

                currentPacket.frame = update.mod_version;
                if (currentPacket.frame > previousPacket.frame)
                {
                    packetCount++;

                    previousPacket.stream.Position = 0;
                    currentPacket.stream.Position  = 0;
                    tempPacket.copyFrom(previousPacket);
                    previousPacket.copyFrom(currentPacket);
                    currentPacket.copyFrom(tempPacket);

                    for (int j = 0; j < update.live_objects.Count; j++)
                    {
                        LiveObject or    = update.live_objects[j];
                        string     label = or.label;

                        LiveObjectStorage ow;
                        lock (lockObject) {
                            //List<LiveObjectStorage> objectsToRemove = new List<LiveObjectStorage>();
                            //objectsToRemove.AddRange(liveObjects.Values);

                            //Add objects that exist
                            if (!liveObjects.TryGetValue(label, out ow))
                            {
                                ow = new LiveObjectStorage(label);
                                liveObjects[label] = ow;
                            }
                            else
                            {
                                ow = liveObjects[label];
                                //objectsToRemove.Remove(ow);
                            }
                            if (update.lhs_frame)
                            {
                                ow.position = new Vector3(-(float)or.x, (float)or.y, (float)or.z);
                                ow.rotation = new Quaternion(-(float)or.qx, (float)or.qy, (float)or.qz, -(float)or.qw);
                            }
                            else
                            {
                                ow.position = new Vector3((float)or.x, (float)or.y, (float)or.z);
                                ow.rotation = new Quaternion((float)or.qx, (float)or.qy, (float)or.qz, (float)or.qw);
                            }
                            ow.buttonBits = or.button_bits;

                            //Remove objects from pool that aren't there.
                            //foreach (LiveObjectStorage missingObject in objectsToRemove) {
                            //     liveObjects.Remove(missingObject.key);
                            //}
                        }
                    }
                }

                if (!receivingPackets)
                {
                    socket.Close();
                    break;
                }
            }
        }