Browse Source

Fix ability to write bytes to link

QGC4.4
DonLakeFlyer 4 years ago committed by Don Gagne
parent
commit
130dadab97
  1. 53
      src/comm/TCPLink.cc
  2. 14
      src/comm/TCPLink.h

53
src/comm/TCPLink.cc

@ -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

14
src/comm/TCPLink.h

@ -81,24 +81,14 @@ public:
bool isConnected(void) const override; bool isConnected(void) const override;
void disconnect (void) override; void disconnect (void) override;
public slots: private slots:
void waitForBytesWritten(int msecs);
void waitForReadyRead (int msecs);
protected slots:
void _socketError (QAbstractSocket::SocketError socketError); void _socketError (QAbstractSocket::SocketError socketError);
void readBytes (void); void _readBytes (void);
protected:
// QThread overrides
void run(void) override;
private slots:
// LinkInterface overrides // LinkInterface overrides
void _writeBytes(const QByteArray data) override; void _writeBytes(const QByteArray data) override;
private: private:
// LinkInterface overrides // LinkInterface overrides
bool _connect(void) override; bool _connect(void) override;

Loading…
Cancel
Save