void sv_Destroy() { NMUInt32 refCount; NMErr err = kNMNoError; NSpProtocolReference pRef; if(!gNetworkInitialised) return; m_ConsPrint("Network UnInitialization\n"); if( _gameReference ) { err = NSpGame_Dispose( _gameReference, kNSpGameFlag_ForceTerminateGame ); } if( _protocolListRef ) { refCount = NSpProtocolList_GetCount( _protocolListRef ); /* get number of protocols */ while( refCount-- && !err ) { pRef = NSpProtocolList_GetIndexedRef( _protocolListRef, refCount ); /* get currect reference */ err = NSpProtocolList_RemoveIndexed( _protocolListRef, refCount ); /* then remove it from the list */ /* now, we can dispose of the reference safely */ NSpProtocol_Dispose( pRef ); /* this should have an NMErr return, but has a void return... */ } /* once all of the protocols are disposed, we can dispose of the containing reference list */ NSpProtocolList_Dispose( _protocolListRef ); } gNetworkInitialised = false; }
NMErr NetworkShutdown( void ) { NMUInt32 refCount; NMErr err = kNMNoError; /* if we have a game object (we should!) dispose if it now */ if( _gameReference ) err = NSpGame_Dispose( _gameReference, kNSpGameFlag_ForceTerminateGame ); /* dispose of our protocol references & the list containing them */ if( _protocolListRef ) { NSpProtocolReference pRef; refCount = NSpProtocolList_GetCount( _protocolListRef ); /* get number of protocols */ while( refCount-- && !err ) { pRef = NSpProtocolList_GetIndexedRef( _protocolListRef, refCount ); /* get currect reference */ err = NSpProtocolList_RemoveIndexed( _protocolListRef, refCount ); /* then remove it from the list */ /* now, we can dispose of the reference safely */ NSpProtocol_Dispose( pRef ); /* this should have an NMErr return, but has a void return... */ } /* once all of the protocols are disposed, we can dispose of the containing reference list */ NSpProtocolList_Dispose( _protocolListRef ); } /* Make sure we can't use old values */ _protocolListRef = NULL; _gameReference = NULL; return( err ); }
NMErr NetworkStartClient ( char *ipAddr, /* IP address (or domain name) to look for server (host) on */ char *port, /* Port to talk to server via */ const unsigned char *playerName /* name of player wanting to join */ ) { NSpAddressReference addRef; NMErr err; /* Given our input strings, create an OpenPlay address reference for talking to server */ addRef = NSpCreateIPAddressReference( ipAddr, port ); if( addRef ) { unsigned char passwordPStr[32]; doCopyC2PStr("Password",passwordPStr); printf( "\nAttempting to join game..." ); fflush(stdout); /* Now, look for a server on the IP/Port given and see if we can connect */ err = NSpGame_Join( &_gameReference, addRef, playerName, passwordPStr, kPlayerType, NULL, 0, 0 ); if( !err ) { NMUInt32 startTime, currentTime; time_t seconds; printf( "connected!\n\nWaiting for approval to join game (press 'q' to quit)..." ); fflush(stdout); time(&seconds); startTime = seconds; _response = _approved = false; /* We connected, now we have to wait for the server to approve our join request */ while( !_response ) { /* Check for a time out in connecting to server */ /* this is before the event handler so that we are not approved, but time out anyway (small chance, but...) */ time(&seconds); currentTime = seconds; if( (currentTime - startTime > 60) && (!_response) ) { printf( "ERROR: Connection timed out!\n\n" ); fflush(stdout); _response = true; _approved = false; } /* Handle system messages and allow user to quit via 'q' */ /* This also gets and handles network messages, like accept/deny */ GameHandleEvents(); } /* if we were not approved, we must dispose of the game object here */ if( !_approved ) { err = NSpGame_Dispose( _gameReference, kNSpGameFlag_ForceTerminateGame ); _gameReference = NULL; } else /* let the user know that they were accepted to the game */ { NSpGameInfo gameInfo; char str[256]; err = NSpGame_GetInfo( _gameReference, &gameInfo ); if( !err ) { GameP2CStr( gameInfo.name, str ); printf( " Welcome to the game '%s', with %d players\n\n", str, (int)gameInfo.currentPlayers ); fflush(stdout); } } } } else err = kNMParameterErr; return( err ); }