|
|
@ -18,11 +18,6 @@ |
|
|
|
#include <QHostInfo> |
|
|
|
#include <QHostInfo> |
|
|
|
#include <QSignalSpy> |
|
|
|
#include <QSignalSpy> |
|
|
|
|
|
|
|
|
|
|
|
/// @file
|
|
|
|
|
|
|
|
/// @brief TCP link type for SITL support
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// @author Don Gagne <don@thegagnes.com>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TCPLink::TCPLink(SharedLinkConfigurationPtr& config) |
|
|
|
TCPLink::TCPLink(SharedLinkConfigurationPtr& config) |
|
|
|
: LinkInterface(config) |
|
|
|
: LinkInterface(config) |
|
|
|
, _tcpConfig(qobject_cast<TCPConfiguration*>(config.get())) |
|
|
|
, _tcpConfig(qobject_cast<TCPConfiguration*>(config.get())) |
|
|
@ -30,22 +25,11 @@ TCPLink::TCPLink(SharedLinkConfigurationPtr& config) |
|
|
|
, _socketIsConnected(false) |
|
|
|
, _socketIsConnected(false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Q_ASSERT(_tcpConfig); |
|
|
|
Q_ASSERT(_tcpConfig); |
|
|
|
moveToThread(this); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TCPLink::~TCPLink() |
|
|
|
TCPLink::~TCPLink() |
|
|
|
{ |
|
|
|
{ |
|
|
|
disconnect(); |
|
|
|
disconnect(); |
|
|
|
// Tell the thread to exit
|
|
|
|
|
|
|
|
quit(); |
|
|
|
|
|
|
|
// Wait for it to exit
|
|
|
|
|
|
|
|
wait(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TCPLink::run() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_hardwareConnect(); |
|
|
|
|
|
|
|
exec(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#ifdef TCPLINK_READWRITE_DEBUG |
|
|
|
#ifdef TCPLINK_READWRITE_DEBUG |
|
|
@ -84,7 +68,7 @@ void TCPLink::_writeBytes(const QByteArray data) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void TCPLink::readBytes() |
|
|
|
void TCPLink::_readBytes() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (_socket) { |
|
|
|
if (_socket) { |
|
|
|
qint64 byteCount = _socket->bytesAvailable(); |
|
|
|
qint64 byteCount = _socket->bytesAvailable(); |
|
|
@ -103,14 +87,11 @@ void TCPLink::readBytes() |
|
|
|
|
|
|
|
|
|
|
|
void TCPLink::disconnect(void) |
|
|
|
void TCPLink::disconnect(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
quit(); |
|
|
|
|
|
|
|
wait(); |
|
|
|
|
|
|
|
if (_socket) { |
|
|
|
if (_socket) { |
|
|
|
// This prevents stale signal from calling the link after it has been deleted
|
|
|
|
// This prevents stale signal from calling the link after it has been deleted
|
|
|
|
QObject::disconnect(_socket, &QTcpSocket::readyRead, this, &TCPLink::readBytes); |
|
|
|
QObject::disconnect(_socket, &QIODevice::readyRead, this, &TCPLink::_readBytes); |
|
|
|
_socketIsConnected = false; |
|
|
|
_socketIsConnected = false; |
|
|
|
_socket->disconnectFromHost(); // Disconnect tcp
|
|
|
|
_socket->disconnectFromHost(); // Disconnect tcp
|
|
|
|
_socket->waitForDisconnected();
|
|
|
|
|
|
|
|
_socket->deleteLater(); // Make sure delete happens on correct thread
|
|
|
|
_socket->deleteLater(); // Make sure delete happens on correct thread
|
|
|
|
_socket = nullptr; |
|
|
|
_socket = nullptr; |
|
|
|
emit disconnected(); |
|
|
|
emit disconnected(); |
|
|
@ -119,29 +100,25 @@ void TCPLink::disconnect(void) |
|
|
|
|
|
|
|
|
|
|
|
bool TCPLink::_connect(void) |
|
|
|
bool TCPLink::_connect(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (isRunning()) |
|
|
|
if (_socket) { |
|
|
|
{ |
|
|
|
qWarning() << "connect called while already connected"; |
|
|
|
quit(); |
|
|
|
|
|
|
|
wait(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
start(HighPriority); |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return _hardwareConnect(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool TCPLink::_hardwareConnect() |
|
|
|
bool TCPLink::_hardwareConnect() |
|
|
|
{ |
|
|
|
{ |
|
|
|
Q_ASSERT(_socket == nullptr); |
|
|
|
Q_ASSERT(_socket == nullptr); |
|
|
|
_socket = new QTcpSocket(); |
|
|
|
_socket = new QTcpSocket(); |
|
|
|
|
|
|
|
QObject::connect(_socket, &QIODevice::readyRead, this, &TCPLink::_readBytes); |
|
|
|
|
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
|
|
|
QSignalSpy errorSpy(_socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error)); |
|
|
|
QSignalSpy errorSpy(_socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error)); |
|
|
|
#else |
|
|
|
#else |
|
|
|
QSignalSpy errorSpy(_socket, &QAbstractSocket::errorOccurred); |
|
|
|
QSignalSpy errorSpy(_socket, &QAbstractSocket::errorOccurred); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
_socket->connectToHost(_tcpConfig->address(), _tcpConfig->port()); |
|
|
|
|
|
|
|
QObject::connect(_socket, &QTcpSocket::readyRead, this, &TCPLink::readBytes); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
|
|
|
QObject::connect(_socket,static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error), |
|
|
|
QObject::connect(_socket,static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error), |
|
|
|
this, &TCPLink::_socketError); |
|
|
|
this, &TCPLink::_socketError); |
|
|
@ -149,6 +126,8 @@ bool TCPLink::_hardwareConnect() |
|
|
|
QObject::connect(_socket, &QAbstractSocket::errorOccurred, this, &TCPLink::_socketError); |
|
|
|
QObject::connect(_socket, &QAbstractSocket::errorOccurred, this, &TCPLink::_socketError); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_socket->connectToHost(_tcpConfig->address(), _tcpConfig->port()); |
|
|
|
|
|
|
|
|
|
|
|
// Give the socket a second to connect to the other side otherwise error out
|
|
|
|
// Give the socket a second to connect to the other side otherwise error out
|
|
|
|
if (!_socket->waitForConnected(1000)) |
|
|
|
if (!_socket->waitForConnected(1000)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -182,18 +161,6 @@ bool TCPLink::isConnected() const |
|
|
|
return _socketIsConnected; |
|
|
|
return _socketIsConnected; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void TCPLink::waitForBytesWritten(int msecs) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Q_ASSERT(_socket); |
|
|
|
|
|
|
|
_socket->waitForBytesWritten(msecs); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TCPLink::waitForReadyRead(int msecs) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Q_ASSERT(_socket); |
|
|
|
|
|
|
|
_socket->waitForReadyRead(msecs); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//-- TCPConfiguration
|
|
|
|
//-- TCPConfiguration
|
|
|
|
|
|
|
|
|
|
|
|