TaisyncHandler.cc 3.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "TaisyncHandler.h"
#include "SettingsManager.h"
#include "QGCApplication.h"
#include "VideoManager.h"


16 17
QGC_LOGGING_CATEGORY(TaisyncLog,     "TaisyncLog")
QGC_LOGGING_CATEGORY(TaisyncVerbose, "TaisyncVerbose")
18 19 20 21 22 23 24 25 26 27 28 29 30 31

//-----------------------------------------------------------------------------
TaisyncHandler::TaisyncHandler(QObject* parent)
    : QObject (parent)
{
}

//-----------------------------------------------------------------------------
TaisyncHandler::~TaisyncHandler()
{
    close();
}

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
32
bool TaisyncHandler::close()
33
{
Gus Grubba's avatar
Gus Grubba committed
34
    bool res = (_tcpSocket || _tcpServer);
35
    if(_tcpSocket) {
Gus Grubba's avatar
Gus Grubba committed
36
        qCDebug(TaisyncLog) << "Close Taisync TCP socket on port" << _tcpSocket->localPort();
37 38 39 40
        _tcpSocket->close();
        _tcpSocket->deleteLater();
        _tcpSocket = nullptr;
    }
Gus Grubba's avatar
Gus Grubba committed
41 42 43 44 45 46 47
    if(_tcpServer) {
        qCDebug(TaisyncLog) << "Close Taisync TCP server on port" << _tcpServer->serverPort();;
        _tcpServer->close();
        _tcpServer->deleteLater();
        _tcpServer = nullptr;
    }
    return res;
48 49 50
}

//-----------------------------------------------------------------------------
51 52
bool
TaisyncHandler::_start(uint16_t port, QHostAddress addr)
53
{
54 55 56
    close();
    _serverMode = addr == QHostAddress::AnyIPv4;
    if(_serverMode) {
Gus Grubba's avatar
Gus Grubba committed
57 58 59 60 61 62
        if(!_tcpServer) {
            qCDebug(TaisyncLog) << "Listen for Taisync TCP on port" << port;
            _tcpServer = new QTcpServer(this);
            QObject::connect(_tcpServer, &QTcpServer::newConnection, this, &TaisyncHandler::_newConnection);
            _tcpServer->listen(QHostAddress::AnyIPv4, port);
        }
63 64 65 66 67 68 69 70 71 72 73 74
    } else {
        _tcpSocket = new QTcpSocket();
        QObject::connect(_tcpSocket, &QIODevice::readyRead, this, &TaisyncHandler::_readBytes);
        qCDebug(TaisyncLog) << "Connecting to" << addr;
        _tcpSocket->connectToHost(addr, port);
        if (!_tcpSocket->waitForConnected(1000)) {
            close();
            return false;
        }
        emit connected();
    }
    return true;
75 76 77 78 79 80
}

//-----------------------------------------------------------------------------
void
TaisyncHandler::_newConnection()
{
Gus Grubba's avatar
Gus Grubba committed
81
    qCDebug(TaisyncLog) << "New Taisync TCP Connection on port" << _tcpServer->serverPort();
82 83 84 85 86
    if(_tcpSocket) {
        _tcpSocket->close();
        _tcpSocket->deleteLater();
    }
    _tcpSocket = _tcpServer->nextPendingConnection();
Gus Grubba's avatar
Gus Grubba committed
87 88 89 90 91 92 93
    if(_tcpSocket) {
        QObject::connect(_tcpSocket, &QIODevice::readyRead, this, &TaisyncHandler::_readBytes);
        QObject::connect(_tcpSocket, &QAbstractSocket::disconnected, this, &TaisyncHandler::_socketDisconnected);
        emit connected();
    } else {
        qCWarning(TaisyncLog) << "New Taisync TCP Connection provided no socket";
    }
94 95 96 97 98 99
}

//-----------------------------------------------------------------------------
void
TaisyncHandler::_socketDisconnected()
{
Gus Grubba's avatar
Gus Grubba committed
100
    qCDebug(TaisyncLog) << "Taisync TCP Connection Closed on port" << _tcpSocket->localPort();
101 102 103 104 105
    if(_tcpSocket) {
        _tcpSocket->close();
        _tcpSocket->deleteLater();
        _tcpSocket = nullptr;
    }
Gus Grubba's avatar
Gus Grubba committed
106
    emit disconnected();
107
}