diff --git a/Source/Core/Common/TraversalClient.cpp b/Source/Core/Common/TraversalClient.cpp index 13089b958c..4c415b2c64 100644 --- a/Source/Core/Common/TraversalClient.cpp +++ b/Source/Core/Common/TraversalClient.cpp @@ -22,8 +22,7 @@ static void GetRandomishBytes(u8* buf, size_t size) } TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port) - : m_NetHost(netHost), m_Client(nullptr), m_ConnectRequestId(0), m_PendingConnect(false), - m_Server(server), m_port(port), m_PingTime(0) + : m_NetHost(netHost), m_Server(server), m_port(port) { netHost->intercept = TraversalClient::InterceptCallback; @@ -32,8 +31,21 @@ TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, c ReconnectToServer(); } -TraversalClient::~TraversalClient() +TraversalClient::~TraversalClient() = default; + +TraversalHostId TraversalClient::GetHostID() const { + return m_HostId; +} + +TraversalClient::State TraversalClient::GetState() const +{ + return m_State; +} + +TraversalClient::FailureReason TraversalClient::GetFailureReason() const +{ + return m_FailureReason; } void TraversalClient::ReconnectToServer() @@ -231,7 +243,7 @@ void TraversalClient::ResendPacket(OutgoingTraversalPacketInfo* info) void TraversalClient::HandleResends() { - enet_uint32 now = enet_time_get(); + const u32 now = enet_time_get(); for (auto& tpi : m_OutgoingTraversalPackets) { if (now - tpi.sendTime >= (u32)(300 * tpi.tries)) @@ -253,7 +265,7 @@ void TraversalClient::HandleResends() void TraversalClient::HandlePing() { - enet_uint32 now = enet_time_get(); + const u32 now = enet_time_get(); if (m_State == Connected && now - m_PingTime >= 500) { TraversalPacket ping = {}; diff --git a/Source/Core/Common/TraversalClient.h b/Source/Core/Common/TraversalClient.h index e85453e0e9..78e1d0c705 100644 --- a/Source/Core/Common/TraversalClient.h +++ b/Source/Core/Common/TraversalClient.h @@ -41,40 +41,46 @@ public: }; TraversalClient(ENetHost* netHost, const std::string& server, const u16 port); ~TraversalClient(); + + TraversalHostId GetHostID() const; + State GetState() const; + FailureReason GetFailureReason() const; + void Reset(); void ConnectToClient(const std::string& host); void ReconnectToServer(); void Update(); - // called from NetHost - bool TestPacket(u8* data, size_t size, ENetAddress* from); void HandleResends(); - ENetHost* m_NetHost; - TraversalClientClient* m_Client; - TraversalHostId m_HostId; - State m_State; - FailureReason m_FailureReason; + TraversalClientClient* m_Client = nullptr; private: struct OutgoingTraversalPacketInfo { TraversalPacket packet; int tries; - enet_uint32 sendTime; + u32 sendTime; }; void HandleServerPacket(TraversalPacket* packet); + // called from NetHost + bool TestPacket(u8* data, size_t size, ENetAddress* from); void ResendPacket(OutgoingTraversalPacketInfo* info); TraversalRequestId SendTraversalPacket(const TraversalPacket& packet); void OnFailure(FailureReason reason); void HandlePing(); static int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event); - TraversalRequestId m_ConnectRequestId; - bool m_PendingConnect; + + ENetHost* m_NetHost; + TraversalHostId m_HostId{}; + State m_State{}; + FailureReason m_FailureReason{}; + TraversalRequestId m_ConnectRequestId = 0; + bool m_PendingConnect = false; std::list m_OutgoingTraversalPackets; - ENetAddress m_ServerAddress; + ENetAddress m_ServerAddress{}; std::string m_Server; u16 m_port; - enet_uint32 m_PingTime; + u32 m_PingTime = 0; }; extern std::unique_ptr g_TraversalClient; // the NetHost connected to the TraversalClient. diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 2f28527a5e..be766d81ad 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -134,7 +134,7 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay m_traversal_client = g_TraversalClient.get(); // If we were disconnected in the background, reconnect. - if (m_traversal_client->m_State == TraversalClient::Failure) + if (m_traversal_client->GetState() == TraversalClient::Failure) m_traversal_client->ReconnectToServer(); m_traversal_client->m_Client = this; m_host_spec = address; @@ -909,17 +909,18 @@ void NetPlayClient::ClearBuffers() // called from ---NETPLAY--- thread void NetPlayClient::OnTraversalStateChanged() { + const TraversalClient::State state = m_traversal_client->GetState(); + if (m_connection_state == ConnectionState::WaitingForTraversalClientConnection && - m_traversal_client->m_State == TraversalClient::Connected) + state == TraversalClient::Connected) { m_connection_state = ConnectionState::WaitingForTraversalClientConnectReady; m_traversal_client->ConnectToClient(m_host_spec); } - else if (m_connection_state != ConnectionState::Failure && - m_traversal_client->m_State == TraversalClient::Failure) + else if (m_connection_state != ConnectionState::Failure && state == TraversalClient::Failure) { Disconnect(); - m_dialog->OnTraversalError(m_traversal_client->m_FailureReason); + m_dialog->OnTraversalError(m_traversal_client->GetFailureReason()); } } diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index e41146a543..2ff168cc0d 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -90,7 +90,7 @@ NetPlayServer::NetPlayServer(const u16 port, const bool forward_port, m_server = g_MainNetHost.get(); - if (g_TraversalClient->m_State == TraversalClient::Failure) + if (g_TraversalClient->GetState() == TraversalClient::Failure) g_TraversalClient->ReconnectToServer(); } else @@ -736,8 +736,8 @@ unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player) void NetPlayServer::OnTraversalStateChanged() { - if (m_dialog && m_traversal_client->m_State == TraversalClient::Failure) - m_dialog->OnTraversalError(m_traversal_client->m_FailureReason); + if (m_dialog && m_traversal_client->GetState() == TraversalClient::Failure) + m_dialog->OnTraversalError(m_traversal_client->GetFailureReason()); } // called from ---GUI--- thread diff --git a/Source/Core/DolphinQt2/NetPlay/NetPlayDialog.cpp b/Source/Core/DolphinQt2/NetPlay/NetPlayDialog.cpp index 5747b46837..556188b911 100644 --- a/Source/Core/DolphinQt2/NetPlay/NetPlayDialog.cpp +++ b/Source/Core/DolphinQt2/NetPlay/NetPlayDialog.cpp @@ -360,19 +360,22 @@ void NetPlayDialog::UpdateGUI() // Update Room ID / IP label if (m_use_traversal && m_room_box->currentIndex() == 0) { - switch (g_TraversalClient->m_State) + switch (g_TraversalClient->GetState()) { case TraversalClient::Connecting: m_hostcode_label->setText(tr("...")); m_hostcode_action_button->setEnabled(false); break; case TraversalClient::Connected: - m_hostcode_label->setText(QString::fromStdString( - std::string(g_TraversalClient->m_HostId.data(), g_TraversalClient->m_HostId.size()))); + { + const auto host_id = g_TraversalClient->GetHostID(); + m_hostcode_label->setText( + QString::fromStdString(std::string(host_id.begin(), host_id.end()))); m_hostcode_action_button->setEnabled(true); m_hostcode_action_button->setText(tr("Copy")); m_is_copy_button_retry = false; break; + } case TraversalClient::Failure: m_hostcode_label->setText(tr("Error")); m_hostcode_action_button->setText(tr("Retry")); diff --git a/Source/Core/DolphinWX/NetPlay/NetWindow.cpp b/Source/Core/DolphinWX/NetPlay/NetWindow.cpp index cd4bba69a6..1ece3e1bc1 100644 --- a/Source/Core/DolphinWX/NetPlay/NetWindow.cpp +++ b/Source/Core/DolphinWX/NetPlay/NetWindow.cpp @@ -777,7 +777,7 @@ void NetPlayDialog::UpdateHostLabel() if (sel == 0) { // the traversal ID - switch (g_TraversalClient->m_State) + switch (g_TraversalClient->GetState()) { case TraversalClient::Connecting: m_host_label->SetForegroundColour(*wxLIGHT_GREY); @@ -786,13 +786,15 @@ void NetPlayDialog::UpdateHostLabel() m_host_copy_btn->Disable(); break; case TraversalClient::Connected: + { + const auto host_id = g_TraversalClient->GetHostID(); m_host_label->SetForegroundColour(*wxBLACK); - m_host_label->SetLabel( - wxString(g_TraversalClient->m_HostId.data(), g_TraversalClient->m_HostId.size())); + m_host_label->SetLabel(wxString(host_id.data(), host_id.size())); m_host_copy_btn->SetLabel(_("Copy")); m_host_copy_btn->Enable(); m_host_copy_btn_is_retry = false; break; + } case TraversalClient::Failure: m_host_label->SetForegroundColour(*wxBLACK); m_host_label->SetLabel("...");