Asynchronous operations and notifications
For operations that may be slow or computationally expensive, the PlayFab Lobby and Matchmaking SDK exposes asynchronous APIs. Asynchronous APIs give you the ability to start expensive or slow operations from your main threads and poll for the completion of those operations on a thread of your choice. This same polling mechanism is also used to deliver asynchronous notifications of SDK updates to your title code. This page gives an overview of the PlayFab Lobby and Matchmaking SDK’s asynchronous API patterns and best practices for programming against them.Basic API patterns
There are two types of asynchronous API patterns to be aware of in the PlayFab Lobby and Matchmaking SDK:Asynchronous operations
It’s simple to use the SDK’s asynchronous APIs. The general pattern for starting and completing asynchronous operations is as follows:- Make a regular method call to the appropriate asynchronous API of your choosing. Common asynchronous operations you’ll likely make use of include:
- Check the HRESULT return value of the API with the SUCCEEDED() or FAILED() macros. This synchronously returned value will tell you whether the operation has successfully started.
- Poll for the asynchronous operation’s completion by looking for the associated operation’s “completion state change” to be provided by PFMultiplayerStartProcessingLobbyStateChanges() or PFMultiplayerStartProcessingMatchmakingStateChanges. An example of the associated “completion state change” for PFMultiplayerCreateAndJoinLobby() is PFLobbyCreateAndJoinLobbyCompletedStateChange. More detailed information on what “state changes” are and how they work can be found in the State Changes section.
- Check the completion state change’s result value to determine whether the operation succeeded or failed. More detailed information on these error values can be found in the SDK’s error handling documentation.
Asynchronous notifications
Some features will generate asynchronous notifications of changes to the Lobby and Matchmaking SDK. Common notifications include:- Lobby update notifications.
- Lobby disconnect notifications.
- Matchmaking ticket status change notifications.
State changes
The Lobby and Matchmaking SDK’s asynchronous API model is built around the PFLobbyStateChange and PFMatchmakingStateChange structs. PFLobbyStateChanges notify you about changes to the lobby subsystem and PFMatchmakingStateChanges notify you about changes to the matchmaking subsystem. These “state changes” are asynchronous notifications of events from the SDK. These notifications are queued internally and you process them by calling PFMultiplayerStartProcessingLobbyStateChanges() and PFMultiplayerStartProcessingMatchmakingStateChanges. These functions will return all queued state changes (for their respective API subsystem) as lists that you can iterate through and process individually. Each state change has a corresponding stateChangeType field that can be inspected to determine which specific state change you are being notified about. Once you know which state change you have been given, you can cast the generic PFLobbyStateChange or PFMatchmakingStateChange struct to a more specific type of state change struct to inspect that event’s specific data. Typically, state change processing is implemented as a simple switch statement which delegates each state change to a handler. Once the list of state changes has been processed from PFMultiplayerStartProcessingLobbyStateChanges or PFMultiplayerStartProcessingMatchmakingStateChanges, it must be returned to PFMultiplayerFinishProcessingMatchmakingStateChanges() or PFMultiplayerFinishProcessingMatchmakingStateChanges(), respectively.Asynchronous operation contexts
Each asynchronous API includes avoid* asyncContext parameter. This value is a pass-through parameter that will be set
on this API call’s associated completion state change once it’s provided by PFMultiplayerStartProcessingLobbyStateChanges()
or PFMultiplayerStartProcessingMatchmakingStateChanges().
This value gives you a mechanism to attach arbitrary, pointer-sized contexts to your asynchronous API calls. These
contexts can be used in many scenarios including:
- associating title-specific data with an SDK call
- tying together multiple asynchronous operations with a shared identifier
Operation queuing
Frequently, when working with asynchronous APIs, multiple asynchronous operations need to run sequentially as part of a larger asynchronous flow. In the Lobby and Matchmaking SDK, one example would be creating a lobby and sending invites to your friends for that lobby. Serialized, this flow would look like:- Call PFMultiplayerCreateAndJoinLobby() to create and join a PlayFab lobby.
- Wait for the PFLobbyCreateAndJoinLobbyCompletedStateChange to reflect that the lobby was successfully created and joined.
- Call PFLobbySendInvite() for each invited friend.
- Wait for the PFLobbySendInviteCompletedStateChange to reflect that the invite was successfully sent.
