|
|
|
@ -19,9 +19,6 @@ |
|
|
|
|
|
|
|
namespace Core { |
|
|
|
|
|
|
|
// Time between room is announced to web_service
|
|
|
|
static constexpr std::chrono::seconds announce_time_interval(15); |
|
|
|
|
|
|
|
AnnounceMultiplayerSession::AnnounceMultiplayerSession() { |
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
|
|
|
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(), |
|
|
|
@ -53,61 +50,15 @@ WebService::WebResult AnnounceMultiplayerSession::Register() { |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::Start() { |
|
|
|
if (announce_multiplayer_thread) { |
|
|
|
Stop(); |
|
|
|
} |
|
|
|
shutdown_event.Reset(); |
|
|
|
announce_multiplayer_thread = |
|
|
|
std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this); |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::Stop() { |
|
|
|
if (announce_multiplayer_thread) { |
|
|
|
shutdown_event.Set(); |
|
|
|
announce_multiplayer_thread->join(); |
|
|
|
announce_multiplayer_thread.reset(); |
|
|
|
backend->Delete(); |
|
|
|
registered = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback( |
|
|
|
std::function<void(const WebService::WebResult&)> function) { |
|
|
|
std::lock_guard lock(callback_mutex); |
|
|
|
auto handle = std::make_shared<std::function<void(const WebService::WebResult&)>>(function); |
|
|
|
error_callbacks.insert(handle); |
|
|
|
return handle; |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) { |
|
|
|
std::lock_guard lock(callback_mutex); |
|
|
|
error_callbacks.erase(handle); |
|
|
|
} |
|
|
|
|
|
|
|
AnnounceMultiplayerSession::~AnnounceMultiplayerSession() { |
|
|
|
if (announce_multiplayer_thread.has_value()) { |
|
|
|
Stop(); |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) { |
|
|
|
Network::RoomInformation room_information = room->GetRoomInformation(); |
|
|
|
std::vector<AnnounceMultiplayerRoom::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); |
|
|
|
backend->ClearPlayers(); |
|
|
|
for (const auto& member : memberlist) { |
|
|
|
backend->AddPlayer(member); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { |
|
|
|
announce_multiplayer_thread.emplace([&](std::stop_token stoken) { |
|
|
|
// Invokes all current bound error callbacks.
|
|
|
|
const auto ErrorCallback = [this](WebService::WebResult result) { |
|
|
|
std::lock_guard lock(callback_mutex); |
|
|
|
for (auto callback : error_callbacks) { |
|
|
|
for (auto callback : error_callbacks) |
|
|
|
(*callback)(result); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
if (!registered) { |
|
|
|
@ -118,10 +69,12 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Time between room is announced to web_service
|
|
|
|
std::chrono::seconds const announce_timeslice(15); |
|
|
|
auto update_time = std::chrono::steady_clock::now(); |
|
|
|
std::future<WebService::WebResult> future; |
|
|
|
while (!shutdown_event.WaitUntil(update_time)) { |
|
|
|
update_time += announce_time_interval; |
|
|
|
update_time = std::chrono::steady_clock::now() + announce_timeslice; |
|
|
|
auto room = Network::GetRoom().lock(); |
|
|
|
if (!room) { |
|
|
|
break; |
|
|
|
@ -143,14 +96,50 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() { |
|
|
|
return backend->GetRoomList(); |
|
|
|
void AnnounceMultiplayerSession::Stop() { |
|
|
|
if (announce_multiplayer_thread.has_value()) { |
|
|
|
shutdown_event.Set(); |
|
|
|
announce_multiplayer_thread.reset(); |
|
|
|
backend->Delete(); |
|
|
|
registered = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback( |
|
|
|
std::function<void(const WebService::WebResult&)> function) { |
|
|
|
std::lock_guard lock(callback_mutex); |
|
|
|
auto handle = std::make_shared<std::function<void(const WebService::WebResult&)>>(function); |
|
|
|
error_callbacks.insert(handle); |
|
|
|
return handle; |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) { |
|
|
|
std::lock_guard lock(callback_mutex); |
|
|
|
error_callbacks.erase(handle); |
|
|
|
} |
|
|
|
|
|
|
|
AnnounceMultiplayerSession::~AnnounceMultiplayerSession() { |
|
|
|
Stop(); |
|
|
|
} |
|
|
|
|
|
|
|
bool AnnounceMultiplayerSession::IsRunning() const { |
|
|
|
return announce_multiplayer_thread != nullptr; |
|
|
|
void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) { |
|
|
|
Network::RoomInformation room_information = room->GetRoomInformation(); |
|
|
|
std::vector<AnnounceMultiplayerRoom::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); |
|
|
|
backend->ClearPlayers(); |
|
|
|
for (const auto& member : memberlist) { |
|
|
|
backend->AddPlayer(member); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() { |
|
|
|
return backend->GetRoomList(); |
|
|
|
} |
|
|
|
|
|
|
|
void AnnounceMultiplayerSession::UpdateCredentials() { |
|
|
|
|