This page describes the changes to your code required to migrate from the mesh API to the session API.
The session API simplifies session management in comparison with managing matchmaking sessions and mesh networks separately. The session API also allows you to unify your error handling code. Example code from the mesh API and session API is provided for each feature so that you can compare them.
As with Mesh::Setting, use Session::Setting for initialization. Session::Setting has all of the same members as Mesh::Setting. It also has an additional member for the size to use for the session search result list. The session search result list size is 0 by default. If only implementing random matchmaking, leave this value set to 0. However, if you want to let users browse and select matchmaking sessions, use a value of at least 1 for the session search result list size.
nn::pia::session::Mesh::Setting setting;
nn::pia::inet::NexNetworkFactory nexFactory; setting.pNetworkFactory = &nexFactory; setting.networkTopology = nn::pia::session::NetworkTopology_FullMesh; nn::pia::session::Mesh::CreateInstance(setting); |
nn::pia::session::Session::Setting setting;
nn::pia::inet::NexNetworkFactory nexFactory; setting.pNetworkFactory = &nexFactory; setting.networkTopology = nn::pia::session::NetworkTopology_FullMesh; // Size of the list of session search results. (Specify a value of at least 1 if you want users to be able to browse through and select matchmaking sessions.) setting.browsedSessionInfoListNum = 8; nn::pia::session::Session::CreateInstance(setting); |
The session API handles startup of the Facade classes such as inet::NexFacade in the network management modules automatically. You do not need to start them manually.
The session API also configures session signatures automatically. You do not need to configure them from the application.
nn::pia::session::Mesh::StartupSetting startupSetting;
startupSetting.maxSilenceTime = nn::pia::session::NN_PIA_SESSION_MAX_SILENCE_TIME_DEFAULT; startupSetting.keepAliveSendingInterval = nn::pia::session::NN_PIA_SESSION_KEEP_ALIVE_INTERVAL_DEFAULT; // Get the player name representing the local station. nn::pia::transport::Station::PlayerName playerName; Session_GetLocalPlayerName(&playerName); startupSetting.pPlayerName = &playerName; // Encryption setting (No encryption). startupSetting.pCryptoSetting = NULL; // Signature setting. char signatureKeyData[] = "sample signature key"; nn::pia::common::SignatureSetting signatureSetting( nn::pia::common::SignatureSetting::MODE_HMAC_MD5, static_cast<const void*>(signatureKeyData), sizeof(signatureKeyData) ); startupSetting.signatureSetting = signatureSetting; // Station identification setting (no setting) startupSetting.pToken = NULL; nn::pia::session::Mesh::GetInstance()->Startup(startupSetting); |
nn::pia::session::Session::StartupSetting startupSetting;
startupSetting.maxSilenceTime = nn::pia::session::NN_PIA_SESSION_MAX_SILENCE_TIME_DEFAULT; startupSetting.keepAliveSendingInterval = nn::pia::session::NN_PIA_SESSION_KEEP_ALIVE_INTERVAL_DEFAULT; // Get the player name representing the local station. nn::pia::transport::Station::PlayerName playerName; Session_GetLocalPlayerName(&playerName); startupSetting.pPlayerName = &playerName; // Encryption setting (no encryption). startupSetting.cryptoMode = nn::pia::common::CryptoSetting::MODE_NOTHING; // Identification information setting (no setting). startupSetting.pToken = NULL; // Signature setting is configured automatically. nn::pia::session::Session::GetInstance()->Startup(startupSetting); |
In the mesh API, you have to create, join, and search for matchmaking sessions manually in the application. In the session API, the following NEX matchmaking API functions are called automatically. You do not need to call them from the application. For more information about when to call the NEX API functions, see 6.22. Information - Calls to the Server API.
In the mesh API, you must call the pia::NexFacade::StartNatSession() and pia::NexFacade::StopNatSession() functions to start or stop a NAT session. In the session API, these NAT session management functions are called automatically when calls to the session creation/join functions are made. You do not need to call them from the application.
When creating a session with the session API, you must pass an instance of a class that inherits from session::CreateSessionSetting and matches the type of network you are using.
For Internet communications, use an instance of the inet::NexCreateSessionSetting class.
For more information about how to configure each of the classes derived from session::CreateSessionSetting, see the API Reference.
nn::nex::MatchmakeSession* pMatchmakeSession = qNew nn::nex::MatchmakeSession();
pMatchmakeSession->SetDescription(sessionDescription); pMatchmakeSession->SetMinParticipants(1); pMatchmakeSession->SetMaxParticipants(sessionMaxParticipants); pMatchmakeSession->SetGameMode(sessionGameMode); pMatchmakeSession->SetAttribute(SESSION_ATTR_INDEX, SESSION_ATTR_VAL); pMatchmakeSession->SetMatchmakeSystemType(nn::nex::MATCHMAKE_SYSTEM_TYPE_ANYBODY); pMatchmakeSession->SetApplicationBuffer(applicationBuffer); pMatchmakeSession->SetOpenParticipation(false); // Set to GatheringHolder. (The MatchmakeSession instance will be destroyed by the destructor of gatheringHolder.) nn::nex::GatheringHolder gatheringHolder(pMatchmakeSession); // Where to copy the session key shared in the matchmaking session. nn::nex::qVector<nn::nex::qByte>* pMatchmakeSessionKey = GetMatchmakeSessionKeyPtr(); pMatchmakeSessionKey->clear(); // Start creating the matchmaking session. nn::nex::ProtocolCallContext createContext; if (pMatchMakingClient->CreateMatchmakeSession(&createContext, gatheringHolder, pJoinedGatheringID, pMatchmakeSessionKey)) { // Wait for the creation process to end. createContext.Wait(); if (! createContext.GetOutcome()) { // Error processing when the creation process fails. } } else { // Error processing when the creation process fails to start. } // Start the process of creating the mesh. result = nn::pia::session::Mesh::GetInstance()->CreateMeshAsync(); if (result.IsFailure()) { // Error processing when the creation process fails to start. } // Wait for the process of creating the mesh to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); // Check the network connection status. bool IsConnected; result = nn::ac::IsApplicationConnected(&IsConnected); if (! IsConnected) { // Error processing when disconnection from the network is detected. } if (! pNgsFacade->IsConnected()) { // Error processing when disconnection from the game server is detected. } if (nn::pia::session::Mesh::GetInstance()->IsCompletedCreateMesh()) { result = nn::pia::session::Mesh::GetInstance()->GetCreateMeshResult(); if (result.IsFailure()) { // Error processing when the creation process fails. } else { // Success. } break; } } |
nn::pia::inet::NexCreateSessionSetting createSessionSetting;
createSessionSetting.SetDescription(sessionDescription); createSessionSetting.SetMinParticipants(1); createSessionSetting.SetMaxParticipants(sessionMaxParticipants); createSessionSetting.SetGameMode(sessionGameMode); createSessionSetting.SetAttribute(SESSION_ATTR_INDEX, SESSION_ATTR_VAL); createSessionSetting.SetSessionType(nn::pia::inet::SESSION_TYPE_ANYBODY); createSessionSetting.SetApplicationData(s_applicationDataBuffer, sizeof(s_applicationDataBuffer)); createSessionSetting.SetOpenParticipation(false); // Start the process of creating a session. result = nn::pia::session::Session::GetInstance()->CreateSessionAsync(createSessionSetting); if (result.IsFailure()) { // Error processing when the creation process fails to start. } // Wait for the process of creating the session to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); if (nn::pia::session::Session::GetInstance()->IsCompletedCreateSession()) { result = nn::pia::session::Session::GetInstance()->GetCreateSessionResult(); if (result.IsFailure()) { // Error processing when the creation process fails. } else { // Success. } break; } } |
When using the session API, you must pass an instance of a class that inherits from session::SessionSearchCriteria and matches the type of network you are using.
For Internet communications, use an instance of the inet::NexSessionSearchCriteria class.
For more information about each of the classes derived from session::SessionSearchCriteria, see the API Reference.
nn::nex::MatchmakeSessionSearchCriteria criteria;
criteria.SetMinParticipants(1); criteria.SetMaxParticipants(sessionMaxParticipants); criteria.SetGameMode(sessionGameMode); criteria.SetAttributeWithRange(SESSION_ATTR_INDEX, 0, SESSION_ATTR_MAX); criteria.SetMatchmakeSystemType(nn::nex::MATCHMAKE_SYSTEM_TYPE_ANYBODY); criteria.SetExcludeLocked(true); criteria.SetVacantOnly(true); nn::nex::ResultRange range; range.SetSize(rangeSize); range.SetOffset(0); // Empty the save list. plistGatheringHolder->clear(); // Start the process of searching for matchmaking sessions. nn::nex::ProtocolCallContext searchContext; if (! pMatchMakingClient->BrowseMatchmakeSession(&searchContext, criteria, range, plistGatheringHolder)) { // The search process failed to start. } else { // Wait for searching to end. searchContext.Wait(); if (! searchContext.GetOutcome()) { // Error processing when the search process fails. } else { // List the sessions. for (nn::nex::qList<nn::nex::GatheringHolder>::iterator it = plistGatheringHolder->begin(); it != plistGatheringHolder->end(); ++it) { nn::nex::MatchmakeSession* pMatchmakeSession = reinterpret_cast<nn::nex::MatchmakeSession*>(it->Get()); PIA_CACHED_PRINTF( "GameMode=%d Id=%08X [%02d/%02d] [%s]", pMatchmakeSession->GetGameMode(), pMatchmakeSession->GetID(), pMatchmakeSession->GetParticipantCount(), pMatchmakeSession->GetMaxParticipants(), pMatchmakeSession->GetOpenParticipation() ? "Opened" : "Closed" ); } } } |
nn::pia::inet::NexSessionSearchCriteria criteria;
criteria.SetMinParticipants(1); criteria.SetMaxParticipants(sessionMaxParticipants); criteria.SetGameMode(sessionGameMode); criteria.SetAttributeRange(SESSION_ATTR_INDEX, 0, SESSION_ATTR_MAX); criteria.SetSessionType(nn::pia::inet::SESSION_TYPE_ANYBODY); criteria.SetOpenedOnly(true); criteria.SetVacantOnly(true); // Start the process of searching for sessions. result = nn::pia::session::Session::GetInstance()->BrowseSessionAsync(&criteria); if (result.IsFailure()) { // Error processing when searching fails to start. } // Wait for searching to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); if (nn::pia::session::Session::GetInstance()->IsCompletedBrowseSession()) { result = nn::pia::session::Session::GetInstance()->GetBrowseSessionResult(); if (result.IsFailure()) { // Error processing when searching fails. } else { // Session startup. nn::pia::session::ISessionInfoList* pSessionInfoList = nn::pia::session::Session::GetInstance()->GetBrowsedSessionInfoList(); for (nn::pia::session::ISessionInfoList::Iterator it = pSessionInfoList->Begin(); it != pSessionInfoList->End(); ++it) { PIA_CACHED_PRINTF( "GameMode=%d Id=%08X [%02d/%02d] [%s]", (*it)->GetGameMode(), (*it)->GetSessionId(), (*it)->GetCurrentParticipants(), (*it)->GetMaxParticipants(), (*it)->IsOpened() ? "Opened" : "Closed" ); } } break; } } |
When using the session API, you must pass an instance of a class that inherits from session::JoinSessionSetting and matches the type of network you are using.
For Internet communications, use an instance of the inet::NexJoinSessionSetting class.
For more information about how to configure each of the classes derived from session::JoinSessionSetting, see the API Reference.
// Get the GatheringId of the matchmaking session to join from the list of matchmaking session search results.
gatheringId = it->Get()->GetID(); // Where to copy the session key shared in the matchmaking session. nn::nex::qVector<nn::nex::qByte>* pMatchmakeSessionKey = GetMatchmakeSessionKeyPtr(); pMatchmakeSessionKey->clear(); // Start creating the matchmaking session. if (! pMatchMakingClient->JoinMatchmakeSession(&joinContext, gatheringId, pMatchmakeSessionKey)) { // Error processing when the join-in process fails to start. } else { // Wait for the join-in process to end. joinContext.Wait(); if (! joinContext.GetOutcome()) { // Error processing when the join-in process fails. } } //Get the session host's StationURL and convert it to StationConnectionInfo. nn::nex::qList<nn::nex::StationURL> lstURLs; nn::nex::ProtocolCallContext oContext; if (! pMatchMakingClient->GetSessionURLs(&oContext, gatheringId, &lstURLs)) { // Error processing when the process of getting the StationURL fails to start. } else { // Wait for the process of getting the StationURL to end. oContext.Wait(); if (! oContext.GetOutcome()) { // Error processing when the process of getting the StationURL fails. } } // Convert StationURL to StationConnectionInfo. nn::pia::transport::StationConnectionInfo stationConnectionInfo; if (nn::pia::inet::NexFacade::ConvertNexStationUrlToStationConnectionInfo(lstURLs, &stationConnectionInfo).IsFailure()) { // Error processing when conversion fails. } // Start the process of joining the mesh. result = nn::pia::session::Mesh::GetInstance()->JoinMeshAsync(stationConnectionInfo); if (result.IsFailure()) { // Error processing when the process of joining the mesh fails to start. } // Wait for the process of joining the mesh to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); // Check the network connection status. bool IsConnected; result = nn::ac::IsApplicationConnected(&IsConnected); if (! IsConnected) { // Error processing when disconnection from the network is detected. } if (! pNgsFacade->IsConnected()) { // Error processing when disconnection from the game server is detected. } if (nn::pia::session::Mesh::GetInstance()->IsCompletedJoinMesh()) { result = nn::pia::session::Mesh::GetInstance()->GetJoinMeshResult(); if (result.IsFailure()) { // Error processing when the join-in process fails. } else { // Success. } break; } } |
// Get information about the session to join from the list of session search results.
nn::pia::session::ISessionInfo* pTargetSessionInfo = *it; // Add the session information to the session join-in settings. nn::pia::inet::NexJoinSessionSetting joinSessionSetting; sessionJoinSessionSetting.SetSessionInfoPtr(pTargetSessionInfo); // Start the process of joining the session. result = nn::pia::session::Session::GetInstance()->JoinSessionAsync(&joinSessionSetting); if (result.IsFailure()) { // Error processing when the process of joining the session fails to start..} // Wait for the process of joining the session to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); if (nn::pia::session::Session::GetInstance()->IsCompletedJoinSession()) { result = nn::pia::session::Session::GetInstance()->GetJoinSessionResult(); if (result.IsFailure()) { // Error processing when the process of joining fails. } else { // Success. } break; } } |
The session API provides a random matchmaking feature (auto-matchmaking) for when you are using Internet communications.
The session API calls the AutoMatchmake() function from the NEX matchmaking API internally. You must pass an instance of a class that inherits from session::CreateSessionSetting and an instance of a class that inherits from session::SessionSearchCriteria when calling the random matchmaking function (both of the derived classes you specify must match the type of network you are using).
// Creating a session when no session matches the criteria.
nn::nex::MatchmakeSession* const pMatchmakeSession = qNew nn::nex::MatchmakeSession(); pMatchmakeSession->SetGameMode(SESSION_GAME_MODE); pMatchmakeSession->SetMaxParticipants(SAMPLE_MAX_ENTRY); pMatchmakeSession->SetAttribute(SESSION_ATTR_INDEX, SESSION_ATTR_VAL); pMatchmakeSession->SetOpenParticipation(false); // Search criteria. nn::nex::qList<nn::nex::MatchmakeSessionSearchCriteria> criteriaList; nn::nex::MatchmakeSessionSearchCriteria criteria; criteria.SetGameMode(MATCHMAKE_GAME_MODE); criteria.SetMaxParticipants(SAMPLE_MAX_ENTRY); criteria.SetAttribute(MATCHMAKE_ATTR_INDEX, 0); criteriaList.push_back(criteria); // Use range specification for the second criteria. nn::nex::MatchmakeSessionSearchCriteria criteria; criteria.SetGameMode(MATCHMAKE_GAME_MODE); criteria.SetMaxParticipants(SAMPLE_MAX_ENTRY); criteria.SetAttributeWithRange(MATCHMAKE_ATTR_INDEX, 0, MATCHMAKE_ATTR_MAX); criteriaList.push_back(criteria); nn::nex::GatheringHolder oInGatheringHolder = pMatchmakeSession; nn::nex::GatheringHolder oOutGatheringHolder; nn::nex::ProtocolCallContext oContext; // Start the process of random matchmaking. bool bSuccess = pMatchMakingClient->AutoMatchmake( &oContext, criteriaList, oInGatheringHolder, &oOutGatheringHolder, _T("AutoMatchmake message") ); if (bSuccess == false) { // Error processing when the process of random matchmaking fails to start. } // Wait for the process of random matchmaking to end. oContext.Wait(); if (! oContext.GetOutcome()) { // Error processing when the process of random matchmaking fails. } nn::nex::GatheringID joinedGatheringId = oOutGatheringHolder->GetID(); nn::nex::PrincipalID ownerPrincipalID = oOutGatheringHolder->GetOwnerPID(); // Check whether the local station is the session host. if (ownerPrincipalID == pMatchMakingClient->GetCredentials()->GetPrincipalID()) { // Start the process of creating a mesh. result = nn::pia::session::Mesh::GetInstance()->CreateMeshAsync(); if (result.IsFailure()) { // Error processing when the process of creating a mesh fails to start. } // Wait for the process of creating a mesh to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); // Check the network connection status. bool IsConnected; result = nn::ac::IsApplicationConnected(&IsConnected); if (! IsConnected) { // Error processing when disconnection from the network is detected. } if (! pNgsFacade->IsConnected()) { // Error processing when disconnection from the game server is detected. } if (nn::pia::session::Mesh::GetInstance()->IsCompletedCreateMesh()) { result = nn::pia::session::Mesh::GetInstance()->GetCreateMeshResult(); if (result.IsFailure()) { // Error processing when the creation process fails. } else { // Success. } break; } } } else { // Joining the mesh. // Get the session host's StationURL and convert it to StationConnectionInfo. nn::nex::qList<nn::nex::StationURL> lstURLs; nn::nex::ProtocolCallContext oContext; if (! pMatchMakingClient->GetSessionURLs(&oContext, gatheringId, &lstURLs)) { // Error processing when the process of getting the StationURL fails to start. } else { // Wait for the process of getting the StationURL to end. oContext.Wait(); if (! oContext.GetOutcome()) { // Error processing when getting the StationURL fails. } } // Convert StationURL to StationConnectionInfo. nn::pia::transport::StationConnectionInfo stationConnectionInfo; if (nn::pia::inet::NexFacade::ConvertNexStationUrlToStationConnectionInfo(lstURLs, &stationConnectionInfo).IsFailure()) { // Error processing when conversion fails. } // Start the process of joining the mesh. result = nn::pia::session::Mesh::GetInstance()->JoinMeshAsync(stationConnectionInfo); if (result.IsFailure()) { // Error processing when the process of joining fails to start. } // Wait for the process of joining the mesh to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); // Check the network connection status. bool IsConnected; result = nn::ac::IsApplicationConnected(&IsConnected); if (! IsConnected) { // Error processing when disconnection from the network is detected. } if (! pNgsFacade->IsConnected()) { // Error processing when disconnection from the game server is detected. } if (nn::pia::session::Mesh::GetInstance()->IsCompletedJoinMesh()) { result = nn::pia::session::Mesh::GetInstance()->GetJoinMeshResult(); if (result.IsFailure()) { // Error processing when the join-in process fails. } else { // Success. } break; } } } |
// Creating a session when no session matches the criteria.
nn::pia::inet::NexCreateSessionSetting createSessionSetting; createSessionSetting.SetGameMode(SESSION_GAME_MODE); createSessionSetting.SetMaxParticipants(SAMPLE_MAX_ENTRY); createSessionSetting.SetAttribute(SESSION_ATTR_INDEX, SESSION_ATTR_VAL); createSessionSetting.SetOpenParticipation(true); // Search criteria. const size_t SEARCH_SETTING_NUM = 2; nn::pia::inet::NexSessionSearchCriteria searchSessionSettingList[SEARCH_SETTING_NUM]; searchSessionSettingList[0].SetGameMode(SESSION_GAME_MODE); searchSessionSettingList[0].SetMaxParticipants(SAMPLE_MAX_ENTRY); searchSessionSettingList[0].SetAttribute(SESSION_ATTR_INDEX, SESSION_ATTR_VAL); // Use range specification for the second criteria. searchSessionSettingList[1].SetGameMode(MATCHMAKE_GAME_MODE); searchSessionSettingList[1].SetMaxParticipantsRange(SAMPLE_MIX_ENTRY, SAMPLE_MAX_ENTRY); searchSessionSettingList[1].SetAttributeRange(SESSION_ATTR_INDEX, 0, SESSION_ATTR_MAX); // Start the process of random matchmaking. nn::Result result = nn::pia::session::Session::GetInstance()->JoinRandomSessionAsync(&createSessionSetting, searchSessionSettingList, SEARCH_SETTING_NUM); if (result.IsFailure()) { // Error processing when the process of random matchmaking fails to start. } // Wait for the process of random matchmaking to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); if (nn::pia::session::Session::GetInstance()->IsCompletedJoinRandomSession()) { result = nn::pia::session::Session::GetInstance()->GetJoinRandomSessionResult(); if (result.IsFailure()) { // Error processing when the process of random matchmaking fails. } else { // Success. } break ; } } |
When using the mesh API, the application needs to determine whether a station can leave the session and whether to destroy the session based on whether that station is the host and whether the host migration feature is enabled. In the session API, the session is automatically destroyed if necessary after the stations leave the session. The application does not need to determine anything. The session API automatically calls the EndParticipation() and UnregisterGathering() functions from the NEX matchmaking API.
// Start the process of leaving a matchmaking session.
nn::nex::ProtocolCallContext oContext; bool bSuccess = pMatchMakingClient->EndParticipation(&oContext, gatheringID); if (bSuccess == false) { // The process of leaving a matchmaking session fails to start. } // Wait for process of leaving a matchmaking session to end. oContext.Wait(); if (! oContext.GetOutcome()) { // Error processing when the process of leaving the matchmaking session fails. } // Start the process of leaving the mesh. nn::Result result = nn::pia::session::Mesh::GetInstance()->LeaveMeshAsync(); if (result.IsFailure()) { // Error processing when the process of leaving the mesh fails. } // Wait for the process of leaving the mesh to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); // Check the network connection status. bool IsConnected; result = nn::ac::IsApplicationConnected(&IsConnected); if (! IsConnected) { // Error processing when disconnection from the network is detected. } if (! pNgsFacade->IsConnected()) { // Error processing when disconnection from the game server is detected. } if (nn::pia::session::Mesh::GetInstance()->IsCompletedLeaveMesh()) { result = nn::pia::session::Mesh::GetInstance()->GetLeaveMeshResult(); if (result.IsFailure()) { // Error processing when the process of leaving fails. } else { // Success. } break; } } |
// Start the process of destroying a matchmaking session.
nn::nex::ProtocolCallContext oContext; bool bSuccess = pMatchMakingClient->UnregisterGathering(&oContext, gatheringID) if (bSuccess == false) { // The process of destroying the matchmaking session fails to start. } // Wait for the process of destroying the matchmaking session to end. oContext.Wait(); if (! oContext.GetOutcome()) { // Error processing when the process of destroying the matchmaking session fails. } // Start the process of destroying the mesh. result = nn::pia::session::Mesh::GetInstance()->DestroyMeshAsync(); if (result.IsFailure()) { // Error processing when the process of destroying the mesh fails. } // Wait for the process of destroying the mesh to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); // Check the network connection status. bool IsConnected; result = nn::ac::IsApplicationConnected(&IsConnected); if (! IsConnected) { // Error processing when disconnection from the network is detected. } if (! pNgsFacade->IsConnected()) { // Error processing when disconnection from the game server is detected } if (nn::pia::session::Mesh::GetInstance()->IsCompletedDestroyMesh()) { result = nn::pia::session::Mesh::GetInstance()->GetDestroyMeshResult(); if (result.IsFailure()) { // Error processing when the destruction process fails. } else { // Success. } break; } } |
// Start the process of leaving the session.
nn::Result result = nn::pia::session::Session::GetInstance()->LeaveSessionAsync(); if (result.IsFailure()) { // Error processing when the process of leaving the session fails to start. } // Wait for the process of leaving the session to end. while (true) { nn::nex::Scheduler::GetInstance()->DispatchAll(); nn::pia::common::Scheduler::GetInstance()->Dispatch(); if (nn::pia::session::Session::GetInstance()->IsCompletedLeaveSession()) { result = nn::pia::session::Session::GetInstance()->GetLeaveSessionResult(); if (result.IsFailure()) { // Error processing when the process of leaving fails. } else { // Success. } break; } } |
Host migration settings must be configured on startup regardless of whether you are using the mesh API or the session API. In the mesh API, the NEX host migration settings must be configured manually. The session API configures host migration settings automatically.
// Specify whether to enable host migration and start the mesh.
nn::pia::session::Mesh::StartupSetting startupSetting; startupSetting.bUsingHostMigration = true; result = nn::pia::session::Mesh::GetInstance()->Startup(startupSetting); if (result.IsFailure()) { // Error processing. } // Configure the settings for the matchmaking session to pass to AutoMatchmake() or CreateMatchmakeSession() for creation so the session owner can change. nn::nex::MatchmakeSession* const pMatchmakeSession = qNew nn::nex::MatchmakeSession(); pMatchmakeSession->SetFlag(nn::nex::GatheringFlags::MigrateOwner); |
// Specify whether to enable host migration and start the session.
nn::pia::session::Session::StartupSetting startupSetting; startupSetting.bUsingHostMigration = true; nn::Result result = nn::pia::session::Session::GetInstance()->Startup(startupSetting); if (result.IsFailure()) { // Error processing. } |
The session API provides a dedicated function for checking the network connection status. This function returns an nn::Result object.
If the stations have been disconnected from the mesh, the function returns session::ResultMeshConnectionIsLost. If this happens, make the other stations leave the session and then clean it up.
If the stations get disconnected from the game server or from the network (when an error occurs in the AC or socket libraries), the function returns ResultNetworkConnectionIsLost. If this happens, wait for any asynchronous processes to finish and then attempt to reconnect to the Internet.
In the session API, asynchronous processes are used to check the network connection status and handle errors. You must wait for these asynchronous processes to finish even if you attempt network disconnection while they are still running.
nn::Result result = nn::pia::transport::Transport::GetInstance()->CheckConnectionError();
if (result.IsFailure()) { if (result == nn::pia::ResultNetworkConnectionIsLost()) { // Disconnected from the network, so perform the finalization process and then reconnect to the Internet. } else { // Unexpected state. } } if (! pNgsFacade->IsConnected()) { // Disconnected from the game server, so perform the finalization process. } if (nn::pia::session::Mesh::CheckConnectionError() == nn::pia::ResultMeshConnectionIsLost()) { // Disconnected, so perform the cleanup process. } |
nn::Result result = nn::pia::session::Session::GetInstance()->CheckConnectionError();
if (result.IsFailure()) { if (result == nn::pia::ResultNetworkConnectionIsLost()) { // Disconnected form the network, so perform the finalization process and then reconnect to the Internet. } else if (result == nn::pia::session::ResultMeshConnectionIsLost()) { // Disconnected from the mesh, so leave the session and then perform the cleanup process. } } |