TaisyncHandler.cc 3.52 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
    } else {
        _tcpSocket = new QTcpSocket();
        QObject::connect(_tcpSocket, &QIODevice::readyRead, this, &TaisyncHandler::_readBytes);
        qCDebug(TaisyncLog) << "Connecting to" << addr;
        _tcpSocket->connectToHost(addr, port);
Gus Grubba's avatar
Gus Grubba committed
68
        //-- TODO: This has to be removed. It's blocking the main thread.
69 70 71 72 73 74 75
        if (!_tcpSocket->waitForConnected(1000)) {
            close();
            return false;
        }
        emit connected();
    }
    return true;
76 77 78 79 80 81
}

//-----------------------------------------------------------------------------
void
TaisyncHandler::_newConnection()
{
Gus Grubba's avatar
Gus Grubba committed
82
    qCDebug(TaisyncLog) << "New Taisync TCP Connection on port" << _tcpServer->serverPort();
83 84 85 86 87
    if(_tcpSocket) {
        _tcpSocket->close();
        _tcpSocket->deleteLater();
    }
    _tcpSocket = _tcpServer->nextPendingConnection();
Gus Grubba's avatar
Gus Grubba committed
88 89 90 91 92 93 94
    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";
    }
95 96 97 98 99 100
}

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