> ## 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.

# 使用 QoS 信标测量玩家到 Azure 的延迟

> 使用 PlayFab QoS 信标测量玩家到 Azure 区域的延迟,并在调用 RequestMultiplayerServer 以进行低延迟匹配时对其进行排名。

# 使用服务质量 (QoS) 信标测量玩家到 Azure 的延迟

您可以将 PlayFab 多人游戏服务器部署到十几个 Azure 区域。这样做有两个原因:

1. 额外的区域提供冗余。如果单个 Azure 区域发生故障,玩家可以访问其他区域中的服务器。
2. 额外的区域允许玩家访问 "附近" 的服务器并提供低延迟连接。

当您调用 [RequestMultiplayerServer](xref:titleid.playfabapi.com.multiplayer.multiplayerserver.requestmultiplayerserver) 时,您指定一个 PlayFab 用于完成请求的 Azure 区域的排名列表。PlayFab 将尝试使用排名 #1 的区域来完成请求,但如果该区域没有备用服务器,或该区域出现其他故障,则会尝试列表中较低位置的次优区域。

只要有可能,您就应该使用玩家延迟数据来告知在请求多人游戏服务器时使用的 Azure 区域的排名。PlayFab 提供服务和工具来帮助完成此任务。

## 服务质量信标

PlayFab 在 PlayFab 多人游戏服务器使用的每个 Azure 区域中运行信标。这些信标将反射 UDP 流量,并可用于使用 UDP 传输测量延迟。

UDP 的使用很重要,因为大多数多人游戏都使用 UDP 传输来进行其最关键性能的游戏流量。互联网服务提供商和互联网生态系统的其他要素可能会为 UDP 与 TCP 与 ICMP 流量提供不同的性能。

这是在玩家设备上下文中使用这些信标的典型流程:

1. 将玩家登录到 PlayFab。这通常通过 [LoginWithCustomID](xref:titleid.playfabapi.com.client.authentication.loginwithcustomid) 或其他登录 API 完成。
2. 调用 [ListQoSServersForTitle](xref:titleid.playfabapi.com.multiplayer.multiplayerserver.listqosserversfortitle)。这会向 PlayFab 的 QoS 信标提供主机名。典型的实现可能会在游戏的**多人游戏菜单**页面上执行此过程。
3. 创建一个 UDP 套接字。
4. 向 QoS 服务器上的 3075 端口发送单个 UDP 数据报。消息内容必须以 0xFFFF (1111 1111 1111 1111) 开头。
5. 服务器将回复一个数据报,消息内容的前 2 个字节 "翻转" 为 **0x0000** (0000 0000 0000 0000)。数据报的其余内容将从初始 ping 复制。
6. 测量发送 UDP 消息和接收响应之间的时间。

## 使用服务质量 SDK

PlayFab [C# SDK](https://github.com/PlayFab/CSharpSDK) 和[跨平台 (CPP) SDK](https://github.com/PlayFab/XPlatCppSdk) 提供了 QoS ping 代码的实现。您可以构建 SDK 并在您的 PC 游戏中将其用作辅助库、引用 C# NuGet 包,或将该代码用作其他平台的示例。

每个 API 都返回一个 `QosResult`,其中包含区域的排序列表以及到每个区域的平均 ping 时间。

### C\#

[C# SDK](https://github.com/PlayFab/CSharpSDK) 中提供了 QoS API。[gsdkSamples 存储库](https://github.com/PlayFab/gsdkSamples)中提供了示例实现 [WindowsRunnerCSharpClient](https://github.com/PlayFab/gsdkSamples/tree/master/WindowsRunnerCSharp#running-the-client)。

代码位于 [*PlayFabQosApi.cs*](https://github.com/PlayFab/CSharpSDK/blob/master/PlayFabSDK/source/Qos/PlayFabQosApi.cs)。

参数:

* `timeoutMs` - 应用于每次 ping 尝试的超时(以毫秒为单位)(默认值:250ms)。
* `pingsPerRegion` - 对每个区域进行的 ping 尝试次数(默认值:10)。增加此数字将增加执行时间,但会降低结果不准确的可能性。
* `degreeOfParallelism` - 并行执行的最大 ping 数(默认值:4)。增加此数字将减少执行时间,但如果此数字过大,网络争用可能会导致结果不准确。

```csharp theme={null}
        public async Task<QosResult> GetQosResultAsync(
            int timeoutMs = DefaultTimeoutMs,
            int pingsPerRegion = DefaultPingsPerRegion,
            int degreeOfParallelism = DefaultDegreeOfParallelism)
        {
```

### C++

[PlayFab 跨平台 (CPP) SDK](https://github.com/PlayFab/XPlatCppSdk) 中提供了两个 QoS API。

代码位于 [*PlayFabQosApi.cpp*](https://github.com/PlayFab/XPlatCppSdk/blob/master/code/source/playfab/QoS/PlayFabQoSApi.cpp)。

参数:

* `numThreads` - 并行执行的最大 ping 数。增加此数字将减少执行时间,但如果此数字过大,网络争用可能会导致结果不准确。
* `timeoutMs` - 应用于每次 ping 尝试的超时(以毫秒为单位)(默认值:250ms)。

```cpp theme={null}
  // Runs a QoS operation asynchronously. The operation pings a set of datacenters and returns a result with average response times.
  std::future<QoSResult> GetQoSResultAsync(unsigned int numThreads, unsigned int timeoutMs = DEFAULT_TIMEOUT_MS);

  // Runs a QoS operation synchronously. The operation pings a set of datacenters and returns a result with average response times.
  QoSResult GetQoSResult(unsigned int numThreads, unsigned int timeoutMs = DEFAULT_TIMEOUT_MS);
```


## Related topics

- [GDK 中的服务质量测量](/zh-CN/build/console-features/networking/game-mesh/qos-networking.md)
- [使用 PlayFab Multiplayer Servers 托管多人游戏](/zh-CN/services/playfab/multiplayer/servers/using-playfab-servers-to-host-games.md)
- [PlayFab 多人文档](/zh-CN/services/playfab/multiplayer/index.md)
- [PlayFab Party QoS 测量](/zh-CN/services/playfab/multiplayer/networking/concepts-regions.md)
- [目标会话初始化和 QoS](/zh-CN/services/xbox-services/multiplayer/matchmaking/concepts/live-matchmaking-target-session.md)
