Skip to content

Commit ef74e90

Browse files
committed
CreateSocketPair - fix confusing "swapped" identities
They are swapped for historical reasons. (It's too dangerous/ difficult to change the API to the more intuitive order.) So, I just added comments to clarify exactly what the behaviour is, and renamed a bunch of variables to hopefully make the code more clear, too. This addresses #404
1 parent 8767517 commit ef74e90

8 files changed

Lines changed: 27 additions & 22 deletions

include/steam/isteamnetworkingsockets.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,12 @@ class ISteamNetworkingSockets
389389
/// identity. Otherwise, if you pass nullptr, the respective connection will assume a generic
390390
/// "localhost" identity. If you use real network loopback, this might be translated to the
391391
/// actual bound loopback port. Otherwise, the port will be zero.
392-
virtual bool CreateSocketPair( HSteamNetConnection *pOutConnection1, HSteamNetConnection *pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity *pIdentity1, const SteamNetworkingIdentity *pIdentity2 ) = 0;
392+
///
393+
/// NOTE: the identities refer to the *remote* identities that the corresponding connection
394+
/// will observe in connection state callbacks and GetConnectionInfo:
395+
/// - pPeerIdentity1: remote identity observed by connection 1, local identity of connection 2
396+
/// - pPeerIdentity2: remote identity observed by connection 2, local identity of connection 1
397+
virtual bool CreateSocketPair( HSteamNetConnection *pOutConnection1, HSteamNetConnection *pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity *pPeerIdentity1, const SteamNetworkingIdentity *pPeerIdentity2 ) = 0;
393398

394399
/// Configure multiple outbound messages streams ("lanes") on a connection, and
395400
/// control head-of-line blocking between them. Messages within a given lane

src/steamnetworkingsockets/clientlib/csteamnetworkingsockets.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,29 +1548,29 @@ bool CSteamNetworkingSockets::GetListenSocketAddress( HSteamListenSocket hSocket
15481548
return pSock->APIGetAddress( pAddress );
15491549
}
15501550

1551-
bool CSteamNetworkingSockets::CreateSocketPair( HSteamNetConnection *pOutConnection1, HSteamNetConnection *pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity *pIdentity1, const SteamNetworkingIdentity *pIdentity2 )
1551+
bool CSteamNetworkingSockets::CreateSocketPair( HSteamNetConnection *pOutConnection1, HSteamNetConnection *pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity *pPeerIdentity1, const SteamNetworkingIdentity *pPeerIdentity2 )
15521552
{
15531553
SteamNetworkingGlobalLock scopeLock( "CreateSocketPair" );
15541554

15551555
// Assume failure
15561556
*pOutConnection1 = k_HSteamNetConnection_Invalid;
15571557
*pOutConnection2 = k_HSteamNetConnection_Invalid;
1558-
SteamNetworkingIdentity identity[ 2 ] = {};
1559-
if ( pIdentity1 )
1560-
identity[0] = *pIdentity1;
1558+
SteamNetworkingIdentity peerIdentity[ 2 ] = {};
1559+
if ( pPeerIdentity1 )
1560+
peerIdentity[0] = *pPeerIdentity1;
15611561
else
1562-
identity[0].SetLocalHost();
1563-
if ( pIdentity2 )
1564-
identity[1] = *pIdentity2;
1562+
peerIdentity[0].SetLocalHost();
1563+
if ( pPeerIdentity2 )
1564+
peerIdentity[1] = *pPeerIdentity2;
15651565
else
1566-
identity[1].SetLocalHost();
1566+
peerIdentity[1].SetLocalHost();
15671567

15681568
// Create network connections?
15691569
if ( bUseNetworkLoopback )
15701570
{
15711571
// Create two connection objects
15721572
CSteamNetworkConnectionlocalhostLoopback *pConn[2];
1573-
if ( !CSteamNetworkConnectionlocalhostLoopback::APICreateSocketPair( this, pConn, identity ) )
1573+
if ( !CSteamNetworkConnectionlocalhostLoopback::APICreateSocketPair( this, pConn, peerIdentity ) )
15741574
return false;
15751575

15761576
// Return their handles
@@ -1581,7 +1581,7 @@ bool CSteamNetworkingSockets::CreateSocketPair( HSteamNetConnection *pOutConnect
15811581
{
15821582
// Create two connection objects
15831583
CSteamNetworkConnectionPipe *pConn[2];
1584-
if ( !CSteamNetworkConnectionPipe::APICreateSocketPair( this, pConn, identity ) )
1584+
if ( !CSteamNetworkConnectionPipe::APICreateSocketPair( this, pConn, peerIdentity ) )
15851585
return false;
15861586

15871587
// Return their handles

src/steamnetworkingsockets/clientlib/csteamnetworkingsockets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class CSteamNetworkingSockets : public IClientNetworkingSockets
9999
virtual EResult GetConnectionRealTimeStatus( HSteamNetConnection hConn, SteamNetConnectionRealTimeStatus_t *pStatus, int nLanes, SteamNetConnectionRealTimeLaneStatus_t *pLanes ) override;
100100
virtual int GetDetailedConnectionStatus( HSteamNetConnection hConn, char *pszBuf, int cbBuf ) override;
101101
virtual bool GetListenSocketAddress( HSteamListenSocket hSocket, SteamNetworkingIPAddr *pAddress ) override;
102-
virtual bool CreateSocketPair( HSteamNetConnection *pOutConnection1, HSteamNetConnection *pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity *pIdentity1, const SteamNetworkingIdentity *pIdentity2 ) override;
102+
virtual bool CreateSocketPair( HSteamNetConnection *pOutConnection1, HSteamNetConnection *pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity *pPeerIdentity1, const SteamNetworkingIdentity *pPeerIdentity2 ) override;
103103
virtual EResult ConfigureConnectionLanes( HSteamNetConnection hConn, int nNumLanes, const int *pLanePriorities, const uint16 *pLaneWeights ) override;
104104
virtual bool GetIdentity( SteamNetworkingIdentity *pIdentity ) override;
105105

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3858,7 +3858,7 @@ CSteamNetworkConnectionP2P *CSteamNetworkConnectionBase::AsSteamNetworkConnectio
38583858
//
38593859
/////////////////////////////////////////////////////////////////////////////
38603860

3861-
bool CSteamNetworkConnectionPipe::APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionPipe *pConn[2], const SteamNetworkingIdentity pIdentity[2] )
3861+
bool CSteamNetworkConnectionPipe::APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionPipe *pConn[2], const SteamNetworkingIdentity pPeerIdentity[2] )
38623862
{
38633863
SteamDatagramErrMsg errMsg;
38643864
SteamNetworkingMicroseconds usecNow = SteamNetworkingSockets_GetLocalTimestamp();
@@ -3868,8 +3868,8 @@ bool CSteamNetworkConnectionPipe::APICreateSocketPair( CSteamNetworkingSockets *
38683868
// very efficiently, without taking the global lock or queuing stuff
38693869
constexpr bool bUseFastPath = true;
38703870

3871-
pConn[1] = new CSteamNetworkConnectionPipe( pSteamNetworkingSocketsInterface, pIdentity[0], scopeLock[0], bUseFastPath );
3872-
pConn[0] = new CSteamNetworkConnectionPipe( pSteamNetworkingSocketsInterface, pIdentity[1], scopeLock[1], bUseFastPath );
3871+
pConn[1] = new CSteamNetworkConnectionPipe( pSteamNetworkingSocketsInterface, pPeerIdentity[0], scopeLock[0], bUseFastPath );
3872+
pConn[0] = new CSteamNetworkConnectionPipe( pSteamNetworkingSocketsInterface, pPeerIdentity[1], scopeLock[1], bUseFastPath );
38733873
if ( !pConn[0] || !pConn[1] )
38743874
{
38753875
failed:

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ class CSteamNetworkConnectionPipe final : public CSteamNetworkConnectionBase, pu
10521052

10531053
/// Create a pair of loopback connections that are immediately connected to each other
10541054
/// No callbacks are posted.
1055-
static bool APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionPipe **pOutConnections, const SteamNetworkingIdentity pIdentity[2] );
1055+
static bool APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionPipe **pOutConnections, const SteamNetworkingIdentity pPeerIdentity[2] );
10561056

10571057
/// Create a pair of loopback connections that act like normal connections, but use internal transport.
10581058
/// The two connections will be placed in the "connecting" state, and will go through the ordinary

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_flat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ STEAMNETWORKINGSOCKETS_INTERFACE bool SteamAPI_ISteamNetworkingSockets_GetListen
9393
{
9494
return self->GetListenSocketAddress( hSocket,address );
9595
}
96-
STEAMNETWORKINGSOCKETS_INTERFACE bool SteamAPI_ISteamNetworkingSockets_CreateSocketPair( ISteamNetworkingSockets* self, HSteamNetConnection * pOutConnection1, HSteamNetConnection * pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity * pIdentity1, const SteamNetworkingIdentity * pIdentity2 )
96+
STEAMNETWORKINGSOCKETS_INTERFACE bool SteamAPI_ISteamNetworkingSockets_CreateSocketPair( ISteamNetworkingSockets* self, HSteamNetConnection * pOutConnection1, HSteamNetConnection * pOutConnection2, bool bUseNetworkLoopback, const SteamNetworkingIdentity * pPeerIdentity1, const SteamNetworkingIdentity * pPeerIdentity2 )
9797
{
98-
return self->CreateSocketPair( pOutConnection1,pOutConnection2,bUseNetworkLoopback,pIdentity1,pIdentity2 );
98+
return self->CreateSocketPair( pOutConnection1,pOutConnection2,bUseNetworkLoopback,pPeerIdentity1,pPeerIdentity2 );
9999
}
100100
STEAMNETWORKINGSOCKETS_INTERFACE EResult SteamAPI_ISteamNetworkingSockets_ConfigureConnectionLanes( ISteamNetworkingSockets* self, HSteamNetConnection hConn, int nNumLanes, const int * pLanePriorities, const uint16 * pLaneWeights )
101101
{

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,15 +1863,15 @@ EUnsignedCert CSteamNetworkConnectionlocalhostLoopback::AllowLocalUnsignedCert()
18631863
return k_EUnsignedCert_Allow;
18641864
}
18651865

1866-
bool CSteamNetworkConnectionlocalhostLoopback::APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionlocalhostLoopback *pConn[2], const SteamNetworkingIdentity pIdentity[2] )
1866+
bool CSteamNetworkConnectionlocalhostLoopback::APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionlocalhostLoopback *pConn[2], const SteamNetworkingIdentity pPeerIdentity[2] )
18671867
{
18681868
SteamNetworkingGlobalLock::AssertHeldByCurrentThread();
18691869
ConnectionScopeLock scopeLock[2];
18701870

18711871
SteamDatagramErrMsg errMsg;
18721872

1873-
pConn[1] = new CSteamNetworkConnectionlocalhostLoopback( pSteamNetworkingSocketsInterface, pIdentity[0], scopeLock[0] );
1874-
pConn[0] = new CSteamNetworkConnectionlocalhostLoopback( pSteamNetworkingSocketsInterface, pIdentity[1], scopeLock[1] );
1873+
pConn[1] = new CSteamNetworkConnectionlocalhostLoopback( pSteamNetworkingSocketsInterface, pPeerIdentity[0], scopeLock[0] );
1874+
pConn[0] = new CSteamNetworkConnectionlocalhostLoopback( pSteamNetworkingSocketsInterface, pPeerIdentity[1], scopeLock[1] );
18751875
if ( !pConn[0] || !pConn[1] )
18761876
{
18771877
failed:

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class CSteamNetworkConnectionlocalhostLoopback final : public CSteamNetworkConne
242242
CSteamNetworkConnectionlocalhostLoopback( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, const SteamNetworkingIdentity &identity, ConnectionScopeLock &scopeLock );
243243

244244
/// Setup two connections to be talking to each other
245-
static bool APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionlocalhostLoopback *pConn[2], const SteamNetworkingIdentity pIdentity[2] );
245+
static bool APICreateSocketPair( CSteamNetworkingSockets *pSteamNetworkingSocketsInterface, CSteamNetworkConnectionlocalhostLoopback *pConn[2], const SteamNetworkingIdentity pPeerIdentity[2] );
246246

247247
/// Base class overrides
248248
virtual EUnsignedCert AllowRemoteUnsignedCert() override;

0 commit comments

Comments
 (0)