Make FS9 works with ISimulatorListener

* The FS9 host is started in the Listener
* When FS9 starts up, it is called via the lobby to join the multiplayer
* Some minor changes in CContextSimulator
This commit is contained in:
Michał Garapich
2015-03-08 22:58:42 +01:00
committed by Roland Winklmeier
parent edc0646ab2
commit 570f4aa4ea
9 changed files with 196 additions and 125 deletions

View File

@@ -291,8 +291,14 @@ namespace BlackSimPlugin
DPNSEND_SYNC | DPNSEND_NOLOOPBACK)))
{
CLogMessage(this).warning("DirectPlay: Failed to send message!");
qDebug() << message;
}
return hr;
}
void CDirectPlayPeer::reset()
{
m_playerUser = 0;
}
}
}

View File

@@ -48,6 +48,8 @@ namespace BlackSimPlugin
//! Send a custom DirectPlay message
HRESULT sendMessage(const QByteArray &data);
void reset();
signals:
//! Received custom FS9 packet
void customPacketReceived(const QByteArray &data);

View File

@@ -72,6 +72,7 @@ namespace BlackSimPlugin
MultiPlayerPacketParser::writeType(message, CFs9Sdk::MPCHAT_PACKET_ID_CHAT_TEXT_SEND);
MultiPlayerPacketParser::writeSize(message, mpChatText.chat_data.size() + 1);
message = MultiPlayerPacketParser::writeMessage(message, mpChatText);
qDebug() << "Message:" << textMessage;
sendMessage(message);
}
@@ -87,7 +88,6 @@ namespace BlackSimPlugin
stopHosting();
}
HRESULT CFs9Host::startHosting(const QString &session, const QString &callsign)
{
HRESULT hr = S_OK;

View File

@@ -92,9 +92,6 @@ namespace BlackSimPlugin
GUID pAppGuid = CFs9Sdk::guid();
// Set to true in order to automatically launch FS9. Perfect for testing.
bool bLaunchNotFound = false;
// Setup the DPL_CONNECT_INFO struct
DPL_CONNECT_INFO dnConnectInfo;
ZeroMemory(&dnConnectInfo, sizeof(DPL_CONNECT_INFO));
@@ -102,7 +99,6 @@ namespace BlackSimPlugin
dnConnectInfo.pvLobbyConnectData = nullptr;
dnConnectInfo.dwLobbyConnectDataSize = 0;
dnConnectInfo.dwFlags = 0;
if (bLaunchNotFound) dnConnectInfo.dwFlags |= DPLCONNECT_LAUNCHNOTFOUND;
dnConnectInfo.guidApplication = pAppGuid;
if (FAILED(hr = allocAndInitConnectSettings(address, &pAppGuid, &dnConnectInfo.pdplConnectionSettings)))
@@ -113,21 +109,18 @@ namespace BlackSimPlugin
&m_applicationHandle,
INFINITE,
0);
if (FAILED(hr))
{
if (hr == DPNERR_NOCONNECTION && !bLaunchNotFound)
qWarning() << "There were no waiting application.";
else
return printDirectPlayError(hr);
}
else
{
if (FAILED(hr)) {
return hr == DPNERR_NOCONNECTION ? S_FALSE : printDirectPlayError(hr);
} else {
qDebug() << "Connected!";
freeConnectSettings(dnConnectInfo.pdplConnectionSettings);
return S_OK;
}
}
freeConnectSettings(dnConnectInfo.pdplConnectionSettings);
void CLobbyClient::cleanup()
{
return S_OK;
}
HRESULT CLobbyClient::allocAndInitConnectSettings(const QString &address, GUID *pAppGuid, DPL_CONNECTION_SETTINGS **ppdplConnectSettings)
@@ -247,7 +240,9 @@ namespace BlackSimPlugin
{
PDPL_MESSAGE_DISCONNECT pDisconnectMsg;
pDisconnectMsg = (PDPL_MESSAGE_DISCONNECT)msgBuffer;
Q_UNUSED(pDisconnectMsg)
Q_UNUSED(pDisconnectMsg);
emit disconnected();
// We should free any data associated with the
// app here, but there is none.

View File

@@ -13,6 +13,10 @@ namespace BlackSimPlugin
{
Q_OBJECT
signals:
//! Emitted when FS9 is closed
void disconnected();
public:
//! Constructor
@@ -27,6 +31,9 @@ namespace BlackSimPlugin
//! Connect FS9 simulator to our host
HRESULT connectFs9ToHost(const QString address);
//! Cleanup & be ready to another connection
void cleanup();
private:
//! Alloc and fill up a DPL_CONNECTION_SETTINGS. Call FreeConnectSettings later to free it.

View File

@@ -16,6 +16,7 @@
#include "multiplayer_packet_parser.h"
#include "blackcore/interpolator_linear.h"
#include "blacksim/simulatorinfo.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/project.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/propertyindexallclasses.h"
@@ -37,36 +38,49 @@ namespace BlackSimPlugin
{
namespace Fs9
{
CSimulatorFs9Factory::CSimulatorFs9Factory(QObject *parent) :
QObject(parent),
m_fs9Host(new CFs9Host(this), [](CFs9Host* host){
host->quit();
host->deleteLater();
}),
m_lobbyClient(new CLobbyClient(this))
{
registerMetadata();
}
BlackCore::ISimulator *CSimulatorFs9Factory::create(
IOwnAircraftProvider *ownAircraftProvider,
IRemoteAircraftProvider *remoteAircraftProvider,
QObject *parent)
{
registerMetadata();
return new Fs9::CSimulatorFs9(ownAircraftProvider, remoteAircraftProvider, parent);
}
BlackCore::ISimulatorListener *CSimulatorFs9Factory::createListener(QObject *parent)
{
return new CSimulatorFs9Listener(m_fs9Host, m_lobbyClient, parent);
}
BlackSim::CSimulatorInfo CSimulatorFs9Factory::getSimulatorInfo() const
{
return CSimulatorInfo::FS9();
}
CSimulatorFs9::CSimulatorFs9(IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent) :
CSimulatorFsCommon(CSimulatorInfo::FS9(), ownAircraftProvider, remoteAircraftProvider, parent),
m_fs9Host(new CFs9Host(this)), m_lobbyClient(new CLobbyClient(this))
CSimulatorFs9::CSimulatorFs9(IOwnAircraftProvider *ownAircraftProvider,
IRemoteAircraftProvider *remoteAircraftProvider,
const QSharedPointer<CFs9Host> &fs9Host,
const QSharedPointer<CLobbyClient> &lobbyClient, QObject *parent) :
CSimulatorFsCommon(CSimulatorInfo::FS9(), ownAircraftProvider, remoteAircraftProvider, parent),
m_fs9Host(new CFs9Host(this)),
m_lobbyClient(new CLobbyClient(this))
{
connect(m_lobbyClient.data(), &CLobbyClient::disconnected, this, std::bind(&CSimulatorFs9::simulatorStatusChanged, this, 0));
connect(m_fs9Host.data(), &CFs9Host::customPacketReceived, this, &CSimulatorFs9::ps_processFs9Message);
connect(m_fs9Host.data(), &CFs9Host::statusChanged, this, &CSimulatorFs9::ps_changeHostStatus);
m_fs9Host->start();
this->m_interpolator = new BlackCore::CInterpolatorLinear(remoteAircraftProvider, this);
this->m_interpolator->start();
}
CSimulatorFs9::~CSimulatorFs9()
{
Q_ASSERT(!m_isHosting);
}
bool CSimulatorFs9::isConnected() const
{
return m_fs9Host->isConnected();
@@ -74,19 +88,11 @@ namespace BlackSimPlugin
bool CSimulatorFs9::connectTo()
{
Q_ASSERT(m_fsuipc);
if (m_useFsuipc) { m_fsuipc->connect(); } // connect FSUIPC too
Q_ASSERT(m_fs9Host->isConnected());
m_fsuipc->connect(); // connect FSUIPC too
startTimer(50);
emitSimulatorCombinedStatus();
// If we are already hosting, connect FS0 through lobby connection
if (m_isHosting)
{
m_lobbyClient->connectFs9ToHost(m_fs9Host->getHostAddress());
}
// If not, deferre connection until host is setup
else
{
m_startedLobbyConnection = true;
}
return true;
}
@@ -99,11 +105,7 @@ namespace BlackSimPlugin
bool CSimulatorFs9::disconnectFrom()
{
disconnectAllClients();
emit connectionStatusChanged(ISimulator::Disconnected);
if (m_fs9Host) { m_fs9Host->quit(); }
CSimulatorFsCommon::disconnectFrom();
m_isHosting = false;
m_fsuipc->disconnect();
return true;
}
@@ -199,12 +201,13 @@ namespace BlackSimPlugin
void CSimulatorFs9::displayStatusMessage(const BlackMisc::CStatusMessage &message) const
{
/* Avoid errors from CDirectPlayPeer as it may end in infinite loop */
if (message.getSeverity() == BlackMisc::CStatusMessage::SeverityError && message.isFromClass<CDirectPlayPeer>())
return;
if (message.getSeverity() != BlackMisc::CStatusMessage::SeverityDebug)
{
if (m_fs9Host)
{
QMetaObject::invokeMethod(m_fs9Host, "sendTextMessage", Q_ARG(QString, message.toQString()));
}
QMetaObject::invokeMethod(m_fs9Host.data(), "sendTextMessage", Q_ARG(QString, message.toQString()));
}
}
@@ -272,34 +275,7 @@ namespace BlackSimPlugin
}
}
void CSimulatorFs9::ps_changeHostStatus(BlackSimPlugin::Fs9::CFs9Host::HostStatus status)
{
switch (status)
{
case CFs9Host::Hosting:
{
m_isHosting = true;
startTimer(50);
emit connectionStatusChanged(Connected);
if (m_startedLobbyConnection)
{
m_lobbyClient->connectFs9ToHost(m_fs9Host->getHostAddress());
m_startedLobbyConnection = false;
}
break;
}
case CFs9Host::Terminated:
{
m_isHosting = false;
emit connectionStatusChanged(Disconnected);
break;
}
default:
break;
}
}
void CSimulatorFs9::updateOwnAircraftFromSimulator(const CAircraft &simDataOwnAircraft)
void CSimulatorFs9::updateOwnAircraftFromSim(const CAircraft &ownAircraft)
{
this->providerUpdateCockpit(
simDataOwnAircraft.getCom1System(),
@@ -316,5 +292,48 @@ namespace BlackSimPlugin
removeRemoteAircraft(fs9Client);
}
}
} // namespace
} // namespace
CSimulatorFs9Listener::CSimulatorFs9Listener(const QSharedPointer<CFs9Host> &fs9Host,
const QSharedPointer<CLobbyClient> &lobbyClient,
QObject *parent) :
BlackCore::ISimulatorListener(parent),
m_timer(new QTimer(this)),
m_fs9Host(fs9Host),
m_lobbyClient(lobbyClient)
{
Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds
m_timer->setInterval(QueryInterval);
connect(m_timer, &QTimer::timeout, [this]()
{
if (m_fs9Host->getHostAddress().isEmpty()) // host not yet set up
return;
if (m_lobbyConnected || m_lobbyClient->connectFs9ToHost(m_fs9Host->getHostAddress()) == S_OK) {
m_lobbyConnected = true;
CLogMessage(this).info("Swift is joining FS9 to the multiplayer session...");
}
if (m_lobbyConnected && m_fs9Host->isConnected()) {
emit simulatorStarted(m_simulatorInfo);
m_lobbyConnected = false;
}
});
m_fs9Host->start();
// After FS9 is disconnected, reset its data stored in the host
connect(m_lobbyClient.data(), &CLobbyClient::disconnected, m_fs9Host.data(), &CFs9Host::reset);
}
void CSimulatorFs9Listener::start()
{
m_timer->start();
}
void CSimulatorFs9Listener::stop()
{
m_timer->stop();
}
}
}

View File

@@ -32,24 +32,6 @@ namespace BlackSimPlugin
{
namespace Fs9
{
//! Factory implementation to create CSimulatorFs9 instances
class CSimulatorFs9Factory : public QObject, public BlackCore::ISimulatorFactory
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.swift.pilotclient.BlackCore.SimulatorInterface" FILE "simulator_fs9.json")
Q_INTERFACES(BlackCore::ISimulatorFactory)
public:
//! \copydoc BlackCore::ISimulatorFactory::create(ownAircraftProvider, remoteAircraftProvider, parent)
virtual BlackCore::ISimulator *create(
BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider,
BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider,
QObject *parent) override;
//! Simulator info
virtual BlackSim::CSimulatorInfo getSimulatorInfo() const override;
};
//! FSX Simulator Implementation
class CSimulatorFs9 : public BlackSimPlugin::FsCommon::CSimulatorFsCommon
{
@@ -63,7 +45,7 @@ namespace BlackSimPlugin
QObject *parent = nullptr);
//! Destructor
virtual ~CSimulatorFs9();
virtual ~CSimulatorFs9() = default;
//! \copydoc ISimulator::isConnected()
virtual bool isConnected() const override;
@@ -116,9 +98,6 @@ namespace BlackSimPlugin
//! Process incoming FS9 message
void ps_processFs9Message(const QByteArray &message);
//! Change DirectPlay host status
void ps_changeHostStatus(BlackSimPlugin::Fs9::CFs9Host::HostStatus status);
private:
//! Called when data about our own aircraft are received
@@ -126,14 +105,63 @@ namespace BlackSimPlugin
void disconnectAllClients();
// DirectPlay object handling
QPointer<CFs9Host> m_fs9Host;
bool m_isHosting = false; //!< Is sim connected?
bool m_startedLobbyConnection = false;
QSharedPointer<CFs9Host> m_fs9Host;
QHash<BlackMisc::Aviation::CCallsign, QPointer<CFs9Client>> m_hashFs9Clients;
CLobbyClient *m_lobbyClient;
QSharedPointer<CLobbyClient> m_lobbyClient;
};
} // namespace
} // namespace
//! Listener for FS9
//! Listener starts the FS9 multiplayer host and tries to make the running instance
//! of simulator to connect to it. When emitting the simulatorStarted() signal,
//! FS9 is already connected.
class CSimulatorFs9Listener : public BlackCore::ISimulatorListener {
Q_OBJECT
public:
//! Constructor
CSimulatorFs9Listener(const QSharedPointer<CFs9Host> &fs9Host, const QSharedPointer<CLobbyClient> &lobbyClient, QObject* parent);
//! \copydoc BlackCore::ISimulatorListener::start
virtual void start() override;
//! \copydoc BlackCore::ISimulatorListener::stop
virtual void stop() override;
private:
QTimer* m_timer = nullptr;
QSharedPointer<CFs9Host> m_fs9Host;
QSharedPointer<CLobbyClient> m_lobbyClient;
bool m_lobbyConnected = false;
const BlackSim::CSimulatorInfo m_simulatorInfo = BlackSim::CSimulatorInfo::FS9();
};
//! Factory implementation to create CSimulatorFs9 instances
class CSimulatorFs9Factory : public QObject, public BlackCore::ISimulatorFactory
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.swift.pilotclient.BlackCore.SimulatorInterface" FILE "simulator_fs9.json")
Q_INTERFACES(BlackCore::ISimulatorFactory)
public:
CSimulatorFs9Factory(QObject* parent = nullptr);
//! \copydoc BlackCore::ISimulatorFactory::create()
virtual BlackCore::ISimulator *create(QObject *parent) override;
//! Simulator info
virtual BlackSim::CSimulatorInfo getSimulatorInfo() const override;
//! \copydoc BlackCore::ISimulatorFactory::getListener
virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override;
private:
QSharedPointer<CFs9Host> m_fs9Host;
QSharedPointer<CLobbyClient> m_lobbyClient;
};
} // namespace Fs9
} // namespace BlackCore
#endif // guard

View File

@@ -54,6 +54,9 @@ namespace BlackSimPlugin
//!
bool read(BlackMisc::Simulation::CSimulatedAircraft &aircraft, bool cockpit, bool situation, bool aircraftParts);
//! Find out whether we can connect to FSUIPC or not
static bool canConnect();
//! Error messages
static const QStringList &errorMessages()
{