2018-06-24 22:13:37 +00:00
|
|
|
#pragma once
|
|
|
|
#include "nvhttp.h"
|
2018-07-06 06:12:55 +00:00
|
|
|
#include "nvpairingmanager.h"
|
2018-06-24 22:13:37 +00:00
|
|
|
|
2018-07-01 06:07:31 +00:00
|
|
|
#include <qmdnsengine/server.h>
|
|
|
|
#include <qmdnsengine/cache.h>
|
|
|
|
#include <qmdnsengine/browser.h>
|
|
|
|
#include <qmdnsengine/service.h>
|
|
|
|
#include <qmdnsengine/resolver.h>
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
#include <QThread>
|
|
|
|
#include <QReadWriteLock>
|
2018-06-27 07:43:46 +00:00
|
|
|
#include <QSettings>
|
2018-07-06 06:12:55 +00:00
|
|
|
#include <QRunnable>
|
2018-06-24 22:13:37 +00:00
|
|
|
|
|
|
|
class NvComputer
|
|
|
|
{
|
2018-07-06 03:54:31 +00:00
|
|
|
friend class PcMonitorThread;
|
2018-08-01 05:21:39 +00:00
|
|
|
friend class ComputerManager;
|
|
|
|
friend class PendingQuitTask;
|
2018-07-06 03:54:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void sortAppList();
|
|
|
|
|
2018-08-01 05:21:39 +00:00
|
|
|
bool pendingQuit;
|
|
|
|
|
2018-06-24 22:13:37 +00:00
|
|
|
public:
|
2018-06-28 02:55:44 +00:00
|
|
|
explicit NvComputer(QString address, QString serverInfo);
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-06-28 02:55:44 +00:00
|
|
|
explicit NvComputer(QSettings& settings);
|
2018-06-27 07:43:46 +00:00
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
bool
|
|
|
|
update(NvComputer& that);
|
|
|
|
|
2018-07-04 05:11:21 +00:00
|
|
|
bool
|
|
|
|
wake();
|
|
|
|
|
|
|
|
QVector<QString>
|
|
|
|
uniqueAddresses();
|
|
|
|
|
2018-06-27 07:43:46 +00:00
|
|
|
void
|
|
|
|
serialize(QSettings& settings);
|
|
|
|
|
2018-06-24 22:13:37 +00:00
|
|
|
enum PairState
|
|
|
|
{
|
|
|
|
PS_UNKNOWN,
|
|
|
|
PS_PAIRED,
|
|
|
|
PS_NOT_PAIRED
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ComputerState
|
|
|
|
{
|
|
|
|
CS_UNKNOWN,
|
|
|
|
CS_ONLINE,
|
|
|
|
CS_OFFLINE
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ephemeral traits
|
|
|
|
ComputerState state;
|
|
|
|
PairState pairState;
|
|
|
|
QString activeAddress;
|
|
|
|
int currentGameId;
|
2018-06-28 08:44:43 +00:00
|
|
|
QString gfeVersion;
|
|
|
|
QString appVersion;
|
2018-07-09 03:53:24 +00:00
|
|
|
QVector<NvDisplayMode> displayModes;
|
|
|
|
int maxLumaPixelsHEVC;
|
|
|
|
int serverCodecModeSupport;
|
2018-06-24 22:13:37 +00:00
|
|
|
|
|
|
|
// Persisted traits
|
|
|
|
QString localAddress;
|
|
|
|
QString remoteAddress;
|
|
|
|
QString manualAddress;
|
|
|
|
QByteArray macAddress;
|
|
|
|
QString name;
|
|
|
|
QString uuid;
|
|
|
|
QVector<NvApp> appList;
|
2018-06-27 04:47:01 +00:00
|
|
|
|
|
|
|
// Synchronization
|
|
|
|
QReadWriteLock lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: MOC isn't finding Q_OBJECT properly when this is confined
|
|
|
|
// to computermanager.cpp as it should be.
|
|
|
|
class PcMonitorThread : public QThread
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
#define TRIES_BEFORE_OFFLINING 2
|
|
|
|
#define POLLS_PER_APPLIST_FETCH 10
|
|
|
|
|
|
|
|
public:
|
|
|
|
PcMonitorThread(NvComputer* computer)
|
|
|
|
: m_Computer(computer)
|
|
|
|
{
|
|
|
|
setObjectName("Polling thread for " + computer->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool tryPollComputer(QString address, bool& changed)
|
|
|
|
{
|
|
|
|
NvHTTP http(address);
|
|
|
|
|
|
|
|
QString serverInfo;
|
|
|
|
try {
|
|
|
|
serverInfo = http.getServerInfo();
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NvComputer newState(address, serverInfo);
|
|
|
|
|
|
|
|
// Ensure the machine that responded is the one we intended to contact
|
|
|
|
if (m_Computer->uuid != newState.uuid) {
|
|
|
|
qInfo() << "Found unexpected PC " << newState.name << " looking for " << m_Computer->name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = m_Computer->update(newState);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool updateAppList(bool& changed)
|
|
|
|
{
|
2018-06-27 06:39:28 +00:00
|
|
|
Q_ASSERT(m_Computer->activeAddress != nullptr);
|
|
|
|
|
|
|
|
NvHTTP http(m_Computer->activeAddress);
|
|
|
|
|
|
|
|
QVector<NvApp> appList;
|
|
|
|
|
|
|
|
try {
|
|
|
|
appList = http.getAppList();
|
|
|
|
if (appList.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWriteLocker lock(&m_Computer->lock);
|
|
|
|
if (m_Computer->appList != appList) {
|
|
|
|
m_Computer->appList = appList;
|
2018-07-06 03:54:31 +00:00
|
|
|
m_Computer->sortAppList();
|
2018-06-27 06:39:28 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-06-27 04:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
|
|
|
// Always fetch the applist the first time
|
|
|
|
int pollsSinceLastAppListFetch = POLLS_PER_APPLIST_FETCH;
|
|
|
|
while (!isInterruptionRequested()) {
|
|
|
|
bool stateChanged = false;
|
2018-07-30 02:08:29 +00:00
|
|
|
bool online = false;
|
|
|
|
for (int i = 0; i < TRIES_BEFORE_OFFLINING && !online; i++) {
|
2018-07-04 05:11:21 +00:00
|
|
|
for (auto& address : m_Computer->uniqueAddresses()) {
|
2018-06-27 04:47:01 +00:00
|
|
|
if (isInterruptionRequested()) {
|
2018-06-28 10:29:22 +00:00
|
|
|
return;
|
2018-06-27 04:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tryPollComputer(address, stateChanged)) {
|
2018-07-30 02:08:29 +00:00
|
|
|
online = true;
|
2018-06-27 04:47:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we failed after all retry attempts
|
|
|
|
// Note: we don't need to acquire the read lock here,
|
|
|
|
// because we're on the writing thread.
|
2018-07-30 02:08:29 +00:00
|
|
|
if (!online && m_Computer->state != NvComputer::CS_OFFLINE) {
|
|
|
|
m_Computer->state = NvComputer::CS_OFFLINE;
|
|
|
|
stateChanged = true;
|
2018-06-27 04:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the applist if it's empty or it's been long enough that we need to refresh
|
|
|
|
pollsSinceLastAppListFetch++;
|
|
|
|
if (m_Computer->state == NvComputer::CS_ONLINE &&
|
|
|
|
m_Computer->pairState == NvComputer::PS_PAIRED &&
|
|
|
|
(m_Computer->appList.isEmpty() || pollsSinceLastAppListFetch >= POLLS_PER_APPLIST_FETCH)) {
|
|
|
|
if (updateAppList(stateChanged)) {
|
|
|
|
pollsSinceLastAppListFetch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stateChanged) {
|
|
|
|
// Tell anyone listening that we've changed state
|
|
|
|
emit computerStateChanged(m_Computer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a bit to poll again
|
|
|
|
QThread::sleep(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void computerStateChanged(NvComputer* computer);
|
|
|
|
|
|
|
|
private:
|
|
|
|
NvComputer* m_Computer;
|
|
|
|
};
|
|
|
|
|
2018-07-01 06:07:31 +00:00
|
|
|
class MdnsPendingComputer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MdnsPendingComputer(QMdnsEngine::Server* server,
|
|
|
|
QMdnsEngine::Cache* cache,
|
|
|
|
const QMdnsEngine::Service& service)
|
|
|
|
: m_Hostname(service.hostname()),
|
|
|
|
m_Resolver(server, m_Hostname, cache)
|
|
|
|
{
|
|
|
|
connect(&m_Resolver, SIGNAL(resolved(QHostAddress)),
|
|
|
|
this, SLOT(handleResolved(QHostAddress)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString hostname()
|
|
|
|
{
|
|
|
|
return m_Hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void handleResolved(const QHostAddress& address)
|
|
|
|
{
|
|
|
|
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
|
|
|
|
m_Resolver.disconnect();
|
|
|
|
emit resolvedv4(this, address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void resolvedv4(MdnsPendingComputer*, const QHostAddress&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QByteArray m_Hostname;
|
|
|
|
QMdnsEngine::Resolver m_Resolver;
|
|
|
|
};
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
class ComputerManager : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2018-07-06 06:12:55 +00:00
|
|
|
friend class DeferredHostDeletionTask;
|
2018-07-06 07:34:16 +00:00
|
|
|
friend class PendingAddTask;
|
2018-07-06 06:12:55 +00:00
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
public:
|
2018-06-28 02:55:44 +00:00
|
|
|
explicit ComputerManager(QObject *parent = nullptr);
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-19 04:27:43 +00:00
|
|
|
virtual ~ComputerManager();
|
|
|
|
|
2018-07-06 05:08:55 +00:00
|
|
|
Q_INVOKABLE void startPolling();
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-06 05:08:55 +00:00
|
|
|
Q_INVOKABLE void stopPollingAsync();
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-06 07:34:16 +00:00
|
|
|
Q_INVOKABLE void addNewHost(QString address, bool mdns);
|
2018-06-27 04:47:01 +00:00
|
|
|
|
2018-07-06 06:12:55 +00:00
|
|
|
void pairHost(NvComputer* computer, QString pin);
|
|
|
|
|
2018-08-01 05:21:39 +00:00
|
|
|
void quitRunningApp(NvComputer* computer);
|
|
|
|
|
2018-06-28 05:16:57 +00:00
|
|
|
QVector<NvComputer*> getComputers();
|
|
|
|
|
|
|
|
// computer is deleted inside this call
|
|
|
|
void deleteHost(NvComputer* computer);
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
signals:
|
|
|
|
void computerStateChanged(NvComputer* computer);
|
|
|
|
|
2018-07-06 06:12:55 +00:00
|
|
|
void pairingCompleted(NvComputer* computer, QString error);
|
|
|
|
|
2018-07-06 07:34:16 +00:00
|
|
|
void computerAddCompleted(QVariant success);
|
|
|
|
|
2018-08-01 05:21:39 +00:00
|
|
|
void quitAppCompleted(QString error);
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
private slots:
|
|
|
|
void handleComputerStateChanged(NvComputer* computer);
|
|
|
|
|
2018-07-01 06:07:31 +00:00
|
|
|
void handleMdnsServiceResolved(MdnsPendingComputer* computer, const QHostAddress& address);
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
private:
|
2018-06-27 07:43:46 +00:00
|
|
|
void saveHosts();
|
|
|
|
|
2018-06-27 04:47:01 +00:00
|
|
|
void startPollingComputer(NvComputer* computer);
|
|
|
|
|
2018-07-09 05:07:20 +00:00
|
|
|
int m_PollingRef;
|
2018-06-27 04:47:01 +00:00
|
|
|
QReadWriteLock m_Lock;
|
|
|
|
QMap<QString, NvComputer*> m_KnownHosts;
|
|
|
|
QMap<QString, QThread*> m_PollThreads;
|
2018-07-01 06:07:31 +00:00
|
|
|
QMdnsEngine::Server m_MdnsServer;
|
|
|
|
QMdnsEngine::Browser* m_MdnsBrowser;
|
|
|
|
QMdnsEngine::Cache m_MdnsCache;
|
|
|
|
QVector<MdnsPendingComputer*> m_PendingResolution;
|
2018-06-24 22:13:37 +00:00
|
|
|
};
|
2018-07-06 06:12:55 +00:00
|
|
|
|
|
|
|
class PendingPairingTask : public QObject, public QRunnable
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
PendingPairingTask(ComputerManager* computerManager, NvComputer* computer, QString pin)
|
2018-07-08 05:26:12 +00:00
|
|
|
: m_Computer(computer),
|
2018-07-06 06:12:55 +00:00
|
|
|
m_Pin(pin)
|
|
|
|
{
|
|
|
|
connect(this, &PendingPairingTask::pairingCompleted,
|
|
|
|
computerManager, &ComputerManager::pairingCompleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void pairingCompleted(NvComputer* computer, QString error);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
NvPairingManager pairingManager(m_Computer->activeAddress);
|
|
|
|
|
|
|
|
try {
|
|
|
|
NvPairingManager::PairState result = pairingManager.pair(m_Computer->appVersion, m_Pin);
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case NvPairingManager::PairState::PIN_WRONG:
|
|
|
|
emit pairingCompleted(m_Computer, "The PIN from the PC didn't match. Please try again.");
|
|
|
|
break;
|
|
|
|
case NvPairingManager::PairState::FAILED:
|
|
|
|
emit pairingCompleted(m_Computer, "Pairing failed. Please try again.");
|
|
|
|
break;
|
|
|
|
case NvPairingManager::PairState::ALREADY_IN_PROGRESS:
|
|
|
|
emit pairingCompleted(m_Computer, "Another pairing attempt is already in progress.");
|
|
|
|
break;
|
|
|
|
case NvPairingManager::PairState::PAIRED:
|
|
|
|
emit pairingCompleted(m_Computer, nullptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (const GfeHttpResponseException& e) {
|
2018-07-07 23:30:26 +00:00
|
|
|
emit pairingCompleted(m_Computer, e.toQString());
|
2018-07-06 06:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NvComputer* m_Computer;
|
|
|
|
QString m_Pin;
|
|
|
|
};
|
2018-07-06 07:34:16 +00:00
|
|
|
|
2018-08-01 05:21:39 +00:00
|
|
|
class PendingQuitTask : public QObject, public QRunnable
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
PendingQuitTask(ComputerManager* computerManager, NvComputer* computer)
|
|
|
|
: m_Computer(computer)
|
|
|
|
{
|
|
|
|
connect(this, &PendingQuitTask::quitAppFailed,
|
|
|
|
computerManager, &ComputerManager::quitAppCompleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void quitAppFailed(QString error);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
NvHTTP http(m_Computer->activeAddress);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (m_Computer->currentGameId != 0) {
|
|
|
|
http.quitApp();
|
|
|
|
|
|
|
|
if (http.getCurrentGame(http.getServerInfo()) != 0) {
|
|
|
|
{
|
|
|
|
QWriteLocker lock(&m_Computer->lock);
|
|
|
|
m_Computer->pendingQuit = false;
|
|
|
|
}
|
|
|
|
emit quitAppFailed("Unable to quit game that wasn't started by this computer. "
|
|
|
|
"You must quit the game on the host PC manually or use the device that originally started the game.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (const GfeHttpResponseException& e) {
|
|
|
|
{
|
|
|
|
QWriteLocker lock(&m_Computer->lock);
|
|
|
|
m_Computer->pendingQuit = false;
|
|
|
|
}
|
|
|
|
emit quitAppFailed(e.toQString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NvComputer* m_Computer;
|
|
|
|
};
|
|
|
|
|
2018-07-06 07:34:16 +00:00
|
|
|
class PendingAddTask : public QObject, public QRunnable
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
PendingAddTask(ComputerManager* computerManager, QString address, bool mdns)
|
|
|
|
: m_ComputerManager(computerManager),
|
|
|
|
m_Address(address),
|
|
|
|
m_Mdns(mdns)
|
|
|
|
{
|
|
|
|
connect(this, &PendingAddTask::computerAddCompleted,
|
|
|
|
computerManager, &ComputerManager::computerAddCompleted);
|
|
|
|
connect(this, &PendingAddTask::computerStateChanged,
|
|
|
|
computerManager, &ComputerManager::handleComputerStateChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void computerAddCompleted(QVariant success);
|
|
|
|
|
|
|
|
void computerStateChanged(NvComputer* computer);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
NvHTTP http(m_Address);
|
|
|
|
|
|
|
|
QString serverInfo;
|
|
|
|
try {
|
|
|
|
serverInfo = http.getServerInfo();
|
|
|
|
} catch (...) {
|
2018-07-14 05:38:58 +00:00
|
|
|
if (!m_Mdns) {
|
|
|
|
emit computerAddCompleted(false);
|
|
|
|
}
|
2018-07-06 07:34:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NvComputer* newComputer = new NvComputer(m_Address, serverInfo);
|
|
|
|
|
|
|
|
// Update addresses depending on the context
|
|
|
|
if (m_Mdns) {
|
|
|
|
newComputer->localAddress = m_Address;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newComputer->manualAddress = m_Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this PC already exists
|
|
|
|
QWriteLocker lock(&m_ComputerManager->m_Lock);
|
|
|
|
NvComputer* existingComputer = m_ComputerManager->m_KnownHosts[newComputer->uuid];
|
|
|
|
if (existingComputer != nullptr) {
|
|
|
|
// Fold it into the existing PC
|
|
|
|
bool changed = existingComputer->update(*newComputer);
|
|
|
|
delete newComputer;
|
|
|
|
|
|
|
|
// Drop the lock before notifying
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
// For non-mDNS clients, let them know it succeeded
|
|
|
|
if (!m_Mdns) {
|
|
|
|
emit computerAddCompleted(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell our client if something changed
|
|
|
|
if (changed) {
|
|
|
|
emit computerStateChanged(existingComputer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Store this in our active sets
|
|
|
|
m_ComputerManager->m_KnownHosts[newComputer->uuid] = newComputer;
|
|
|
|
|
|
|
|
// Start polling if enabled (write lock required)
|
|
|
|
m_ComputerManager->startPollingComputer(newComputer);
|
|
|
|
|
|
|
|
// Drop the lock before notifying
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
// For non-mDNS clients, let them know it succeeded
|
|
|
|
if (!m_Mdns) {
|
|
|
|
emit computerAddCompleted(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell our client about this new PC
|
|
|
|
emit computerStateChanged(newComputer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputerManager* m_ComputerManager;
|
|
|
|
QString m_Address;
|
|
|
|
bool m_Mdns;
|
|
|
|
};
|