core, web_service: Check for error when registering rooms

The `Register()` function can now handle error results and the error will be passed immediately to the Qt frontend, instead of being ignored silently and failing later with a "Room is not registered".
This commit is contained in:
zhupengfei 2019-04-19 22:55:49 +08:00
parent 81988d96fe
commit 36051204cc
7 changed files with 67 additions and 33 deletions

View File

@ -7,6 +7,7 @@
#include <QImage> #include <QImage>
#include <QList> #include <QList>
#include <QLocale> #include <QLocale>
#include <QMessageBox>
#include <QMetaType> #include <QMetaType>
#include <QTime> #include <QTime>
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
@ -148,7 +149,22 @@ void HostRoomWindow::Host() {
if (is_public) { if (is_public) {
if (auto session = announce_multiplayer_session.lock()) { if (auto session = announce_multiplayer_session.lock()) {
// Register the room first to ensure verify_UID is present when we connect // Register the room first to ensure verify_UID is present when we connect
session->Register(); Common::WebResult result = session->Register();
if (result.result_code != Common::WebResult::Code::Success) {
QMessageBox::warning(
this, tr("Error"),
tr("Failed to announce the room to the public lobby. In order to host a "
"room publicly, you must have a valid Citra account configured in "
"Emulation -> Configure -> Web. If you do not want to publish a room in "
"the public lobby, then select Unlisted instead.\nDebug Message: ") +
QString::fromStdString(result.result_string),
QMessageBox::Ok);
ui->host->setEnabled(true);
if (auto room = Network::GetRoom().lock()) {
room->Destroy();
}
return;
}
session->Start(); session->Start();
} else { } else {
LOG_ERROR(Network, "Starting announce session failed"); LOG_ERROR(Network, "Starting announce session failed");

View File

@ -174,12 +174,9 @@ void MultiplayerState::OnNetworkError(const Network::RoomMember::Error& error) {
void MultiplayerState::OnAnnounceFailed(const Common::WebResult& result) { void MultiplayerState::OnAnnounceFailed(const Common::WebResult& result) {
announce_multiplayer_session->Stop(); announce_multiplayer_session->Stop();
QMessageBox::warning( QMessageBox::warning(this, tr("Error"),
this, tr("Error"), tr("Failed to update the room information. Please check your Internet "
tr("Failed to announce the room to the public lobby. In order to host a room publicly, you " "connection and try hosting the room again.\nDebug Message: ") +
"must have a valid Citra account configured in Emulation -> Configure -> Web. If you do "
"not want to publish a room in the public lobby, then select Unlisted instead.\n"
"Debug Message: ") +
QString::fromStdString(result.result_string), QString::fromStdString(result.result_string),
QMessageBox::Ok); QMessageBox::Ok);
} }

View File

@ -83,9 +83,10 @@ public:
/** /**
* Registers the data in the announce service * Registers the data in the announce service
* @result A global Guid of the room which may be used for verification * @result The result of the register attempt. When the result code is Success, A global Guid of
* the room which may be used for verification will be in the result's returned_data.
*/ */
virtual std::string Register() = 0; virtual Common::WebResult Register() = 0;
/** /**
* Empties the stored players * Empties the stored players
@ -121,8 +122,8 @@ public:
Common::WebResult Update() override { Common::WebResult Update() override {
return Common::WebResult{Common::WebResult::Code::NoWebservice, "WebService is missing"}; return Common::WebResult{Common::WebResult::Code::NoWebservice, "WebService is missing"};
} }
std::string Register() override { Common::WebResult Register() override {
return ""; return Common::WebResult{Common::WebResult::Code::NoWebservice, "WebService is missing"};
} }
void ClearPlayers() override {} void ClearPlayers() override {}
RoomList GetRoomList() override { RoomList GetRoomList() override {

View File

@ -29,19 +29,23 @@ AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
#endif #endif
} }
void AnnounceMultiplayerSession::Register() { Common::WebResult AnnounceMultiplayerSession::Register() {
std::shared_ptr<Network::Room> room = Network::GetRoom().lock(); std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
if (!room) { if (!room) {
return; return Common::WebResult{Common::WebResult::Code::LibError, "Network is not initialized"};
} }
if (room->GetState() != Network::Room::State::Open) { if (room->GetState() != Network::Room::State::Open) {
return; return Common::WebResult{Common::WebResult::Code::LibError, "Room is not open"};
} }
UpdateBackendData(room); UpdateBackendData(room);
std::string result = backend->Register(); Common::WebResult result = backend->Register();
if (result.result_code != Common::WebResult::Code::Success) {
return result;
}
LOG_INFO(WebService, "Room has been registered"); LOG_INFO(WebService, "Room has been registered");
room->SetVerifyUID(result); room->SetVerifyUID(result.returned_data);
registered = true; registered = true;
return Common::WebResult{Common::WebResult::Code::Success};
} }
void AnnounceMultiplayerSession::Start() { void AnnounceMultiplayerSession::Start() {
@ -95,9 +99,22 @@ void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room
} }
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
if (!registered) { // Invokes all current bound error callbacks.
Register(); const auto ErrorCallback = [this](Common::WebResult result) {
std::lock_guard<std::mutex> lock(callback_mutex);
for (auto callback : error_callbacks) {
(*callback)(result);
} }
};
if (!registered) {
Common::WebResult result = Register();
if (result.result_code != Common::WebResult::Code::Success) {
ErrorCallback(result);
return;
}
}
auto update_time = std::chrono::steady_clock::now(); auto update_time = std::chrono::steady_clock::now();
std::future<Common::WebResult> future; std::future<Common::WebResult> future;
while (!shutdown_event.WaitUntil(update_time)) { while (!shutdown_event.WaitUntil(update_time)) {
@ -112,15 +129,15 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
UpdateBackendData(room); UpdateBackendData(room);
Common::WebResult result = backend->Update(); Common::WebResult result = backend->Update();
if (result.result_code != Common::WebResult::Code::Success) { if (result.result_code != Common::WebResult::Code::Success) {
std::lock_guard lock(callback_mutex); ErrorCallback(result);
for (auto callback : error_callbacks) {
(*callback)(result);
}
} }
if (result.result_string == "404") { if (result.result_string == "404") {
registered = false; registered = false;
// Needs to register the room again // Needs to register the room again
Register(); Common::WebResult result = Register();
if (result.result_code != Common::WebResult::Code::Success) {
ErrorCallback(result);
}
} }
} }
} }

View File

@ -44,8 +44,11 @@ public:
*/ */
void UnbindErrorCallback(CallbackHandle handle); void UnbindErrorCallback(CallbackHandle handle);
/// Registers a room to web services /**
void Register(); * Registers a room to web services
* @return The result of the registration attempt.
*/
Common::WebResult Register();
/** /**
* Starts the announce of a room to web services * Starts the announce of a room to web services

View File

@ -117,16 +117,16 @@ Common::WebResult RoomJson::Update() {
return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false); return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
} }
std::string RoomJson::Register() { Common::WebResult RoomJson::Register() {
nlohmann::json json = room; nlohmann::json json = room;
auto reply = client.PostJson("/lobby", json.dump(), false).returned_data; auto result = client.PostJson("/lobby", json.dump(), false);
if (reply.empty()) { if (result.result_code != Common::WebResult::Code::Success) {
return ""; return result;
} }
auto reply_json = nlohmann::json::parse(reply); auto reply_json = nlohmann::json::parse(result.returned_data);
room = reply_json.get<AnnounceMultiplayerRoom::Room>(); room = reply_json.get<AnnounceMultiplayerRoom::Room>();
room_id = reply_json.at("id").get<std::string>(); room_id = reply_json.at("id").get<std::string>();
return room.verify_UID; return Common::WebResult{Common::WebResult::Code::Success, "", room.verify_UID};
} }
void RoomJson::ClearPlayers() { void RoomJson::ClearPlayers() {

View File

@ -29,7 +29,7 @@ public:
const AnnounceMultiplayerRoom::MacAddress& mac_address, const u64 game_id, const AnnounceMultiplayerRoom::MacAddress& mac_address, const u64 game_id,
const std::string& game_name) override; const std::string& game_name) override;
Common::WebResult Update() override; Common::WebResult Update() override;
std::string Register() override; Common::WebResult Register() override;
void ClearPlayers() override; void ClearPlayers() override;
AnnounceMultiplayerRoom::RoomList GetRoomList() override; AnnounceMultiplayerRoom::RoomList GetRoomList() override;
void Delete() override; void Delete() override;