Destroy the QMdnsEngine Server and Cache when not polling

These set background timers to refresh state, rebind sockets,
and refresh the cache. This is all stuff that we don't want to
be doing while streaming.
This commit is contained in:
Cameron Gutman 2023-06-21 00:51:31 -05:00
parent ba46905243
commit ef988cc8af
2 changed files with 24 additions and 9 deletions

View file

@ -319,12 +319,13 @@ void ComputerManager::startPolling()
if (prefs.enableMdns) { if (prefs.enableMdns) {
// Start an MDNS query for GameStream hosts // Start an MDNS query for GameStream hosts
m_MdnsBrowser = new QMdnsEngine::Browser(&m_MdnsServer, "_nvstream._tcp.local.", &m_MdnsCache); m_MdnsServer.reset(new QMdnsEngine::Server());
m_MdnsBrowser = new QMdnsEngine::Browser(m_MdnsServer.get(), "_nvstream._tcp.local.");
connect(m_MdnsBrowser, &QMdnsEngine::Browser::serviceAdded, connect(m_MdnsBrowser, &QMdnsEngine::Browser::serviceAdded,
this, [this](const QMdnsEngine::Service& service) { this, [this](const QMdnsEngine::Service& service) {
qInfo() << "Discovered mDNS host:" << service.hostname(); qInfo() << "Discovered mDNS host:" << service.hostname();
MdnsPendingComputer* pendingComputer = new MdnsPendingComputer(&m_MdnsServer, service); MdnsPendingComputer* pendingComputer = new MdnsPendingComputer(m_MdnsServer, service);
connect(pendingComputer, &MdnsPendingComputer::resolvedHost, connect(pendingComputer, &MdnsPendingComputer::resolvedHost,
this, &ComputerManager::handleMdnsServiceResolved); this, &ComputerManager::handleMdnsServiceResolved);
m_PendingResolution.append(pendingComputer); m_PendingResolution.append(pendingComputer);
@ -646,9 +647,10 @@ void ComputerManager::stopPollingAsync()
m_PendingResolution.removeFirst(); m_PendingResolution.removeFirst();
} }
// Delete the browser to stop discovery // Delete the browser and server to stop discovery and refresh polling
delete m_MdnsBrowser; delete m_MdnsBrowser;
m_MdnsBrowser = nullptr; m_MdnsBrowser = nullptr;
m_MdnsServer.reset();
// Interrupt all threads, but don't wait for them to terminate // Interrupt all threads, but don't wait for them to terminate
for (ComputerPollingEntry* entry : m_PollEntries) { for (ComputerPollingEntry* entry : m_PollEntries) {

View file

@ -42,11 +42,11 @@ class MdnsPendingComputer : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit MdnsPendingComputer(QMdnsEngine::Server* server, explicit MdnsPendingComputer(const QSharedPointer<QMdnsEngine::Server> server,
const QMdnsEngine::Service& service) const QMdnsEngine::Service& service)
: m_Hostname(service.hostname()), : m_Hostname(service.hostname()),
m_Port(service.port()), m_Port(service.port()),
m_Server(server), m_ServerWeak(server),
m_Resolver(nullptr) m_Resolver(nullptr)
{ {
// Start resolving // Start resolving
@ -94,8 +94,21 @@ signals:
private: private:
void resolve() void resolve()
{ {
// Delete our resolver, so we're guaranteed that nothing is referencing m_Server.
delete m_Resolver; delete m_Resolver;
m_Resolver = new QMdnsEngine::Resolver(m_Server, m_Hostname); m_Resolver = nullptr;
// Now delete our strong reference that we held on behalf of m_Resolver.
// The server may be destroyed after we make this call.
m_Server.reset();
// Re-acquire a strong reference if the server still exists.
m_Server = m_ServerWeak.toStrongRef();
if (!m_Server) {
return;
}
m_Resolver = new QMdnsEngine::Resolver(m_Server.get(), m_Hostname);
connect(m_Resolver, &QMdnsEngine::Resolver::resolved, connect(m_Resolver, &QMdnsEngine::Resolver::resolved,
this, &MdnsPendingComputer::handleResolvedAddress); this, &MdnsPendingComputer::handleResolvedAddress);
QTimer::singleShot(2000, this, &MdnsPendingComputer::handleResolvedTimeout); QTimer::singleShot(2000, this, &MdnsPendingComputer::handleResolvedTimeout);
@ -103,7 +116,8 @@ private:
QByteArray m_Hostname; QByteArray m_Hostname;
uint16_t m_Port; uint16_t m_Port;
QMdnsEngine::Server* m_Server; QWeakPointer<QMdnsEngine::Server> m_ServerWeak;
QSharedPointer<QMdnsEngine::Server> m_Server;
QMdnsEngine::Resolver* m_Resolver; QMdnsEngine::Resolver* m_Resolver;
QVector<QHostAddress> m_Addresses; QVector<QHostAddress> m_Addresses;
}; };
@ -245,9 +259,8 @@ private:
QReadWriteLock m_Lock; QReadWriteLock m_Lock;
QMap<QString, NvComputer*> m_KnownHosts; QMap<QString, NvComputer*> m_KnownHosts;
QMap<QString, ComputerPollingEntry*> m_PollEntries; QMap<QString, ComputerPollingEntry*> m_PollEntries;
QMdnsEngine::Server m_MdnsServer; QSharedPointer<QMdnsEngine::Server> m_MdnsServer;
QMdnsEngine::Browser* m_MdnsBrowser; QMdnsEngine::Browser* m_MdnsBrowser;
QMdnsEngine::Cache m_MdnsCache;
QVector<MdnsPendingComputer*> m_PendingResolution; QVector<MdnsPendingComputer*> m_PendingResolution;
CompatFetcher m_CompatFetcher; CompatFetcher m_CompatFetcher;
DelayedFlushThread* m_DelayedFlushThread; DelayedFlushThread* m_DelayedFlushThread;