Added "poll group" interface.

This is used to poll many connections in a single function call.  Previously,
this was only possible if all of the connections were those accepted on the
same listen socket.  (ReceiveMessagesOnListenSocket).  But this left out at
least two important use cases with known users:

- If you create more than one listen socket (because there is more way to
  contact your service, e.g. once for P2P and another for direct IP, and
  another for relayed connections), then you could not poll all of the
  connections efficiently.
- In P2P use cases, we may initiate many connections to peers, and we want
  to poll all of them at once.

This change is relevant to: Issue #49, Issue #50, and issue #52.  (But I don't
this it really "fixes" any of them.)
This commit is contained in:
Fletcher Dunn
2019-12-05 15:51:33 -08:00
parent 8fcfe152eb
commit a9e5e86b28
10 changed files with 470 additions and 57 deletions
+19 -1
View File
@@ -243,6 +243,9 @@ public:
m_hListenSock = m_pInterface->CreateListenSocketIP( serverLocalAddr, 0, nullptr );
if ( m_hListenSock == k_HSteamListenSocket_Invalid )
FatalError( "Failed to listen on port %d", nPort );
m_hPollGroup = m_pInterface->CreatePollGroup();
if ( m_hPollGroup == k_HSteamNetPollGroup_Invalid )
FatalError( "Failed to listen on port %d", nPort );
Printf( "Server listening on port %d\n", nPort );
while ( !g_bQuit )
@@ -268,10 +271,17 @@ public:
m_pInterface->CloseConnection( it.first, 0, "Server Shutdown", true );
}
m_mapClients.clear();
m_pInterface->CloseListenSocket( m_hListenSock );
m_hListenSock = k_HSteamListenSocket_Invalid;
m_pInterface->DestroyPollGroup( m_hPollGroup );
m_hPollGroup = k_HSteamNetPollGroup_Invalid;
}
private:
HSteamListenSocket m_hListenSock;
HSteamNetPollGroup m_hPollGroup;
ISteamNetworkingSockets *m_pInterface;
struct Client_t
@@ -302,7 +312,7 @@ private:
while ( !g_bQuit )
{
ISteamNetworkingMessage *pIncomingMsg = nullptr;
int numMsgs = m_pInterface->ReceiveMessagesOnListenSocket( m_hListenSock, &pIncomingMsg, 1 );
int numMsgs = m_pInterface->ReceiveMessagesOnPollGroup( m_hPollGroup, &pIncomingMsg, 1 );
if ( numMsgs == 0 )
break;
if ( numMsgs < 0 )
@@ -468,6 +478,14 @@ private:
break;
}
// Assign the poll group
if ( !m_pInterface->SetConnectionPollGroup( pInfo->m_hConn, m_hPollGroup ) )
{
m_pInterface->CloseConnection( pInfo->m_hConn, 0, nullptr, false );
Printf( "Failed to set poll group?" );
break;
}
// Generate a random nick. A random temporary nick
// is really dumb and not how you would write a real chat server.
// You would want them to have some sort of signon message,