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; } } }
//public Vector2 getLiveObjectAxisButton(string name, int index) { // LiveObjectStorage storage; // lock (lockObject) { // if (!liveObjects.TryGetValue(name, out storage)) { // //print ("Body not found: " + name); // return Vector2.zero; // } // } // return storage.axisButtons[index]; //} /////////////////////////////////////////////////////////////////////////// // // 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.bits = 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; } } }