void WriterThreadFunction()
        {
            try
            {
                using (var rtProtocol = new RTProtocol(RT_LOWEST_SUPPORTED_VERSION_MAJOR, RT_LOWEST_SUPPORTED_VERSION_MINOR))
                {
                    if (!rtProtocol.Connect(IpAddress, udpPort, RT_LOWEST_SUPPORTED_VERSION_MAJOR, RT_LOWEST_SUPPORTED_VERSION_MINOR))
                    {
                        throw new WriterThreadException("Error Creating Connection to server" + rtProtocol.GetErrorString());
                    }

                    RtProtocolVersion version = new RtProtocolVersion(RTProtocol.Constants.MAJOR_VERSION, RTProtocol.Constants.MINOR_VERSION);

                    //Upgrade protocol version
                    for (; version.minor >= RT_LOWEST_SUPPORTED_VERSION_MINOR; --version.minor)
                    {
                        lock (syncLock)
                        {
                            writerThreadState.rtProtocolVersion.CopyFrom(version);
                        }
                        string response;
                        if (rtProtocol.SetVersion(version.major, version.minor, out response))
                        {
                            break;
                        }
                    }

                    if (version.minor < RT_LOWEST_SUPPORTED_VERSION_MINOR)
                    {
                        throw new WriterThreadException("Failed to negotiate RT Protocol version with QTM");
                    }

                    lock (syncLock)
                    {
                        writerThreadState.connectionState = RTConnectionState.Connected;
                        if (!UpdateSettings(writerThreadState, rtProtocol, componentSelection))
                        {
                            throw new WriterThreadException("Failed to update settings: " + rtProtocol.GetErrorString());
                        }

                        if (!StartStreaming(writerThreadState, rtProtocol, streamRate, udpPort))
                        {
                            throw new WriterThreadException("Failed to start stream: " + rtProtocol.GetErrorString());
                        }
                    }


                    while (true)
                    {
                        if (!rtProtocol.IsConnected())
                        {
                            throw new WriterThreadException("Connection lost");
                        }

                        if (killThread)
                        {
                            throw new WriterThreadException("Thread was killed");
                        }

                        PacketType packetType;
                        if (rtProtocol.ReceiveRTPacket(out packetType, false) <= 0)
                        {
                            continue;
                        }

                        var packet = rtProtocol.GetRTPacket();
                        if (packet != null)
                        {
                            if (packetType == PacketType.PacketData)
                            {
                                lock (syncLock)
                                {
                                    Process(writerThreadState, packet);
                                }
                            }
                            else if (packetType == PacketType.PacketEvent)
                            {
                                QTMEvent currentEvent = packet.GetEvent();
                                switch (currentEvent)
                                {
                                case QTMEvent.QTMShuttingDown:
                                    throw new WriterThreadException("Qtm closed connection");

                                case QTMEvent.RTFromFileStarted:
                                case QTMEvent.Connected:
                                case QTMEvent.CaptureStarted:
                                case QTMEvent.CalibrationStarted:
                                case QTMEvent.CameraSettingsChanged:
                                    lock (syncLock)
                                    {
                                        // reload settings when we start streaming to get proper settings
                                        if (!UpdateSettings(writerThreadState, rtProtocol, componentSelection))
                                        {
                                            throw new WriterThreadException("Failed to update settings: " + rtProtocol.GetErrorString());
                                        }

                                        if (!StartStreaming(writerThreadState, rtProtocol, streamRate, udpPort))
                                        {
                                            throw new WriterThreadException("Failed to start stream: " + rtProtocol.GetErrorString());
                                        }
                                    }
                                    break;

                                case QTMEvent.ConnectionClosed:
                                default: break;
                                }
                            }
                        }
                    }
                }
            }
            catch (WriterThreadException writerThreadException)
            {
                lock (syncLock)
                {
                    writerThreadState.errorString     = writerThreadException.Message;
                    writerThreadState.connectionState = RTConnectionState.Disconnected;
                }
            }
            catch (System.Exception e)
            {
                lock (syncLock)
                {
                    writerThreadState.errorString = "Exception "
                                                    + e.GetType().Name + ": " + e.Message + "\n"
                                                    + e.StackTrace.Replace(" at ", "\n at ");

                    writerThreadState.connectionState = RTConnectionState.Disconnected;
                }
            }
        }
示例#2
0
 // Get protocol error string
 public string GetErrorString()
 {
     return(mProtocol.GetErrorString());
 }