Skip to content
TCPLink.h 3.92 KiB
Newer Older
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
 * (c) 2009-2020 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.
 *
 ****************************************************************************/
#pragma once
Don Gagne's avatar
Don Gagne committed

#include <QString>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QHostAddress>
#include <LinkInterface.h>
Don Gagne's avatar
Don Gagne committed

// Even though QAbstractSocket::SocketError is used in a signal by Qt, Qt doesn't declare it as a meta type.
// This in turn causes debug output to be kicked out about not being able to queue the signal. We declare it
// as a meta type to silence that.
#include <QMetaType>
#include <QTcpSocket>

Don Gagne's avatar
Don Gagne committed
//#define TCPLINK_READWRITE_DEBUG   // Use to debug data reads/writes

class TCPLinkTest;
#define QGC_TCP_PORT 5760

class TCPConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
    Q_OBJECT

    Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged)
    Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
    TCPConfiguration(const QString& name);
    TCPConfiguration(TCPConfiguration* source);

    quint16             port        (void)                          { return _port; }
    const QHostAddress& address     (void)                          { return _address; }
    const QString       host        (void)                          { return _address.toString(); }
    void                setPort     (quint16 port);
    void                setAddress  (const QHostAddress& address);
    void                setHost     (const QString host);

    //LinkConfiguration overrides
    LinkType    type                (void) override                                         { return LinkConfiguration::TypeTcp; }
    void        copyFrom            (LinkConfiguration* source) override;
    bool        isHighLatencyAllowed(void) override                                         { return true; }
    void        loadSettings        (QSettings& settings, const QString& root) override;
    void        saveSettings        (QSettings& settings, const QString& root) override;
    QString     settingsURL         (void) override                                         { return "TcpSettings.qml"; }
    QString     settingsTitle       (void) override                                         { return tr("TCP Link Settings"); }
    void portChanged(void);
    void hostChanged(void);
    QHostAddress    _address;
    quint16         _port;
Don Gagne's avatar
Don Gagne committed
class TCPLink : public LinkInterface
{
    Q_OBJECT
    friend class TCPLinkTest;
    friend class TCPConfiguration;
    friend class LinkManager;
    QTcpSocket* getSocket           (void) { return _socket; }
    void        signalBytesWritten  (void);
Don Gagne's avatar
Don Gagne committed
    bool isConnected(void) const override;
    void disconnect (void) override;
    void waitForBytesWritten(int msecs);
    void waitForReadyRead   (int msecs);

protected slots:
    void _socketError   (QAbstractSocket::SocketError socketError);
    void readBytes      (void);
Don Gagne's avatar
Don Gagne committed
protected:
    // QThread overrides
    void run(void) override;
private slots:
    // LinkInterface overrides
    void _writeBytes(const QByteArray data) override;
private:
    TCPLink(SharedLinkConfigurationPtr& config);
    // LinkInterface overrides
    bool _connect(void) override;
Don Gagne's avatar
Don Gagne committed
#ifdef TCPLINK_READWRITE_DEBUG
    void _writeDebugBytes   (const QByteArray data);
Don Gagne's avatar
Don Gagne committed
#endif
    TCPConfiguration* _tcpConfig;
    QTcpSocket*       _socket;
    bool              _socketIsConnected;
    quint64 _bitsSentTotal;
    quint64 _bitsSentCurrent;
    quint64 _bitsSentMax;
    quint64 _bitsReceivedTotal;
    quint64 _bitsReceivedCurrent;
    quint64 _bitsReceivedMax;
    quint64 _connectionStartTime;
    QMutex  _statisticsMutex;
Don Gagne's avatar
Don Gagne committed
};