mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-13 15:45:42 +08:00
Adjust rehosting with automatic server
This commit is contained in:
@@ -269,24 +269,7 @@ namespace BlackCore::Fsd
|
|||||||
|
|
||||||
this->updateConnectionStatus(CConnectionStatus::Connecting);
|
this->updateConnectionStatus(CConnectionStatus::Connecting);
|
||||||
|
|
||||||
const CServer s = this->getServer();
|
initiateConnection();
|
||||||
|
|
||||||
QHostAddress serverAddress(s.getAddress());
|
|
||||||
|
|
||||||
if (serverAddress.isNull() && s.getName() == "AUTOMATIC" && s.getEcosystem() == CEcosystem::VATSIM)
|
|
||||||
{
|
|
||||||
// Not an IP -> Get IP for loadbalancing via HTTP
|
|
||||||
Q_ASSERT_X(sApp, Q_FUNC_INFO, "Need app");
|
|
||||||
CUrl url = sApp->getVatsimFsdHttpUrl();
|
|
||||||
sApp->getFromNetwork(url, { this, &CFSDClient::handleVatsimServerIpResponse });
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
const QString host = s.getAddress();
|
|
||||||
const quint16 port = static_cast<quint16>(s.getPort());
|
|
||||||
m_socket->connectToHost(host, port);
|
|
||||||
this->startPositionTimers();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CFSDClient::handleVatsimServerIpResponse(QNetworkReply *nwReplyPtr)
|
void CFSDClient::handleVatsimServerIpResponse(QNetworkReply *nwReplyPtr)
|
||||||
@@ -303,9 +286,16 @@ namespace BlackCore::Fsd
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const quint16 port = static_cast<quint16>(s.getPort());
|
if (m_rehosting)
|
||||||
m_socket->connectToHost(host, port);
|
{
|
||||||
this->startPositionTimers();
|
m_rehosting_socket->connectToHost(host, m_socket->peerPort());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const quint16 port = static_cast<quint16>(s.getPort());
|
||||||
|
m_socket->connectToHost(host, port);
|
||||||
|
this->startPositionTimers();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CFSDClient::disconnectFromServer()
|
void CFSDClient::disconnectFromServer()
|
||||||
@@ -1687,26 +1677,63 @@ namespace BlackCore::Fsd
|
|||||||
|
|
||||||
CLogMessage(this).info(u"Server requested we switch server to %1") << rehost.m_hostname;
|
CLogMessage(this).info(u"Server requested we switch server to %1") << rehost.m_hostname;
|
||||||
|
|
||||||
|
Q_ASSERT_X(m_rehosting_host.isEmpty(), Q_FUNC_INFO, "Rehosting already in progress");
|
||||||
|
Q_ASSERT_X(!m_rehosting_socket, Q_FUNC_INFO, "Rehosting already in progress");
|
||||||
|
|
||||||
|
m_rehosting_host = rehost.m_hostname;
|
||||||
m_rehosting = true;
|
m_rehosting = true;
|
||||||
auto newSocket = new QTcpSocket(this);
|
m_rehosting_socket = new QTcpSocket(this);
|
||||||
connect(newSocket, &QTcpSocket::connected, this, [this, newSocket]
|
connect(m_rehosting_socket, &QTcpSocket::connected, this, [this]
|
||||||
{
|
{
|
||||||
readDataFromSocket();
|
readDataFromSocket();
|
||||||
CLogMessage(this).debug(u"Successfully switched server");
|
CLogMessage(this).debug(u"Successfully switched server");
|
||||||
QObject::disconnect(newSocket);
|
QObject::disconnect(m_rehosting_socket);
|
||||||
m_socket.reset(newSocket);
|
m_socket.reset(m_rehosting_socket);
|
||||||
|
m_rehosting_socket = nullptr;
|
||||||
m_rehosting = false;
|
m_rehosting = false;
|
||||||
|
m_rehosting_host = "";
|
||||||
connectSocketSignals();
|
connectSocketSignals();
|
||||||
readDataFromSocket();
|
readDataFromSocket();
|
||||||
});
|
});
|
||||||
connect(newSocket, &QTcpSocket::errorOccurred, this, [this, newSocket]
|
connect(m_rehosting_socket, &QTcpSocket::errorOccurred, this, [this]
|
||||||
{
|
{
|
||||||
CLogMessage(this).warning(u"Failed to switch server: %1") << newSocket->errorString();
|
CLogMessage(this).warning(u"Failed to switch server: %1") << m_rehosting_socket->errorString();
|
||||||
m_rehosting = false;
|
m_rehosting = false;
|
||||||
delete newSocket;
|
delete m_rehosting_socket;
|
||||||
|
m_rehosting_host = "";
|
||||||
if (m_socket->state() != QAbstractSocket::ConnectedState) { updateConnectionStatus(CConnectionStatus::Disconnected); }
|
if (m_socket->state() != QAbstractSocket::ConnectedState) { updateConnectionStatus(CConnectionStatus::Disconnected); }
|
||||||
});
|
});
|
||||||
newSocket->connectToHost(rehost.m_hostname, m_socket->peerPort());
|
|
||||||
|
initiateConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CFSDClient::initiateConnection()
|
||||||
|
{
|
||||||
|
const CServer s = this->getServer();
|
||||||
|
|
||||||
|
QHostAddress serverAddress(m_rehosting ? m_rehosting_host : s.getAddress());
|
||||||
|
|
||||||
|
if (serverAddress.isNull() && (s.getName() == "AUTOMATIC" || m_rehosting) && s.getEcosystem() == CEcosystem::VATSIM)
|
||||||
|
{
|
||||||
|
// Not an IP -> Get IP for loadbalancing via HTTP
|
||||||
|
Q_ASSERT_X(sApp, Q_FUNC_INFO, "Need app");
|
||||||
|
CUrl url = sApp->getVatsimFsdHttpUrl();
|
||||||
|
sApp->getFromNetwork(url, { this, &CFSDClient::handleVatsimServerIpResponse });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (m_rehosting)
|
||||||
|
{
|
||||||
|
m_rehosting_socket->connectToHost(m_rehosting_host, m_socket->peerPort());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const QString host = s.getAddress();
|
||||||
|
const quint16 port = static_cast<quint16>(s.getPort());
|
||||||
|
m_socket->connectToHost(host, port);
|
||||||
|
this->startPositionTimers();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CFSDClient::handleCustomPilotPacket(const QStringList &tokens)
|
void CFSDClient::handleCustomPilotPacket(const QStringList &tokens)
|
||||||
|
|||||||
@@ -481,7 +481,7 @@ namespace BlackCore::Fsd
|
|||||||
|
|
||||||
std::unique_ptr<QTcpSocket> m_socket = std::make_unique<QTcpSocket>(this); //!< used TCP socket, parent needed as it runs in worker thread
|
std::unique_ptr<QTcpSocket> m_socket = std::make_unique<QTcpSocket>(this); //!< used TCP socket, parent needed as it runs in worker thread
|
||||||
void connectSocketSignals();
|
void connectSocketSignals();
|
||||||
bool m_rehosting = false;
|
void initiateConnection();
|
||||||
|
|
||||||
std::atomic_bool m_unitTestMode { false };
|
std::atomic_bool m_unitTestMode { false };
|
||||||
std::atomic_bool m_printToConsole { false };
|
std::atomic_bool m_printToConsole { false };
|
||||||
@@ -558,6 +558,11 @@ namespace BlackCore::Fsd
|
|||||||
ServerType m_serverType = ServerType::LegacyFsd;
|
ServerType m_serverType = ServerType::LegacyFsd;
|
||||||
Capabilities m_capabilities = Capabilities::None;
|
Capabilities m_capabilities = Capabilities::None;
|
||||||
|
|
||||||
|
// Current rehosting
|
||||||
|
QTcpSocket* m_rehosting_socket = nullptr;
|
||||||
|
QString m_rehosting_host = "";
|
||||||
|
bool m_rehosting = false;
|
||||||
|
|
||||||
// buffered data for FSD
|
// buffered data for FSD
|
||||||
BlackMisc::Aviation::CCallsign m_ownCallsign; //!< "buffered callsign", as this must not change when connected
|
BlackMisc::Aviation::CCallsign m_ownCallsign; //!< "buffered callsign", as this must not change when connected
|
||||||
BlackMisc::Aviation::CCallsign m_partnerCallsign; //!< "buffered"callsign", of partner flying in shared cockpit
|
BlackMisc::Aviation::CCallsign m_partnerCallsign; //!< "buffered"callsign", of partner flying in shared cockpit
|
||||||
|
|||||||
Reference in New Issue
Block a user