Merge pull request #4749 from zhaowenlan1779/webfix

web_service: Misc fixes
This commit is contained in:
Pengfei Zhu 2019-05-01 17:00:17 +08:00 committed by GitHub
commit e0a0bca13a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 220 additions and 63 deletions

View file

@ -29,19 +29,23 @@ AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
#endif
}
void AnnounceMultiplayerSession::Register() {
Common::WebResult AnnounceMultiplayerSession::Register() {
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
if (!room) {
return;
return Common::WebResult{Common::WebResult::Code::LibError, "Network is not initialized"};
}
if (room->GetState() != Network::Room::State::Open) {
return;
return Common::WebResult{Common::WebResult::Code::LibError, "Room is not open"};
}
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");
room->SetVerifyUID(result);
room->SetVerifyUID(result.returned_data);
registered = true;
return Common::WebResult{Common::WebResult::Code::Success};
}
void AnnounceMultiplayerSession::Start() {
@ -95,9 +99,22 @@ void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room
}
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
// Invokes all current bound error callbacks.
const auto ErrorCallback = [this](Common::WebResult result) {
std::lock_guard<std::mutex> lock(callback_mutex);
for (auto callback : error_callbacks) {
(*callback)(result);
}
};
if (!registered) {
Register();
Common::WebResult result = Register();
if (result.result_code != Common::WebResult::Code::Success) {
ErrorCallback(result);
return;
}
}
auto update_time = std::chrono::steady_clock::now();
std::future<Common::WebResult> future;
while (!shutdown_event.WaitUntil(update_time)) {
@ -112,15 +129,15 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
UpdateBackendData(room);
Common::WebResult result = backend->Update();
if (result.result_code != Common::WebResult::Code::Success) {
std::lock_guard lock(callback_mutex);
for (auto callback : error_callbacks) {
(*callback)(result);
}
ErrorCallback(result);
}
if (result.result_string == "404") {
registered = false;
// Needs to register the room again
Register();
Common::WebResult result = Register();
if (result.result_code != Common::WebResult::Code::Success) {
ErrorCallback(result);
}
}
}
}
@ -129,4 +146,18 @@ AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() {
return backend->GetRoomList();
}
bool AnnounceMultiplayerSession::IsRunning() const {
return announce_multiplayer_thread != nullptr;
}
void AnnounceMultiplayerSession::UpdateCredentials() {
ASSERT_MSG(!IsRunning(), "Credentials can only be updated when session is not running");
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url,
Settings::values.citra_username,
Settings::values.citra_token);
#endif
}
} // namespace Core

View file

@ -44,8 +44,11 @@ public:
*/
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
@ -64,6 +67,17 @@ public:
*/
AnnounceMultiplayerRoom::RoomList GetRoomList();
/**
* Whether the announce session is still running
*/
bool IsRunning() const;
/**
* Recreates the backend, updating the credentials.
* This can only be used when the announce session is not running.
*/
void UpdateCredentials();
private:
Common::Event shutdown_event;
std::mutex callback_mutex;

View file

@ -92,13 +92,6 @@ bool VerifyLogin(const std::string& username, const std::string& token) {
}
TelemetrySession::TelemetrySession() {
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::TelemetryJson>(Settings::values.web_api_url,
Settings::values.citra_username,
Settings::values.citra_token);
#else
backend = std::make_unique<Telemetry::NullVisitor>();
#endif
// Log one-time top-level information
AddField(Telemetry::FieldType::None, "TelemetryId", GetTelemetryId());
@ -192,9 +185,15 @@ TelemetrySession::~TelemetrySession() {
.count()};
AddField(Telemetry::FieldType::Session, "Shutdown_Time", shutdown_time);
#ifdef ENABLE_WEB_SERVICE
auto backend = std::make_unique<WebService::TelemetryJson>(Settings::values.web_api_url,
Settings::values.citra_username,
Settings::values.citra_token);
#else
auto backend = std::make_unique<Telemetry::NullVisitor>();
#endif
// Complete the session, submitting to web service if necessary
// This is just a placeholder to wrap up the session once the core completes and this is
// destroyed. This will be moved elsewhere once we are actually doing real I/O with the service.
field_collection.Accept(*backend);
if (Settings::values.enable_telemetry)
backend->Complete();
@ -203,6 +202,9 @@ TelemetrySession::~TelemetrySession() {
bool TelemetrySession::SubmitTestcase() {
#ifdef ENABLE_WEB_SERVICE
auto backend = std::make_unique<WebService::TelemetryJson>(Settings::values.web_api_url,
Settings::values.citra_username,
Settings::values.citra_token);
field_collection.Accept(*backend);
return backend->SubmitTestcase();
#else

View file

@ -39,7 +39,6 @@ public:
private:
Telemetry::FieldCollection field_collection; ///< Tracks all added fields for the session
std::unique_ptr<Telemetry::VisitorInterface> backend; ///< Backend interface that logs fields
};
/**