-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityRelayUtilities.cs
82 lines (73 loc) · 3.53 KB
/
UnityRelayUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using Unity.Services.Relay;
using Unity.Services.Relay.Models;
namespace Unity.Multiplayer.Samples.BossRoom
{
public static class UnityRelayUtilities
{
const string k_KDtlsConnType = "dtls";
/// <summary>
/// Deprecated, please see updated ConnectionManager sample code for an example on how to connect to Relay
/// </summary>
/// <param name="maxConnections"></param>
/// <param name="region"></param>
/// <param name="relayInstance"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<(string ipv4address, ushort port, byte[] allocationIdBytes, byte[] connectionData, byte[] key, string joinCode)>
AllocateRelayServerAndGetJoinCode(int maxConnections, string region = null)
{
Allocation allocation;
string joinCode;
try
{
allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections, region);
}
catch (Exception exception)
{
throw new Exception($"Creating allocation request has failed: \n {exception.Message}");
}
Debug.Log($"server: connection data: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}, allocation ID:{allocation.AllocationId}, region:{allocation.Region}");
try
{
joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
}
catch (Exception exception)
{
throw new Exception($"Creating join code request has failed: \n {exception.Message}");
}
var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == k_KDtlsConnType);
return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes,
allocation.ConnectionData, allocation.Key, joinCode);
}
/// <summary>
/// Deprecated, please see updated ConnectionManager sample code for an example on how to connect to Relay
/// </summary>
/// <param name="joinCode"></param>
/// <param name="relayInstance"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<(string ipv4address, ushort port, byte[] allocationIdBytes, Guid allocationId, byte[] connectionData, byte[] hostConnectionData, byte[] key)>
JoinRelayServerFromJoinCode(string joinCode)
{
JoinAllocation allocation;
try
{
allocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
}
catch (Exception exception)
{
throw new Exception($"Creating join code request has failed: \n {exception.Message}");
}
Debug.Log($"client: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
Debug.Log($"host: {allocation.HostConnectionData[0]} {allocation.HostConnectionData[1]}");
Debug.Log($"client: {allocation.AllocationId}");
var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == k_KDtlsConnType);
return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes, allocation.AllocationId,
allocation.ConnectionData, allocation.HostConnectionData, allocation.Key);
}
}
}