6.8. Basic Features - Leaving Sessions

This section describes the process of leaving a session.

Leaving the Session

Leaving a session is an asynchronous process. Calling the LeaveSessionAsync() function starts an asynchronous process. The end of that process can be detected with the IsCompletedLeaveSession() function. GetLeaveSessionResult() returns a result of true for IsSuccess() when the search process is successful.

Both hosts and clients call the same API function.

When a host that is not using host migration leaves a session, it sends a notification to all of the clients participating in that session that the session has been destroyed. The clients that receive that notification then transition to a disconnected state.

Code 6-19. Leaving a Session
// Leaving a session. (Starting the asynchronous process.)
result = nn::pia::session::Session::GetInstance()->LeaveSessionAsync();
if (result.IsFailure())
{
    // Error processing.
}
 
// If the asynchronous process has started successfully, you must call the dispatch function periodically and wait for the asynchronous process to proceed.
while (nn::pia::session::Session::GetInstance()->IsCompletedLeaveSession() == false)
{
    if (networkType == INTERNET)
    {
        // If you are using Internet communication, you must call the NEX Scheduler::Dispatch function for keep-alive communications with the server.
        nn::nex::Scheduler::GetInstance()->DispatchAll();
    }
    nn::pia::common::Scheduler::GetInstance()->Dispatch();
}
 
// Check the result of the asynchronous process.
result = nn::pia::session::Session::GetInstance()->GetLeaveSessionResult();
if ( result.IsFailure() )
{
    // Error processing.
    // Will not fail if the asynchronous process for leaving the session started successfully.
}