Beispiel #1
0
        void ServerThread()
        {
            Debug.Log("Starting server thread");
            Socket     socket          = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress  ip              = IPAddress.Parse("192.168.1.44");
            IPEndPoint ipEndPoint      = new IPEndPoint(IPAddress.Parse("192.168.1.44"), 0);
            IPEndPoint send_ipEndPoint = new IPEndPoint(ip, BLACK_BOX_SERVER_PORT);

            try {
                socket.Bind(ipEndPoint);
            } catch (SocketException e) {
                Debug.Log("Error binding socket: " + e.ToString());
                sendingPackets = false;
            }

            while (sendingPackets)
            {
                update             = new update_protocol_v3.Update();
                update.label       = "neuron";
                update.mod_version = lastLoadedFrame;
                update.lhs_frame   = false;
                lastLoadedFrame++;

                foreach (KeyValuePair <string, LiveObjectStorage> entry in liveObjects)
                {
                    LiveObject o = entry.Value.ToLiveObject();
                    update.live_objects.Add(o);
                }

                using (MemoryStream stream = new MemoryStream()) {
                    Serializer.Serialize <Update>(stream, update);
                    packetBytes = stream.GetBuffer();
                    socket.SendTo(packetBytes, send_ipEndPoint);
                }

                if (!sendingPackets)
                {
                    socket.Close();
                    break;
                }
            }
        }
        public void Send()
        {
            //Debug.Log("Attempting to open send thread with ip/port: " + ip.ToString() + " " + port);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            socket.SendTimeout = 1000;

            IPEndPoint ipEndPoint = new IPEndPoint(ip, 0);
            //string sendingIP = "192.168.1.44";
            string     sendingIP       = address;
            IPEndPoint send_ipEndPoint = new IPEndPoint(IPAddress.Parse(sendingIP), port);

            try {
                socket.Bind(ipEndPoint);
            } catch (SocketException e) {
                Debug.LogWarning("Error binding socket: " + ip.ToString() + " " + port + " " + e.ToString());
                isRunning = false;
            }

            //Checks if sending ip is local. If not, then we'll ping
            //the server for a unicast stream
            if (!sendingIP.StartsWith("192."))
            {
                update             = new update_protocol_v3.Update();
                update.label       = "ping";
                update.mod_version = 0;
                update.lhs_frame   = false;

                using (MemoryStream stream = new MemoryStream()) {
                    Serializer.Serialize <Update> (stream, update);
                    packetBytes = stream.ToArray();
                    socket.SendTo(packetBytes, send_ipEndPoint);
                }
            }

            while (isRunning)
            {
                System.Threading.Thread.Sleep(10);
                if (managedObjects.Values.Count == 0)
                {
                    continue;
                }
                lock (lockObject) {
                    update             = new update_protocol_v3.Update();
                    update.label       = "SendData";
                    update.mod_version = lastLoadedFrame;
                    update.lhs_frame   = false;
                    lastLoadedFrame++;

                    foreach (KeyValuePair <string, HolojamObject> entry in managedObjects)
                    {
                        LiveObject o = entry.Value.ToLiveObject();
                        update.live_objects.Add(o);
                    }
                    using (MemoryStream stream = new MemoryStream()) {
                        packetCount++;
                        Serializer.Serialize <Update>(stream, update);
                        packetBytes = stream.ToArray();
                        socket.SendTo(packetBytes, send_ipEndPoint);
                    }
                }

                if (!isRunning)
                {
                    socket.Close();
                    break;
                }
            }
        }
        public void Receive()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            socket.Bind(new IPEndPoint(IPAddress.Any, port));
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                   new MulticastOption(IPAddress.Parse(address)));
            socket.ReceiveTimeout = 1000;

            int nBytesReceived = 0;

            while (isRunning)
            {
                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);
                lock (lockObject) {
                    //managedObjects.Clear();

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

                        objectTimers[label] = 0;                       //Reset timer--we received data for this object

                        HolojamObject ho;

                        //Reform managedObjects every frame.
                        //Inefficient for now, but will allow us to determine
                        //if an object is registered.

                        ho = new HolojamObject(label);
                        managedObjects[label] = ho;

                        if (update.lhs_frame)
                        {
                            ho.position = new Vector3(-(float)or.x, (float)or.y, (float)or.z);
                            ho.rotation = new Quaternion(-(float)or.qx,
                                                         (float)or.qy,
                                                         (float)or.qz,
                                                         -(float)or.qw);
                        }
                        else
                        {
                            ho.position = new Vector3((float)or.x, (float)or.y, (float)or.z);
                            ho.rotation = new Quaternion((float)or.qx,
                                                         (float)or.qy,
                                                         (float)or.qz,
                                                         (float)or.qw);
                        }
                        ho.bits = or.button_bits;

                        //Get blob if it's there. Inefficient
                        ho.blob = or.extra_data;

                        ho.isTracked = or.is_tracked;
                    }
                }

                if (!isRunning)
                {
                    socket.Close();
                    break;
                }
            }
        }
Beispiel #4
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;
                }
            }
        }
        ///////////////////////////////////////////////////////////////////////////
        //
        // 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;
                }
            }
        }