2017-10-31 14:32:42 +05:30
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <vector>
|
|
|
|
#include "announce_multiplayer_session.h"
|
|
|
|
#include "common/announce_multiplayer_room.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "core/settings.h"
|
|
|
|
#include "network/network.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
|
|
|
#include "web_service/announce_room_json.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
// Time between room is announced to web_service
|
|
|
|
static constexpr std::chrono::seconds announce_time_interval(15);
|
|
|
|
|
2017-11-11 00:07:26 +05:30
|
|
|
AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
|
2017-10-31 14:32:42 +05:30
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
2018-09-12 21:52:48 +05:30
|
|
|
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url,
|
2018-09-12 22:37:06 +05:30
|
|
|
Settings::values.citra_username,
|
|
|
|
Settings::values.citra_token);
|
2017-10-31 14:32:42 +05:30
|
|
|
#else
|
|
|
|
backend = std::make_unique<AnnounceMultiplayerRoom::NullBackend>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-27 13:10:15 +05:30
|
|
|
void AnnounceMultiplayerSession::Register() {
|
|
|
|
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
|
|
|
|
if (!room) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (room->GetState() != Network::Room::State::Open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UpdateBackendData(room);
|
|
|
|
std::string result = backend->Register();
|
|
|
|
LOG_INFO(WebService, "Room has been registered");
|
|
|
|
room->SetVerifyUID(result);
|
|
|
|
registered = true;
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:32:42 +05:30
|
|
|
void AnnounceMultiplayerSession::Start() {
|
|
|
|
if (announce_multiplayer_thread) {
|
|
|
|
Stop();
|
|
|
|
}
|
2017-11-11 00:07:26 +05:30
|
|
|
shutdown_event.Reset();
|
2017-10-31 14:32:42 +05:30
|
|
|
announce_multiplayer_thread =
|
|
|
|
std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::Stop() {
|
|
|
|
if (announce_multiplayer_thread) {
|
2017-11-11 00:07:26 +05:30
|
|
|
shutdown_event.Set();
|
2017-11-08 02:21:11 +05:30
|
|
|
announce_multiplayer_thread->join();
|
2017-10-31 14:32:42 +05:30
|
|
|
announce_multiplayer_thread.reset();
|
|
|
|
backend->Delete();
|
2018-10-27 13:10:15 +05:30
|
|
|
registered = false;
|
2017-10-31 14:32:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 02:21:11 +05:30
|
|
|
AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback(
|
2017-10-31 14:32:42 +05:30
|
|
|
std::function<void(const Common::WebResult&)> function) {
|
2019-04-01 21:59:59 +05:30
|
|
|
std::lock_guard lock(callback_mutex);
|
2017-10-31 14:32:42 +05:30
|
|
|
auto handle = std::make_shared<std::function<void(const Common::WebResult&)>>(function);
|
|
|
|
error_callbacks.insert(handle);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2017-11-08 02:21:11 +05:30
|
|
|
void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) {
|
2019-04-01 21:59:59 +05:30
|
|
|
std::lock_guard lock(callback_mutex);
|
2017-10-31 14:32:42 +05:30
|
|
|
error_callbacks.erase(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnounceMultiplayerSession::~AnnounceMultiplayerSession() {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2018-10-27 13:10:15 +05:30
|
|
|
void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) {
|
|
|
|
Network::RoomInformation room_information = room->GetRoomInformation();
|
|
|
|
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();
|
|
|
|
backend->SetRoomInformation(
|
|
|
|
room_information.name, room_information.description, room_information.port,
|
|
|
|
room_information.member_slots, Network::network_version, room->HasPassword(),
|
|
|
|
room_information.preferred_game, room_information.preferred_game_id);
|
|
|
|
backend->ClearPlayers();
|
|
|
|
for (const auto& member : memberlist) {
|
|
|
|
backend->AddPlayer(member.username, member.nickname, member.avatar_url, member.mac_address,
|
|
|
|
member.game_info.id, member.game_info.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:32:42 +05:30
|
|
|
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
|
2018-10-27 13:10:15 +05:30
|
|
|
if (!registered) {
|
|
|
|
Register();
|
|
|
|
}
|
2017-11-11 00:07:26 +05:30
|
|
|
auto update_time = std::chrono::steady_clock::now();
|
2017-10-31 14:32:42 +05:30
|
|
|
std::future<Common::WebResult> future;
|
2017-11-11 00:07:26 +05:30
|
|
|
while (!shutdown_event.WaitUntil(update_time)) {
|
|
|
|
update_time += announce_time_interval;
|
2017-11-06 00:53:22 +05:30
|
|
|
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
|
|
|
|
if (!room) {
|
2017-11-11 00:07:26 +05:30
|
|
|
break;
|
2017-11-06 00:53:22 +05:30
|
|
|
}
|
|
|
|
if (room->GetState() != Network::Room::State::Open) {
|
2017-11-11 00:07:26 +05:30
|
|
|
break;
|
2017-11-06 00:53:22 +05:30
|
|
|
}
|
2018-10-27 13:10:15 +05:30
|
|
|
UpdateBackendData(room);
|
|
|
|
Common::WebResult result = backend->Update();
|
2018-09-12 21:52:48 +05:30
|
|
|
if (result.result_code != Common::WebResult::Code::Success) {
|
2019-04-01 21:59:59 +05:30
|
|
|
std::lock_guard lock(callback_mutex);
|
2018-09-12 21:52:48 +05:30
|
|
|
for (auto callback : error_callbacks) {
|
|
|
|
(*callback)(result);
|
2017-10-31 14:32:42 +05:30
|
|
|
}
|
|
|
|
}
|
2018-10-27 13:10:15 +05:30
|
|
|
if (result.result_string == "404") {
|
|
|
|
registered = false;
|
|
|
|
// Needs to register the room again
|
|
|
|
Register();
|
|
|
|
}
|
2017-10-31 14:32:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 21:52:48 +05:30
|
|
|
AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() {
|
|
|
|
return backend->GetRoomList();
|
2017-10-31 14:32:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core
|