announce_multiplayer_session.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <chrono>
  4. #include <future>
  5. #include <vector>
  6. #include "announce_multiplayer_session.h"
  7. #include "common/announce_multiplayer_room.h"
  8. #include "common/assert.h"
  9. #include "common/settings.h"
  10. #include "network/network.h"
  11. #ifdef ENABLE_WEB_SERVICE
  12. #include "web_service/announce_room_json.h"
  13. #endif
  14. namespace Core {
  15. // Time between room is announced to web_service
  16. static constexpr std::chrono::seconds announce_time_interval(15);
  17. AnnounceMultiplayerSession::AnnounceMultiplayerSession(Network::RoomNetwork& room_network_)
  18. : room_network{room_network_} {
  19. #ifdef ENABLE_WEB_SERVICE
  20. backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
  21. Settings::values.yuzu_username.GetValue(),
  22. Settings::values.yuzu_token.GetValue());
  23. #else
  24. backend = std::make_unique<AnnounceMultiplayerRoom::NullBackend>();
  25. #endif
  26. }
  27. WebService::WebResult AnnounceMultiplayerSession::Register() {
  28. auto room = room_network.GetRoom().lock();
  29. if (!room) {
  30. return WebService::WebResult{WebService::WebResult::Code::LibError,
  31. "Network is not initialized", ""};
  32. }
  33. if (room->GetState() != Network::Room::State::Open) {
  34. return WebService::WebResult{WebService::WebResult::Code::LibError, "Room is not open", ""};
  35. }
  36. UpdateBackendData(room);
  37. WebService::WebResult result = backend->Register();
  38. if (result.result_code != WebService::WebResult::Code::Success) {
  39. return result;
  40. }
  41. LOG_INFO(WebService, "Room has been registered");
  42. room->SetVerifyUID(result.returned_data);
  43. registered = true;
  44. return WebService::WebResult{WebService::WebResult::Code::Success, "", ""};
  45. }
  46. void AnnounceMultiplayerSession::Start() {
  47. if (announce_multiplayer_thread) {
  48. Stop();
  49. }
  50. shutdown_event.Reset();
  51. announce_multiplayer_thread =
  52. std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this);
  53. }
  54. void AnnounceMultiplayerSession::Stop() {
  55. if (announce_multiplayer_thread) {
  56. shutdown_event.Set();
  57. announce_multiplayer_thread->join();
  58. announce_multiplayer_thread.reset();
  59. backend->Delete();
  60. registered = false;
  61. }
  62. }
  63. AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback(
  64. std::function<void(const WebService::WebResult&)> function) {
  65. std::lock_guard lock(callback_mutex);
  66. auto handle = std::make_shared<std::function<void(const WebService::WebResult&)>>(function);
  67. error_callbacks.insert(handle);
  68. return handle;
  69. }
  70. void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) {
  71. std::lock_guard lock(callback_mutex);
  72. error_callbacks.erase(handle);
  73. }
  74. AnnounceMultiplayerSession::~AnnounceMultiplayerSession() {
  75. Stop();
  76. }
  77. void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) {
  78. Network::RoomInformation room_information = room->GetRoomInformation();
  79. std::vector<AnnounceMultiplayerRoom::Member> memberlist = room->GetRoomMemberList();
  80. backend->SetRoomInformation(room_information.name, room_information.description,
  81. room_information.port, room_information.member_slots,
  82. Network::network_version, room->HasPassword(),
  83. room_information.preferred_game);
  84. backend->ClearPlayers();
  85. for (const auto& member : memberlist) {
  86. backend->AddPlayer(member);
  87. }
  88. }
  89. void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
  90. // Invokes all current bound error callbacks.
  91. const auto ErrorCallback = [this](WebService::WebResult result) {
  92. std::lock_guard lock(callback_mutex);
  93. for (auto callback : error_callbacks) {
  94. (*callback)(result);
  95. }
  96. };
  97. if (!registered) {
  98. WebService::WebResult result = Register();
  99. if (result.result_code != WebService::WebResult::Code::Success) {
  100. ErrorCallback(result);
  101. return;
  102. }
  103. }
  104. auto update_time = std::chrono::steady_clock::now();
  105. std::future<WebService::WebResult> future;
  106. while (!shutdown_event.WaitUntil(update_time)) {
  107. update_time += announce_time_interval;
  108. auto room = room_network.GetRoom().lock();
  109. if (!room) {
  110. break;
  111. }
  112. if (room->GetState() != Network::Room::State::Open) {
  113. break;
  114. }
  115. UpdateBackendData(room);
  116. WebService::WebResult result = backend->Update();
  117. if (result.result_code != WebService::WebResult::Code::Success) {
  118. ErrorCallback(result);
  119. }
  120. if (result.result_string == "404") {
  121. registered = false;
  122. // Needs to register the room again
  123. WebService::WebResult register_result = Register();
  124. if (register_result.result_code != WebService::WebResult::Code::Success) {
  125. ErrorCallback(register_result);
  126. }
  127. }
  128. }
  129. }
  130. AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() {
  131. return backend->GetRoomList();
  132. }
  133. bool AnnounceMultiplayerSession::IsRunning() const {
  134. return announce_multiplayer_thread != nullptr;
  135. }
  136. void AnnounceMultiplayerSession::UpdateCredentials() {
  137. ASSERT_MSG(!IsRunning(), "Credentials can only be updated when session is not running");
  138. #ifdef ENABLE_WEB_SERVICE
  139. backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
  140. Settings::values.yuzu_username.GetValue(),
  141. Settings::values.yuzu_token.GetValue());
  142. #endif
  143. }
  144. } // namespace Core