Skip to content

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 Ferreiraa3f2ae5b97bb
diff --git a/src/xcloud/gssv.cpp b/src/xcloud/gssv.cpp
index 6f2177e..60d975d 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.cpp
index de0020b..ebf6be0 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -903,7 +903,36 @@ static void testAudioDecoderEmptyPacket() {
CHECK(!out.empty());
}
+// 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();