Skip to content

Commit

Permalink
Comms: TCPLink Options Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Oct 30, 2024
1 parent 617b651 commit 4319228
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/Comms/LinkManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ void LinkManager::_linkDisconnected()
if (it->get() == link) {
qCDebug(LinkManagerLog) << "LinkManager::_linkDisconnected" << it->get()->linkConfiguration()->name() << it->use_count();
(void) _rgLinks.erase(it);
// TODO: emit config->linkChanged() ?
return;
}
}
Expand Down
92 changes: 50 additions & 42 deletions src/Comms/TCPLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
QGC_LOGGING_CATEGORY(TCPLinkLog, "qgc.comms.tcplink")

namespace {
constexpr int SEND_BUFFER_SIZE = 1024; // >= MAVLINK_MAX_PACKET_LEN
constexpr int RECEIVE_BUFFER_SIZE = 1024; // >= MAVLINK_MAX_PACKET_LEN
constexpr int READ_BUFFER_SIZE = 1024; // >= MAVLINK_MAX_PACKET_LEN
constexpr int CONNECT_TIMEOUT_MS = 1000;
constexpr int TYPE_OF_SERVICE = 32; // Optional: Set ToS for low delay
constexpr int MAX_RECONNECTION_ATTEMPTS = 5;
constexpr int TYPE_OF_SERVICE = 32; // Set ToS for low delay
constexpr int MAX_RECONNECTION_ATTEMPTS = 3;
}

TCPLink::TCPLink(SharedLinkConfigurationPtr &config, QObject *parent)
Expand All @@ -43,12 +40,35 @@ TCPLink::TCPLink(SharedLinkConfigurationPtr &config, QObject *parent)
return;
}

_socket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, SEND_BUFFER_SIZE);
_socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, RECEIVE_BUFFER_SIZE);
_socket->setReadBufferSize(READ_BUFFER_SIZE);
_setupSocket();
}

TCPLink::~TCPLink()
{
if (isConnected()) {
_socket->disconnectFromHost();
}

_socket->deleteLater();

// qCDebug(TCPLinkLog) << Q_FUNC_INFO << this;
}

void TCPLink::_setupSocket()
{
qCDebug(TCPLinkLog) << "Default Socket Settings -"
<< "LowDelayOption:" << _socket->socketOption(QAbstractSocket::LowDelayOption)
<< "KeepAliveOption:" << _socket->socketOption(QAbstractSocket::KeepAliveOption)
<< "TypeOfServiceOption" << _socket->socketOption(QAbstractSocket::TypeOfServiceOption);

_socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
_socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
// _socket->setSocketOption(QAbstractSocket::TypeOfServiceOption, TYPE_OF_SERVICE);
_socket->setSocketOption(QAbstractSocket::TypeOfServiceOption, TYPE_OF_SERVICE);

qCDebug(TCPLinkLog) << "Adjusted Socket Settings -"
<< "LowDelayOption:" << _socket->socketOption(QAbstractSocket::LowDelayOption)
<< "KeepAliveOption:" << _socket->socketOption(QAbstractSocket::KeepAliveOption)
<< "TypeOfServiceOption:" << _socket->socketOption(QAbstractSocket::TypeOfServiceOption);

(void) QObject::connect(_socket, &QTcpSocket::connected, this, [this]() {
_isConnected = true;
Expand All @@ -61,8 +81,6 @@ TCPLink::TCPLink(SharedLinkConfigurationPtr &config, QObject *parent)
_isConnected = false;
qCDebug(TCPLinkLog) << "TCP disconnected from" << _tcpConfig->host() << ":" << _tcpConfig->port();
emit disconnected();
// TODO: Uncomment after threading changes
// _attemptReconnection();
}, Qt::AutoConnection);

(void) QObject::connect(_socket, &QTcpSocket::readyRead, this, &TCPLink::_readBytes, Qt::AutoConnection);
Expand All @@ -73,6 +91,11 @@ TCPLink::TCPLink(SharedLinkConfigurationPtr &config, QObject *parent)
tr("TCP Link Error"),
tr("Link %1: %2.").arg(_tcpConfig->name(), _socket->errorString())
);

// TODO: Uncomment after threading changes
// if (!isConnected()) {
// _attemptReconnection();
// }
}, Qt::AutoConnection);

#ifdef QT_DEBUG
Expand All @@ -86,18 +109,9 @@ TCPLink::TCPLink(SharedLinkConfigurationPtr &config, QObject *parent)
#endif
}

TCPLink::~TCPLink()
bool TCPLink::isConnected() const
{
if (_socket->isOpen()) {
_socket->disconnectFromHost();
if (_socket->state() != QAbstractSocket::UnconnectedState) {
_socket->waitForDisconnected(CONNECT_TIMEOUT_MS);
}
}

_socket->deleteLater();

// qCDebug(TCPLinkLog) << Q_FUNC_INFO << this;
return (_isConnected && _socket && _socket->isValid());
}

void TCPLink::disconnect()
Expand Down Expand Up @@ -139,44 +153,37 @@ bool TCPLink::_connect()

void TCPLink::_writeBytes(const QByteArray &bytes)
{
if (!_socket->isValid()) {
return;
}

static const QString title = tr("TCP Link Write Error");

if (!isConnected()) {
emit communicationError(
title,
tr("TCP Link Write Error"),
tr("Link %1: Could Not Send Data - Link is Disconnected!").arg(_tcpConfig->name())
);
return;
}

const qint64 bytesWritten = _socket->write(bytes);
if (bytesWritten < 0) {
qint64 totalBytesWritten = 0;
while (totalBytesWritten < bytes.size()) {
const qint64 bytesWritten = _socket->write(bytes.constData() + totalBytesWritten, bytes.size() - totalBytesWritten);
if (bytesWritten <= 0) {
break;
}

totalBytesWritten += bytesWritten;
}

if (totalBytesWritten < 0) {
emit communicationError(
title,
tr("TCP Link Write Error"),
tr("Link %1: Could Not Send Data - Write Failed: %2").arg(_tcpConfig->name(), _socket->errorString())
);
return;
}

if (bytesWritten < bytes.size()) {
qCWarning(TCPLinkLog) << "Wrote" << bytesWritten << "Out of" << bytes.size() << "total bytes";
const QByteArray remainingBytes = bytes.mid(bytesWritten);
writeBytesThreadSafe(remainingBytes.constData(), remainingBytes.size());
}

emit bytesSent(this, bytes);
}

void TCPLink::_readBytes()
{
if (!_socket->isValid()) {
return;
}

if (!isConnected()) {
emit communicationError(
tr("TCP Link Read Error"),
Expand Down Expand Up @@ -209,6 +216,7 @@ void TCPLink::_attemptReconnection()
_reconnectionAttempts++;
const int delay = qPow(2, _reconnectionAttempts) * 1000; // Exponential backoff
qCDebug(TCPLinkLog) << "Attempting reconnection #" << _reconnectionAttempts << "in" << delay << "ms";

QTimer::singleShot(delay, this, [this]() {
_connect();
});
Expand Down
3 changes: 2 additions & 1 deletion src/Comms/TCPLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TCPLink : public LinkInterface
virtual ~TCPLink();

void run() override {};
bool isConnected() const override { return _isConnected; }
bool isConnected() const override;
void disconnect() override;
bool isSecureConnection() override;

Expand All @@ -76,6 +76,7 @@ private slots:

private:
bool _connect() override;
void _setupSocket();
void _attemptReconnection();

const TCPConfiguration *_tcpConfig = nullptr;
Expand Down

0 comments on commit 4319228

Please sign in to comment.