NetPlayClient/Server: apply Parameter Object pattern to traversal parameters

This commit is contained in:
Michael M 2017-08-07 00:22:33 -07:00
parent fdb5828d62
commit 4b50e77a26
7 changed files with 37 additions and 25 deletions

View File

@ -74,13 +74,12 @@ NetPlayClient::~NetPlayClient()
// called from ---GUI--- thread // called from ---GUI--- thread
NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog,
const std::string& name, bool traversal, const std::string& name, const NetTraversalConfig& traversal_config)
const std::string& centralServer, u16 centralPort)
: m_dialog(dialog), m_player_name(name) : m_dialog(dialog), m_player_name(name)
{ {
ClearBuffers(); ClearBuffers();
if (!traversal) if (!traversal_config.use_traversal)
{ {
// Direct Connection // Direct Connection
m_client = enet_host_create(nullptr, 1, 3, 0, 0); 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; return;
} }
if (!EnsureTraversalClient(centralServer, centralPort)) if (!EnsureTraversalClient(traversal_config.traversal_host, traversal_config.traversal_port))
return; return;
m_client = g_MainNetHost.get(); m_client = g_MainNetHost.get();

View File

@ -67,8 +67,7 @@ public:
void SendAsync(sf::Packet&& packet); void SendAsync(sf::Packet&& packet);
NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog,
const std::string& name, bool traversal, const std::string& centralServer, const std::string& name, const NetTraversalConfig& traversal_config);
u16 centralPort);
~NetPlayClient(); ~NetPlayClient();
void GetPlayerList(std::string& list, std::vector<int>& pid_list); void GetPlayerList(std::string& list, std::vector<int>& pid_list);

View File

@ -27,6 +27,20 @@ struct NetSettings
ExpansionInterface::TEXIDevices m_EXIDevice[2]; 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 NetSettings g_NetPlaySettings;
extern u64 g_netplay_initial_rtc; extern u64 g_netplay_initial_rtc;

View File

@ -63,8 +63,7 @@ NetPlayServer::~NetPlayServer()
} }
// called from ---GUI--- thread // called from ---GUI--- thread
NetPlayServer::NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, NetPlayServer::NetPlayServer(const u16 port, const NetTraversalConfig& traversal_config)
u16 centralPort)
{ {
//--use server time //--use server time
if (enet_initialize() != 0) if (enet_initialize() != 0)
@ -75,9 +74,10 @@ NetPlayServer::NetPlayServer(const u16 port, bool traversal, const std::string&
m_pad_map.fill(-1); m_pad_map.fill(-1);
m_wiimote_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; return;
g_TraversalClient->m_Client = this; g_TraversalClient->m_Client = this;

View File

@ -28,7 +28,7 @@ public:
void ThreadFunc(); void ThreadFunc();
void SendAsyncToClients(sf::Packet&& packet); 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(); ~NetPlayServer();
bool ChangeGame(const std::string& game); bool ChangeGame(const std::string& game);

View File

@ -684,10 +684,10 @@ bool MainWindow::NetPlayJoin()
const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME); const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME);
// Create Client // Create Client
Settings::Instance().ResetNetPlayClient( Settings::Instance().ResetNetPlayClient(new NetPlayClient(
new NetPlayClient(host_ip, host_port, m_netplay_dialog, nickname, host_ip, host_port, m_netplay_dialog, nickname,
Settings::Instance().GetNetPlayServer() != nullptr ? false : is_traversal, NetTraversalConfig{Settings::Instance().GetNetPlayServer() != nullptr ? false : is_traversal,
traversal_host, traversal_port)); traversal_host, traversal_port}));
if (!Settings::Instance().GetNetPlayClient()->IsConnected()) if (!Settings::Instance().GetNetPlayClient()->IsConnected())
{ {
@ -733,8 +733,8 @@ bool MainWindow::NetPlayHost(const QString& game_id)
host_port = Config::Get(Config::NETPLAY_LISTEN_PORT); host_port = Config::Get(Config::NETPLAY_LISTEN_PORT);
// Create Server // Create Server
Settings::Instance().ResetNetPlayServer( Settings::Instance().ResetNetPlayServer(new NetPlayServer(
new NetPlayServer(host_port, is_traversal, traversal_host, traversal_port)); host_port, NetTraversalConfig{is_traversal, traversal_host, traversal_port}));
if (!Settings::Instance().GetNetPlayServer()->is_connected) if (!Settings::Instance().GetNetPlayServer()->is_connected)
{ {

View File

@ -23,8 +23,9 @@ bool NetPlayLauncher::Host(const NetPlayHostConfig& config)
return false; return false;
} }
netplay_server = new NetPlayServer(config.listen_port, config.use_traversal, netplay_server = new NetPlayServer(
config.traversal_host, config.traversal_port); config.listen_port,
NetTraversalConfig{config.use_traversal, config.traversal_host, config.traversal_port});
if (!netplay_server->is_connected) 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); npd = new NetPlayDialog(config.parent_window, config.game_list_ctrl, config.game_name, true);
NetPlayClient*& netplay_client = NetPlayDialog::GetNetPlayClient(); NetPlayClient*& netplay_client = NetPlayDialog::GetNetPlayClient();
netplay_client = netplay_client = new NetPlayClient("127.0.0.1", netplay_server->GetPort(), npd,
new NetPlayClient("127.0.0.1", netplay_server->GetPort(), npd, config.player_name, false, config.player_name, NetTraversalConfig{});
config.traversal_host, config.traversal_port);
if (netplay_client->IsConnected()) if (netplay_client->IsConnected())
{ {
@ -76,9 +76,9 @@ bool NetPlayLauncher::Join(const NetPlayJoinConfig& config)
else else
host = config.connect_host; host = config.connect_host;
netplay_client = netplay_client = new NetPlayClient(
new NetPlayClient(host, config.connect_port, npd, config.player_name, config.use_traversal, host, config.connect_port, npd, config.player_name,
config.traversal_host, config.traversal_port); NetTraversalConfig{config.use_traversal, config.traversal_host, config.traversal_port});
if (netplay_client->IsConnected()) if (netplay_client->IsConnected())
{ {
npd->SetSize(config.window_pos); npd->SetSize(config.window_pos);