Fix possible thread issue.

merge-requests/28/head
Mr_Goldberg 2020-01-17 15:46:50 -05:00
parent 29e713b94c
commit e6031c7597
No known key found for this signature in database
GPG Key ID: 8597D87419DEF278
1 changed files with 13 additions and 6 deletions

View File

@ -70,6 +70,8 @@ public ISteamNetworking
std::recursive_mutex messages_mutex; std::recursive_mutex messages_mutex;
std::vector<Common_Message> messages; std::vector<Common_Message> messages;
std::recursive_mutex connections_edit_mutex;
std::vector<struct Steam_Networking_Connection> connections; std::vector<struct Steam_Networking_Connection> connections;
std::vector<struct steam_listen_socket> listen_sockets; std::vector<struct steam_listen_socket> listen_sockets;
@ -79,11 +81,13 @@ public ISteamNetworking
bool connection_exists(CSteamID id) bool connection_exists(CSteamID id)
{ {
std::lock_guard<std::recursive_mutex> lock(connections_edit_mutex);
return std::find_if(connections.begin(), connections.end(), [&id](struct Steam_Networking_Connection const& conn) { return conn.remote == id;}) != connections.end(); return std::find_if(connections.begin(), connections.end(), [&id](struct Steam_Networking_Connection const& conn) { return conn.remote == id;}) != connections.end();
} }
struct Steam_Networking_Connection *get_or_create_connection(CSteamID id) struct Steam_Networking_Connection *get_or_create_connection(CSteamID id)
{ {
std::lock_guard<std::recursive_mutex> lock(connections_edit_mutex);
auto conn = std::find_if(connections.begin(), connections.end(), [&id](struct Steam_Networking_Connection const& conn) { return conn.remote == id;}); auto conn = std::find_if(connections.begin(), connections.end(), [&id](struct Steam_Networking_Connection const& conn) { return conn.remote == id;});
if (connections.end() == conn) { if (connections.end() == conn) {
@ -98,13 +102,16 @@ struct Steam_Networking_Connection *get_or_create_connection(CSteamID id)
void remove_connection(CSteamID id) void remove_connection(CSteamID id)
{ {
auto conn = std::begin(connections); {
while (conn != std::end(connections)) { std::lock_guard<std::recursive_mutex> lock(connections_edit_mutex);
if (conn->remote == id) { auto conn = std::begin(connections);
while (conn != std::end(connections)) {
if (conn->remote == id) {
conn = connections.erase(conn); conn = connections.erase(conn);
} else { } else {
++conn; ++conn;
}
} }
} }