From 4b50e77a26f651d6e77b3cc8a02f5c51739259a7 Mon Sep 17 00:00:00 2001 From: Michael M Date: Mon, 7 Aug 2017 00:22:33 -0700 Subject: [PATCH] NetPlayClient/Server: apply Parameter Object pattern to traversal parameters --- Source/Core/Core/NetPlayClient.cpp | 7 +++---- Source/Core/Core/NetPlayClient.h | 3 +-- Source/Core/Core/NetPlayProto.h | 14 ++++++++++++++ Source/Core/Core/NetPlayServer.cpp | 8 ++++---- Source/Core/Core/NetPlayServer.h | 2 +- Source/Core/DolphinQt2/MainWindow.cpp | 12 ++++++------ .../Core/DolphinWX/NetPlay/NetPlayLauncher.cpp | 16 ++++++++-------- 7 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index b0c7f6fd16..2c98e18721 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -74,13 +74,12 @@ NetPlayClient::~NetPlayClient() // called from ---GUI--- thread NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, - const std::string& name, bool traversal, - const std::string& centralServer, u16 centralPort) + const std::string& name, const NetTraversalConfig& traversal_config) : m_dialog(dialog), m_player_name(name) { ClearBuffers(); - if (!traversal) + if (!traversal_config.use_traversal) { // Direct Connection m_client = enet_host_create(nullptr, 1, 3, 0, 0); @@ -124,7 +123,7 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay return; } - if (!EnsureTraversalClient(centralServer, centralPort)) + if (!EnsureTraversalClient(traversal_config.traversal_host, traversal_config.traversal_port)) return; m_client = g_MainNetHost.get(); diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index 00decb8093..978cec9cb4 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -67,8 +67,7 @@ public: void SendAsync(sf::Packet&& packet); NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, - const std::string& name, bool traversal, const std::string& centralServer, - u16 centralPort); + const std::string& name, const NetTraversalConfig& traversal_config); ~NetPlayClient(); void GetPlayerList(std::string& list, std::vector& pid_list); diff --git a/Source/Core/Core/NetPlayProto.h b/Source/Core/Core/NetPlayProto.h index deb8d073c2..3172c7a1c8 100644 --- a/Source/Core/Core/NetPlayProto.h +++ b/Source/Core/Core/NetPlayProto.h @@ -27,6 +27,20 @@ struct NetSettings ExpansionInterface::TEXIDevices m_EXIDevice[2]; }; +struct NetTraversalConfig +{ + NetTraversalConfig() = default; + NetTraversalConfig(bool use_traversal_, std::string traversal_host_, u16 traversal_port_) + : use_traversal{use_traversal_}, traversal_host{std::move(traversal_host_)}, + traversal_port{traversal_port_} + { + } + + bool use_traversal = false; + std::string traversal_host; + u16 traversal_port = 0; +}; + extern NetSettings g_NetPlaySettings; extern u64 g_netplay_initial_rtc; diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 11a05e069b..68a01365aa 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -63,8 +63,7 @@ NetPlayServer::~NetPlayServer() } // called from ---GUI--- thread -NetPlayServer::NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, - u16 centralPort) +NetPlayServer::NetPlayServer(const u16 port, const NetTraversalConfig& traversal_config) { //--use server time if (enet_initialize() != 0) @@ -75,9 +74,10 @@ NetPlayServer::NetPlayServer(const u16 port, bool traversal, const std::string& m_pad_map.fill(-1); m_wiimote_map.fill(-1); - if (traversal) + if (traversal_config.use_traversal) { - if (!EnsureTraversalClient(centralServer, centralPort, port)) + if (!EnsureTraversalClient(traversal_config.traversal_host, traversal_config.traversal_port, + port)) return; g_TraversalClient->m_Client = this; diff --git a/Source/Core/Core/NetPlayServer.h b/Source/Core/Core/NetPlayServer.h index cf8d2e958a..517f5fc61a 100644 --- a/Source/Core/Core/NetPlayServer.h +++ b/Source/Core/Core/NetPlayServer.h @@ -28,7 +28,7 @@ public: void ThreadFunc(); void SendAsyncToClients(sf::Packet&& packet); - NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, u16 centralPort); + NetPlayServer(const u16 port, const NetTraversalConfig& traversal_config); ~NetPlayServer(); bool ChangeGame(const std::string& game); diff --git a/Source/Core/DolphinQt2/MainWindow.cpp b/Source/Core/DolphinQt2/MainWindow.cpp index b906b2801d..87a2f8e5e2 100644 --- a/Source/Core/DolphinQt2/MainWindow.cpp +++ b/Source/Core/DolphinQt2/MainWindow.cpp @@ -684,10 +684,10 @@ bool MainWindow::NetPlayJoin() const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME); // Create Client - Settings::Instance().ResetNetPlayClient( - new NetPlayClient(host_ip, host_port, m_netplay_dialog, nickname, - Settings::Instance().GetNetPlayServer() != nullptr ? false : is_traversal, - traversal_host, traversal_port)); + Settings::Instance().ResetNetPlayClient(new NetPlayClient( + host_ip, host_port, m_netplay_dialog, nickname, + NetTraversalConfig{Settings::Instance().GetNetPlayServer() != nullptr ? false : is_traversal, + traversal_host, traversal_port})); if (!Settings::Instance().GetNetPlayClient()->IsConnected()) { @@ -733,8 +733,8 @@ bool MainWindow::NetPlayHost(const QString& game_id) host_port = Config::Get(Config::NETPLAY_LISTEN_PORT); // Create Server - Settings::Instance().ResetNetPlayServer( - new NetPlayServer(host_port, is_traversal, traversal_host, traversal_port)); + Settings::Instance().ResetNetPlayServer(new NetPlayServer( + host_port, NetTraversalConfig{is_traversal, traversal_host, traversal_port})); if (!Settings::Instance().GetNetPlayServer()->is_connected) { diff --git a/Source/Core/DolphinWX/NetPlay/NetPlayLauncher.cpp b/Source/Core/DolphinWX/NetPlay/NetPlayLauncher.cpp index 307a03385e..758cfa7af0 100644 --- a/Source/Core/DolphinWX/NetPlay/NetPlayLauncher.cpp +++ b/Source/Core/DolphinWX/NetPlay/NetPlayLauncher.cpp @@ -23,8 +23,9 @@ bool NetPlayLauncher::Host(const NetPlayHostConfig& config) return false; } - netplay_server = new NetPlayServer(config.listen_port, config.use_traversal, - config.traversal_host, config.traversal_port); + netplay_server = new NetPlayServer( + config.listen_port, + NetTraversalConfig{config.use_traversal, config.traversal_host, config.traversal_port}); if (!netplay_server->is_connected) { @@ -45,9 +46,8 @@ bool NetPlayLauncher::Host(const NetPlayHostConfig& config) npd = new NetPlayDialog(config.parent_window, config.game_list_ctrl, config.game_name, true); NetPlayClient*& netplay_client = NetPlayDialog::GetNetPlayClient(); - netplay_client = - new NetPlayClient("127.0.0.1", netplay_server->GetPort(), npd, config.player_name, false, - config.traversal_host, config.traversal_port); + netplay_client = new NetPlayClient("127.0.0.1", netplay_server->GetPort(), npd, + config.player_name, NetTraversalConfig{}); if (netplay_client->IsConnected()) { @@ -76,9 +76,9 @@ bool NetPlayLauncher::Join(const NetPlayJoinConfig& config) else host = config.connect_host; - netplay_client = - new NetPlayClient(host, config.connect_port, npd, config.player_name, config.use_traversal, - config.traversal_host, config.traversal_port); + netplay_client = new NetPlayClient( + host, config.connect_port, npd, config.player_name, + NetTraversalConfig{config.use_traversal, config.traversal_host, config.traversal_port}); if (netplay_client->IsConnected()) { npd->SetSize(config.window_pos);