浏览代码

Merge pull request #31 from Unity-Technologies/fix/updateAPIUsage

fix: update the usage of Relay API to avoid using deprecated fields
/main
GitHub 2 年前
当前提交
53b95e47
共有 4 个文件被更改,包括 18 次插入20 次删除
  1. 4
      Assets/Scripts/GameLobby/NGO/RelayNGOUtpSetup.cs
  2. 22
      Assets/Scripts/GameLobby/Relay/RelayUtpSetup.cs
  3. 10
      Assets/Scripts/GameLobby/Tests/PlayMode/RelayRoundTripTests.cs
  4. 2
      Assets/Scripts/GameLobby/Tests/PlayMode/UtpTests.cs

4
Assets/Scripts/GameLobby/NGO/RelayNGOUtpSetup.cs


{
RelayAPIInterface.GetJoinCodeAsync(allocation.AllocationId, OnRelayCode);
bool isSecure = false;
var endpoint = RelayUtpSetup.GetEndpointForAllocation(allocation.ServerEndpoints, allocation.RelayServer.IpV4, allocation.RelayServer.Port, out isSecure);
var endpoint = RelayUtpSetup.GetEndpointForAllocation(allocation.ServerEndpoints, out isSecure);
m_setupInGame.SetRelayServerData(RelayUtpSetup.AddressFromEndpoint(endpoint), endpoint.Port, allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData, allocation.ConnectionData, isSecure);
m_onJoin?.Invoke();
}

if (joinAllocation == null || this == null) // The returned JoinAllocation is null if allocation failed. This would be destroyed already if you quit the lobby while Relay is connecting.
return;
bool isSecure = false;
var endpoint = RelayUtpSetup.GetEndpointForAllocation(joinAllocation.ServerEndpoints, joinAllocation.RelayServer.IpV4, joinAllocation.RelayServer.Port, out isSecure);
var endpoint = RelayUtpSetup.GetEndpointForAllocation(joinAllocation.ServerEndpoints, out isSecure);
m_setupInGame.SetRelayServerData(RelayUtpSetup.AddressFromEndpoint(endpoint), endpoint.Port, joinAllocation.AllocationIdBytes, joinAllocation.Key, joinAllocation.ConnectionData, joinAllocation.HostConnectionData, isSecure);
m_onJoin?.Invoke();
}

22
Assets/Scripts/GameLobby/Relay/RelayUtpSetup.cs


/// Determine the server endpoint for connecting to the Relay server, for either an Allocation or a JoinAllocation.
/// If DTLS encryption is available, and there's a secure server endpoint available, use that as a secure connection. Otherwise, just connect to the Relay IP unsecured.
/// </summary>
public static NetworkEndPoint GetEndpointForAllocation(List<RelayServerEndpoint> endpoints, string ip, int port, out bool isSecure)
public static NetworkEndPoint GetEndpointForAllocation(List<RelayServerEndpoint> endpoints, out bool isSecure)
foreach (RelayServerEndpoint endpoint in endpoints)
{
if (endpoint.Secure && endpoint.Network == RelayServerEndpoint.NetworkOptions.Udp)
{
isSecure = true;
return NetworkEndPoint.Parse(endpoint.Host, (ushort)endpoint.Port);
}
}
isSecure = true;
var endpoint = endpoints.Find(e => e.ConnectionType == "dtls");
#else
isSecure = false;
var endpoint = endpoints.Find(e => e.ConnectionType == "udp");
isSecure = false;
return NetworkEndPoint.Parse(ip, (ushort)port);
return NetworkEndPoint.Parse(endpoint.Host, (ushort)endpoint.Port);
}
/// <summary>

m_allocation = allocation;
RelayAPIInterface.GetJoinCodeAsync(allocation.AllocationId, OnRelayCode);
bool isSecure = false;
m_endpointForServer = GetEndpointForAllocation(allocation.ServerEndpoints, allocation.RelayServer.IpV4, allocation.RelayServer.Port, out isSecure);
m_endpointForServer = GetEndpointForAllocation(allocation.ServerEndpoints, out isSecure);
BindToAllocation(m_endpointForServer, allocation.AllocationIdBytes, allocation.ConnectionData, allocation.ConnectionData, allocation.Key, 16, isSecure);
}

return;
m_allocation = joinAllocation;
bool isSecure = false;
m_endpointForServer = GetEndpointForAllocation(joinAllocation.ServerEndpoints, joinAllocation.RelayServer.IpV4, joinAllocation.RelayServer.Port, out isSecure);
m_endpointForServer = GetEndpointForAllocation(joinAllocation.ServerEndpoints, out isSecure);
BindToAllocation(m_endpointForServer, joinAllocation.AllocationIdBytes, joinAllocation.ConnectionData, joinAllocation.HostConnectionData, joinAllocation.Key, 1, isSecure);
m_localLobby.RelayServer = new ServerAddress(AddressFromEndpoint(m_endpointForServer), m_endpointForServer.Port);
}

10
Assets/Scripts/GameLobby/Tests/PlayMode/RelayRoundTripTests.cs


Assert.Greater(timeout, 0, "Timeout Check (Allocate)");
Guid allocationId = allocation.AllocationId;
var allocationIP = allocation.RelayServer.IpV4;
var allocationPort = allocation.RelayServer.Port;
var endpoint = allocation.ServerEndpoints.Find(e => e.ConnectionType == "udp");
var allocationIP = endpoint.Host;
var allocationPort = endpoint.Port;
Assert.NotNull(allocationId);
Assert.NotNull(allocationIP);
Assert.NotNull(allocationPort);

}
Assert.Greater(timeout, 0, "Timeout Check (Join)");
var codeIp = joinResponse.RelayServer.IpV4;
var codePort = joinResponse.RelayServer.Port;
var joinEndpoint = joinResponse.ServerEndpoints.Find(e => e.ConnectionType == "udp");
var codeIp = joinEndpoint.Host;
var codePort = joinEndpoint.Port;
Assert.AreEqual(codeIp, allocationIP);
Assert.AreEqual(codePort, allocationPort);
}

2
Assets/Scripts/GameLobby/Tests/PlayMode/UtpTests.cs


void OnAllocation(Allocation allocation)
{
bool isSecure = false;
NetworkEndPoint endpoint = GetEndpointForAllocation(allocation.ServerEndpoints, allocation.RelayServer.IpV4, allocation.RelayServer.Port, out isSecure);
NetworkEndPoint endpoint = GetEndpointForAllocation(allocation.ServerEndpoints, out isSecure);
OnGetEndpoint?.Invoke(endpoint, isSecure);
// The allocation will be cleaned up automatically, since we won't be pinging it regularly.
}

正在加载...
取消
保存