From 8b6bb39e82b66c0faa7ac29512b3f68167874404 Mon Sep 17 00:00:00 2001 From: spycrab Date: Thu, 11 Apr 2019 01:21:40 +0200 Subject: [PATCH] Qt/NetPlay: Show feedback for index adding --- Source/Core/Core/NetPlayClient.h | 3 +++ Source/Core/Core/NetPlayServer.cpp | 11 ++++++++++- Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp | 13 +++++++++++++ Source/Core/DolphinQt/NetPlay/NetPlayDialog.h | 3 +++ Source/Core/UICommon/NetPlayIndex.cpp | 14 ++++++++++++++ Source/Core/UICommon/NetPlayIndex.h | 7 +++++++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index 3afaadd815..7ece1a5a68 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -64,6 +64,9 @@ public: virtual void SetMD5Result(int pid, const std::string& result) = 0; virtual void AbortMD5() = 0; + virtual void OnIndexAdded(bool success, std::string error) = 0; + virtual void OnIndexRefreshFailed(std::string error) = 0; + virtual void ShowChunkedProgressDialog(const std::string& title, u64 data_size, const std::vector& players) = 0; virtual void HideChunkedProgressDialog() = 0; diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 151cf2c194..0e111aa088 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -183,6 +183,9 @@ void NetPlayServer::SetupIndex() if (m_traversal_client) { + if (m_traversal_client->GetState() != TraversalClient::Connected) + return; + session.server_id = std::string(g_TraversalClient->GetHostID().data(), 8); } else @@ -201,7 +204,13 @@ void NetPlayServer::SetupIndex() session.EncryptID(Config::Get(Config::NETPLAY_INDEX_PASSWORD)); - m_index.Add(session); + if (m_dialog != nullptr) + m_dialog->OnIndexAdded(m_index.Add(session), m_index.GetLastError()); + + m_index.SetErrorCallback([this] { + if (m_dialog != nullptr) + m_dialog->OnIndexRefreshFailed(m_index.GetLastError()); + }); } // called from ---NETPLAY--- thread diff --git a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp index 09fa19e850..5b0eebc9d4 100644 --- a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp +++ b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp @@ -393,6 +393,19 @@ void NetPlayDialog::OnChat() }); } +void NetPlayDialog::OnIndexAdded(bool success, const std::string error) +{ + DisplayMessage(success ? tr("Successfully added to the NetPlay index") : + tr("Failed to add this session to the NetPlay index: %1") + .arg(QString::fromStdString(error)), + success ? "green" : "red"); +} + +void NetPlayDialog::OnIndexRefreshFailed(const std::string error) +{ + DisplayMessage(QString::fromStdString(error), "red"); +} + void NetPlayDialog::OnStart() { if (!Settings::Instance().GetNetPlayClient()->DoAllPlayersHaveGame()) diff --git a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h index e6d601afaa..39c0573340 100644 --- a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h +++ b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h @@ -60,6 +60,9 @@ public: void OnGameStartAborted() override; void OnGolferChanged(bool is_golfer, const std::string& golfer_name) override; + void OnIndexAdded(bool success, const std::string error) override; + void OnIndexRefreshFailed(const std::string error) override; + bool IsRecording() override; std::string FindGame(const std::string& game) override; std::shared_ptr FindGameFile(const std::string& game) override; diff --git a/Source/Core/UICommon/NetPlayIndex.cpp b/Source/Core/UICommon/NetPlayIndex.cpp index 36cb103243..ab90872d47 100644 --- a/Source/Core/UICommon/NetPlayIndex.cpp +++ b/Source/Core/UICommon/NetPlayIndex.cpp @@ -142,6 +142,8 @@ void NetPlayIndex::NotificationLoop() if (!json) { m_last_error = "BAD_JSON"; + m_secret.clear(); + m_error_callback(); return; } @@ -150,6 +152,8 @@ void NetPlayIndex::NotificationLoop() if (status != "OK") { m_last_error = std::move(status); + m_secret.clear(); + m_error_callback(); return; } } @@ -318,3 +322,13 @@ const std::string& NetPlayIndex::GetLastError() const { return m_last_error; } + +bool NetPlayIndex::HasActiveSession() const +{ + return !m_secret.empty(); +} + +void NetPlayIndex::SetErrorCallback(std::function function) +{ + m_error_callback = function; +} diff --git a/Source/Core/UICommon/NetPlayIndex.h b/Source/Core/UICommon/NetPlayIndex.h index b892d42a48..f451301152 100644 --- a/Source/Core/UICommon/NetPlayIndex.h +++ b/Source/Core/UICommon/NetPlayIndex.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -46,12 +47,16 @@ public: bool Add(NetPlaySession session); void Remove(); + bool HasActiveSession() const; + void SetPlayerCount(int player_count); void SetInGame(bool in_game); void SetGame(std::string game); const std::string& GetLastError() const; + void SetErrorCallback(std::function callback); + private: void NotificationLoop(); @@ -64,4 +69,6 @@ private: std::thread m_session_thread; Common::Event m_session_thread_exit_event; + + std::function m_error_callback = nullptr; };