Skip to content

Commit 6f64190

Browse files
committed
ICE client refactor: Simplify
Delete UpdateHostCandidates, just create the host candidate immediately when we create the interface. Also delete ICECandidateBase::CalcPriority. We only called that when adding a new local candidate, so just do the work directly in the constructor.
1 parent e735bc0 commit 6f64190

2 files changed

Lines changed: 19 additions & 62 deletions

File tree

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_ice_client.cpp

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,13 @@ void CSteamNetworkingICESession::GatherInterfaces()
12311231
m_vecInterfaces.emplace_back( std::move( pIntf ) );
12321232
if ( uNextPriority > 0 )
12331233
--uNextPriority;
1234+
1235+
ICESessionInterface *pNewIntf = m_vecInterfaces.back().get();
1236+
ICELocalCandidate *pHostCandidate = push_back_get_ptr( m_vecCandidates,
1237+
ICELocalCandidate( ICECandidateKind::Host, pNewIntf->m_pSocket->m_boundAddr, pNewIntf ) );
1238+
if ( m_pCallbacks != nullptr )
1239+
m_pCallbacks->OnLocalCandidateDiscovered( *pHostCandidate );
1240+
m_bCandidatePairsNeedUpdate = true;
12341241
}
12351242
}
12361243

@@ -1478,8 +1485,6 @@ void CSteamNetworkingICESession::Think( SteamNetworkingMicroseconds usecNow )
14781485
// We tried to update interfaces but failed. Try again later.
14791486
if ( m_bInterfaceListStale )
14801487
return;
1481-
1482-
UpdateHostCandidates();
14831488
}
14841489

14851490
Think_KeepAliveOnCandidates( usecNow );
@@ -1547,38 +1552,6 @@ void CSteamNetworkingICESession::Think_DiscoverServerReflexiveCandidates()
15471552
}
15481553
}
15491554

1550-
void CSteamNetworkingICESession::UpdateHostCandidates()
1551-
{
1552-
SteamNetworkingGlobalLock::AssertHeldByCurrentThread( "CSteamNetworkingICESession::UpdateHostCandidates" );
1553-
1554-
std_vector<ICELocalCandidate> vecPreviousCandidates;
1555-
std::swap( vecPreviousCandidates, m_vecCandidates );
1556-
1557-
for ( const std::unique_ptr<ICESessionInterface> &pIntf : m_vecInterfaces )
1558-
{
1559-
const SteamNetworkingIPAddr &hostCandidateAddr = pIntf->m_pSocket->m_boundAddr;
1560-
1561-
// Restore any candidates previously discovered for this interface.
1562-
ICELocalCandidate *pHostCandidate = nullptr;
1563-
for ( const ICELocalCandidate& prevCandidate : vecPreviousCandidates )
1564-
{
1565-
if ( prevCandidate.m_pInterface == pIntf.get() )
1566-
{
1567-
ICELocalCandidate *pAdded = push_back_get_ptr( m_vecCandidates, prevCandidate );
1568-
if ( prevCandidate.m_type == ICECandidateKind::Host )
1569-
pHostCandidate = pAdded;
1570-
}
1571-
}
1572-
1573-
if ( pHostCandidate == nullptr )
1574-
pHostCandidate = push_back_get_ptr( m_vecCandidates, ICELocalCandidate( ICECandidateKind::Host, hostCandidateAddr, pIntf.get() ) );
1575-
pHostCandidate->m_nPriority = pHostCandidate->CalcPriority( pIntf->m_nPriority );
1576-
if ( m_pCallbacks != nullptr )
1577-
m_pCallbacks->OnLocalCandidateDiscovered( *pHostCandidate );
1578-
}
1579-
1580-
}
1581-
15821555

15831556

15841557
void CSteamNetworkingICESession::STUNRequestCallback_ServerReflexiveCandidate( const RecvSTUNPktInfo_t &info )
@@ -1612,7 +1585,6 @@ void CSteamNetworkingICESession::STUNRequestCallback_ServerReflexiveCandidate( c
16121585
if ( bindResult == localAddr )
16131586
bindResult.Clear();
16141587
ICELocalCandidate *pCand = push_back_get_ptr( m_vecCandidates, ICELocalCandidate( ICECandidateKind::ServerReflexive, bindResult, pIntf ) );
1615-
pCand->m_nPriority = pCand->CalcPriority( pIntf->m_nPriority );
16161588
pCand->m_stunServer = info.m_pRequest->m_remoteAddr;
16171589
if ( m_pCallbacks != nullptr && !bindResult.IsIPv6AllZeros() )
16181590
m_pCallbacks->OnLocalCandidateDiscovered( *pCand );
@@ -1632,7 +1604,6 @@ void CSteamNetworkingICESession::STUNRequestCallback_ServerReflexiveCandidate( c
16321604
// be retried forever every think tick, creating unbounded churn.
16331605
bindResult.Clear();
16341606
ICELocalCandidate *pCand = push_back_get_ptr( m_vecCandidates, ICELocalCandidate( ICECandidateKind::ServerReflexive, bindResult, pIntf ) );
1635-
pCand->m_nPriority = 0;
16361607
pCand->m_stunServer = info.m_pRequest->m_remoteAddr;
16371608
return;
16381609
}
@@ -1982,33 +1953,21 @@ CSteamNetworkingICESession::ICELocalCandidate::ICELocalCandidate( ICECandidateKi
19821953
, m_pInterface( pInterface )
19831954
{
19841955
m_stunServer.Clear();
1985-
}
19861956

1987-
uint32 CSteamNetworkingICESession::ICECandidateBase::CalcPriority( uint32 nLocalPreference )
1988-
{
1989-
/*priority = (2^24)*(type preference) +
1990-
(2^8)*(local preference) +
1991-
(2^0)*(256 - component ID) */
1992-
1993-
if ( m_type == ICECandidateKind::None )
1994-
return 0;
1995-
if ( m_addr.IsIPv6AllZeros() )
1996-
return 0;
1997-
1998-
uint32 nTypePreference = 0;
1999-
/* The RECOMMENDED values for type preferences are 126 for host
2000-
candidates, 110 for peer-reflexive candidates, 100 for server-
2001-
reflexive candidates, and 0 for relayed candidates. */
2002-
switch ( m_type )
1957+
// priority = (2^24)*(type preference) + (2^8)*(local preference) + (2^0)*(256 - component ID)
1958+
// Type preferences per RFC 8445: host=126, peer-reflexive=110, server-reflexive=100, relay=0.
1959+
if ( m_type != ICECandidateKind::None && !m_addr.IsIPv6AllZeros() )
20031960
{
2004-
case ICECandidateKind::Host: nTypePreference = 126; break;
2005-
case ICECandidateKind::ServerReflexive: nTypePreference = 100; break;
2006-
case ICECandidateKind::PeerReflexive: nTypePreference = 110; break;
2007-
case ICECandidateKind::None: default: nTypePreference = 0; break;
1961+
uint32 nTypePreference;
1962+
switch ( m_type )
1963+
{
1964+
case ICECandidateKind::Host: nTypePreference = 126; break;
1965+
case ICECandidateKind::ServerReflexive: nTypePreference = 100; break;
1966+
case ICECandidateKind::PeerReflexive: nTypePreference = 110; break;
1967+
default: nTypePreference = 0; break;
1968+
}
1969+
m_nPriority = ( nTypePreference << 24 ) + ( ( pInterface->m_nPriority & 0xFFFF ) << 8 ) + 255u;
20081970
}
2009-
2010-
uint32 nComponentID = 1;
2011-
return (( nTypePreference & 0xFF ) << 24 ) + (( nLocalPreference & 0xFFFF ) << 8 ) + ( 256 - ( nComponentID & 0xFF ) );
20121971
}
20131972

20141973
// Compute a candidate-attribute from https://datatracker.ietf.org/doc/html/rfc5245#section-15.1

src/steamnetworkingsockets/clientlib/steamnetworkingsockets_ice_client.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ namespace SteamNetworkingSocketsLib {
218218
uint32 m_nPriority;
219219
ICECandidateBase();
220220
ICECandidateBase( ICECandidateKind t, const SteamNetworkingIPAddr& addr );
221-
uint32 CalcPriority( uint32 nLocalPreference );
222221
EICECandidateType CalcType() const;
223222
};
224223
struct ICELocalCandidate : public ICECandidateBase
@@ -376,7 +375,6 @@ namespace SteamNetworkingSocketsLib {
376375
std_vector< ICECandidatePair* > m_vecTriggeredCheckQueue;
377376

378377
void GatherInterfaces();
379-
void UpdateHostCandidates();
380378
void UpdateKeepalive( const ICELocalCandidate& c );
381379
uint32 GetInterfaceLocalPreference( const SteamNetworkingIPAddr& addr );
382380

0 commit comments

Comments
 (0)