Version:

Starting and Stopping the Session Service

Starting and Stopping the Session Service

The session service is responsible for hosting or joining sessions and is represented by the GridMate::SessionService abstract class.

When a session service is created, a descriptor class derived from GridMate::SessionServiceDesc is passed in as a constructor argument.

The implementations of GridMate::SessionService that are included with the base Lumberyard engine are as follows.


ImplementationDescriptorDescription
GridMate::LANSessionServiceGridMate::SessionServiceDescSessions hosted over a local area network.

Starting a Session Service

Only one session service can be present per GridMate::IGridMate instance.

Note
Attempting to register multiple session services causes an assert and overrides any previously registered session services.

You have two ways to start a session service:

  • Create a session service object and register it with GridMate.
  • Register an existing session service object with GridMate.

Starting MethodDetails
GridMate::StartGridMateService()Creates a session service object and registers it with GridMate::IGridMate.
GridMate::RegisterService()Registers an existing session service object with GridMate::IGridMate.

Stopping a Session Service

The method for stopping the session service depends on how the session service was started.


Starting MethodStopping MethodDetails
GridMate::StartGridMateService()See details.The session service is stopped when GridMate::IGridMate is destroyed by using the GridMate::GridMateDestroy() method.
GridMate::RegisterService()GridMate::UnregisterService()The session is service is stopped and memory freed when GridMate::UnregisterService() is called.

Examples

The following examples assume that GridMate has been initialized.

Starting and Stopping with GridMate::StartGridMateService

The following example uses GridMate::StartGridMateService.

void MyClass::StartSessionService()
{
    IGridMate* gridMate = gEnv->pNetwork->GetGridMate();

    if(gridMate)
    {
        // The session service is started and will be stopped when IGridMate is destroyed.
        GridMate::SessionServiceDesc desc;
        GridMate::StartGridMateService<GridMate::LANSessionService>(gridMate, desc);
    }
}

Starting and Stopping with GridMate::RegisterService() and GridMate::UnregisterService()

The following example uses GridMate::RegisterService() and GridMate::UnregisterService().

void MyClass::StartSessionService()
{
    IGridMate* gridMate = gEnv->pNetwork->GetGridMate()
    GridMate::SessionService* sessionService = nullptr;

    if(gridMate)
    {
        GridMate::SessionServiceDesc desc;
        sessionService = aznew GridMate::LANSessionService(desc);
        gridMate->RegisterService(sessionService);
    }

    return sessionService;
}

void MyClass::StopSessionService(GridMate::SessionService* sessionService)
{
    IGridMate* gridMate = gEnv->pNetwork->GetGridMate()

    if(gridMate)
    {
        // Unregister the session service and free the session service pointer.
        gridMate->UnregisterService(sessionService);
    }
}