From 0e566a5992546d2f37f618e64472e3e856a9f5fa Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 24 Oct 2019 16:28:14 +0200 Subject: [PATCH] [FSD] Pending connection timeout - avoid that a pending FSD conenction blocks forever --- src/blackcore/fsd/fsdclient.cpp | 25 ++++++++++++++++++++++++- src/blackcore/fsd/fsdclient.h | 15 ++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/blackcore/fsd/fsdclient.cpp b/src/blackcore/fsd/fsdclient.cpp index 7ca97f93b..ad1384a01 100644 --- a/src/blackcore/fsd/fsdclient.cpp +++ b/src/blackcore/fsd/fsdclient.cpp @@ -211,6 +211,16 @@ namespace BlackCore this->clearState(); m_filterPasswordFromLogin = true; + m_loginSince = QDateTime::currentMSecsSinceEpoch(); + QPointer myself(this); + const qint64 timerMs = qRound(PendingConnectionTimeoutMs * 1.25); + + QTimer::singleShot(timerMs, this, [ = ] + { + if (!myself || !sApp || sApp->isShuttingDown()) { return; } + this->pendingTimeoutCheck(); + }); + this->updateConnectionStatus(CConnectionStatus::Connecting); const QString host = m_server.getAddress(); @@ -1479,6 +1489,7 @@ namespace BlackCore m_lastPositionUpdate.clear(); m_lastOffsetTimes.clear(); m_sentAircraftConfig = CAircraftParts::null(); + m_loginSince = -1; } void CFSDClient::clearState(const CCallsign &callsign) @@ -1853,13 +1864,25 @@ namespace BlackCore } } - emit atisReplyReceived(cs, atisMessage); + emit this->atisReplyReceived(cs, atisMessage); m_mapAtisMessages.remove(callsign); return; } } + void CFSDClient::pendingTimeoutCheck() + { + if (!this->isPendingConnection()) { return; } + + const qint64 age = QDateTime::currentMSecsSinceEpoch() - m_loginSince; + if (age < PendingConnectionTimeoutMs) { return; } + + // time out + CLogMessage(this).warning(u"Timeout on pending connection to '%1'") << this->getServer().getName(); + this->disconnectFromServer(); + } + const CLength &CFSDClient::fixAtcRange(const CLength &networkRange, const CCallsign &cs) { /** T702, https://discordapp.com/channels/539048679160676382/539846348275449887/597814208125730826 diff --git a/src/blackcore/fsd/fsdclient.h b/src/blackcore/fsd/fsdclient.h index 0567fe5c3..9c5c64eb8 100644 --- a/src/blackcore/fsd/fsdclient.h +++ b/src/blackcore/fsd/fsdclient.h @@ -235,6 +235,7 @@ namespace BlackCore void killRequestReceived(const QString &reason); private: + //! \private friend BlackFsdTest::CTestFSDClient; //! Send FSD message @@ -283,6 +284,7 @@ namespace BlackCore QString socketErrorToQString(QAbstractSocket::SocketError error); void parseMessage(const QString &line); + //! Init. the message types void initializeMessageTypes(); //! Handle response tokens @{ @@ -342,10 +344,11 @@ namespace BlackCore bool isInterimPositionReceivingEnabledForServer() const; const BlackMisc::Network::CFsdSetup &getSetupForServer() const; - //! Handles ATIS replies from non-VATSIM servers. If the conditions are not met, the message is - //! released as normal text message. + //! Handles ATIS replies from non-VATSIM servers. If the conditions are not met, + //! the message is released as normal text message. void maybeHandleAtisReply(const BlackMisc::Aviation::CCallsign &sender, const BlackMisc::Aviation::CCallsign &receiver, const QString &message); + //! Settings have been changed void fsdMessageSettingsChanged(); //! Emit raw FSD message (mostly for debugging) @@ -364,8 +367,12 @@ namespace BlackCore void stopPositionTimers(); //! @} + //! Update the ATIS map void updateAtisMap(const QString &callsign, AtisLineType type, const QString &line); + //! Check if there is a pending logon attempt which "hangs" + void pendingTimeoutCheck(); + //! Fix ATC station range static const BlackMisc::PhysicalQuantities::CLength &fixAtcRange(const BlackMisc::PhysicalQuantities::CLength &networkRange, const BlackMisc::Aviation::CCallsign &cs); @@ -387,6 +394,8 @@ namespace BlackCore vatsim_auth *clientAuth = nullptr; vatsim_auth *serverAuth = nullptr; QString m_lastServerAuthChallenge; + qint64 m_loginSince = -1; //!< when login was triggered + static constexpr qint64 PendingConnectionTimeoutMs = 7500; // User data BlackMisc::Network::CServer m_server; @@ -399,7 +408,7 @@ namespace BlackCore // Parser QHash m_messageTypeMapping; - QTcpSocket m_socket; + QTcpSocket m_socket; //!< used TCP socket bool m_unitTestMode = false; bool m_printToConsole = false;