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,16 +189,29 @@ 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
{ {
if (state == NvComputer::CS_ONLINE) { QByteArray wolPayload;
qWarning() << name << "is already online";
return true;
}
if (macAddress.isEmpty()) { {
qWarning() << name << "has no MAC address stored"; QReadLocker readLocker(&lock);
return false;
if (state == NvComputer::CS_ONLINE) {
qWarning() << name << "is already online";
return true;
}
if (macAddress.isEmpty()) {
qWarning() << name << "has no MAC address stored";
return false;
}
// Create the WoL payload
wolPayload.append(QByteArray::fromHex("FFFFFFFFFFFF"));
for (int i = 0; i < 16; i++) {
wolPayload.append(macAddress);
}
Q_ASSERT(wolPayload.count() == 102);
} }
const quint16 WOL_PORTS[] = { const quint16 WOL_PORTS[] = {
@ -205,14 +220,6 @@ bool NvComputer::wake()
47009, // Port opened by Moonlight Internet Hosting Tool for WoL (non-privileged port) 47009, // Port opened by Moonlight Internet Hosting Tool for WoL (non-privileged port)
}; };
// Create the WoL payload
QByteArray wolPayload;
wolPayload.append(QByteArray::fromHex("FFFFFFFFFFFF"));
for (int i = 0; i < 16; i++) {
wolPayload.append(macAddress);
}
Q_ASSERT(wolPayload.count() == 102);
// 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
// case the host has timed out in ARP entries. // case the host has timed out in ARP entries.
@ -278,16 +285,25 @@ bool NvComputer::wake()
return success; return success;
} }
bool NvComputer::isReachableOverVpn() bool NvComputer::isReachableOverVpn() const
{ {
if (activeAddress.isNull()) { NvAddress copyOfActiveAddress;
return false;
{
QReadLocker readLocker(&lock);
if (activeAddress.isNull()) {
return false;
}
// Grab a copy of the active address to avoid having to hold
// the computer lock while doing socket operations
copyOfActiveAddress = activeAddress;
} }
QTcpSocket s; 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;