Skip to content

Commit 9875f39

Browse files
committed
[Steam only] Tests can disable P2P local loopback optimization
P4:6571413
1 parent 101e385 commit 9875f39

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/steamnetworkingsockets/clientlib/csteamnetworkingsockets.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class CSteamNetworkingSockets : public IClientNetworkingSockets
112112

113113
#ifdef STEAMNETWORKINGSOCKETS_STEAMCLIENT
114114
virtual int ReceiveMessagesOnListenSocketLegacyPollGroup( HSteamListenSocket hSocket, SteamNetworkingMessage_t **ppOutMessages, int nMaxMessages ) override;
115+
virtual void TEST_EnableP2PLooopbackOptimization( bool flag ) override { m_TEST_bEnableP2PLoopbackOptimization = flag; }
116+
bool m_TEST_bEnableP2PLoopbackOptimization = true;
117+
#else
118+
static constexpr bool m_TEST_bEnableP2PLoopbackOptimization = true;
115119
#endif
116120

117121
virtual void RunCallbacks() override;

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_p2p.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,35 +2408,39 @@ CSteamNetworkConnectionBase *CSteamNetworkingSockets::InternalConnectP2PDefaultS
24082408

24092409
SteamDatagramErrMsg errMsg;
24102410

2411-
// Check for connecting to an identity in this process
2412-
for ( CSteamNetworkingSockets *pLocalInstance: CSteamNetworkingSockets::s_vecSteamNetworkingSocketsInstances )
2411+
// Check for connecting to an identity in this process. In some test environments we may intentionally
2412+
// disable this optimization to force two clients to talk to each other through the relay
2413+
if ( m_TEST_bEnableP2PLoopbackOptimization )
24132414
{
2414-
if ( pLocalInstance->InternalGetIdentity() == identityRemote )
2415+
for ( CSteamNetworkingSockets *pLocalInstance: CSteamNetworkingSockets::s_vecSteamNetworkingSocketsInstances )
24152416
{
2416-
2417-
// This is the guy we want to talk to. Are we listening on that virtual port?
2418-
int idx = pLocalInstance->m_mapListenSocketsByVirtualPort.Find( nRemoteVirtualPort );
2419-
if ( idx == pLocalInstance->m_mapListenSocketsByVirtualPort.InvalidIndex() )
2417+
if ( pLocalInstance->InternalGetIdentity() == identityRemote )
24202418
{
2421-
SpewError( "Cannot create P2P connection to local identity %s. We are not listening on vport %d", SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort );
2422-
return nullptr;
2423-
}
24242419

2425-
// Create a loopback connection
2426-
CSteamNetworkConnectionPipe *pConn = CSteamNetworkConnectionPipe::CreateLoopbackConnection( this, nOptions, pOptions, pLocalInstance->m_mapListenSocketsByVirtualPort[ idx ], errMsg, scopeLock );
2427-
if ( pConn )
2428-
{
2429-
SpewVerbose( "[%s] Using loopback for P2P connection to local identity %s on vport %d. Partner is [%s]\n",
2430-
pConn->GetDescription(),
2431-
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort,
2432-
pConn->m_pPartner->GetDescription() );
2433-
return pConn;
2434-
}
2420+
// This is the guy we want to talk to. Are we listening on that virtual port?
2421+
int idx = pLocalInstance->m_mapListenSocketsByVirtualPort.Find( nRemoteVirtualPort );
2422+
if ( idx == pLocalInstance->m_mapListenSocketsByVirtualPort.InvalidIndex() )
2423+
{
2424+
SpewError( "Cannot create P2P connection to local identity %s. We are not listening on vport %d", SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort );
2425+
return nullptr;
2426+
}
24352427

2436-
// Failed?
2437-
SpewError( "P2P connection to local identity %s on vport %d; FAILED to create loopback. %s\n",
2438-
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort, errMsg );
2439-
return nullptr;
2428+
// Create a loopback connection
2429+
CSteamNetworkConnectionPipe *pConn = CSteamNetworkConnectionPipe::CreateLoopbackConnection( this, nOptions, pOptions, pLocalInstance->m_mapListenSocketsByVirtualPort[ idx ], errMsg, scopeLock );
2430+
if ( pConn )
2431+
{
2432+
SpewVerbose( "[%s] Using loopback for P2P connection to local identity %s on vport %d. Partner is [%s]\n",
2433+
pConn->GetDescription(),
2434+
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort,
2435+
pConn->m_pPartner->GetDescription() );
2436+
return pConn;
2437+
}
2438+
2439+
// Failed?
2440+
SpewError( "P2P connection to local identity %s on vport %d; FAILED to create loopback. %s\n",
2441+
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort, errMsg );
2442+
return nullptr;
2443+
}
24402444
}
24412445
}
24422446

0 commit comments

Comments
 (0)