> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Multiplayer Manager 处理协议激活

> 使用 XBOX Multiplayer Manager 处理玩家接受游戏邀请或从玩家卡加入时的协议激活,包括大厅加入处理。

本主题介绍如何使用 Multiplayer Manager 处理协议激活。

*激活* 是指系统响应另一操作而自动启动游戏。这通常在玩家接受来自另一玩家的游戏邀请时发生。

你的游戏可以通过以下方式被协议激活。

* 用户接受游戏邀请
* 用户从玩家的玩家卡中选择"加入游戏"

此场景涵盖当你的游戏启动时,玩家加入大厅并正在进行游戏(如果存在)时如何处理协议激活。

有关协议激活过程的流程图,请参阅[处理协议激活(流程图)](/services/xbox-services/multiplayer/mpm/concepts/flowcharts/live-mpm-on-protocol-activation)。

当玩家接受游戏邀请或通过玩家的玩家卡加入好友的游戏时,系统会通过协议激活在其设备上启动游戏。
游戏启动后,通过 [XGameActivationRegisterForEvent](/reference/system/xgameactivation/functions/xgameactivationregisterforevent) 获取邀请句柄。使用邀请句柄调用 [XblMultiplayerManagerJoinLobby](/reference/live/xsapi-c/multiplayer_manager_c/functions/xblmultiplayermanagerjoinlobby)。

如果未通过 [XblMultiplayerManagerLobbySessionAddLocalUser](/reference/live/xsapi-c/multiplayer_manager_c/functions/xblmultiplayermanagerlobbysessionaddlocaluser) 添加被邀请的用户,则 [XblMultiplayerManagerJoinLobby](/reference/live/xsapi-c/multiplayer_manager_c/functions/xblmultiplayermanagerjoinlobby) 会失败,并通过 `JoinLobbyCompleted` 事件调用 [XblMultiplayerEventArgsXuid](/reference/live/xsapi-c/multiplayer_manager_c/functions/xblmultiplayereventargsxuid) 提供邀请所针对的 xuid。

加入大厅后,我们建议设置本地成员的连接地址以及该成员的任何自定义属性。
如果不存在主机,你还可以通过 [XblMultiplayerManagerLobbySessionSetSynchronizedHost](/reference/live/xsapi-c/multiplayer_manager_c/functions/xblmultiplayermanagerlobbysessionsetsynchronizedhost) 设置主机。

最后,如果游戏已在进行中且有空位容纳被邀请者,Multiplayer Manager 会自动将用户加入游戏会话。
游戏会通过 `JoinGameCompleted` 事件收到通知,并提供相应的错误代码和消息。

#### 扁平 C API

```cpp theme={null}
#include <XTaskQueue.h>
#include <XGameActivation.h>  
  
XTaskQueueHandle g_taskQueue;  
XTaskQueueRegistrationToken g_activationToken;  
  
void CALLBACK OnActivation(void* context, const XGameActivationInfo* activationInfo)  
{  
    if (activationInfo->type == XGameActivationType::AcceptedGameInvite)
    {
        if (activationInfo->inviteUri != nullptr)
        {
            std::string inviteString(activationInfo->inviteUri);
            auto pos = inviteString.find("handle=");
            auto inviteHandeId = inviteString.substr(pos + 7, 36);
            // Now, call XblMultiplayerManagerJoinLobby.
        }
    }
}  
  
void InitializeGame()  
{  
    XGameActivationRegisterForEvent(g_taskQueue, nullptr, OnActivation, &g_activationToken);  
}  
  
void ShutdownGame()  
{  
    XGameActivationUnregisterForEvent(g_activationToken, true);  
}  
```

```cpp theme={null}
HRESULT hr = XblMultiplayerManagerLobbySessionSetLocalMemberConnectionAddress(
    xblUserHandle, connectionAddress, context);
```

有关详细信息,请参阅 [XblMultiplayerManagerLobbySessionSetLocalMemberConnectionAddress](/reference/live/xsapi-c/multiplayer_manager_c/functions/xblmultiplayermanagerlobbysessionsetlocalmemberconnectionaddress)。

Multiplayer Manager 执行以下功能。

* 注册 Real-Time Activity 和多人游戏订阅。
* 加入大厅会话。
* 清理现有的大厅状态。
* 将所有本地玩家加入为活动状态。
* 上传安全设备地址 (SDA)。
* 设置成员属性。
* 注册会话变更事件。
* 将大厅会话设置为活动会话。
* 加入游戏会话(如果存在)。
* 使用传输句柄。


## Related topics

- [Multiplayer Manager 示例代码](/zh-CN/services/xbox-services/multiplayer/mpm/how-to/live-mm-howto-nav.md)
- [发送邀请](/zh-CN/services/xbox-services/multiplayer/invites/how-to/live-invites-send.md)
- [Multiplayer Manager 概述](/zh-CN/services/xbox-services/multiplayer/mpm/live-multiplayer-manager-overview.md)
- [处理协议激活(流程图)](/zh-CN/services/xbox-services/multiplayer/mpm/concepts/flowcharts/live-mpm-on-protocol-activation.md)
- [操作指南](/zh-CN/services/xbox-services/multiplayer/mpm/how-to/index.md)
