From 2b7d66c710923459c1b4e0c3bd7aea162a3b7d4b Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Fri, 6 Dec 2019 10:32:45 -0800 Subject: [PATCH] Slight optimization; comments --- domain-server/src/DomainServer.cpp | 38 +++++++++++-------- domain-server/src/DomainServer.h | 2 +- .../ScreenshareScriptingInterface.cpp | 7 ++++ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 9a8033e03c..c2de9c78ce 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -3616,28 +3616,18 @@ void DomainServer::handleOctreeFileReplacementRequest(QSharedPointer message) { - QUuid avatar = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); - QUuid zone = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); + QUuid avatarID = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); + QUuid zoneID = QUuid::fromRfc4122(message->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); - if (avatar.isNull()) { + if (avatarID.isNull()) { qCWarning(domain_server) << "Ignoring null avatar presence"; return; } - auto limitedNodeList = DependencyManager::get(); - auto matchingNode = limitedNodeList->nodeWithUUID(avatar); - if (!matchingNode) { - qCWarning(domain_server) << "Ignoring avatar presence for unknown avatar" << avatar; - return; - } - QString verifiedUsername = matchingNode->getPermissions().getVerifiedUserName(); - if (verifiedUsername.isEmpty()) { // Silently bail for users who are not logged in. - return; - } static const int SCREENSHARE_EXPIRATION_SECONDS = 24 * 60 * 60; - screensharePresence(zone.isNull() ? "" : zone.toString(), verifiedUsername, avatar, SCREENSHARE_EXPIRATION_SECONDS); + screensharePresence(zoneID.isNull() ? "" : zoneID.toString(), avatarID, SCREENSHARE_EXPIRATION_SECONDS); } -void DomainServer::screensharePresence(QString roomname, QString username, QUuid avatarID, int expirationSeconds) { +void DomainServer::screensharePresence(QString roomname, QUuid avatarID, int expirationSeconds) { if (!DependencyManager::get()->hasValidAccessToken()) { static std::once_flag presenceAuthorityWarning; std::call_once(presenceAuthorityWarning, [] { @@ -3645,10 +3635,25 @@ void DomainServer::screensharePresence(QString roomname, QString username, QUuid }); return; } + + auto limitedNodeList = DependencyManager::get(); + auto matchingNode = limitedNodeList->nodeWithUUID(avatarID); + if (!matchingNode) { + qCWarning(domain_server) << "Ignoring avatar presence for unknown avatar ID" << avatarID; + return; + } + QString verifiedUsername = matchingNode->getPermissions().getVerifiedUserName(); + if (verifiedUsername.isEmpty()) { // Silently bail for users who are not logged in. + return; + } + JSONCallbackParameters callbackParams; callbackParams.callbackReceiver = this; callbackParams.jsonCallbackMethod = "handleSuccessfulScreensharePresence"; callbackParams.errorCallbackMethod = "handleFailedScreensharePresence"; + // Construct `callbackData`, which is data that will be available to the callback functions. + // In this case, the "success" callback needs access to the "roomname" (the zone ID) and the + // relevant avatar's UUID. QJsonObject callbackData; callbackData.insert("roomname", roomname); callbackData.insert("avatarID", avatarID.toString()); @@ -3656,7 +3661,7 @@ void DomainServer::screensharePresence(QString roomname, QString username, QUuid const QString PATH = "api/v1/domains/%1/screenshare"; QString domain_id = uuidStringWithoutCurlyBraces(getID()); QJsonObject json, screenshare; - screenshare["username"] = username; + screenshare["username"] = verifiedUsername; screenshare["roomname"] = roomname; if (expirationSeconds > 0) { screenshare["expiration"] = expirationSeconds; @@ -3677,6 +3682,7 @@ void DomainServer::handleSuccessfulScreensharePresence(QNetworkReply* requestRep return; } + // Tell the client that we just authorized to screenshare which zone ID in which they are authorized to screenshare. auto nodeList = DependencyManager::get(); auto packet = NLPacket::create(PacketType::AvatarZonePresence, NUM_BYTES_RFC4122_UUID, true); packet->write(QUuid(callbackData["roomname"].toString()).toRfc4122()); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 92ef5a90b3..95b4b784cb 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -78,7 +78,7 @@ public: bool isAssetServerEnabled(); - void screensharePresence(QString roomname, QString username, QUuid avatarID, int expiration_seconds = 0); + void screensharePresence(QString roomname, QUuid avatarID, int expiration_seconds = 0); public slots: /// Called by NodeList to inform us a node has been added diff --git a/interface/src/scripting/ScreenshareScriptingInterface.cpp b/interface/src/scripting/ScreenshareScriptingInterface.cpp index 1e03e90482..3bf8336fe4 100644 --- a/interface/src/scripting/ScreenshareScriptingInterface.cpp +++ b/interface/src/scripting/ScreenshareScriptingInterface.cpp @@ -39,6 +39,7 @@ ScreenshareScriptingInterface::ScreenshareScriptingInterface() { _requestScreenshareInfoRetryTimer->setInterval(SCREENSHARE_INFO_REQUEST_RETRY_TIMEOUT_MS); connect(_requestScreenshareInfoRetryTimer, &QTimer::timeout, this, &ScreenshareScriptingInterface::requestScreenshareInfo); + // This packet listener handles the packet containing information about the latest zone ID in which we are allowed to share. auto nodeList = DependencyManager::get(); PacketReceiver& packetReceiver = nodeList->getPacketReceiver(); packetReceiver.registerListener(PacketType::AvatarZonePresence, this, "processAvatarZonePresencePacketOnClient"); @@ -56,8 +57,11 @@ void ScreenshareScriptingInterface::processAvatarZonePresencePacketOnClient(QSha return; } + // Set the last known authorized screenshare zone ID to the zone that the Domain Server just told us about. _lastAuthorizedZoneID = zone; + // If we had previously started the screenshare process but knew that we weren't going to be authorized to screenshare, + // let's continue the screenshare process here. if (_waitingForAuthorization) { requestScreenshareInfo(); } @@ -65,6 +69,9 @@ void ScreenshareScriptingInterface::processAvatarZonePresencePacketOnClient(QSha static const int MAX_NUM_SCREENSHARE_INFO_REQUEST_RETRIES = 5; void ScreenshareScriptingInterface::requestScreenshareInfo() { + // If the screenshare zone that we're currently in (i.e. `startScreenshare()` was called) is different from + // the zone in which we are authorized to screenshare... + // ...return early here and wait for the DS to send us a packet containing this zone's ID. if (_screenshareZoneID != _lastAuthorizedZoneID) { qDebug() << "Client not yet authorized to screenshare. Waiting for authorization message from domain server..."; _waitingForAuthorization = true;