MCPcopy Create free account
hub / github.com/ValveSoftware/GameNetworkingSockets / Run

Method Run

examples/example_chat.cpp:234–283  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

232{
233public:
234 void Run( uint16 nPort )
235 {
236 // Select instance to use. For now we'll always use the default.
237 // But we could use SteamGameServerNetworkingSockets() on Steam.
238 m_pInterface = SteamNetworkingSockets();
239
240 // Start listening
241 SteamNetworkingIPAddr serverLocalAddr;
242 serverLocalAddr.Clear();
243 serverLocalAddr.m_port = nPort;
244 SteamNetworkingConfigValue_t opt;
245 opt.SetPtr( k_ESteamNetworkingConfig_Callback_ConnectionStatusChanged, (void*)SteamNetConnectionStatusChangedCallback );
246 m_hListenSock = m_pInterface->CreateListenSocketIP( serverLocalAddr, 1, &opt );
247 if ( m_hListenSock == k_HSteamListenSocket_Invalid )
248 FatalError( "Failed to listen on port %d", nPort );
249 m_hPollGroup = m_pInterface->CreatePollGroup();
250 if ( m_hPollGroup == k_HSteamNetPollGroup_Invalid )
251 FatalError( "Failed to listen on port %d", nPort );
252 Printf( "Server listening on port %d\n", nPort );
253
254 while ( !g_bQuit )
255 {
256 PollIncomingMessages();
257 PollConnectionStateChanges();
258 PollLocalUserInput();
259 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
260 }
261
262 // Close all the connections
263 Printf( "Closing connections...\n" );
264 for ( auto it: m_mapClients )
265 {
266 // Send them one more goodbye message. Note that we also have the
267 // connection close reason as a place to send final data. However,
268 // that's usually best left for more diagnostic/debug text not actual
269 // protocol strings.
270 SendStringToClient( it.first, "Server is shutting down. Goodbye." );
271
272 // Close the connection. We use "linger mode" to ask SteamNetworkingSockets
273 // to flush this out and close gracefully.
274 m_pInterface->CloseConnection( it.first, 0, "Server Shutdown", true );
275 }
276 m_mapClients.clear();
277
278 m_pInterface->CloseListenSocket( m_hListenSock );
279 m_hListenSock = k_HSteamListenSocket_Invalid;
280
281 m_pInterface->DestroyPollGroup( m_hPollGroup );
282 m_hPollGroup = k_HSteamNetPollGroup_Invalid;
283 }
284private:
285
286 HSteamListenSocket m_hListenSock;

Callers 1

mainFunction · 0.45

Calls 11

SteamNetworkingSocketsFunction · 0.85
SetPtrMethod · 0.80
CreateListenSocketIPMethod · 0.80
CreatePollGroupMethod · 0.80
clearMethod · 0.80
CloseListenSocketMethod · 0.80
DestroyPollGroupMethod · 0.80
FatalErrorFunction · 0.70
PrintfFunction · 0.70
ClearMethod · 0.45
CloseConnectionMethod · 0.45

Tested by

no test coverage detected