From 46abe34aec08ded577662cdd008d55ab452c403d Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 28 Mar 2026 11:54:22 +0100 Subject: [PATCH] Enable automatic reconnect for TCP and UDP NMEA receivers The reconnect infrastructure (mReconnectOnDisconnect flag, mReconnectTimer with 2s delay, state change handler) exists in both TcpReceiver and UdpReceiver but is never activated. The flag mReconnectOnDisconnect is initialized to false and never set to true, so when a connection drops the reconnect timer never starts. This is inconsistent with BluetoothReceiver which correctly sets its equivalent flag (mConnectOnDisconnect = true) in handleConnectDevice() and tracks connection failures. Fix: set mReconnectOnDisconnect = true in handleConnectDevice() for both TCP and UDP receivers, with a failure counter that bails out after 10 failed reconnection attempts (matching BluetoothReceiver behavior). The flag is reset to false in handleDisconnectDevice() to prevent reconnection when the user explicitly disconnects. --- src/core/positioning/tcpreceiver.cpp | 17 ++++++++++++++++- src/core/positioning/tcpreceiver.h | 1 + src/core/positioning/udpreceiver.cpp | 17 ++++++++++++++++- src/core/positioning/udpreceiver.h | 1 + 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/core/positioning/tcpreceiver.cpp b/src/core/positioning/tcpreceiver.cpp index 2e3aff2b6c..f425f88a6d 100644 --- a/src/core/positioning/tcpreceiver.cpp +++ b/src/core/positioning/tcpreceiver.cpp @@ -26,11 +26,14 @@ TcpReceiver::TcpReceiver( const QString &address, const int port, QObject *paren { connect( mSocket, qOverload( &QAbstractSocket::errorOccurred ), this, &TcpReceiver::handleError ); connect( mSocket, &QTcpSocket::stateChanged, this, [this]( QAbstractSocket::SocketState state ) { - setSocketState( state ); if ( state == QAbstractSocket::SocketState::UnconnectedState && mReconnectOnDisconnect ) { mReconnectTimer.start( 2000 ); } + else + { + setSocketState( state ); + } } ); connect( mSocket, &QAbstractSocket::connected, this, [this] { @@ -61,6 +64,8 @@ void TcpReceiver::handleConnectDevice() return; } qInfo() << QStringLiteral( "TcpReceiver: Initiating connection to address %1 (port %2)" ).arg( mAddress, QString::number( mPort ) ); + mConnectionFailureCount = 0; + mReconnectOnDisconnect = true; mSocket->connectToHost( mAddress, mPort, QTcpSocket::ReadWrite ); } @@ -100,5 +105,15 @@ void TcpReceiver::handleError( QAbstractSocket::SocketError error ) } qInfo() << QStringLiteral( "TcpReceiver: Error: %1" ).arg( mLastError ); + if ( mReconnectOnDisconnect ) + { + mConnectionFailureCount++; + } + + if ( mConnectionFailureCount > 10 ) + { + mReconnectOnDisconnect = false; + } + emit lastErrorChanged( mLastError ); } diff --git a/src/core/positioning/tcpreceiver.h b/src/core/positioning/tcpreceiver.h index fa7de817f2..078df41f8a 100644 --- a/src/core/positioning/tcpreceiver.h +++ b/src/core/positioning/tcpreceiver.h @@ -50,6 +50,7 @@ class TcpReceiver : public NmeaGnssReceiver QTcpSocket *mSocket = nullptr; bool mReconnectOnDisconnect = false; + int mConnectionFailureCount = 0; QTimer mReconnectTimer; }; diff --git a/src/core/positioning/udpreceiver.cpp b/src/core/positioning/udpreceiver.cpp index 528b94a9aa..5b1f3379a4 100644 --- a/src/core/positioning/udpreceiver.cpp +++ b/src/core/positioning/udpreceiver.cpp @@ -39,11 +39,14 @@ UdpReceiver::UdpReceiver( const QString &address, const int port, QObject *paren connect( mSocket, qOverload( &QAbstractSocket::errorOccurred ), this, &UdpReceiver::handleError ); connect( mSocket, &QUdpSocket::stateChanged, this, [this]( QAbstractSocket::SocketState state ) { - setSocketState( state ); if ( state == QAbstractSocket::SocketState::UnconnectedState && mReconnectOnDisconnect ) { mReconnectTimer.start( 2000 ); } + else + { + setSocketState( state ); + } } ); connect( mSocket, &QUdpSocket::readyRead, this, [this]() { @@ -85,6 +88,8 @@ void UdpReceiver::handleConnectDevice() return; } qInfo() << QStringLiteral( "UdpReceiver: Initiating connection to address %1 (port %2)" ).arg( mAddress, QString::number( mPort ) ); + mConnectionFailureCount = 0; + mReconnectOnDisconnect = true; mBuffer->open( QIODevice::ReadWrite ); mSocket->bind( QHostAddress( mAddress ), mPort, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint ); mSocket->joinMulticastGroup( QHostAddress( mAddress ) ); @@ -127,5 +132,15 @@ void UdpReceiver::handleError( QAbstractSocket::SocketError error ) } qInfo() << QStringLiteral( "UdpReceiver: Error: %1" ).arg( mLastError ); + if ( mReconnectOnDisconnect ) + { + mConnectionFailureCount++; + } + + if ( mConnectionFailureCount > 10 ) + { + mReconnectOnDisconnect = false; + } + emit lastErrorChanged( mLastError ); } diff --git a/src/core/positioning/udpreceiver.h b/src/core/positioning/udpreceiver.h index 2a8dc261c6..ebc159fe1b 100644 --- a/src/core/positioning/udpreceiver.h +++ b/src/core/positioning/udpreceiver.h @@ -52,6 +52,7 @@ class UdpReceiver : public NmeaGnssReceiver QBuffer *mBuffer = nullptr; bool mReconnectOnDisconnect = false; + int mConnectionFailureCount = 0; QTimer mReconnectTimer; };