@@ -269,24 +269,7 @@ namespace BlackCore::Fsd
269269
270270 this ->updateConnectionStatus (CConnectionStatus::Connecting);
271271
272- const CServer s = this ->getServer ();
273-
274- QHostAddress serverAddress (s.getAddress ());
275-
276- if (serverAddress.isNull () && s.getName () == " AUTOMATIC" && s.getEcosystem () == CEcosystem::VATSIM)
277- {
278- // Not an IP -> Get IP for loadbalancing via HTTP
279- Q_ASSERT_X (sApp , Q_FUNC_INFO, " Need app" );
280- CUrl url = sApp ->getVatsimFsdHttpUrl ();
281- sApp ->getFromNetwork (url, { this , &CFSDClient::handleVatsimServerIpResponse });
282- }
283- else
284- {
285- const QString host = s.getAddress ();
286- const quint16 port = static_cast <quint16>(s.getPort ());
287- m_socket->connectToHost (host, port);
288- this ->startPositionTimers ();
289- }
272+ initiateConnection ();
290273 }
291274
292275 void CFSDClient::handleVatsimServerIpResponse (QNetworkReply *nwReplyPtr)
@@ -303,9 +286,16 @@ namespace BlackCore::Fsd
303286
304287 }
305288
306- const quint16 port = static_cast <quint16>(s.getPort ());
307- m_socket->connectToHost (host, port);
308- this ->startPositionTimers ();
289+ if (m_rehosting)
290+ {
291+ m_rehosting_socket->connectToHost (host, m_socket->peerPort ());
292+ }
293+ else
294+ {
295+ const quint16 port = static_cast <quint16>(s.getPort ());
296+ m_socket->connectToHost (host, port);
297+ this ->startPositionTimers ();
298+ }
309299 }
310300
311301 void CFSDClient::disconnectFromServer ()
@@ -1687,26 +1677,63 @@ namespace BlackCore::Fsd
16871677
16881678 CLogMessage (this ).info (u" Server requested we switch server to %1" ) << rehost.m_hostname ;
16891679
1680+ Q_ASSERT_X (m_rehosting_host.isEmpty (), Q_FUNC_INFO, " Rehosting already in progress" );
1681+ Q_ASSERT_X (!m_rehosting_socket, Q_FUNC_INFO, " Rehosting already in progress" );
1682+
1683+ m_rehosting_host = rehost.m_hostname ;
16901684 m_rehosting = true ;
1691- auto newSocket = new QTcpSocket (this );
1692- connect (newSocket , &QTcpSocket::connected, this , [this , newSocket ]
1685+ m_rehosting_socket = new QTcpSocket (this );
1686+ connect (m_rehosting_socket , &QTcpSocket::connected, this , [this ]
16931687 {
16941688 readDataFromSocket ();
16951689 CLogMessage (this ).debug (u" Successfully switched server" );
1696- QObject::disconnect (newSocket);
1697- m_socket.reset (newSocket);
1690+ QObject::disconnect (m_rehosting_socket);
1691+ m_socket.reset (m_rehosting_socket);
1692+ m_rehosting_socket = nullptr ;
16981693 m_rehosting = false ;
1694+ m_rehosting_host = " " ;
16991695 connectSocketSignals ();
17001696 readDataFromSocket ();
17011697 });
1702- connect (newSocket , &QTcpSocket::errorOccurred, this , [this , newSocket ]
1698+ connect (m_rehosting_socket , &QTcpSocket::errorOccurred, this , [this ]
17031699 {
1704- CLogMessage (this ).warning (u" Failed to switch server: %1" ) << newSocket ->errorString ();
1700+ CLogMessage (this ).warning (u" Failed to switch server: %1" ) << m_rehosting_socket ->errorString ();
17051701 m_rehosting = false ;
1706- delete newSocket;
1702+ delete m_rehosting_socket;
1703+ m_rehosting_host = " " ;
17071704 if (m_socket->state () != QAbstractSocket::ConnectedState) { updateConnectionStatus (CConnectionStatus::Disconnected); }
17081705 });
1709- newSocket->connectToHost (rehost.m_hostname , m_socket->peerPort ());
1706+
1707+ initiateConnection ();
1708+ }
1709+
1710+ void CFSDClient::initiateConnection ()
1711+ {
1712+ const CServer s = this ->getServer ();
1713+
1714+ QHostAddress serverAddress (m_rehosting ? m_rehosting_host : s.getAddress ());
1715+
1716+ if (serverAddress.isNull () && (s.getName () == " AUTOMATIC" || m_rehosting) && s.getEcosystem () == CEcosystem::VATSIM)
1717+ {
1718+ // Not an IP -> Get IP for loadbalancing via HTTP
1719+ Q_ASSERT_X (sApp , Q_FUNC_INFO, " Need app" );
1720+ CUrl url = sApp ->getVatsimFsdHttpUrl ();
1721+ sApp ->getFromNetwork (url, { this , &CFSDClient::handleVatsimServerIpResponse });
1722+ }
1723+ else
1724+ {
1725+ if (m_rehosting)
1726+ {
1727+ m_rehosting_socket->connectToHost (m_rehosting_host, m_socket->peerPort ());
1728+ }
1729+ else
1730+ {
1731+ const QString host = s.getAddress ();
1732+ const quint16 port = static_cast <quint16>(s.getPort ());
1733+ m_socket->connectToHost (host, port);
1734+ this ->startPositionTimers ();
1735+ }
1736+ }
17101737 }
17111738
17121739 void CFSDClient::handleCustomPilotPacket (const QStringList &tokens)
0 commit comments