Improve locking in NvComputer class

This commit is contained in:
Cameron Gutman 2022-10-24 23:53:02 -05:00
parent 2a0f837120
commit b7a340bd00
2 changed files with 42 additions and 25 deletions

View file

@ -65,6 +65,8 @@ NvComputer::NvComputer(QSettings& settings)
void NvComputer::setRemoteAddress(QHostAddress address) void NvComputer::setRemoteAddress(QHostAddress address)
{ {
QWriteLocker lock(&this->lock);
Q_ASSERT(this->externalPort != 0); Q_ASSERT(this->externalPort != 0);
this->remoteAddress = NvAddress(address, this->externalPort); this->remoteAddress = NvAddress(address, this->externalPort);
@ -187,8 +189,13 @@ NvComputer::NvComputer(NvHTTP& http, QString serverInfo)
this->isSupportedServerVersion = CompatFetcher::isGfeVersionSupported(this->gfeVersion); this->isSupportedServerVersion = CompatFetcher::isGfeVersionSupported(this->gfeVersion);
} }
bool NvComputer::wake() bool NvComputer::wake() const
{ {
QByteArray wolPayload;
{
QReadLocker readLocker(&lock);
if (state == NvComputer::CS_ONLINE) { if (state == NvComputer::CS_ONLINE) {
qWarning() << name << "is already online"; qWarning() << name << "is already online";
return true; return true;
@ -199,19 +206,19 @@ bool NvComputer::wake()
return false; return false;
} }
const quint16 WOL_PORTS[] = {
9, // Standard WOL port (privileged port)
47998, 47999, 48000, 48002, 48010, // Ports opened by GFE
47009, // Port opened by Moonlight Internet Hosting Tool for WoL (non-privileged port)
};
// Create the WoL payload // Create the WoL payload
QByteArray wolPayload;
wolPayload.append(QByteArray::fromHex("FFFFFFFFFFFF")); wolPayload.append(QByteArray::fromHex("FFFFFFFFFFFF"));
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
wolPayload.append(macAddress); wolPayload.append(macAddress);
} }
Q_ASSERT(wolPayload.count() == 102); Q_ASSERT(wolPayload.count() == 102);
}
const quint16 WOL_PORTS[] = {
9, // Standard WOL port (privileged port)
47998, 47999, 48000, 48002, 48010, // Ports opened by GFE
47009, // Port opened by Moonlight Internet Hosting Tool for WoL (non-privileged port)
};
// Add the addresses that we know this host to be // Add the addresses that we know this host to be
// and broadcast addresses for this link just in // and broadcast addresses for this link just in
@ -278,16 +285,25 @@ bool NvComputer::wake()
return success; return success;
} }
bool NvComputer::isReachableOverVpn() bool NvComputer::isReachableOverVpn() const
{ {
NvAddress copyOfActiveAddress;
{
QReadLocker readLocker(&lock);
if (activeAddress.isNull()) { if (activeAddress.isNull()) {
return false; return false;
} }
QTcpSocket s; // Grab a copy of the active address to avoid having to hold
// the computer lock while doing socket operations
copyOfActiveAddress = activeAddress;
}
QTcpSocket s;
s.setProxy(QNetworkProxy::NoProxy); s.setProxy(QNetworkProxy::NoProxy);
s.connectToHost(activeAddress.address(), activeAddress.port()); s.connectToHost(copyOfActiveAddress.address(), copyOfActiveAddress.port());
if (s.waitForConnected(3000)) { if (s.waitForConnected(3000)) {
Q_ASSERT(!s.localAddress().isNull()); Q_ASSERT(!s.localAddress().isNull());
@ -375,6 +391,7 @@ bool NvComputer::updateAppList(QVector<NvApp> newAppList) {
QVector<NvAddress> NvComputer::uniqueAddresses() const QVector<NvAddress> NvComputer::uniqueAddresses() const
{ {
QReadLocker readLocker(&lock);
QVector<NvAddress> uniqueAddressList; QVector<NvAddress> uniqueAddressList;
// Start with addresses correctly ordered // Start with addresses correctly ordered
@ -406,7 +423,7 @@ QVector<NvAddress> NvComputer::uniqueAddresses() const
return uniqueAddressList; return uniqueAddressList;
} }
bool NvComputer::update(NvComputer& that) bool NvComputer::update(const NvComputer& that)
{ {
bool changed = false; bool changed = false;

View file

@ -30,13 +30,13 @@ public:
setRemoteAddress(QHostAddress); setRemoteAddress(QHostAddress);
bool bool
update(NvComputer& that); update(const NvComputer& that);
bool bool
wake(); wake() const;
bool bool
isReachableOverVpn(); isReachableOverVpn() const;
QVector<NvAddress> QVector<NvAddress>
uniqueAddresses() const; uniqueAddresses() const;