diff --git a/src/plugins/simulator/fs9/lobby_client.cpp b/src/plugins/simulator/fs9/lobby_client.cpp index db64445ee..af2bb5a94 100644 --- a/src/plugins/simulator/fs9/lobby_client.cpp +++ b/src/plugins/simulator/fs9/lobby_client.cpp @@ -12,6 +12,7 @@ #include "blackmisc/project.h" #include "fs9.h" #include "lobby_client.h" +#include "blackmisc/logmessage.h" #include #include #include @@ -86,6 +87,35 @@ namespace BlackSimPlugin return S_OK; } + bool CLobbyClient::canLobbyConnect() + { + GUID appGuid = CFs9Sdk::guid(); + DWORD dwSize = 0; + DWORD dwItems = 0; + HRESULT hr = m_dpLobbyClient->EnumLocalPrograms(&appGuid, nullptr, &dwSize, &dwItems, 0); + if (hr == DPNERR_BUFFERTOOSMALL) + { + QScopedArrayPointer memPtr (new BYTE[dwSize]); + DPL_APPLICATION_INFO *appInfo = reinterpret_cast(memPtr.data()); + + m_dpLobbyClient->EnumLocalPrograms(&appGuid, memPtr.data(), &dwSize, &dwItems, 0); + + if (dwItems > 0) + { + BlackMisc::CLogMessage(this).debug() << "Found lobby application:" << QString::fromWCharArray(appInfo->pwszApplicationName);; + return true; + } + else + { + return false; + } + } + else + { + return false; + } + } + HRESULT CLobbyClient::connectFs9ToHost(const QString address) { HRESULT hr = S_OK; @@ -109,9 +139,12 @@ namespace BlackSimPlugin &m_applicationHandle, INFINITE, 0); - if (FAILED(hr)) { - return hr == DPNERR_NOCONNECTION ? S_FALSE : printDirectPlayError(hr); - } else { + if (FAILED(hr)) + { + return hr; + } + else + { qDebug() << "Connected!"; freeConnectSettings(dnConnectInfo.pdplConnectionSettings); return S_OK; diff --git a/src/plugins/simulator/fs9/lobby_client.h b/src/plugins/simulator/fs9/lobby_client.h index 6a5a416ff..1b4f1928a 100644 --- a/src/plugins/simulator/fs9/lobby_client.h +++ b/src/plugins/simulator/fs9/lobby_client.h @@ -28,6 +28,9 @@ namespace BlackSimPlugin //! Initialize DirectPlay HRESULT initDirectPlay(); + //! Can FS9 be lobby connected? + bool canLobbyConnect(); + //! Connect FS9 simulator to our host HRESULT connectFs9ToHost(const QString address); diff --git a/src/plugins/simulator/fs9/simulator_fs9.cpp b/src/plugins/simulator/fs9/simulator_fs9.cpp index 9f4881f82..3edd45b26 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.cpp +++ b/src/plugins/simulator/fs9/simulator_fs9.cpp @@ -290,27 +290,35 @@ namespace BlackSimPlugin Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds m_timer->setInterval(QueryInterval); - connect(m_timer, &QTimer::timeout, [this]() + // Test whether we can lobby connect at all. + bool canLobbyConnect = lobbyClient->canLobbyConnect(); + + connect(m_timer, &QTimer::timeout, [this, canLobbyConnect]() { if (fs9Host->getHostAddress().isEmpty()) // host not yet set up return; - if (m_lobbyConnected || lobbyClient->connectFs9ToHost(fs9Host->getHostAddress()) == S_OK) + if (canLobbyConnect) { - m_lobbyConnected = true; - CLogMessage(this).info("Swift is joining FS9 to the multiplayer session..."); + if (m_isConnecting || lobbyClient->connectFs9ToHost(fs9Host->getHostAddress()) == S_OK) + { + m_isConnecting = true; + CLogMessage(this).info("Swift is joining FS9 to the multiplayer session..."); + } } - if (m_lobbyConnected && fs9Host->isConnected()) + if (!m_isStarted && fs9Host->isConnected()) { emit simulatorStarted(); - m_lobbyConnected = false; + m_isStarted = true; + m_isConnecting = false; } }); } void CSimulatorFs9Listener::start() { + m_isStarted = false; m_timer->start(); } diff --git a/src/plugins/simulator/fs9/simulator_fs9.h b/src/plugins/simulator/fs9/simulator_fs9.h index f1ef9930b..9ceaec735 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.h +++ b/src/plugins/simulator/fs9/simulator_fs9.h @@ -131,7 +131,8 @@ namespace BlackSimPlugin private: QTimer *m_timer = nullptr; - bool m_lobbyConnected = false; + bool m_isConnecting = false; + bool m_isStarted = false; };