Exemplo n.º 1
0
        private static void HandleDataFromServer(object client)
        {
            TcpClient serverConnection = (TcpClient)client;


            BinaryReader binaryReader = new BinaryReader(new BufferedStream(serverConnection.GetStream()));

            int bytesRead;

            while (true)
            {
                bytesRead = 0;
                bool readTimestamp = false;
                try
                {
                    //blocks until a client sends a message

                    char messageType = binaryReader.ReadChar();
                    bytesRead = 1;

                    switch (messageType)
                    {
                    case 'M':     // Marker

                        Marker receivedMarker = new Marker();
                        receivedMarker.MarkerId = binaryReader.ReadInt32();

                        //if have been sent End of Frame indication
                        if (receivedMarker.MarkerId == -2147483648)
                        {
                            receivedMarker.TimeStamp = binaryReader.ReadInt64();
                            readTimestamp            = true;
                            MarkerList.RemoveExcessMarkersFromList(receivedMarker.TimeStamp);

                            if (WholeFrameReceivedEvent != null)
                            {
                                WholeFrameReceivedEvent();
                            }
                        }

                        if (readTimestamp == false)
                        {
                            receivedMarker.TimeStamp = binaryReader.ReadInt64();
                        }

                        receivedMarker.xCoordinate = binaryReader.ReadDouble();
                        receivedMarker.yCoordinate = binaryReader.ReadDouble();
                        receivedMarker.zCoordinate = binaryReader.ReadDouble();

                        bytesRead = 36;

                        if (receivedMarker.MarkerId != -2147483648)
                        {
                            MarkerList.AddMarker(receivedMarker);
                        }

                        break;

                    case 'F':     // Filtered Marker

                        Marker receivedFilteredMarker = new Marker();
                        receivedFilteredMarker.MarkerId = binaryReader.ReadInt32();

                        //if have been sent End of Frame indication
                        if (receivedFilteredMarker.MarkerId == -2147483648)
                        {
                            receivedFilteredMarker.TimeStamp = binaryReader.ReadInt64();
                            readTimestamp = true;
                            FilteredMarkerList.RemoveExcessMarkersFromList(receivedFilteredMarker.TimeStamp);

                            if (FilteredMarkerListReceivedEvent != null)
                            {
                                FilteredMarkerListReceivedEvent(FilteredMarkerList.listOfMarkers);
                            }
                        }

                        if (readTimestamp == false)
                        {
                            receivedFilteredMarker.TimeStamp = binaryReader.ReadInt64();
                        }

                        receivedFilteredMarker.xCoordinate = binaryReader.ReadDouble();
                        receivedFilteredMarker.yCoordinate = binaryReader.ReadDouble();
                        receivedFilteredMarker.zCoordinate = binaryReader.ReadDouble();

                        bytesRead = 36;

                        if (receivedFilteredMarker.MarkerId != -2147483648)
                        {
                            FilteredMarkerList.AddMarker(receivedFilteredMarker);
                        }

                        break;

                    case 'R':     // Trackable

                        Trackable receivedTrackable = new Trackable();
                        receivedTrackable.ID = binaryReader.ReadInt32();

                        receivedTrackable.TimeStamp = binaryReader.ReadInt64();

                        if (receivedTrackable.ID == -2147483648)
                        {
                            if (receivedTrackable.TimeStamp == 0)
                            {
                                trackableList.Clear();
                            }
                            //End of List
                            if (TrackableListReceivedEvent != null)
                            {
                                TrackableListReceivedEvent(trackableList);
                            }
                            //Read in any remaining data out of the buffer
                            receivedTrackable.TrackableIndex = binaryReader.ReadInt32();
                            receivedTrackable.Name           = binaryReader.ReadString();
                            receivedTrackable.xCoordinate    = binaryReader.ReadInt32();
                            receivedTrackable.yCoordinate    = binaryReader.ReadInt32();
                            receivedTrackable.zCoordinate    = binaryReader.ReadInt32();
                            receivedTrackable.Roll           = binaryReader.ReadDouble();
                            receivedTrackable.Pitch          = binaryReader.ReadDouble();
                            receivedTrackable.Yaw            = binaryReader.ReadDouble();
                        }
                        else
                        {
                            if (trackableList.Count != 0)     // alread contains data
                            {
                                //If from a previous time frame, clear the list
                                if (receivedTrackable.TimeStamp != trackableList[0].TimeStamp)
                                {
                                    trackableList.Clear();
                                }
                            }

                            receivedTrackable.TrackableIndex = binaryReader.ReadInt32();
                            receivedTrackable.Name           = binaryReader.ReadString();
                            receivedTrackable.xCoordinate    = binaryReader.ReadInt32();
                            receivedTrackable.yCoordinate    = binaryReader.ReadInt32();
                            receivedTrackable.zCoordinate    = binaryReader.ReadInt32();
                            receivedTrackable.Roll           = binaryReader.ReadDouble();
                            receivedTrackable.Pitch          = binaryReader.ReadDouble();
                            receivedTrackable.Yaw            = binaryReader.ReadDouble();
                            trackableList.Add(receivedTrackable);
                        }

                        break;

                    case 'J':     // Joint

                        Joint receivedJoint = new Joint();
                        receivedJoint.ID = binaryReader.ReadInt32();

                        receivedJoint.TimeStamp = binaryReader.ReadInt64();

                        if (receivedJoint.ID == -2147483648)
                        {
                            if (receivedJoint.TimeStamp == 0)
                            {
                                jointList.Clear();
                            }

                            //End of List
                            if (JointListReceivedEvent != null)
                            {
                                JointListReceivedEvent(jointList);
                            }


                            //Read in any remaining data out of the buffer
                            receivedJoint.Exists      = binaryReader.ReadBoolean();
                            receivedJoint.Name        = binaryReader.ReadString();
                            receivedJoint.Trackable1  = binaryReader.ReadInt32();
                            receivedJoint.Trackable2  = binaryReader.ReadInt32();
                            receivedJoint.RollOffset  = binaryReader.ReadDouble();
                            receivedJoint.PitchOffset = binaryReader.ReadDouble();
                            receivedJoint.YawOffset   = binaryReader.ReadDouble();
                            receivedJoint.Roll        = binaryReader.ReadDouble();
                            receivedJoint.Pitch       = binaryReader.ReadDouble();
                            receivedJoint.Yaw         = binaryReader.ReadDouble();
                            receivedJoint.xCoordinate = binaryReader.ReadInt32();
                            receivedJoint.yCoordinate = binaryReader.ReadInt32();
                            receivedJoint.zCoordinate = binaryReader.ReadInt32();
                        }
                        else
                        {
                            if (jointList.Count != 0)     // alread contains data
                            {
                                //If from a previous time frame, clear the list
                                if (receivedJoint.TimeStamp != jointList[0].TimeStamp)
                                {
                                    jointList.Clear();
                                }
                            }

                            receivedJoint.Exists      = binaryReader.ReadBoolean();
                            receivedJoint.Name        = binaryReader.ReadString();
                            receivedJoint.Trackable1  = binaryReader.ReadInt32();
                            receivedJoint.Trackable2  = binaryReader.ReadInt32();
                            receivedJoint.RollOffset  = binaryReader.ReadDouble();
                            receivedJoint.PitchOffset = binaryReader.ReadDouble();
                            receivedJoint.YawOffset   = binaryReader.ReadDouble();
                            receivedJoint.Roll        = binaryReader.ReadDouble();
                            receivedJoint.Pitch       = binaryReader.ReadDouble();
                            receivedJoint.Yaw         = binaryReader.ReadDouble();
                            receivedJoint.xCoordinate = binaryReader.ReadInt32();
                            receivedJoint.yCoordinate = binaryReader.ReadInt32();
                            receivedJoint.zCoordinate = binaryReader.ReadInt32();
                            jointList.Add(receivedJoint);
                        }

                        break;


                    case 'C':     // Camera

                        OptiTrackCamera receivedCamera = new OptiTrackCamera();
                        receivedCamera.CameraNumber = binaryReader.ReadInt32();
                        receivedCamera.xCoordinate  = binaryReader.ReadDouble();
                        receivedCamera.yCoordinate  = binaryReader.ReadDouble();
                        receivedCamera.zCoordinate  = binaryReader.ReadDouble();

                        double[] rotationMatrixBuffer = new double[9];

                        for (int i = 0; i < 9; i++)
                        {
                            rotationMatrixBuffer[i] = binaryReader.ReadDouble();
                        }
                        receivedCamera.RotationMatrix = rotationMatrixBuffer;

                        bytesRead = 108;

                        OptitrackCameraList.AddCamera(receivedCamera);

                        break;

                    case 'T':     // Treadmill (Speed)
                        if (AvatarSpeedChangedEvent != null)
                        {
                            AvatarSpeedChangedEvent((float)binaryReader.ReadDouble());
                            bytesRead = 8;
                        }
                        else
                        {
                            binaryReader.ReadDouble();
                            bytesRead = 8;
                        }
                        break;

                    case 'B':     //flag type and flag
                    {
                        System.Diagnostics.Debug.WriteLine("MessageType: " + messageType.ToString());

                        char type = binaryReader.ReadChar();

                        System.Diagnostics.Debug.WriteLine("Type: " + type.ToString());

                        bool flag = binaryReader.ReadBoolean();

                        System.Diagnostics.Debug.WriteLine("Flag: " + flag.ToString());

                        bytesRead = 9;

                        switch (type)
                        {
                        case 'F':             //Toggle Feet
                        {
                            if (ToggleDrawingOfFeetEvent != null)
                            {
                                ToggleDrawingOfFeetEvent(flag);
                            }
                            break;
                        }
                        }
                        break;
                    }

                    default:
                        System.Diagnostics.Debug.WriteLine("A transmit error has occured. " + messageType.ToString() + " recieved");
                        bytesRead = 5;
                        break;
                    }
                }
                catch (System.IO.IOException)
                {
                    // a socket error has occured
                    System.Diagnostics.Debug.WriteLine("Socket error has occured in TCPServer.HandleClientComm");
                    break;
                }
                if (bytesRead == 0)
                {
                    // the client has disconnected from the server
                    System.Diagnostics.Debug.WriteLine("The client has disconnected from the server");
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the list of markers with lastest marker coordinates
        /// </summary>
        public static void UpdateCoordinates(bool updateMarker, bool updateTrackable)
        {
            if (APIRunning)
            {
                // Get the timestamp to apply to all markers and trackables
                long timeStamp = System.DateTime.Now.Ticks;
                try
                {
                    if (TrackingTools.UpdateAll() == 0) // If it updates correctly
                    {
                        if (updateMarker)
                        {
                            int markerCount = TrackingTools.FrameMarkerCount();

                            Marker marker = new Marker();
                            for (int i = 0; i < markerCount; i++)
                            {
                                //Get the marker details from TrackingTools
                                marker.MarkerId    = i;
                                marker.TimeStamp   = timeStamp;
                                marker.xCoordinate = -(int)(TrackingTools.FrameMarkerX(i) * 1000);
                                marker.yCoordinate = (int)(TrackingTools.FrameMarkerY(i) * 1000);
                                marker.zCoordinate = (int)(TrackingTools.FrameMarkerZ(i) * 1000);

                                MarkerList.AddMarker(marker);
                                marker = new Marker();
                            }
                            MarkerList.RemoveExcessMarkersFromList(markerCount);

                            OnMarkerListAvaliable(MarkerList.listOfMarkers, timeStamp);
                        }

                        if (updateTrackable)
                        {
                            int trackableCount = TrackingTools.TrackableCount();
                            trackableList.Clear();
                            Trackable newTrackable = null;

                            // Go through the loaded trackables and if they are in the current frame
                            // add them to the trackableList
                            for (int i = 0; i < trackableCount; i++)
                            {
                                if (TrackingTools.IsTrackableTracked(i))
                                {
                                    newTrackable           = TrackingTools.GetTrackableLocation(i);
                                    newTrackable.TimeStamp = timeStamp;
                                    trackableList.Add(newTrackable);
                                }
                            }
                            OnTrackableListAvaliable(trackableList, timeStamp);
                        }
                    }
                }
                catch (AccessViolationException ex)
                {
                    System.Diagnostics.Debug.WriteLine("Error With UpdateALL() " + ex + "   Time Stamp : " + timeStamp);
                }
                catch (System.Runtime.InteropServices.SEHException ex)
                {
                    System.Diagnostics.Debug.WriteLine("Error With UpdateALL() " + ex + "   Time Stamp : " + timeStamp);
                }
            }
        }
Exemplo n.º 3
0
        private static void HandleDataFromServer(object client)
        {
            TcpClient serverConnection = (TcpClient)client;
            //NetworkStream serverStream = serverConnection.GetStream();

            Marker          receivedMarker = new Marker();
            OptiTrackCamera receivedCamera = new OptiTrackCamera();

            BinaryReader binaryReader = new BinaryReader(new BufferedStream(serverConnection.GetStream()));

            int bytesRead;

            while (true)
            {
                bytesRead = 0;
                bool readTimestamp = false;
                try
                {
                    //blocks until a client sends a message

                    char messageType = binaryReader.ReadChar();
                    bytesRead = 1;

                    switch (messageType)
                    {
                    case 'M':
                        receivedMarker.MarkerId = binaryReader.ReadInt32();

                        //if have been sent End of Frame indication
                        //TimeStamp field of End of Frame Indicator contains number of markers in the frame
                        //ie. receivedMarker.TimeStamp = number of markers in list
                        if (receivedMarker.MarkerId == -2147483648)
                        {
                            receivedMarker.TimeStamp = binaryReader.ReadInt32();
                            readTimestamp            = true;
                            MarkerList.RemoveExcessMarkersFromList(receivedMarker.TimeStamp);

                            //MarkerList.MarkerBinList();

                            if (WholeFrameReceivedEvent != null)
                            {
                                WholeFrameReceivedEvent();
                            }
                        }

                        if (readTimestamp == false)
                        {
                            receivedMarker.TimeStamp = binaryReader.ReadInt32();
                        }

                        receivedMarker.xCoordinate = binaryReader.ReadDouble();
                        receivedMarker.yCoordinate = binaryReader.ReadDouble();
                        receivedMarker.zCoordinate = binaryReader.ReadDouble();

                        bytesRead = 36;

                        if (receivedMarker.MarkerId != -2147483648)
                        {
                            MarkerList.AddMarker(receivedMarker);
                        }

                        break;

                    case 'C':
                        receivedCamera.CameraNumber = binaryReader.ReadInt32();
                        receivedCamera.xCoordinate  = binaryReader.ReadDouble();
                        receivedCamera.yCoordinate  = binaryReader.ReadDouble();
                        receivedCamera.zCoordinate  = binaryReader.ReadDouble();

                        double[] rotationMatrixBuffer = new double[9];

                        for (int i = 0; i < 9; i++)
                        {
                            rotationMatrixBuffer[i] = binaryReader.ReadDouble();
                        }
                        receivedCamera.RotationMatrix = rotationMatrixBuffer;

                        bytesRead = 108;

                        OptitrackCameraList.AddCamera(receivedCamera);

                        break;

                    case 'T':
                        if (AvatarSpeedChangedEvent != null)
                        {
                            AvatarSpeedChangedEvent((float)binaryReader.ReadDouble());
                            bytesRead = 8;
                        }
                        else
                        {
                            binaryReader.ReadDouble();
                            bytesRead = 8;
                        }
                        break;

                    default:
                        System.Diagnostics.Debug.WriteLine("A transmit error has occured. " + messageType.ToString() + " recieved");
                        bytesRead = 5;
                        break;
                    }
                }
                catch (System.IO.IOException)
                {
                    // a socket error has occured
                    System.Diagnostics.Debug.WriteLine("Socket error has occured in TCPServer.HandleClientComm");
                    break;
                }
                if (bytesRead == 0)
                {
                    // the client has disconnected from the server
                    System.Diagnostics.Debug.WriteLine("The client has disconnected from the server");
                    break;
                }

                //else message has been successfully recieved

                //OptitrackCommandParser commandParser = new OptitrackCommandParser();

                //commandParser.handleCommand(recievedCommand[0]);
            }
        }