From 1f6791431d0bcb2b9ed0cf0c162b2121c324d96b Mon Sep 17 00:00:00 2001 From: James Rowe Date: Thu, 19 Apr 2018 00:47:11 -0600 Subject: [PATCH] Move almost all state change tracking to MultiplayerState Each window can still watch for state changes to update the ui or to close the window as appropriate, but for any error announcements, they all belong in Multiplayer state now. --- src/citra_qt/multiplayer/client_room.cpp | 40 +-------------- src/citra_qt/multiplayer/client_room.h | 5 -- src/citra_qt/multiplayer/direct_connect.cpp | 14 +----- src/citra_qt/multiplayer/host_room.cpp | 18 +------ src/citra_qt/multiplayer/host_room.h | 7 --- src/citra_qt/multiplayer/message.cpp | 5 +- src/citra_qt/multiplayer/state.cpp | 54 ++++++++++++++++++--- 7 files changed, 55 insertions(+), 88 deletions(-) diff --git a/src/citra_qt/multiplayer/client_room.cpp b/src/citra_qt/multiplayer/client_room.cpp index 8e37426bd6..60f488587a 100644 --- a/src/citra_qt/multiplayer/client_room.cpp +++ b/src/citra_qt/multiplayer/client_room.cpp @@ -49,44 +49,9 @@ void ClientRoomWindow::OnRoomUpdate(const Network::RoomInformation& info) { } void ClientRoomWindow::OnStateChange(const Network::RoomMember::State& state) { - switch (state) { - case Network::RoomMember::State::Idle: - NGLOG_INFO(Network, "State: Idle"); - break; - case Network::RoomMember::State::Joining: - NGLOG_INFO(Network, "State: Joining"); - break; - case Network::RoomMember::State::Joined: - NGLOG_INFO(Network, "State: Joined"); + if (state == Network::RoomMember::State::Joined) { ui->chat->Clear(); ui->chat->AppendStatusMessage(tr("Connected")); - break; - case Network::RoomMember::State::LostConnection: - NetworkMessage::ShowError(NetworkMessage::LOST_CONNECTION); - NGLOG_INFO(Network, "State: LostConnection"); - break; - case Network::RoomMember::State::CouldNotConnect: - NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT); - NGLOG_INFO(Network, "State: CouldNotConnect"); - break; - case Network::RoomMember::State::NameCollision: - NetworkMessage::ShowError(NetworkMessage::USERNAME_IN_USE); - NGLOG_INFO(Network, "State: NameCollision"); - break; - case Network::RoomMember::State::MacCollision: - NetworkMessage::ShowError(NetworkMessage::MAC_COLLISION); - NGLOG_INFO(Network, "State: MacCollision"); - break; - case Network::RoomMember::State::WrongPassword: - NetworkMessage::ShowError(NetworkMessage::WRONG_PASSWORD); - NGLOG_INFO(Network, "State: WrongPassword"); - break; - case Network::RoomMember::State::WrongVersion: - NetworkMessage::ShowError(NetworkMessage::WRONG_VERSION); - NGLOG_INFO(Network, "State: WrongVersion"); - break; - default: - break; } UpdateView(); } @@ -99,7 +64,6 @@ void ClientRoomWindow::Disconnect() { member->Leave(); ui->chat->AppendStatusMessage(tr("Disconnected")); close(); - emit Closed(); } } @@ -120,6 +84,4 @@ void ClientRoomWindow::UpdateView() { } // TODO(B3N30): can't get RoomMember*, show error and close window close(); - emit Closed(); - return; } diff --git a/src/citra_qt/multiplayer/client_room.h b/src/citra_qt/multiplayer/client_room.h index 960dbe97f4..8e8ee24ebc 100644 --- a/src/citra_qt/multiplayer/client_room.h +++ b/src/citra_qt/multiplayer/client_room.h @@ -22,11 +22,6 @@ public slots: void OnStateChange(const Network::RoomMember::State&); signals: - /** - * Signalled by this widget when it is closing itself and destroying any state such as - * connections that it might have. - */ - void Closed(); void RoomInformationChanged(const Network::RoomInformation&); void StateChanged(const Network::RoomMember::State&); diff --git a/src/citra_qt/multiplayer/direct_connect.cpp b/src/citra_qt/multiplayer/direct_connect.cpp index 7a47918fb2..a9b64c98c6 100644 --- a/src/citra_qt/multiplayer/direct_connect.cpp +++ b/src/citra_qt/multiplayer/direct_connect.cpp @@ -105,20 +105,8 @@ void DirectConnectWindow::EndConnecting() { void DirectConnectWindow::OnConnection() { EndConnecting(); - bool isConnected = true; if (auto room_member = Network::GetRoomMember().lock()) { - switch (room_member->GetState()) { - case Network::RoomMember::State::CouldNotConnect: - isConnected = false; - ShowError(NetworkMessage::UNABLE_TO_CONNECT); - break; - case Network::RoomMember::State::NameCollision: - isConnected = false; - ShowError(NetworkMessage::USERNAME_IN_USE); - break; - case Network::RoomMember::State::Joining: - auto parent = static_cast(parentWidget()); - parent->OnOpenNetworkRoom(); + if (room_member->GetState() == Network::RoomMember::State::Joined) { close(); } } diff --git a/src/citra_qt/multiplayer/host_room.cpp b/src/citra_qt/multiplayer/host_room.cpp index 9469153789..a57f310520 100644 --- a/src/citra_qt/multiplayer/host_room.cpp +++ b/src/citra_qt/multiplayer/host_room.cpp @@ -119,17 +119,8 @@ void HostRoomWindow::Host() { void HostRoomWindow::OnConnection() { ui->host->setEnabled(true); if (auto room_member = Network::GetRoomMember().lock()) { - switch (room_member->GetState()) { - case Network::RoomMember::State::CouldNotConnect: - ShowError(NetworkMessage::UNABLE_TO_CONNECT); - break; - case Network::RoomMember::State::NameCollision: - ShowError(NetworkMessage::USERNAME_IN_USE); - break; - case Network::RoomMember::State::Error: - ShowError(NetworkMessage::UNABLE_TO_CONNECT); - break; - case Network::RoomMember::State::Joining: + if (room_member->GetState() == Network::RoomMember::State::Joining) { + // Start the announce session if they chose Public if (ui->host_type->currentIndex() == 0) { if (auto session = announce_multiplayer_session.lock()) { session->Start(); @@ -137,12 +128,7 @@ void HostRoomWindow::OnConnection() { NGLOG_ERROR(Network, "Starting announce session failed"); } } - auto parent = static_cast(parentWidget()); - // parent->ChangeRoomState(); - parent->OnOpenNetworkRoom(); close(); - emit Closed(); - break; } } } diff --git a/src/citra_qt/multiplayer/host_room.h b/src/citra_qt/multiplayer/host_room.h index 038b9f538d..574dc28244 100644 --- a/src/citra_qt/multiplayer/host_room.h +++ b/src/citra_qt/multiplayer/host_room.h @@ -34,13 +34,6 @@ public: std::shared_ptr session); ~HostRoomWindow(); -signals: - /** - * Signalled by this widget when it is closing itself and destroying any state such as - * connections that it might have. - */ - void Closed(); - private slots: /** * Handler for connection status changes. Launches the chat window if successful or diff --git a/src/citra_qt/multiplayer/message.cpp b/src/citra_qt/multiplayer/message.cpp index 28d62bb81d..57cd7671c9 100644 --- a/src/citra_qt/multiplayer/message.cpp +++ b/src/citra_qt/multiplayer/message.cpp @@ -28,10 +28,11 @@ const ConnectionError HOST_BANNED( QT_TR_NOOP("The host of the room has banned you. Speak with the host to unban you " "or try a different room.")); const ConnectionError WRONG_VERSION( - QT_TR_NOOP("Version mismatch! Please update to the latest version of citra. If the problem " + QT_TR_NOOP("Version mismatch! Please update to the latest version of Citra. If the problem " "persists, contact the room host and ask them to update the server.")); const ConnectionError WRONG_PASSWORD(QT_TR_NOOP("Incorrect password.")); -const ConnectionError GENERIC_ERROR(QT_TR_NOOP("An error occured.")); +const ConnectionError GENERIC_ERROR( + QT_TR_NOOP("An unknown error occured. If this error continues to occur, please open an issue")); const ConnectionError LOST_CONNECTION(QT_TR_NOOP("Connection to room lost. Try to reconnect.")); const ConnectionError MAC_COLLISION( QT_TR_NOOP("MAC address is already in use. Please choose another.")); diff --git a/src/citra_qt/multiplayer/state.cpp b/src/citra_qt/multiplayer/state.cpp index 3c40a7d866..f741b299b5 100644 --- a/src/citra_qt/multiplayer/state.cpp +++ b/src/citra_qt/multiplayer/state.cpp @@ -67,17 +67,59 @@ void MultiplayerState::Close() { void MultiplayerState::OnNetworkStateChanged(const Network::RoomMember::State& state) { NGLOG_DEBUG(Frontend, "Network state change"); - if (state == Network::RoomMember::State::Joined) { + bool is_connected = false; + switch (state) { + case Network::RoomMember::State::Idle: + NGLOG_DEBUG(Network, "State: Idle"); + break; + case Network::RoomMember::State::Joining: + NGLOG_DEBUG(Network, "State: Joining"); + break; + case Network::RoomMember::State::LostConnection: + NetworkMessage::ShowError(NetworkMessage::LOST_CONNECTION); + NGLOG_DEBUG(Network, "State: LostConnection"); + break; + case Network::RoomMember::State::CouldNotConnect: + NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT); + NGLOG_DEBUG(Network, "State: CouldNotConnect"); + break; + case Network::RoomMember::State::NameCollision: + NetworkMessage::ShowError(NetworkMessage::USERNAME_IN_USE); + NGLOG_DEBUG(Network, "State: NameCollision"); + break; + case Network::RoomMember::State::MacCollision: + NetworkMessage::ShowError(NetworkMessage::MAC_COLLISION); + NGLOG_DEBUG(Network, "State: MacCollision"); + break; + case Network::RoomMember::State::WrongPassword: + NetworkMessage::ShowError(NetworkMessage::WRONG_PASSWORD); + NGLOG_DEBUG(Network, "State: WrongPassword"); + break; + case Network::RoomMember::State::WrongVersion: + NetworkMessage::ShowError(NetworkMessage::WRONG_VERSION); + NGLOG_DEBUG(Network, "State: WrongVersion"); + break; + case Network::RoomMember::State::Error: + NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT); + NGLOG_DEBUG(Network, "State: GenericError"); + break; + case Network::RoomMember::State::Joined: + NGLOG_DEBUG(Network, "State: Joined"); + is_connected = true; + OnOpenNetworkRoom(); + break; + } + if (is_connected) { status_icon->setPixmap(QIcon::fromTheme("connected").pixmap(16)); status_text->setText(tr("Connected")); leave_room->setEnabled(true); show_room->setEnabled(true); - return; + } else { + status_icon->setPixmap(QIcon::fromTheme("disconnected").pixmap(16)); + status_text->setText(tr("Not Connected")); + leave_room->setEnabled(false); + show_room->setEnabled(false); } - status_icon->setPixmap(QIcon::fromTheme("disconnected").pixmap(16)); - status_text->setText(tr("Not Connected")); - leave_room->setEnabled(false); - show_room->setEnabled(false); } void MultiplayerState::OnAnnounceFailed(const Common::WebResult& result) {