Skip to content

Commit

Permalink
[Steam only] Tests can disable P2P local loopback optimization
Browse files Browse the repository at this point in the history
P4:6571413
  • Loading branch information
zpostfacto committed May 29, 2021
1 parent 101e385 commit 9875f39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ class CSteamNetworkingSockets : public IClientNetworkingSockets

#ifdef STEAMNETWORKINGSOCKETS_STEAMCLIENT
virtual int ReceiveMessagesOnListenSocketLegacyPollGroup( HSteamListenSocket hSocket, SteamNetworkingMessage_t **ppOutMessages, int nMaxMessages ) override;
virtual void TEST_EnableP2PLooopbackOptimization( bool flag ) override { m_TEST_bEnableP2PLoopbackOptimization = flag; }
bool m_TEST_bEnableP2PLoopbackOptimization = true;
#else
static constexpr bool m_TEST_bEnableP2PLoopbackOptimization = true;
#endif

virtual void RunCallbacks() override;
Expand Down
52 changes: 28 additions & 24 deletions src/steamnetworkingsockets/clientlib/steamnetworkingsockets_p2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2408,35 +2408,39 @@ CSteamNetworkConnectionBase *CSteamNetworkingSockets::InternalConnectP2PDefaultS

SteamDatagramErrMsg errMsg;

// Check for connecting to an identity in this process
for ( CSteamNetworkingSockets *pLocalInstance: CSteamNetworkingSockets::s_vecSteamNetworkingSocketsInstances )
// Check for connecting to an identity in this process. In some test environments we may intentionally
// disable this optimization to force two clients to talk to each other through the relay
if ( m_TEST_bEnableP2PLoopbackOptimization )
{
if ( pLocalInstance->InternalGetIdentity() == identityRemote )
for ( CSteamNetworkingSockets *pLocalInstance: CSteamNetworkingSockets::s_vecSteamNetworkingSocketsInstances )
{

// This is the guy we want to talk to. Are we listening on that virtual port?
int idx = pLocalInstance->m_mapListenSocketsByVirtualPort.Find( nRemoteVirtualPort );
if ( idx == pLocalInstance->m_mapListenSocketsByVirtualPort.InvalidIndex() )
if ( pLocalInstance->InternalGetIdentity() == identityRemote )
{
SpewError( "Cannot create P2P connection to local identity %s. We are not listening on vport %d", SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort );
return nullptr;
}

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

// Failed?
SpewError( "P2P connection to local identity %s on vport %d; FAILED to create loopback. %s\n",
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort, errMsg );
return nullptr;
// Create a loopback connection
CSteamNetworkConnectionPipe *pConn = CSteamNetworkConnectionPipe::CreateLoopbackConnection( this, nOptions, pOptions, pLocalInstance->m_mapListenSocketsByVirtualPort[ idx ], errMsg, scopeLock );
if ( pConn )
{
SpewVerbose( "[%s] Using loopback for P2P connection to local identity %s on vport %d. Partner is [%s]\n",
pConn->GetDescription(),
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort,
pConn->m_pPartner->GetDescription() );
return pConn;
}

// Failed?
SpewError( "P2P connection to local identity %s on vport %d; FAILED to create loopback. %s\n",
SteamNetworkingIdentityRender( identityRemote ).c_str(), nRemoteVirtualPort, errMsg );
return nullptr;
}
}
}

Expand Down

0 comments on commit 9875f39

Please sign in to comment.