-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionMethod.cs
158 lines (129 loc) · 6.65 KB
/
ConnectionMethod.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Threading.Tasks;
using Unity.BossRoom.UnityServices.Lobbies;
using Unity.BossRoom.Utils;
using Unity.Netcode.Transports.UTP;
using Unity.Networking.Transport.Relay;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Relay;
using Unity.Services.Relay.Models;
using UnityEngine;
namespace Unity.BossRoom.ConnectionManagement
{
/// <summary>
/// ConnectionMethod contains all setup needed to setup NGO to be ready to start a connection, either host or client side.
/// Please override this abstract class to add a new transport or way of connecting.
/// </summary>
public abstract class ConnectionMethodBase
{
protected ConnectionManager m_ConnectionManager;
readonly ProfileManager m_ProfileManager;
protected readonly string m_PlayerName;
public abstract Task SetupHostConnectionAsync();
public abstract Task SetupClientConnectionAsync();
public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager profileManager, string playerName)
{
m_ConnectionManager = connectionManager;
m_ProfileManager = profileManager;
m_PlayerName = playerName;
}
protected void SetConnectionPayload(string playerId, string playerName)
{
var payload = JsonUtility.ToJson(new ConnectionPayload()
{
playerId = playerId,
playerName = playerName,
isDebug = Debug.isDebugBuild
});
var payloadBytes = System.Text.Encoding.UTF8.GetBytes(payload);
m_ConnectionManager.NetworkManager.NetworkConfig.ConnectionData = payloadBytes;
}
protected string GetPlayerId()
{
if (Services.Core.UnityServices.State != ServicesInitializationState.Initialized)
{
return ClientPrefs.GetGuid() + m_ProfileManager.Profile;
}
return AuthenticationService.Instance.IsSignedIn ? AuthenticationService.Instance.PlayerId : ClientPrefs.GetGuid() + m_ProfileManager.Profile;
}
}
/// <summary>
/// Simple IP connection setup with UTP
/// </summary>
class ConnectionMethodIP : ConnectionMethodBase
{
string m_Ipaddress;
ushort m_Port;
public ConnectionMethodIP(string ip, ushort port, ConnectionManager connectionManager, ProfileManager profileManager, string playerName)
: base(connectionManager, profileManager, playerName)
{
m_Ipaddress = ip;
m_Port = port;
m_ConnectionManager = connectionManager;
}
public override async Task SetupClientConnectionAsync()
{
SetConnectionPayload(GetPlayerId(), m_PlayerName);
var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport;
utp.SetConnectionData(m_Ipaddress, m_Port);
}
public override async Task SetupHostConnectionAsync()
{
SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too
var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport;
utp.SetConnectionData(m_Ipaddress, m_Port);
}
}
/// <summary>
/// UTP's Relay connection setup
/// </summary>
class ConnectionMethodRelay : ConnectionMethodBase
{
LobbyServiceFacade m_LobbyServiceFacade;
LocalLobby m_LocalLobby;
public ConnectionMethodRelay(LobbyServiceFacade lobbyServiceFacade, LocalLobby localLobby, ConnectionManager connectionManager, ProfileManager profileManager, string playerName)
: base(connectionManager, profileManager, playerName)
{
m_LobbyServiceFacade = lobbyServiceFacade;
m_LocalLobby = localLobby;
m_ConnectionManager = connectionManager;
}
public override async Task SetupClientConnectionAsync()
{
Debug.Log("Setting up Unity Relay client");
SetConnectionPayload(GetPlayerId(), m_PlayerName);
if (m_LobbyServiceFacade.CurrentUnityLobby == null)
{
throw new Exception("Trying to start relay while Lobby isn't set");
}
Debug.Log($"Setting Unity Relay client with join code {m_LocalLobby.RelayJoinCode}");
// Create client joining allocation from join code
var joinedAllocation = await RelayService.Instance.JoinAllocationAsync(m_LocalLobby.RelayJoinCode);
Debug.Log($"client: {joinedAllocation.ConnectionData[0]} {joinedAllocation.ConnectionData[1]}, " +
$"host: {joinedAllocation.HostConnectionData[0]} {joinedAllocation.HostConnectionData[1]}, " +
$"client: {joinedAllocation.AllocationId}");
await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(joinedAllocation.AllocationId.ToString(), m_LocalLobby.RelayJoinCode);
// Configure UTP with allocation
var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport;
utp.SetRelayServerData(new RelayServerData(joinedAllocation, OnlineState.k_DtlsConnType));
}
public override async Task SetupHostConnectionAsync()
{
Debug.Log("Setting up Unity Relay host");
SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too
// Create relay allocation
Allocation hostAllocation = await RelayService.Instance.CreateAllocationAsync(m_ConnectionManager.MaxConnectedPlayers, region: null);
var joinCode = await RelayService.Instance.GetJoinCodeAsync(hostAllocation.AllocationId);
Debug.Log($"server: connection data: {hostAllocation.ConnectionData[0]} {hostAllocation.ConnectionData[1]}, " +
$"allocation ID:{hostAllocation.AllocationId}, region:{hostAllocation.Region}");
m_LocalLobby.RelayJoinCode = joinCode;
//next line enable lobby and relay services integration
await m_LobbyServiceFacade.UpdateLobbyDataAsync(m_LocalLobby.GetDataForUnityServices());
await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(hostAllocation.AllocationIdBytes.ToString(), joinCode);
// Setup UTP with relay connection info
var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport;
utp.SetRelayServerData(new RelayServerData(hostAllocation, OnlineState.k_DtlsConnType)); // This is with DTLS enabled for a secure connection
}
}
}