浏览代码

Adding in a test for DTLS and cleaning up a couple minor points with the DTLS changes. These do not currently work on 2020.3 (since DTLS support has not been ported back yet but will be) but I've tested them on 2021.2.0b11; the test executes just fine, but for some reason in practice there's an issue where we no longer know the correct packet length when reading Relay data. I'm told there should not be any change to this needed on the developer's end when DTLS is active, but investigation is ongoing.

/main/staging/relay_dtls
nathaniel.buck@unity3d.com 3 年前
当前提交
20496d52
共有 3 个文件被更改,包括 76 次插入12 次删除
  1. 4
      Assets/Scripts/Relay/RelayUtpClient.cs
  2. 3
      Assets/Scripts/Relay/RelayUtpSetup.cs
  3. 81
      Assets/Scripts/Tests/PlayMode/UtpTests.cs

4
Assets/Scripts/Relay/RelayUtpClient.cs


message.Add((byte)strBytes.Length);
message.AddRange(strBytes);
if (driver.BeginSend(connection, out var dataStream) == 0) // Oh, should check this first?
if (driver.BeginSend(connection, out var dataStream) == 0)
{
byte[] bytes = message.ToArray();
unsafe

message.AddRange(idBytes);
message.Add(value);
if (driver.BeginSend(connection, out var dataStream) == 0) // Oh, should check this first?
if (driver.BeginSend(connection, out var dataStream) == 0)
{
byte[] bytes = message.ToArray();
unsafe

3
Assets/Scripts/Relay/RelayUtpSetup.cs


protected NetworkEndPoint GetEndpointForAllocation(List<RelayServerEndpoint> endpoints, string ip, int port, out bool isSecure)
{
#if ENABLE_MANAGED_UNITYTLS
for (int ep = endpoints.Count - 1; ep >= 0; ep--)
foreach (RelayServerEndpoint endpoint in endpoints)
RelayServerEndpoint endpoint = endpoints[ep];
if (endpoint.Secure && endpoint.Network == RelayServerEndpoint.NetworkOptions.Udp)
{
isSecure = true;

81
Assets/Scripts/Tests/PlayMode/UtpTests.cs


using System.Collections;
using System.Collections.Generic;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;
namespace Test

private class RelayUtpTest : RelayUtpSetupHost
{
public NetworkEndPoint GetEndpointButPublic(List<RelayServerEndpoint> endpoints, string ip, int port, out bool isSecure)
public Action<NetworkEndPoint, bool> OnGetEndpoint { private get; set; }
public void JoinRelayPublic()
{
JoinRelay();
}
protected override void JoinRelay()
return GetEndpointForAllocation(endpoints, ip, port, out isSecure);
RelayAPIInterface.AllocateAsync(1, OnAllocation);
void OnAllocation(Allocation allocation)
{
bool isSecure = false;
NetworkEndPoint endpoint = GetEndpointForAllocation(allocation.ServerEndpoints, allocation.RelayServer.IpV4, allocation.RelayServer.Port, out isSecure);
OnGetEndpoint?.Invoke(endpoint, isSecure);
// The allocation will be cleaned up automatically, since we won't be pinging it regularly.
}
private LobbyRelaySample.Auth.SubIdentity_Authentication m_auth;
private bool m_didSigninComplete = false;
GameObject m_dummy;
[OneTimeSetUp]
public void Setup()
{
m_dummy = new GameObject();
m_auth = new LobbyRelaySample.Auth.SubIdentity_Authentication(() => { m_didSigninComplete = true; });
}
[OneTimeTearDown]
public void Teardown()
{
m_auth?.Dispose();
GameObject.Destroy(m_dummy);
}
yield return null; // TEMP
#if ENABLE_MANAGED_UNITYTLS
#if ENABLE_MANAGED_UNITYTLS
Assert.Fail("TODO: Implement.");
if (!m_didSigninComplete)
yield return new WaitForSeconds(3);
if (!m_didSigninComplete)
Assert.Fail("Did not sign in.");
yield return new WaitForSeconds(1); // To prevent a possible 429 after a previous test.
RelayUtpTest relaySetup = m_dummy.AddComponent<RelayUtpTest>();
relaySetup.OnGetEndpoint = OnGetEndpoint;
bool? isSecure = null;
NetworkEndPoint endpoint = default;
relaySetup.JoinRelayPublic();
float timeout = 5;
while (!isSecure.HasValue && timeout > 0)
{
timeout -= 0.25f;
yield return new WaitForSeconds(0.25f);
}
Component.Destroy(relaySetup);
Assert.IsTrue(timeout > 0, "Timeout check.");
Assert.IsTrue(isSecure, "Should have a secure server endpoint.");
Assert.IsTrue(endpoint.IsValid, "Endpoint should be valid.");
void OnGetEndpoint(NetworkEndPoint resultEndpoint, bool resultIsSecure)
{
endpoint = resultEndpoint;
isSecure = resultIsSecure;
}
Assert.Ignore("DTLS is not available in this version of Unity.");
Assert.Ignore("DTLS encryption for Relay is not currently available for this version of Unity.");
yield break;
#endif
}
}
正在加载...
取消
保存