|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Unity.BossRoom.UnityServices.Lobbies; |
| 4 | +using Unity.BossRoom.Utils; |
| 5 | +using Unity.Netcode.Transports.UTP; |
| 6 | +using Unity.Networking.Transport.Relay; |
| 7 | +using Unity.Services.Authentication; |
| 8 | +using Unity.Services.Core; |
| 9 | +using Unity.Services.Relay; |
| 10 | +using Unity.Services.Relay.Models; |
| 11 | +using UnityEngine; |
| 12 | + |
| 13 | +namespace Unity.BossRoom.ConnectionManagement |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// ConnectionMethod contains all setup needed to setup NGO to be ready to start a connection, either host or client side. |
| 17 | + /// Please override this abstract class to add a new transport or way of connecting. |
| 18 | + /// </summary> |
| 19 | + public abstract class ConnectionMethodBase |
| 20 | + { |
| 21 | + protected ConnectionManager m_ConnectionManager; |
| 22 | + readonly ProfileManager m_ProfileManager; |
| 23 | + protected readonly string m_PlayerName; |
| 24 | + |
| 25 | + public abstract Task SetupHostConnectionAsync(); |
| 26 | + |
| 27 | + public abstract Task SetupClientConnectionAsync(); |
| 28 | + |
| 29 | + public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager profileManager, string playerName) |
| 30 | + { |
| 31 | + m_ConnectionManager = connectionManager; |
| 32 | + m_ProfileManager = profileManager; |
| 33 | + m_PlayerName = playerName; |
| 34 | + } |
| 35 | + |
| 36 | + protected void SetConnectionPayload(string playerId, string playerName) |
| 37 | + { |
| 38 | + var payload = JsonUtility.ToJson(new ConnectionPayload() |
| 39 | + { |
| 40 | + playerId = playerId, |
| 41 | + playerName = playerName, |
| 42 | + isDebug = Debug.isDebugBuild |
| 43 | + }); |
| 44 | + |
| 45 | + var payloadBytes = System.Text.Encoding.UTF8.GetBytes(payload); |
| 46 | + |
| 47 | + m_ConnectionManager.NetworkManager.NetworkConfig.ConnectionData = payloadBytes; |
| 48 | + } |
| 49 | + |
| 50 | + protected string GetPlayerId() |
| 51 | + { |
| 52 | + if (Services.Core.UnityServices.State != ServicesInitializationState.Initialized) |
| 53 | + { |
| 54 | + return ClientPrefs.GetGuid() + m_ProfileManager.Profile; |
| 55 | + } |
| 56 | + |
| 57 | + return AuthenticationService.Instance.IsSignedIn ? AuthenticationService.Instance.PlayerId : ClientPrefs.GetGuid() + m_ProfileManager.Profile; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Simple IP connection setup with UTP |
| 63 | + /// </summary> |
| 64 | + class ConnectionMethodIP : ConnectionMethodBase |
| 65 | + { |
| 66 | + string m_Ipaddress; |
| 67 | + ushort m_Port; |
| 68 | + |
| 69 | + public ConnectionMethodIP(string ip, ushort port, ConnectionManager connectionManager, ProfileManager profileManager, string playerName) |
| 70 | + : base(connectionManager, profileManager, playerName) |
| 71 | + { |
| 72 | + m_Ipaddress = ip; |
| 73 | + m_Port = port; |
| 74 | + m_ConnectionManager = connectionManager; |
| 75 | + } |
| 76 | + |
| 77 | + public override async Task SetupClientConnectionAsync() |
| 78 | + { |
| 79 | + SetConnectionPayload(GetPlayerId(), m_PlayerName); |
| 80 | + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; |
| 81 | + utp.SetConnectionData(m_Ipaddress, m_Port); |
| 82 | + } |
| 83 | + |
| 84 | + public override async Task SetupHostConnectionAsync() |
| 85 | + { |
| 86 | + SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too |
| 87 | + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; |
| 88 | + utp.SetConnectionData(m_Ipaddress, m_Port); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// UTP's Relay connection setup |
| 94 | + /// </summary> |
| 95 | + class ConnectionMethodRelay : ConnectionMethodBase |
| 96 | + { |
| 97 | + LobbyServiceFacade m_LobbyServiceFacade; |
| 98 | + LocalLobby m_LocalLobby; |
| 99 | + |
| 100 | + public ConnectionMethodRelay(LobbyServiceFacade lobbyServiceFacade, LocalLobby localLobby, ConnectionManager connectionManager, ProfileManager profileManager, string playerName) |
| 101 | + : base(connectionManager, profileManager, playerName) |
| 102 | + { |
| 103 | + m_LobbyServiceFacade = lobbyServiceFacade; |
| 104 | + m_LocalLobby = localLobby; |
| 105 | + m_ConnectionManager = connectionManager; |
| 106 | + } |
| 107 | + |
| 108 | + public override async Task SetupClientConnectionAsync() |
| 109 | + { |
| 110 | + Debug.Log("Setting up Unity Relay client"); |
| 111 | + |
| 112 | + SetConnectionPayload(GetPlayerId(), m_PlayerName); |
| 113 | + |
| 114 | + if (m_LobbyServiceFacade.CurrentUnityLobby == null) |
| 115 | + { |
| 116 | + throw new Exception("Trying to start relay while Lobby isn't set"); |
| 117 | + } |
| 118 | + |
| 119 | + Debug.Log($"Setting Unity Relay client with join code {m_LocalLobby.RelayJoinCode}"); |
| 120 | + |
| 121 | + // Create client joining allocation from join code |
| 122 | + var joinedAllocation = await RelayService.Instance.JoinAllocationAsync(m_LocalLobby.RelayJoinCode); |
| 123 | + Debug.Log($"client: {joinedAllocation.ConnectionData[0]} {joinedAllocation.ConnectionData[1]}, " + |
| 124 | + $"host: {joinedAllocation.HostConnectionData[0]} {joinedAllocation.HostConnectionData[1]}, " + |
| 125 | + $"client: {joinedAllocation.AllocationId}"); |
| 126 | + |
| 127 | + await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(joinedAllocation.AllocationId.ToString(), m_LocalLobby.RelayJoinCode); |
| 128 | + |
| 129 | + // Configure UTP with allocation |
| 130 | + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; |
| 131 | + utp.SetRelayServerData(new RelayServerData(joinedAllocation, OnlineState.k_DtlsConnType)); |
| 132 | + } |
| 133 | + |
| 134 | + public override async Task SetupHostConnectionAsync() |
| 135 | + { |
| 136 | + Debug.Log("Setting up Unity Relay host"); |
| 137 | + |
| 138 | + SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too |
| 139 | + |
| 140 | + // Create relay allocation |
| 141 | + Allocation hostAllocation = await RelayService.Instance.CreateAllocationAsync(m_ConnectionManager.MaxConnectedPlayers, region: null); |
| 142 | + var joinCode = await RelayService.Instance.GetJoinCodeAsync(hostAllocation.AllocationId); |
| 143 | + |
| 144 | + Debug.Log($"server: connection data: {hostAllocation.ConnectionData[0]} {hostAllocation.ConnectionData[1]}, " + |
| 145 | + $"allocation ID:{hostAllocation.AllocationId}, region:{hostAllocation.Region}"); |
| 146 | + |
| 147 | + m_LocalLobby.RelayJoinCode = joinCode; |
| 148 | + |
| 149 | + //next line enable lobby and relay services integration |
| 150 | + await m_LobbyServiceFacade.UpdateLobbyDataAsync(m_LocalLobby.GetDataForUnityServices()); |
| 151 | + await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(hostAllocation.AllocationIdBytes.ToString(), joinCode); |
| 152 | + |
| 153 | + // Setup UTP with relay connection info |
| 154 | + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; |
| 155 | + utp.SetRelayServerData(new RelayServerData(hostAllocation, OnlineState.k_DtlsConnType)); // This is with DTLS enabled for a secure connection |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments