-
Notifications
You must be signed in to change notification settings - Fork 583
Expand file tree
/
Copy pathConnectionMethod.cs
More file actions
167 lines (145 loc) · 7.18 KB
/
ConnectionMethod.cs
File metadata and controls
167 lines (145 loc) · 7.18 KB
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
159
160
161
162
163
164
165
166
167
using System.Threading.Tasks;
using Unity.BossRoom.UnityServices.Sessions;
using Unity.BossRoom.Utils;
using Unity.Netcode.Transports.UTP;
using Unity.Services.Authentication;
using Unity.Services.Core;
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;
/// <summary>
/// Setup the host connection prior to starting the NetworkManager
/// </summary>
/// <returns></returns>
public abstract void SetupHostConnection();
/// <summary>
/// Setup the client connection prior to starting the NetworkManager
/// </summary>
/// <returns></returns>
public abstract void SetupClientConnection();
/// <summary>
/// Setup the client for reconnection prior to reconnecting
/// </summary>
/// <returns>
/// success = true if succeeded in setting up reconnection, false if failed.
/// shouldTryAgain = true if we should try again after failing, false if not.
/// </returns>
public abstract Task<(bool success, bool shouldTryAgain)> SetupClientReconnectionAsync();
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;
}
/// Using authentication, this makes sure your session is associated with your account and not your device. This means you could reconnect
/// from a different device for example. A playerId is also a bit more permanent than player prefs. In a browser for example,
/// player prefs can be cleared as easily as cookies.
/// The forked flow here is for debug purposes and to make UGS optional in Boss Room. This way you can study the sample without
/// setting up a UGS account. It's recommended to investigate your own initialization and IsSigned flows to see if you need
/// those checks on your own and react accordingly. We offer here the option for offline access for debug purposes, but in your own game you
/// might want to show an error popup and ask your player to connect to the internet.
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 void SetupClientConnection()
{
SetConnectionPayload(GetPlayerId(), m_PlayerName);
var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport;
utp.SetConnectionData(m_Ipaddress, m_Port);
}
public override Task<(bool success, bool shouldTryAgain)> SetupClientReconnectionAsync()
{
// Nothing to do here
return Task.FromResult((true, true));
}
public override void SetupHostConnection()
{
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 using the Session integration
/// </summary>
class ConnectionMethodRelay : ConnectionMethodBase
{
MultiplayerServicesFacade m_MultiplayerServicesFacade;
public ConnectionMethodRelay(MultiplayerServicesFacade multiplayerServicesFacade,
ConnectionManager connectionManager,
ProfileManager profileManager,
string playerName)
: base(connectionManager, profileManager, playerName)
{
m_MultiplayerServicesFacade = multiplayerServicesFacade;
m_ConnectionManager = connectionManager;
}
public override void SetupClientConnection()
{
SetConnectionPayload(GetPlayerId(), m_PlayerName);
}
public override async Task<(bool success, bool shouldTryAgain)> SetupClientReconnectionAsync()
{
if (m_MultiplayerServicesFacade.CurrentUnitySession == null)
{
Debug.Log("Session does not exist anymore, stopping reconnection attempts.");
return (false, false);
}
// When using Session with Relay, if a user is disconnected from the Relay server, the server will notify the
// Session service and mark the user as disconnected, but will not remove them from the Session. They then have
// some time to attempt to reconnect (defined by the "Disconnect removal time" parameter on the dashboard),
// after which they will be removed from the Session completely.
// See https://docs.unity.com/ugs/en-us/manual/mps-sdk/manual/join-session#Reconnect_to_a_session
var session = await m_MultiplayerServicesFacade.ReconnectToSessionAsync();
var success = session != null;
Debug.Log(success ? "Successfully reconnected to Session." : "Failed to reconnect to Session.");
return (success, true); // return a success if reconnecting to session returns a session
}
public override void SetupHostConnection()
{
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
}
}
}