Skip to content

Commit

Permalink
feat: Transports may support any number of schemes (#291)
Browse files Browse the repository at this point in the history
for example,  Websocket transport supports ws and wss
and multiplex transport support anything that the child transports support

Co-authored-by: Paul Pacheco <paul.pacheco@aa.com>
  • Loading branch information
paulpach and paulpach committed Jul 15, 2020
1 parent 2d36681 commit 2af7b9d
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
Expand Down Expand Up @@ -112,7 +113,7 @@ public Task StartClient(string serverIp)
var builder = new UriBuilder
{
Host = serverIp,
Scheme = transport.Scheme,
Scheme = transport.Scheme.First(),
};

return client.ConnectAsync(builder.Uri);
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Runtime/Transport/AsyncFallbackTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;


Expand All @@ -9,7 +10,7 @@ public class AsyncFallbackTransport : Transport

public Transport[] transports;

public override string Scheme
public override IEnumerable<string> Scheme
{
get
{
Expand Down
18 changes: 5 additions & 13 deletions Assets/Mirror/Runtime/Transport/AsyncMultiplexTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@ public class AsyncMultiplexTransport : Transport

private Dictionary<Task<IConnection>, Transport> Accepters;

public override string Scheme
{
get
{
foreach (Transport transport in transports)
{
if (transport.Supported)
return transport.Scheme;
}
throw new PlatformNotSupportedException("No transport was able to provide scheme");
}
}
public override IEnumerable<string> Scheme =>
transports
.Where(transport => transport.Supported)
.SelectMany(transport => transport.Scheme);

private Transport GetTransport()
{
Expand Down Expand Up @@ -82,7 +74,7 @@ public override Task<IConnection> ConnectAsync(Uri uri)
{
foreach (Transport transport in transports)
{
if (transport.Supported && transport.Scheme == uri.Scheme)
if (transport.Supported && transport.Scheme.Contains(uri.Scheme))
return transport.ConnectAsync(uri);
}
throw new ArgumentException($"No transport was able to connect to {uri}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
Expand All @@ -12,7 +13,7 @@ public class AsyncTcpTransport : Transport
private TcpListener listener;
public int Port = 7777;

public override string Scheme => "tcp4";
public override IEnumerable<string> Scheme => new[] { "tcp4" };

public override bool Supported => Application.platform != RuntimePlatform.WebGLPlayer;

Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Runtime/Transport/Transport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

Expand All @@ -10,7 +11,7 @@ namespace Mirror
/// </summary>
public abstract class Transport : MonoBehaviour
{
public abstract string Scheme { get; }
public abstract IEnumerable<string> Scheme { get; }

/// <summary>
/// Open up the port and listen for connections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
Expand All @@ -16,7 +17,7 @@ public class AsyncWsTransport : Transport
private TcpListener listener;
private readonly IWebSocketServerFactory webSocketServerFactory = new WebSocketServerFactory();

public override string Scheme => "ws";
public override IEnumerable<string> Scheme => new[] { "ws", "wss" };

// supported in all platforms
public override bool Supported => true;
Expand Down
6 changes: 4 additions & 2 deletions Assets/Mirror/Tests/Common/LoopbackTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mirror.Tests
Expand All @@ -15,7 +17,7 @@ public override async Task<IConnection> AcceptAsync()

public readonly AsyncQueue<IConnection> CoonnectedConnections = new AsyncQueue<IConnection>();

public override string Scheme => "local";
public override IEnumerable<string> Scheme => new [] { "local" };

public override bool Supported => true;

Expand All @@ -41,7 +43,7 @@ public override Uri ServerUri()
{
return new UriBuilder
{
Scheme = Scheme,
Scheme = Scheme.First(),
Host = "localhost"
}.Uri;
}
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Tests/Common/MockTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mirror.Tests
Expand All @@ -15,7 +16,7 @@ public override async Task<IConnection> AcceptAsync()

public readonly AsyncQueue<IConnection> ConnectConnections = new AsyncQueue<IConnection>();

public override string Scheme => "tcp4";
public override IEnumerable<string> Scheme => new []{"tcp4"};

public override bool Supported => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,18 @@ public void ServerUriNone()
[Test]
public void Scheme1()
{
transport1.Scheme.Returns("tcp4");
transport1.Scheme.Returns(new[] { "tcp4" });

Assert.That(transport.Scheme, Is.EqualTo("tcp4"));
Assert.That(transport.Scheme, Is.EquivalentTo(new[] { "tcp4" }));
}

[Test]
public void Scheme2()
{
transport1.Supported.Returns(false);
transport2.Scheme.Returns("tcp4");
transport2.Scheme.Returns(new[] { "tcp4" });

Assert.That(transport.Scheme, Is.EqualTo("tcp4"));
Assert.That(transport.Scheme, Is.EquivalentTo(new[] { "tcp4" }));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,28 @@ public void ServerUri()
[Test]
public void Scheme1()
{
transport1.Scheme.Returns("tcp4");
transport1.Scheme.Returns(new[] { "tcp4" });

Assert.That(transport.Scheme, Is.EqualTo("tcp4"));
Assert.That(transport.Scheme, Is.EquivalentTo(new[] { "tcp4" }));
}

[Test]
public void SchemeNone()
{
transport1.Scheme.Returns(new[] { "yomama" });
transport2.Scheme.Returns(new[] { "pepe" });
transport1.Supported.Returns(false);
transport2.Supported.Returns(false);

Assert.Throws<PlatformNotSupportedException>(() =>
{
_ = transport.Scheme;
});

Assert.That(transport.Scheme, Is.Empty);
}

[UnityTest]
public IEnumerator Connect() => RunAsync(async () =>
{
transport1.Scheme.Returns("yomama");
transport2.Scheme.Returns("tcp4");
transport1.Scheme.Returns(new[] { "yomama" });
transport2.Scheme.Returns(new[] { "tcp4" });
transport1.ConnectAsync(Arg.Any<Uri>())
.Returns(Task.FromException<IConnection>(new ArgumentException("Invalid protocol")));
Expand Down
10 changes: 5 additions & 5 deletions Assets/Mirror/Tests/Editor/Transport/AsyncTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

namespace Mirror.Tests
{
[TestFixture(typeof(AsyncTcpTransport), "tcp4", "tcp4://localhost", 7777)]
[TestFixture(typeof(AsyncWsTransport), "ws", "ws://localhost", 7778)]
[TestFixture(typeof(AsyncTcpTransport), new[] { "tcp4" }, "tcp4://localhost", 7777)]
[TestFixture(typeof(AsyncWsTransport), new[] { "ws", "wss" }, "ws://localhost", 7778)]
public class AsyncTransportTests<T> where T : Transport
{
#region SetUp
Expand All @@ -25,9 +25,9 @@ public class AsyncTransportTests<T> where T : Transport
private GameObject transportObj;
private Uri uri;
private int port;
private string scheme;
private string[] scheme;

public AsyncTransportTests(string scheme, string uri, int port)
public AsyncTransportTests(string[] scheme, string uri, int port)
{
this.scheme = scheme;
this.uri = new Uri(uri);
Expand Down Expand Up @@ -182,7 +182,7 @@ public void TestServerUri()
[Test]
public void TestScheme()
{
Assert.That(transport.Scheme, Is.EqualTo(scheme));
Assert.That(transport.Scheme, Is.EquivalentTo(scheme));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Tests/Runtime/ClientServerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public virtual void ExtraSetup()
var builder = new UriBuilder
{
Host = "localhost",
Scheme = client.Transport.Scheme,
Scheme = client.Transport.Scheme.First(),
};
// now start the client
Expand Down

0 comments on commit 2af7b9d

Please sign in to comment.