1
0
Fork 0
forked from suyu/suyu

network: Move global state into a seperate class

Co-Authored-By: Narr the Reg <5944268+german77@users.noreply.github.com>
This commit is contained in:
FearlessTobi 2022-07-22 16:31:13 +02:00
parent 899c8bb330
commit 7d82e57b91
21 changed files with 152 additions and 98 deletions

View file

@ -20,7 +20,8 @@ namespace Core {
// Time between room is announced to web_service // Time between room is announced to web_service
static constexpr std::chrono::seconds announce_time_interval(15); static constexpr std::chrono::seconds announce_time_interval(15);
AnnounceMultiplayerSession::AnnounceMultiplayerSession() { AnnounceMultiplayerSession::AnnounceMultiplayerSession(Network::RoomNetwork& room_network_)
: room_network{room_network_} {
#ifdef ENABLE_WEB_SERVICE #ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(), backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
Settings::values.yuzu_username.GetValue(), Settings::values.yuzu_username.GetValue(),
@ -31,7 +32,7 @@ AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
} }
WebService::WebResult AnnounceMultiplayerSession::Register() { WebService::WebResult AnnounceMultiplayerSession::Register() {
std::shared_ptr<Network::Room> room = Network::GetRoom().lock(); std::shared_ptr<Network::Room> room = room_network.GetRoom().lock();
if (!room) { if (!room) {
return WebService::WebResult{WebService::WebResult::Code::LibError, return WebService::WebResult{WebService::WebResult::Code::LibError,
"Network is not initialized", ""}; "Network is not initialized", ""};
@ -120,7 +121,7 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
std::future<WebService::WebResult> future; std::future<WebService::WebResult> future;
while (!shutdown_event.WaitUntil(update_time)) { while (!shutdown_event.WaitUntil(update_time)) {
update_time += announce_time_interval; update_time += announce_time_interval;
std::shared_ptr<Network::Room> room = Network::GetRoom().lock(); std::shared_ptr<Network::Room> room = room_network.GetRoom().lock();
if (!room) { if (!room) {
break; break;
} }

View file

@ -16,7 +16,8 @@
namespace Network { namespace Network {
class Room; class Room;
} class RoomNetwork;
} // namespace Network
namespace Core { namespace Core {
@ -28,7 +29,7 @@ namespace Core {
class AnnounceMultiplayerSession { class AnnounceMultiplayerSession {
public: public:
using CallbackHandle = std::shared_ptr<std::function<void(const WebService::WebResult&)>>; using CallbackHandle = std::shared_ptr<std::function<void(const WebService::WebResult&)>>;
AnnounceMultiplayerSession(); AnnounceMultiplayerSession(Network::RoomNetwork& room_network_);
~AnnounceMultiplayerSession(); ~AnnounceMultiplayerSession();
/** /**
@ -79,6 +80,9 @@ public:
void UpdateCredentials(); void UpdateCredentials();
private: private:
void UpdateBackendData(std::shared_ptr<Network::Room> room);
void AnnounceMultiplayerLoop();
Common::Event shutdown_event; Common::Event shutdown_event;
std::mutex callback_mutex; std::mutex callback_mutex;
std::set<CallbackHandle> error_callbacks; std::set<CallbackHandle> error_callbacks;
@ -89,8 +93,7 @@ private:
std::atomic_bool registered = false; ///< Whether the room has been registered std::atomic_bool registered = false; ///< Whether the room has been registered
void UpdateBackendData(std::shared_ptr<Network::Room> room); Network::RoomNetwork& room_network;
void AnnounceMultiplayerLoop();
}; };
} // namespace Core } // namespace Core

View file

@ -131,7 +131,7 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
struct System::Impl { struct System::Impl {
explicit Impl(System& system) explicit Impl(System& system)
: kernel{system}, fs_controller{system}, memory{system}, hid_core{}, : kernel{system}, fs_controller{system}, memory{system}, hid_core{}, room_network{},
cpu_manager{system}, reporter{system}, applet_manager{system}, time_manager{system} {} cpu_manager{system}, reporter{system}, applet_manager{system}, time_manager{system} {}
SystemResultStatus Run() { SystemResultStatus Run() {
@ -320,7 +320,7 @@ struct System::Impl {
if (app_loader->ReadTitle(name) != Loader::ResultStatus::Success) { if (app_loader->ReadTitle(name) != Loader::ResultStatus::Success) {
LOG_ERROR(Core, "Failed to read title for ROM (Error {})", load_result); LOG_ERROR(Core, "Failed to read title for ROM (Error {})", load_result);
} }
if (auto room_member = Network::GetRoomMember().lock()) { if (auto room_member = room_network.GetRoomMember().lock()) {
Network::GameInfo game_info; Network::GameInfo game_info;
game_info.name = name; game_info.name = name;
game_info.id = program_id; game_info.id = program_id;
@ -374,7 +374,7 @@ struct System::Impl {
memory.Reset(); memory.Reset();
applet_manager.ClearAll(); applet_manager.ClearAll();
if (auto room_member = Network::GetRoomMember().lock()) { if (auto room_member = room_network.GetRoomMember().lock()) {
Network::GameInfo game_info{}; Network::GameInfo game_info{};
room_member->SendGameInfo(game_info); room_member->SendGameInfo(game_info);
} }
@ -451,6 +451,8 @@ struct System::Impl {
std::unique_ptr<AudioCore::AudioCore> audio_core; std::unique_ptr<AudioCore::AudioCore> audio_core;
Core::Memory::Memory memory; Core::Memory::Memory memory;
Core::HID::HIDCore hid_core; Core::HID::HIDCore hid_core;
Network::RoomNetwork room_network;
CpuManager cpu_manager; CpuManager cpu_manager;
std::atomic_bool is_powered_on{}; std::atomic_bool is_powered_on{};
bool exit_lock = false; bool exit_lock = false;
@ -896,6 +898,14 @@ const Core::Debugger& System::GetDebugger() const {
return *impl->debugger; return *impl->debugger;
} }
Network::RoomNetwork& System::GetRoomNetwork() {
return impl->room_network;
}
const Network::RoomNetwork& System::GetRoomNetwork() const {
return impl->room_network;
}
void System::RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback) { void System::RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback) {
impl->execute_program_callback = std::move(callback); impl->execute_program_callback = std::move(callback);
} }

View file

@ -97,6 +97,10 @@ namespace Core::HID {
class HIDCore; class HIDCore;
} }
namespace Network {
class RoomNetwork;
}
namespace Core { namespace Core {
class ARM_Interface; class ARM_Interface;
@ -379,6 +383,12 @@ public:
[[nodiscard]] Core::Debugger& GetDebugger(); [[nodiscard]] Core::Debugger& GetDebugger();
[[nodiscard]] const Core::Debugger& GetDebugger() const; [[nodiscard]] const Core::Debugger& GetDebugger() const;
/// Gets a mutable reference to the Room Network.
[[nodiscard]] Network::RoomNetwork& GetRoomNetwork();
/// Gets an immutable reference to the Room Network.
[[nodiscard]] const Network::RoomNetwork& GetRoomNetwork() const;
void SetExitLock(bool locked); void SetExitLock(bool locked);
[[nodiscard]] bool GetExitLock() const; [[nodiscard]] bool GetExitLock() const;

View file

@ -9,11 +9,12 @@
namespace Network { namespace Network {
static std::shared_ptr<RoomMember> g_room_member; ///< RoomMember (Client) for network games RoomNetwork::RoomNetwork() {
static std::shared_ptr<Room> g_room; ///< Room (Server) for network games g_room = std::make_shared<Room>();
// TODO(B3N30): Put these globals into a networking class g_room_member = std::make_shared<RoomMember>();
}
bool Init() { bool RoomNetwork::Init() {
if (enet_initialize() != 0) { if (enet_initialize() != 0) {
LOG_ERROR(Network, "Error initalizing ENet"); LOG_ERROR(Network, "Error initalizing ENet");
return false; return false;
@ -24,15 +25,15 @@ bool Init() {
return true; return true;
} }
std::weak_ptr<Room> GetRoom() { std::weak_ptr<Room> RoomNetwork::GetRoom() {
return g_room; return g_room;
} }
std::weak_ptr<RoomMember> GetRoomMember() { std::weak_ptr<RoomMember> RoomNetwork::GetRoomMember() {
return g_room_member; return g_room_member;
} }
void Shutdown() { void RoomNetwork::Shutdown() {
if (g_room_member) { if (g_room_member) {
if (g_room_member->IsConnected()) if (g_room_member->IsConnected())
g_room_member->Leave(); g_room_member->Leave();

View file

@ -10,16 +10,25 @@
namespace Network { namespace Network {
/// Initializes and registers the network device, the room, and the room member. class RoomNetwork {
bool Init(); public:
RoomNetwork();
/// Returns a pointer to the room handle /// Initializes and registers the network device, the room, and the room member.
std::weak_ptr<Room> GetRoom(); bool Init();
/// Returns a pointer to the room member handle /// Returns a pointer to the room handle
std::weak_ptr<RoomMember> GetRoomMember(); std::weak_ptr<Room> GetRoom();
/// Unregisters the network device, the room, and the room member and shut them down. /// Returns a pointer to the room member handle
void Shutdown(); std::weak_ptr<RoomMember> GetRoomMember();
/// Unregisters the network device, the room, and the room member and shut them down.
void Shutdown();
private:
std::shared_ptr<RoomMember> g_room_member; ///< RoomMember (Client) for network games
std::shared_ptr<Room> g_room; ///< Room (Server) for network games
};
} // namespace Network } // namespace Network

View file

@ -273,7 +273,7 @@ GMainWindow::GMainWindow(bool has_broken_vulkan)
SetDiscordEnabled(UISettings::values.enable_discord_presence.GetValue()); SetDiscordEnabled(UISettings::values.enable_discord_presence.GetValue());
discord_rpc->Update(); discord_rpc->Update();
Network::Init(); system->GetRoomNetwork().Init();
RegisterMetaTypes(); RegisterMetaTypes();
@ -463,7 +463,7 @@ GMainWindow::~GMainWindow() {
if (render_window->parent() == nullptr) { if (render_window->parent() == nullptr) {
delete render_window; delete render_window;
} }
Network::Shutdown(); system->GetRoomNetwork().Shutdown();
} }
void GMainWindow::RegisterMetaTypes() { void GMainWindow::RegisterMetaTypes() {
@ -828,7 +828,7 @@ void GMainWindow::InitializeWidgets() {
}); });
multiplayer_state = new MultiplayerState(this, game_list->GetModel(), ui->action_Leave_Room, multiplayer_state = new MultiplayerState(this, game_list->GetModel(), ui->action_Leave_Room,
ui->action_Show_Room); ui->action_Show_Room, system->GetRoomNetwork());
multiplayer_state->setVisible(false); multiplayer_state->setVisible(false);
// Create status bar // Create status bar

View file

@ -28,7 +28,8 @@
class ChatMessage { class ChatMessage {
public: public:
explicit ChatMessage(const Network::ChatEntry& chat, QTime ts = {}) { explicit ChatMessage(const Network::ChatEntry& chat, Network::RoomNetwork& room_network,
QTime ts = {}) {
/// Convert the time to their default locale defined format /// Convert the time to their default locale defined format
QLocale locale; QLocale locale;
timestamp = locale.toString(ts.isValid() ? ts : QTime::currentTime(), QLocale::ShortFormat); timestamp = locale.toString(ts.isValid() ? ts : QTime::currentTime(), QLocale::ShortFormat);
@ -38,7 +39,7 @@ public:
// Check for user pings // Check for user pings
QString cur_nickname, cur_username; QString cur_nickname, cur_username;
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network.GetRoomMember().lock()) {
cur_nickname = QString::fromStdString(room->GetNickname()); cur_nickname = QString::fromStdString(room->GetNickname());
cur_username = QString::fromStdString(room->GetUsername()); cur_username = QString::fromStdString(room->GetUsername());
} }
@ -173,20 +174,6 @@ ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_unique<Ui::C
qRegisterMetaType<Network::RoomInformation>(); qRegisterMetaType<Network::RoomInformation>();
qRegisterMetaType<Network::RoomMember::State>(); qRegisterMetaType<Network::RoomMember::State>();
// setup the callbacks for network updates
if (auto member = Network::GetRoomMember().lock()) {
member->BindOnChatMessageRecieved(
[this](const Network::ChatEntry& chat) { emit ChatReceived(chat); });
member->BindOnStatusMessageReceived(
[this](const Network::StatusMessageEntry& status_message) {
emit StatusMessageReceived(status_message);
});
connect(this, &ChatRoom::ChatReceived, this, &ChatRoom::OnChatReceive);
connect(this, &ChatRoom::StatusMessageReceived, this, &ChatRoom::OnStatusMessageReceive);
} else {
// TODO (jroweboy) network was not initialized?
}
// Connect all the widgets to the appropriate events // Connect all the widgets to the appropriate events
connect(ui->player_view, &QTreeView::customContextMenuRequested, this, connect(ui->player_view, &QTreeView::customContextMenuRequested, this,
&ChatRoom::PopupContextMenu); &ChatRoom::PopupContextMenu);
@ -197,6 +184,21 @@ ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_unique<Ui::C
ChatRoom::~ChatRoom() = default; ChatRoom::~ChatRoom() = default;
void ChatRoom::Initialize(Network::RoomNetwork* room_network_) {
room_network = room_network_;
// setup the callbacks for network updates
if (auto member = room_network->GetRoomMember().lock()) {
member->BindOnChatMessageRecieved(
[this](const Network::ChatEntry& chat) { emit ChatReceived(chat); });
member->BindOnStatusMessageReceived(
[this](const Network::StatusMessageEntry& status_message) {
emit StatusMessageReceived(status_message);
});
connect(this, &ChatRoom::ChatReceived, this, &ChatRoom::OnChatReceive);
connect(this, &ChatRoom::StatusMessageReceived, this, &ChatRoom::OnStatusMessageReceive);
}
}
void ChatRoom::SetModPerms(bool is_mod) { void ChatRoom::SetModPerms(bool is_mod) {
has_mod_perms = is_mod; has_mod_perms = is_mod;
} }
@ -219,7 +221,7 @@ void ChatRoom::AppendChatMessage(const QString& msg) {
} }
void ChatRoom::SendModerationRequest(Network::RoomMessageTypes type, const std::string& nickname) { void ChatRoom::SendModerationRequest(Network::RoomMessageTypes type, const std::string& nickname) {
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network->GetRoomMember().lock()) {
auto members = room->GetMemberInformation(); auto members = room->GetMemberInformation();
auto it = std::find_if(members.begin(), members.end(), auto it = std::find_if(members.begin(), members.end(),
[&nickname](const Network::RoomMember::MemberInformation& member) { [&nickname](const Network::RoomMember::MemberInformation& member) {
@ -239,7 +241,7 @@ bool ChatRoom::ValidateMessage(const std::string& msg) {
void ChatRoom::OnRoomUpdate(const Network::RoomInformation& info) { void ChatRoom::OnRoomUpdate(const Network::RoomInformation& info) {
// TODO(B3N30): change title // TODO(B3N30): change title
if (auto room_member = Network::GetRoomMember().lock()) { if (auto room_member = room_network->GetRoomMember().lock()) {
SetPlayerList(room_member->GetMemberInformation()); SetPlayerList(room_member->GetMemberInformation());
} }
} }
@ -258,7 +260,7 @@ void ChatRoom::OnChatReceive(const Network::ChatEntry& chat) {
if (!ValidateMessage(chat.message)) { if (!ValidateMessage(chat.message)) {
return; return;
} }
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network->GetRoomMember().lock()) {
// get the id of the player // get the id of the player
auto members = room->GetMemberInformation(); auto members = room->GetMemberInformation();
auto it = std::find_if(members.begin(), members.end(), auto it = std::find_if(members.begin(), members.end(),
@ -276,7 +278,7 @@ void ChatRoom::OnChatReceive(const Network::ChatEntry& chat) {
return; return;
} }
auto player = std::distance(members.begin(), it); auto player = std::distance(members.begin(), it);
ChatMessage m(chat); ChatMessage m(chat, *room_network);
if (m.ContainsPing()) { if (m.ContainsPing()) {
emit UserPinged(); emit UserPinged();
} }
@ -315,7 +317,7 @@ void ChatRoom::OnStatusMessageReceive(const Network::StatusMessageEntry& status_
} }
void ChatRoom::OnSendChat() { void ChatRoom::OnSendChat() {
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network->GetRoomMember().lock()) {
if (room->GetState() != Network::RoomMember::State::Joined && if (room->GetState() != Network::RoomMember::State::Joined &&
room->GetState() != Network::RoomMember::State::Moderator) { room->GetState() != Network::RoomMember::State::Moderator) {
@ -339,7 +341,7 @@ void ChatRoom::OnSendChat() {
LOG_INFO(Network, "Cannot find self in the player list when sending a message."); LOG_INFO(Network, "Cannot find self in the player list when sending a message.");
} }
auto player = std::distance(members.begin(), it); auto player = std::distance(members.begin(), it);
ChatMessage m(chat); ChatMessage m(chat, *room_network);
room->SendChatMessage(message); room->SendChatMessage(message);
AppendChatMessage(m.GetPlayerChatMessage(player)); AppendChatMessage(m.GetPlayerChatMessage(player));
ui->chat_message->clear(); ui->chat_message->clear();
@ -433,7 +435,7 @@ void ChatRoom::PopupContextMenu(const QPoint& menu_location) {
} }
std::string cur_nickname; std::string cur_nickname;
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network->GetRoomMember().lock()) {
cur_nickname = room->GetNickname(); cur_nickname = room->GetNickname();
} }

View file

@ -30,6 +30,7 @@ class ChatRoom : public QWidget {
public: public:
explicit ChatRoom(QWidget* parent); explicit ChatRoom(QWidget* parent);
void Initialize(Network::RoomNetwork* room_network);
void RetranslateUi(); void RetranslateUi();
void SetPlayerList(const Network::RoomMember::MemberList& member_list); void SetPlayerList(const Network::RoomMember::MemberList& member_list);
void Clear(); void Clear();
@ -65,6 +66,7 @@ private:
std::unique_ptr<Ui::ChatRoom> ui; std::unique_ptr<Ui::ChatRoom> ui;
std::unordered_set<std::string> block_list; std::unordered_set<std::string> block_list;
std::unordered_map<std::string, QPixmap> icon_cache; std::unordered_map<std::string, QPixmap> icon_cache;
Network::RoomNetwork* room_network;
}; };
Q_DECLARE_METATYPE(Network::ChatEntry); Q_DECLARE_METATYPE(Network::ChatEntry);

View file

@ -19,13 +19,14 @@
#include "yuzu/multiplayer/moderation_dialog.h" #include "yuzu/multiplayer/moderation_dialog.h"
#include "yuzu/multiplayer/state.h" #include "yuzu/multiplayer/state.h"
ClientRoomWindow::ClientRoomWindow(QWidget* parent) ClientRoomWindow::ClientRoomWindow(QWidget* parent, Network::RoomNetwork& room_network_)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint), : QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint),
ui(std::make_unique<Ui::ClientRoom>()) { ui(std::make_unique<Ui::ClientRoom>()), room_network{room_network_} {
ui->setupUi(this); ui->setupUi(this);
ui->chat->Initialize(&room_network);
// setup the callbacks for network updates // setup the callbacks for network updates
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
member->BindOnRoomInformationChanged( member->BindOnRoomInformationChanged(
[this](const Network::RoomInformation& info) { emit RoomInformationChanged(info); }); [this](const Network::RoomInformation& info) { emit RoomInformationChanged(info); });
member->BindOnStateChanged( member->BindOnStateChanged(
@ -44,7 +45,7 @@ ClientRoomWindow::ClientRoomWindow(QWidget* parent)
ui->disconnect->setDefault(false); ui->disconnect->setDefault(false);
ui->disconnect->setAutoDefault(false); ui->disconnect->setAutoDefault(false);
connect(ui->moderation, &QPushButton::clicked, [this] { connect(ui->moderation, &QPushButton::clicked, [this] {
ModerationDialog dialog(this); ModerationDialog dialog(room_network, this);
dialog.exec(); dialog.exec();
}); });
ui->moderation->setDefault(false); ui->moderation->setDefault(false);
@ -91,7 +92,7 @@ void ClientRoomWindow::Disconnect() {
} }
void ClientRoomWindow::UpdateView() { void ClientRoomWindow::UpdateView() {
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
if (member->IsConnected()) { if (member->IsConnected()) {
ui->chat->Enable(); ui->chat->Enable();
ui->disconnect->setEnabled(true); ui->disconnect->setEnabled(true);

View file

@ -14,7 +14,7 @@ class ClientRoomWindow : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
explicit ClientRoomWindow(QWidget* parent); explicit ClientRoomWindow(QWidget* parent, Network::RoomNetwork& room_network_);
~ClientRoomWindow(); ~ClientRoomWindow();
void RetranslateUi(); void RetranslateUi();
@ -36,4 +36,5 @@ private:
QStandardItemModel* player_list; QStandardItemModel* player_list;
std::unique_ptr<Ui::ClientRoom> ui; std::unique_ptr<Ui::ClientRoom> ui;
Network::RoomNetwork& room_network;
}; };

View file

@ -21,9 +21,9 @@
enum class ConnectionType : u8 { TraversalServer, IP }; enum class ConnectionType : u8 { TraversalServer, IP };
DirectConnectWindow::DirectConnectWindow(QWidget* parent) DirectConnectWindow::DirectConnectWindow(Network::RoomNetwork& room_network_, QWidget* parent)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint), : QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint),
ui(std::make_unique<Ui::DirectConnect>()) { ui(std::make_unique<Ui::DirectConnect>()), room_network{room_network_} {
ui->setupUi(this); ui->setupUi(this);
@ -58,7 +58,7 @@ void DirectConnectWindow::Connect() {
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::USERNAME_NOT_VALID); NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::USERNAME_NOT_VALID);
return; return;
} }
if (const auto member = Network::GetRoomMember().lock()) { if (const auto member = room_network.GetRoomMember().lock()) {
// Prevent the user from trying to join a room while they are already joining. // Prevent the user from trying to join a room while they are already joining.
if (member->GetState() == Network::RoomMember::State::Joining) { if (member->GetState() == Network::RoomMember::State::Joining) {
return; return;
@ -96,7 +96,7 @@ void DirectConnectWindow::Connect() {
// attempt to connect in a different thread // attempt to connect in a different thread
QFuture<void> f = QtConcurrent::run([&] { QFuture<void> f = QtConcurrent::run([&] {
if (auto room_member = Network::GetRoomMember().lock()) { if (auto room_member = room_network.GetRoomMember().lock()) {
auto port = UISettings::values.multiplayer_port.GetValue(); auto port = UISettings::values.multiplayer_port.GetValue();
room_member->Join(ui->nickname->text().toStdString(), "", room_member->Join(ui->nickname->text().toStdString(), "",
ui->ip->text().toStdString().c_str(), port, 0, ui->ip->text().toStdString().c_str(), port, 0,
@ -121,7 +121,7 @@ void DirectConnectWindow::EndConnecting() {
void DirectConnectWindow::OnConnection() { void DirectConnectWindow::OnConnection() {
EndConnecting(); EndConnecting();
if (auto room_member = Network::GetRoomMember().lock()) { if (auto room_member = room_network.GetRoomMember().lock()) {
if (room_member->GetState() == Network::RoomMember::State::Joined || if (room_member->GetState() == Network::RoomMember::State::Joined ||
room_member->GetState() == Network::RoomMember::State::Moderator) { room_member->GetState() == Network::RoomMember::State::Moderator) {

View file

@ -17,7 +17,7 @@ class DirectConnectWindow : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
explicit DirectConnectWindow(QWidget* parent = nullptr); explicit DirectConnectWindow(Network::RoomNetwork& room_network_, QWidget* parent = nullptr);
~DirectConnectWindow(); ~DirectConnectWindow();
void RetranslateUi(); void RetranslateUi();
@ -40,4 +40,5 @@ private:
QFutureWatcher<void>* watcher; QFutureWatcher<void>* watcher;
std::unique_ptr<Ui::DirectConnect> ui; std::unique_ptr<Ui::DirectConnect> ui;
Validation validation; Validation validation;
Network::RoomNetwork& room_network;
}; };

View file

@ -27,9 +27,11 @@
#endif #endif
HostRoomWindow::HostRoomWindow(QWidget* parent, QStandardItemModel* list, HostRoomWindow::HostRoomWindow(QWidget* parent, QStandardItemModel* list,
std::shared_ptr<Core::AnnounceMultiplayerSession> session) std::shared_ptr<Core::AnnounceMultiplayerSession> session,
Network::RoomNetwork& room_network_)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint), : QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint),
ui(std::make_unique<Ui::HostRoom>()), announce_multiplayer_session(session) { ui(std::make_unique<Ui::HostRoom>()),
announce_multiplayer_session(session), room_network{room_network_} {
ui->setupUi(this); ui->setupUi(this);
// set up validation for all of the fields // set up validation for all of the fields
@ -120,7 +122,7 @@ void HostRoomWindow::Host() {
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::GAME_NOT_SELECTED); NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::GAME_NOT_SELECTED);
return; return;
} }
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
if (member->GetState() == Network::RoomMember::State::Joining) { if (member->GetState() == Network::RoomMember::State::Joining) {
return; return;
} else if (member->IsConnected()) { } else if (member->IsConnected()) {
@ -144,7 +146,7 @@ void HostRoomWindow::Host() {
if (ui->load_ban_list->isChecked()) { if (ui->load_ban_list->isChecked()) {
ban_list = UISettings::values.multiplayer_ban_list; ban_list = UISettings::values.multiplayer_ban_list;
} }
if (auto room = Network::GetRoom().lock()) { if (auto room = room_network.GetRoom().lock()) {
const bool created = const bool created =
room->Create(ui->room_name->text().toStdString(), room->Create(ui->room_name->text().toStdString(),
ui->room_description->toPlainText().toStdString(), "", port, password, ui->room_description->toPlainText().toStdString(), "", port, password,
@ -173,7 +175,7 @@ void HostRoomWindow::Host() {
QString::fromStdString(result.result_string), QString::fromStdString(result.result_string),
QMessageBox::Ok); QMessageBox::Ok);
ui->host->setEnabled(true); ui->host->setEnabled(true);
if (auto room = Network::GetRoom().lock()) { if (auto room = room_network.GetRoom().lock()) {
room->Destroy(); room->Destroy();
} }
return; return;
@ -189,7 +191,7 @@ void HostRoomWindow::Host() {
WebService::Client client(Settings::values.web_api_url.GetValue(), WebService::Client client(Settings::values.web_api_url.GetValue(),
Settings::values.yuzu_username.GetValue(), Settings::values.yuzu_username.GetValue(),
Settings::values.yuzu_token.GetValue()); Settings::values.yuzu_token.GetValue());
if (auto room = Network::GetRoom().lock()) { if (auto room = room_network.GetRoom().lock()) {
token = client.GetExternalJWT(room->GetVerifyUID()).returned_data; token = client.GetExternalJWT(room->GetVerifyUID()).returned_data;
} }
if (token.empty()) { if (token.empty()) {

View file

@ -35,7 +35,8 @@ class HostRoomWindow : public QDialog {
public: public:
explicit HostRoomWindow(QWidget* parent, QStandardItemModel* list, explicit HostRoomWindow(QWidget* parent, QStandardItemModel* list,
std::shared_ptr<Core::AnnounceMultiplayerSession> session); std::shared_ptr<Core::AnnounceMultiplayerSession> session,
Network::RoomNetwork& room_network_);
~HostRoomWindow(); ~HostRoomWindow();
/** /**
@ -54,6 +55,7 @@ private:
QStandardItemModel* game_list; QStandardItemModel* game_list;
ComboBoxProxyModel* proxy; ComboBoxProxyModel* proxy;
Validation validation; Validation validation;
Network::RoomNetwork& room_network;
}; };
/** /**

View file

@ -23,9 +23,11 @@
#endif #endif
Lobby::Lobby(QWidget* parent, QStandardItemModel* list, Lobby::Lobby(QWidget* parent, QStandardItemModel* list,
std::shared_ptr<Core::AnnounceMultiplayerSession> session) std::shared_ptr<Core::AnnounceMultiplayerSession> session,
Network::RoomNetwork& room_network_)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint), : QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint),
ui(std::make_unique<Ui::Lobby>()), announce_multiplayer_session(session) { ui(std::make_unique<Ui::Lobby>()),
announce_multiplayer_session(session), room_network{room_network_} {
ui->setupUi(this); ui->setupUi(this);
// setup the watcher for background connections // setup the watcher for background connections
@ -113,7 +115,7 @@ void Lobby::OnExpandRoom(const QModelIndex& index) {
} }
void Lobby::OnJoinRoom(const QModelIndex& source) { void Lobby::OnJoinRoom(const QModelIndex& source) {
if (const auto member = Network::GetRoomMember().lock()) { if (const auto member = room_network.GetRoomMember().lock()) {
// Prevent the user from trying to join a room while they are already joining. // Prevent the user from trying to join a room while they are already joining.
if (member->GetState() == Network::RoomMember::State::Joining) { if (member->GetState() == Network::RoomMember::State::Joining) {
return; return;
@ -151,7 +153,7 @@ void Lobby::OnJoinRoom(const QModelIndex& source) {
proxy->data(connection_index, LobbyItemHost::HostVerifyUIDRole).toString().toStdString(); proxy->data(connection_index, LobbyItemHost::HostVerifyUIDRole).toString().toStdString();
// attempt to connect in a different thread // attempt to connect in a different thread
QFuture<void> f = QtConcurrent::run([nickname, ip, port, password, verify_UID] { QFuture<void> f = QtConcurrent::run([nickname, ip, port, password, verify_UID, this] {
std::string token; std::string token;
#ifdef ENABLE_WEB_SERVICE #ifdef ENABLE_WEB_SERVICE
if (!Settings::values.yuzu_username.GetValue().empty() && if (!Settings::values.yuzu_username.GetValue().empty() &&
@ -167,7 +169,7 @@ void Lobby::OnJoinRoom(const QModelIndex& source) {
} }
} }
#endif #endif
if (auto room_member = Network::GetRoomMember().lock()) { if (auto room_member = room_network.GetRoomMember().lock()) {
room_member->Join(nickname, "", ip.c_str(), port, 0, Network::NoPreferredMac, password, room_member->Join(nickname, "", ip.c_str(), port, 0, Network::NoPreferredMac, password,
token); token);
} }

View file

@ -30,7 +30,8 @@ class Lobby : public QDialog {
public: public:
explicit Lobby(QWidget* parent, QStandardItemModel* list, explicit Lobby(QWidget* parent, QStandardItemModel* list,
std::shared_ptr<Core::AnnounceMultiplayerSession> session); std::shared_ptr<Core::AnnounceMultiplayerSession> session,
Network::RoomNetwork& room_network_);
~Lobby() override; ~Lobby() override;
/** /**
@ -94,6 +95,7 @@ private:
std::weak_ptr<Core::AnnounceMultiplayerSession> announce_multiplayer_session; std::weak_ptr<Core::AnnounceMultiplayerSession> announce_multiplayer_session;
QFutureWatcher<void>* watcher; QFutureWatcher<void>* watcher;
Validation validation; Validation validation;
Network::RoomNetwork& room_network;
}; };
/** /**

View file

@ -17,13 +17,13 @@ enum {
}; };
} }
ModerationDialog::ModerationDialog(QWidget* parent) ModerationDialog::ModerationDialog(Network::RoomNetwork& room_network_, QWidget* parent)
: QDialog(parent), ui(std::make_unique<Ui::ModerationDialog>()) { : QDialog(parent), ui(std::make_unique<Ui::ModerationDialog>()), room_network{room_network_} {
ui->setupUi(this); ui->setupUi(this);
qRegisterMetaType<Network::Room::BanList>(); qRegisterMetaType<Network::Room::BanList>();
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
callback_handle_status_message = member->BindOnStatusMessageReceived( callback_handle_status_message = member->BindOnStatusMessageReceived(
[this](const Network::StatusMessageEntry& status_message) { [this](const Network::StatusMessageEntry& status_message) {
emit StatusMessageReceived(status_message); emit StatusMessageReceived(status_message);
@ -56,20 +56,20 @@ ModerationDialog::ModerationDialog(QWidget* parent)
ModerationDialog::~ModerationDialog() { ModerationDialog::~ModerationDialog() {
if (callback_handle_status_message) { if (callback_handle_status_message) {
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network.GetRoomMember().lock()) {
room->Unbind(callback_handle_status_message); room->Unbind(callback_handle_status_message);
} }
} }
if (callback_handle_ban_list) { if (callback_handle_ban_list) {
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network.GetRoomMember().lock()) {
room->Unbind(callback_handle_ban_list); room->Unbind(callback_handle_ban_list);
} }
} }
} }
void ModerationDialog::LoadBanList() { void ModerationDialog::LoadBanList() {
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network.GetRoomMember().lock()) {
ui->refresh->setEnabled(false); ui->refresh->setEnabled(false);
ui->refresh->setText(tr("Refreshing")); ui->refresh->setText(tr("Refreshing"));
ui->unban->setEnabled(false); ui->unban->setEnabled(false);
@ -98,7 +98,7 @@ void ModerationDialog::PopulateBanList(const Network::Room::BanList& ban_list) {
} }
void ModerationDialog::SendUnbanRequest(const QString& subject) { void ModerationDialog::SendUnbanRequest(const QString& subject) {
if (auto room = Network::GetRoomMember().lock()) { if (auto room = room_network.GetRoomMember().lock()) {
room->SendModerationRequest(Network::IdModUnban, subject.toStdString()); room->SendModerationRequest(Network::IdModUnban, subject.toStdString());
} }
} }

View file

@ -20,7 +20,7 @@ class ModerationDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
explicit ModerationDialog(QWidget* parent = nullptr); explicit ModerationDialog(Network::RoomNetwork& room_network_, QWidget* parent = nullptr);
~ModerationDialog(); ~ModerationDialog();
signals: signals:
@ -37,6 +37,8 @@ private:
QStandardItemModel* model; QStandardItemModel* model;
Network::RoomMember::CallbackHandle<Network::StatusMessageEntry> callback_handle_status_message; Network::RoomMember::CallbackHandle<Network::StatusMessageEntry> callback_handle_status_message;
Network::RoomMember::CallbackHandle<Network::Room::BanList> callback_handle_ban_list; Network::RoomMember::CallbackHandle<Network::Room::BanList> callback_handle_ban_list;
Network::RoomNetwork& room_network;
}; };
Q_DECLARE_METATYPE(Network::Room::BanList); Q_DECLARE_METATYPE(Network::Room::BanList);

View file

@ -20,10 +20,11 @@
#include "yuzu/util/clickable_label.h" #include "yuzu/util/clickable_label.h"
MultiplayerState::MultiplayerState(QWidget* parent, QStandardItemModel* game_list_model_, MultiplayerState::MultiplayerState(QWidget* parent, QStandardItemModel* game_list_model_,
QAction* leave_room_, QAction* show_room_) QAction* leave_room_, QAction* show_room_,
Network::RoomNetwork& room_network_)
: QWidget(parent), game_list_model(game_list_model_), leave_room(leave_room_), : QWidget(parent), game_list_model(game_list_model_), leave_room(leave_room_),
show_room(show_room_) { show_room(show_room_), room_network{room_network_} {
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
// register the network structs to use in slots and signals // register the network structs to use in slots and signals
state_callback_handle = member->BindOnStateChanged( state_callback_handle = member->BindOnStateChanged(
[this](const Network::RoomMember::State& state) { emit NetworkStateChanged(state); }); [this](const Network::RoomMember::State& state) { emit NetworkStateChanged(state); });
@ -37,7 +38,7 @@ MultiplayerState::MultiplayerState(QWidget* parent, QStandardItemModel* game_lis
qRegisterMetaType<Network::RoomMember::State>(); qRegisterMetaType<Network::RoomMember::State>();
qRegisterMetaType<Network::RoomMember::Error>(); qRegisterMetaType<Network::RoomMember::Error>();
qRegisterMetaType<WebService::WebResult>(); qRegisterMetaType<WebService::WebResult>();
announce_multiplayer_session = std::make_shared<Core::AnnounceMultiplayerSession>(); announce_multiplayer_session = std::make_shared<Core::AnnounceMultiplayerSession>(room_network);
announce_multiplayer_session->BindErrorCallback( announce_multiplayer_session->BindErrorCallback(
[this](const WebService::WebResult& result) { emit AnnounceFailed(result); }); [this](const WebService::WebResult& result) { emit AnnounceFailed(result); });
connect(this, &MultiplayerState::AnnounceFailed, this, &MultiplayerState::OnAnnounceFailed); connect(this, &MultiplayerState::AnnounceFailed, this, &MultiplayerState::OnAnnounceFailed);
@ -61,13 +62,13 @@ MultiplayerState::MultiplayerState(QWidget* parent, QStandardItemModel* game_lis
MultiplayerState::~MultiplayerState() { MultiplayerState::~MultiplayerState() {
if (state_callback_handle) { if (state_callback_handle) {
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
member->Unbind(state_callback_handle); member->Unbind(state_callback_handle);
} }
} }
if (error_callback_handle) { if (error_callback_handle) {
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
member->Unbind(error_callback_handle); member->Unbind(error_callback_handle);
} }
} }
@ -205,14 +206,15 @@ static void BringWidgetToFront(QWidget* widget) {
void MultiplayerState::OnViewLobby() { void MultiplayerState::OnViewLobby() {
if (lobby == nullptr) { if (lobby == nullptr) {
lobby = new Lobby(this, game_list_model, announce_multiplayer_session); lobby = new Lobby(this, game_list_model, announce_multiplayer_session, room_network);
} }
BringWidgetToFront(lobby); BringWidgetToFront(lobby);
} }
void MultiplayerState::OnCreateRoom() { void MultiplayerState::OnCreateRoom() {
if (host_room == nullptr) { if (host_room == nullptr) {
host_room = new HostRoomWindow(this, game_list_model, announce_multiplayer_session); host_room =
new HostRoomWindow(this, game_list_model, announce_multiplayer_session, room_network);
} }
BringWidgetToFront(host_room); BringWidgetToFront(host_room);
} }
@ -220,9 +222,9 @@ void MultiplayerState::OnCreateRoom() {
bool MultiplayerState::OnCloseRoom() { bool MultiplayerState::OnCloseRoom() {
if (!NetworkMessage::WarnCloseRoom()) if (!NetworkMessage::WarnCloseRoom())
return false; return false;
if (auto room = Network::GetRoom().lock()) { if (auto room = room_network.GetRoom().lock()) {
// if you are in a room, leave it // if you are in a room, leave it
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
member->Leave(); member->Leave();
LOG_DEBUG(Frontend, "Left the room (as a client)"); LOG_DEBUG(Frontend, "Left the room (as a client)");
} }
@ -257,10 +259,10 @@ void MultiplayerState::HideNotification() {
} }
void MultiplayerState::OnOpenNetworkRoom() { void MultiplayerState::OnOpenNetworkRoom() {
if (auto member = Network::GetRoomMember().lock()) { if (auto member = room_network.GetRoomMember().lock()) {
if (member->IsConnected()) { if (member->IsConnected()) {
if (client_room == nullptr) { if (client_room == nullptr) {
client_room = new ClientRoomWindow(this); client_room = new ClientRoomWindow(this, room_network);
connect(client_room, &ClientRoomWindow::ShowNotification, this, connect(client_room, &ClientRoomWindow::ShowNotification, this,
&MultiplayerState::ShowNotification); &MultiplayerState::ShowNotification);
} }
@ -275,7 +277,7 @@ void MultiplayerState::OnOpenNetworkRoom() {
void MultiplayerState::OnDirectConnectToRoom() { void MultiplayerState::OnDirectConnectToRoom() {
if (direct_connect == nullptr) { if (direct_connect == nullptr) {
direct_connect = new DirectConnectWindow(this); direct_connect = new DirectConnectWindow(room_network, this);
} }
BringWidgetToFront(direct_connect); BringWidgetToFront(direct_connect);
} }

View file

@ -20,7 +20,7 @@ class MultiplayerState : public QWidget {
public: public:
explicit MultiplayerState(QWidget* parent, QStandardItemModel* game_list, QAction* leave_room, explicit MultiplayerState(QWidget* parent, QStandardItemModel* game_list, QAction* leave_room,
QAction* show_room); QAction* show_room, Network::RoomNetwork& room_network_);
~MultiplayerState(); ~MultiplayerState();
/** /**
@ -87,6 +87,7 @@ private:
Network::RoomMember::CallbackHandle<Network::RoomMember::Error> error_callback_handle; Network::RoomMember::CallbackHandle<Network::RoomMember::Error> error_callback_handle;
bool show_notification = false; bool show_notification = false;
Network::RoomNetwork& room_network;
}; };
Q_DECLARE_METATYPE(WebService::WebResult); Q_DECLARE_METATYPE(WebService::WebResult);