Merge pull request #7258 from Techjar/netplay-fix-segfaults

Fix more segfaults on NetPlay quit
This commit is contained in:
spycrab 2018-07-30 11:52:38 +02:00 committed by GitHub
commit 2a776bf2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 29 deletions

View File

@ -70,6 +70,10 @@ NetPlayClient::~NetPlayClient()
if (m_is_connected)
{
m_should_compute_MD5 = false;
m_dialog->AbortMD5();
if (m_MD5_thread.joinable())
m_MD5_thread.join();
m_do_loop.Clear();
m_thread.join();
}
@ -1680,12 +1684,14 @@ void NetPlayClient::ComputeMD5(const std::string& file_identifier)
return;
}
if (m_MD5_thread.joinable())
m_MD5_thread.join();
m_MD5_thread = std::thread([this, file]() {
std::string sum = MD5::MD5Sum(file, [&](int progress) {
sf::Packet packet;
packet << static_cast<MessageId>(NP_MSG_MD5_PROGRESS);
packet << progress;
Send(packet);
SendAsync(std::move(packet));
return m_should_compute_MD5;
});
@ -1693,9 +1699,8 @@ void NetPlayClient::ComputeMD5(const std::string& file_identifier)
sf::Packet packet;
packet << static_cast<MessageId>(NP_MSG_MD5_RESULT);
packet << sum;
Send(packet);
SendAsync(std::move(packet));
});
m_MD5_thread.detach();
}
const PadMappingArray& NetPlayClient::GetPadMapping() const

View File

@ -668,7 +668,7 @@ bool MainWindow::RequestStop()
const Core::State state = Core::GetState();
// Only pause the game, if NetPlay is not running
bool pause = Settings::Instance().GetNetPlayClient() == nullptr;
bool pause = !Settings::Instance().GetNetPlayClient();
if (pause)
Core::SetState(Core::State::Paused);
@ -1071,7 +1071,7 @@ bool MainWindow::NetPlayJoin()
std::string host_ip;
u16 host_port;
if (Settings::Instance().GetNetPlayServer() != nullptr)
if (Settings::Instance().GetNetPlayServer())
{
host_ip = "127.0.0.1";
host_port = Settings::Instance().GetNetPlayServer()->GetPort();

View File

@ -21,7 +21,11 @@
static QString GetPlayerNameFromPID(int pid)
{
QString player_name = QObject::tr("Invalid Player ID");
for (const auto* player : Settings::Instance().GetNetPlayClient()->GetPlayers())
auto client = Settings::Instance().GetNetPlayClient();
if (!client)
return player_name;
for (const auto* player : client->GetPlayers())
{
if (player->pid == pid)
{
@ -82,7 +86,11 @@ void MD5Dialog::show(const QString& title)
m_results.clear();
m_check_label->setText(QString::fromStdString(""));
for (const auto* player : Settings::Instance().GetNetPlayClient()->GetPlayers())
auto client = Settings::Instance().GetNetPlayClient();
if (!client)
return;
for (const auto* player : client->GetPlayers())
{
m_progress_bars[player->pid] = new QProgressBar;
m_status_labels[player->pid] = new QLabel;
@ -118,7 +126,8 @@ void MD5Dialog::SetResult(int pid, const std::string& result)
m_results.push_back(result);
if (m_results.size() >= Settings::Instance().GetNetPlayClient()->GetPlayers().size())
auto client = Settings::Instance().GetNetPlayClient();
if (client && m_results.size() >= client->GetPlayers().size())
{
if (std::adjacent_find(m_results.begin(), m_results.end(), std::not_equal_to<>()) ==
m_results.end())
@ -134,7 +143,7 @@ void MD5Dialog::SetResult(int pid, const std::string& result)
void MD5Dialog::reject()
{
auto* server = Settings::Instance().GetNetPlayServer();
auto server = Settings::Instance().GetNetPlayServer();
if (server)
server->AbortMD5();

View File

@ -258,8 +258,9 @@ void NetPlayDialog::ConnectWidgets()
if (value == m_buffer_size)
return;
if (Settings::Instance().GetNetPlayServer() != nullptr)
Settings::Instance().GetNetPlayServer()->AdjustPadBufferSize(value);
auto server = Settings::Instance().GetNetPlayServer();
if (server)
server->AdjustPadBufferSize(value);
});
connect(m_start_button, &QPushButton::clicked, this, &NetPlayDialog::OnStart);
@ -459,7 +460,10 @@ void NetPlayDialog::show(std::string nickname, bool use_traversal)
void NetPlayDialog::UpdateGUI()
{
auto* client = Settings::Instance().GetNetPlayClient();
auto client = Settings::Instance().GetNetPlayClient();
auto server = Settings::Instance().GetNetPlayServer();
if (!client)
return;
// Update Player List
const auto players = client->GetPlayers();
@ -554,11 +558,10 @@ void NetPlayDialog::UpdateGUI()
break;
}
}
else if (Settings::Instance().GetNetPlayServer())
else if (server)
{
m_hostcode_label->setText(
QString::fromStdString(Settings::Instance().GetNetPlayServer()->GetInterfaceHost(
m_room_box->currentData().toString().toStdString())));
m_hostcode_label->setText(QString::fromStdString(
server->GetInterfaceHost(m_room_box->currentData().toString().toStdString())));
m_hostcode_action_button->setText(tr("Copy"));
m_hostcode_action_button->setEnabled(true);
}
@ -643,7 +646,7 @@ void NetPlayDialog::GameStatusChanged(bool running)
void NetPlayDialog::SetOptionsEnabled(bool enabled)
{
if (Settings::Instance().GetNetPlayServer() != nullptr)
if (Settings::Instance().GetNetPlayServer())
{
m_start_button->setEnabled(enabled);
m_game_button->setEnabled(enabled);
@ -663,7 +666,9 @@ void NetPlayDialog::OnMsgStartGame()
DisplayMessage(tr("Started game"), "green");
QueueOnObject(this, [this] {
Settings::Instance().GetNetPlayClient()->StartGame(FindGame(m_current_game));
auto client = Settings::Instance().GetNetPlayClient();
if (client)
client->StartGame(FindGame(m_current_game));
});
}

View File

@ -59,8 +59,8 @@ void PadMappingDialog::ConnectWidgets()
int PadMappingDialog::exec()
{
auto* client = Settings::Instance().GetNetPlayClient();
auto* server = Settings::Instance().GetNetPlayServer();
auto client = Settings::Instance().GetNetPlayClient();
auto server = Settings::Instance().GetNetPlayServer();
// Load Settings
m_players = client->GetPlayers();
m_pad_mapping = server->GetPadMapping();

View File

@ -286,9 +286,9 @@ GameListModel* Settings::GetGameListModel() const
return model;
}
NetPlay::NetPlayClient* Settings::GetNetPlayClient()
std::shared_ptr<NetPlay::NetPlayClient> Settings::GetNetPlayClient()
{
return m_client.get();
return m_client;
}
void Settings::ResetNetPlayClient(NetPlay::NetPlayClient* client)
@ -296,9 +296,9 @@ void Settings::ResetNetPlayClient(NetPlay::NetPlayClient* client)
m_client.reset(client);
}
NetPlay::NetPlayServer* Settings::GetNetPlayServer()
std::shared_ptr<NetPlay::NetPlayServer> Settings::GetNetPlayServer()
{
return m_server.get();
return m_server;
}
void Settings::ResetNetPlayServer(NetPlay::NetPlayServer* server)

View File

@ -25,7 +25,7 @@ namespace NetPlay
{
class NetPlayClient;
class NetPlayServer;
}
} // namespace NetPlay
class GameListModel;
class InputConfig;
@ -100,9 +100,9 @@ public:
void DecreaseVolume(int volume);
// NetPlay
NetPlay::NetPlayClient* GetNetPlayClient();
std::shared_ptr<NetPlay::NetPlayClient> GetNetPlayClient();
void ResetNetPlayClient(NetPlay::NetPlayClient* client = nullptr);
NetPlay::NetPlayServer* GetNetPlayServer();
std::shared_ptr<NetPlay::NetPlayServer> GetNetPlayServer();
void ResetNetPlayServer(NetPlay::NetPlayServer* server = nullptr);
// Cheats
@ -173,8 +173,8 @@ signals:
private:
bool m_batch = false;
bool m_controller_state_needed = false;
std::unique_ptr<NetPlay::NetPlayClient> m_client;
std::unique_ptr<NetPlay::NetPlayServer> m_server;
std::shared_ptr<NetPlay::NetPlayClient> m_client;
std::shared_ptr<NetPlay::NetPlayServer> m_server;
Settings();
};