Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/core/positioning/tcpreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ TcpReceiver::TcpReceiver( const QString &address, const int port, QObject *paren
{
connect( mSocket, qOverload<QAbstractSocket::SocketError>( &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] {
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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 );
}
1 change: 1 addition & 0 deletions src/core/positioning/tcpreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TcpReceiver : public NmeaGnssReceiver
QTcpSocket *mSocket = nullptr;

bool mReconnectOnDisconnect = false;
int mConnectionFailureCount = 0;
QTimer mReconnectTimer;
};

Expand Down
17 changes: 16 additions & 1 deletion src/core/positioning/udpreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ UdpReceiver::UdpReceiver( const QString &address, const int port, QObject *paren

connect( mSocket, qOverload<QAbstractSocket::SocketError>( &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]() {
Expand Down Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 );
}
1 change: 1 addition & 0 deletions src/core/positioning/udpreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class UdpReceiver : public NmeaGnssReceiver
QBuffer *mBuffer = nullptr;

bool mReconnectOnDisconnect = false;
int mConnectionFailureCount = 0;
QTimer mReconnectTimer;
};

Expand Down
Loading