Add a mutex specifically for the P2P packet functions to speed things up.

This commit is contained in:
Mr_Goldberg 2020-01-17 13:13:59 -05:00
parent b53d5224f3
commit 29e713b94c
No known key found for this signature in database
GPG Key ID: 8597D87419DEF278
1 changed files with 18 additions and 8 deletions

View File

@ -68,6 +68,7 @@ public ISteamNetworking
class SteamCallBacks *callbacks; class SteamCallBacks *callbacks;
class RunEveryRunCB *run_every_runcb; class RunEveryRunCB *run_every_runcb;
std::recursive_mutex messages_mutex;
std::vector<Common_Message> messages; std::vector<Common_Message> messages;
std::vector<struct Steam_Networking_Connection> connections; std::vector<struct Steam_Networking_Connection> connections;
@ -108,12 +109,15 @@ void remove_connection(CSteamID id)
} }
//pretty sure steam also clears the entire queue of messages for that connection //pretty sure steam also clears the entire queue of messages for that connection
auto msg = std::begin(messages); {
while (msg != std::end(messages)) { std::lock_guard<std::recursive_mutex> lock(messages_mutex);
if (msg->source_id() == id.ConvertToUint64()) { auto msg = std::begin(messages);
msg = messages.erase(msg); while (msg != std::end(messages)) {
} else { if (msg->source_id() == id.ConvertToUint64()) {
++msg; msg = messages.erase(msg);
} else {
++msg;
}
} }
} }
} }
@ -278,7 +282,7 @@ bool SendP2PPacket( CSteamID steamIDRemote, const void *pubData, uint32 cubData,
bool IsP2PPacketAvailable( uint32 *pcubMsgSize, int nChannel) bool IsP2PPacketAvailable( uint32 *pcubMsgSize, int nChannel)
{ {
PRINT_DEBUG("Steam_Networking::IsP2PPacketAvailable channel: %i\n", nChannel); PRINT_DEBUG("Steam_Networking::IsP2PPacketAvailable channel: %i\n", nChannel);
std::lock_guard<std::recursive_mutex> lock(global_mutex); std::lock_guard<std::recursive_mutex> lock(messages_mutex);
//Not sure if this should be here because it slightly screws up games that don't like such low "pings" //Not sure if this should be here because it slightly screws up games that don't like such low "pings"
//Commenting it out for now because it looks like it causes a bug where 20xx gets stuck in an infinite receive packet loop //Commenting it out for now because it looks like it causes a bug where 20xx gets stuck in an infinite receive packet loop
//this->network->Run(); //this->network->Run();
@ -312,7 +316,7 @@ bool IsP2PPacketAvailable( uint32 *pcubMsgSize)
bool ReadP2PPacket( void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, CSteamID *psteamIDRemote, int nChannel) bool ReadP2PPacket( void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, CSteamID *psteamIDRemote, int nChannel)
{ {
PRINT_DEBUG("Steam_Networking::ReadP2PPacket %u %i\n", cubDest, nChannel); PRINT_DEBUG("Steam_Networking::ReadP2PPacket %u %i\n", cubDest, nChannel);
std::lock_guard<std::recursive_mutex> lock(global_mutex); std::lock_guard<std::recursive_mutex> lock(messages_mutex);
//Not sure if this should be here because it slightly screws up games that don't like such low "pings" //Not sure if this should be here because it slightly screws up games that don't like such low "pings"
//Commenting it out for now because it looks like it causes a bug where 20xx gets stuck in an infinite receive packet loop //Commenting it out for now because it looks like it causes a bug where 20xx gets stuck in an infinite receive packet loop
//this->network->Run(); //this->network->Run();
@ -800,6 +804,9 @@ void RunCallbacks()
{ {
uint64 current_time = std::chrono::duration_cast<std::chrono::duration<uint64>>(std::chrono::system_clock::now().time_since_epoch()).count(); uint64 current_time = std::chrono::duration_cast<std::chrono::duration<uint64>>(std::chrono::system_clock::now().time_since_epoch()).count();
{
std::lock_guard<std::recursive_mutex> lock(messages_mutex);
for (auto &msg : messages) { for (auto &msg : messages) {
CSteamID source_id((uint64)msg.source_id()); CSteamID source_id((uint64)msg.source_id());
if (!msg.network().processed()) { if (!msg.network().processed()) {
@ -839,6 +846,8 @@ void RunCallbacks()
} }
} }
}
//TODO: not sure if sockets should be wiped right away //TODO: not sure if sockets should be wiped right away
remove_killed_connection_sockets(); remove_killed_connection_sockets();
@ -862,6 +871,7 @@ void Callback(Common_Message *msg)
}PRINT_DEBUG("\n"); }PRINT_DEBUG("\n");
#endif #endif
std::lock_guard<std::recursive_mutex> lock(messages_mutex);
if (msg->network().type() == Network::DATA) { if (msg->network().type() == Network::DATA) {
messages.push_back(Common_Message(*msg)); messages.push_back(Common_Message(*msg));
} }