Xbox Cloud Gaming app for PS5 Homebrew
C
xCloud login tried again on a 5xx or no answer (up to four tries, 1/2/4 s apart)
Rafael Abud Ferreira45487a5a14b7
diff --git a/src/xcloud/gssv.cpp b/src/xcloud/gssv.cppindex a78020b..1be60f6 100644--- a/src/xcloud/gssv.cpp+++ b/src/xcloud/gssv.cpp@@ -18,6 +18,7 @@ namespace xc::xcloud {namespace {constexpr const char* kClientHeader = "XboxComBrowser";+constexpr int kLoginTries = 4; // xCloud login: tries on a 5xx / no answer// Sent like the Windows Xbox app does: "Windows" plus a 1080p display is what// gets a 1080p stream; an Android device with a 720p display gets 720p.@@ -270,9 +271,21 @@ bool GssvClient::login(const auth::XblToken& gssvXsts, std::string& err) {req.headers.emplace_back("X-Forwarded-For", address);XC_LOGI("xCloud login as country %s", loginCountry_.c_str()); // not the address}- auto r = net::perform(req);+ // The service fails now and then with a 5xx (or is slow to answer) and+ // works on the next try: up to four tries, 1, 2 and 4 s apart. A refusal+ // (4xx) is an answer, not tried again.+ net::Response r;+ std::optional<json::Value> j;+ for (int attempt = 1;; ++attempt) {+ r = net::perform(req);+ j = json::parse(r.body);+ if (r.ok() && j && (*j)["gsToken"].isString()) break;+ bool transient = r.status == 0 || r.status == 408 || r.status == 429 || r.status >= 500;+ if (!transient || attempt == kLoginTries) break;+ XC_LOGW("xCloud login: %s, trying again (%d/%d)", describe(r).c_str(), attempt + 1, kLoginTries);+ platform::sleepMs(1000u << (attempt - 1));+ }loginStatus_ = r.status;- auto j = json::parse(r.body);if (!r.ok() || !j || !(*j)["gsToken"].isString()) {err = "xCloud login failed: " + describe(r);if (r.status == 403) err += " (no Game Pass plan with cloud gaming, or xCloud is not available in this region)";diff --git a/tests/test_main.cpp b/tests/test_main.cppindex ab6f3b7..cf5d72d 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -1019,7 +1019,36 @@ static void testKeyMap() {std::remove(path.c_str());}+// The xCloud login: a 5xx is tried again, a refusal is not.+static void testLoginRetry() {+ using namespace xc;+ int calls = 0;+ net::setTestTransport([&](const net::Request&) {+ net::Response r;+ if (++calls < 3) {+ r.status = 500;+ return r;+ }+ r.status = 200;+ r.body = R"({"gsToken":"t","market":"BR","durationInSeconds":3600,"offeringSettings":{"regions":[{"name":"WestUS2","baseUri":"https://w","isDefault":true}]}})";+ return r;+ });+ xcloud::GssvClient gssv;+ std::string err;+ CHECK(gssv.login(auth::XblToken{}, err) && calls == 3);+ calls = 0;+ net::setTestTransport([&](const net::Request&) {+ ++calls;+ net::Response r;+ r.status = 403;+ return r;+ });+ CHECK(!gssv.login(auth::XblToken{}, err) && calls == 1 && gssv.loginRefused());+ net::setTestTransport({});+}+int main() {+ testLoginRetry();testReport();testCrashGuard();testBuiltinCa();