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

merge-requests/28/head
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 RunEveryRunCB *run_every_runcb;
std::recursive_mutex messages_mutex;
std::vector<Common_Message> messages;
std::vector<struct Steam_Networking_Connection> connections;
@ -108,6 +109,8 @@ void remove_connection(CSteamID id)
}
//pretty sure steam also clears the entire queue of messages for that connection
{
std::lock_guard<std::recursive_mutex> lock(messages_mutex);
auto msg = std::begin(messages);
while (msg != std::end(messages)) {
if (msg->source_id() == id.ConvertToUint64()) {
@ -116,6 +119,7 @@ void remove_connection(CSteamID id)
++msg;
}
}
}
}
SNetSocket_t create_connection_socket(CSteamID target, int nVirtualPort, uint32 nIP, uint16 nPort, SNetListenSocket_t id=0, enum steam_socket_connection_status status=SOCKET_CONNECTING, SNetSocket_t other_id=0)
@ -278,7 +282,7 @@ bool SendP2PPacket( CSteamID steamIDRemote, const void *pubData, uint32 cubData,
bool IsP2PPacketAvailable( uint32 *pcubMsgSize, int 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"
//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();
@ -312,7 +316,7 @@ bool IsP2PPacketAvailable( uint32 *pcubMsgSize)
bool ReadP2PPacket( void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, CSteamID *psteamIDRemote, int 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"
//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();
@ -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();
{
std::lock_guard<std::recursive_mutex> lock(messages_mutex);
for (auto &msg : messages) {
CSteamID source_id((uint64)msg.source_id());
if (!msg.network().processed()) {
@ -839,6 +846,8 @@ void RunCallbacks()
}
}
}
//TODO: not sure if sockets should be wiped right away
remove_killed_connection_sockets();
@ -862,6 +871,7 @@ void Callback(Common_Message *msg)
}PRINT_DEBUG("\n");
#endif
std::lock_guard<std::recursive_mutex> lock(messages_mutex);
if (msg->network().type() == Network::DATA) {
messages.push_back(Common_Message(*msg));
}