From 43420d828647b026a315c58db621202fc5a17403 Mon Sep 17 00:00:00 2001 From: Wim Vriend Date: Tue, 8 Feb 2011 21:17:36 +0000 Subject: Updating FlightGear protocol git-svn-id: svn+ssh://svn.code.sf.net/p/facetracknoir/code@49 19e81ba0-9b1a-49c3-bd6c-561e1906d5fb --- FaceTrackNoIR/FGServer.cpp | 68 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 30 deletions(-) (limited to 'FaceTrackNoIR/FGServer.cpp') diff --git a/FaceTrackNoIR/FGServer.cpp b/FaceTrackNoIR/FGServer.cpp index 53cd68ff..292c194b 100644 --- a/FaceTrackNoIR/FGServer.cpp +++ b/FaceTrackNoIR/FGServer.cpp @@ -61,22 +61,6 @@ int no_bytes; QHostAddress sender; quint16 senderPort; - // - // Create UDP-sockets if they don't exist already. - // They must be created here, because they must be in the Tracker thread (Tracker::run()) - // - if (inSocket == 0) { - qDebug() << "FGServer::sendHeadposeToGame creating sockets"; - inSocket = new QUdpSocket(); - // Connect the inSocket to the port, to receive messages -// inSocket->bind(QHostAddress::LocalHost, 5551); - inSocket->bind(QHostAddress::Any, destPort+1); - } - - if (outSocket == 0) { - outSocket = new QUdpSocket(); - } - // // Copy the Raw measurements directly to the client. // @@ -94,28 +78,32 @@ quint16 senderPort; //! [1] // no_bytes = outSocket->writeDatagram((const char *) &TestData, sizeof( TestData ), QHostAddress::LocalHost, 5550); - no_bytes = outSocket->writeDatagram((const char *) &TestData, sizeof( TestData ), destIP, destPort); - if ( no_bytes > 0) { -// qDebug() << "FGServer::writePendingDatagrams says: bytes send =" << no_bytes << sizeof( double ); - } - else { - qDebug() << "FGServer::writePendingDatagrams says: nothing sent!"; + if (outSocket != 0) { + no_bytes = outSocket->writeDatagram((const char *) &TestData, sizeof( TestData ), destIP, destPort); + if ( no_bytes > 0) { + // qDebug() << "FGServer::writePendingDatagrams says: bytes send =" << no_bytes << sizeof( double ); + } + else { + qDebug() << "FGServer::writePendingDatagrams says: nothing sent!"; + } } // // FlightGear keeps sending data, so we must read that here. // - while (inSocket->hasPendingDatagrams()) { + if (inSocket != 0) { + while (inSocket->hasPendingDatagrams()) { - QByteArray datagram; - datagram.resize(inSocket->pendingDatagramSize()); + QByteArray datagram; + datagram.resize(inSocket->pendingDatagramSize()); - inSocket->readDatagram( (char * ) &cmd, sizeof(cmd), &sender, &senderPort); + inSocket->readDatagram( (char * ) &cmd, sizeof(cmd), &sender, &senderPort); - fg_cmd = cmd; // Let's just accept that command for now... - if ( cmd > 0 ) { - qDebug() << "FGServer::sendHeadposeToGame hasPendingDatagrams, cmd = " << cmd; - headTracker->handleGameCommand ( cmd ); // Send it upstream, for the Tracker to handle + fg_cmd = cmd; // Let's just accept that command for now... + if ( cmd > 0 ) { + qDebug() << "FGServer::sendHeadposeToGame hasPendingDatagrams, cmd = " << cmd; + headTracker->handleGameCommand ( cmd ); // Send it upstream, for the Tracker to handle + } } } } @@ -139,6 +127,26 @@ bool FGServer::checkServerInstallationOK( HANDLE handle ) inSocket = 0; outSocket = 0; + // + // Create UDP-sockets. + // + if (inSocket == 0) { + qDebug() << "FGServer::sendHeadposeToGame creating insocket"; + inSocket = new QUdpSocket(); + + // Connect the inSocket to the port, to receive messages + if (!inSocket->bind(QHostAddress::Any, destPort+1)) { + QMessageBox::warning(0,"FaceTrackNoIR Error", "Unable to bind UDP-port",QMessageBox::Ok,QMessageBox::NoButton); + delete inSocket; + inSocket = 0; + return false; + } + } + + if (outSocket == 0) { + outSocket = new QUdpSocket(); + } + return true; } -- cgit v1.2.3