Skip to content

Commit

Permalink
feat: Transports can tell if they are supported (#282)
Browse files Browse the repository at this point in the history
* feat: Transports can tell if they are supported

* Fix some tests

Co-authored-by: Paul Pacheco <paul.pacheco@aa.com>
  • Loading branch information
paulpach and paulpach committed Jul 14, 2020
1 parent 90d2bd0 commit 890c6b8
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 145 deletions.
89 changes: 18 additions & 71 deletions Assets/Mirror/Runtime/Transport/AsyncFallbackTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,102 +13,49 @@ public override string Scheme
{
get
{
foreach (Transport transport in transports)
{
try
{
return transport.Scheme;
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
}
throw new PlatformNotSupportedException("None of the transports is supported in this platform");
return GetTransport().Scheme;
}
}

public override async Task<IConnection> AcceptAsync()
private Transport GetTransport()
{
foreach (Transport transport in transports)
{
try
{
return await transport.AcceptAsync();
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
if (transport.Supported)
return transport;
}

throw new PlatformNotSupportedException("None of the transports is supported in this platform");
}
}

public override async Task<IConnection> ConnectAsync(Uri uri)
public override bool Supported => GetTransport() != null;

public override Task<IConnection> AcceptAsync()
{
foreach (Transport transport in transports)
{
try
{
return await transport.ConnectAsync(uri);
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
}
throw new PlatformNotSupportedException($"No transport was able to connect to {uri}");
return GetTransport().AcceptAsync();
}

public override Task<IConnection> ConnectAsync(Uri uri)
{
return GetTransport().ConnectAsync(uri);
}

public override void Disconnect()
{
foreach (Transport transport in transports)
{
try
{
if (transport.Supported)
transport.Disconnect();
return;
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
}
throw new PlatformNotSupportedException($"No transport available in this platform");
}

public override async Task ListenAsync()
public override Task ListenAsync()
{
foreach (Transport transport in transports)
{
try
{
await transport.ListenAsync();
return;
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
}
throw new PlatformNotSupportedException($"No transport available in this platform");
return GetTransport().ListenAsync();
}

public override Uri ServerUri()
{
foreach (Transport transport in transports)
{
try
{
return transport.ServerUri();
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
}
throw new PlatformNotSupportedException($"No transport available in this platform");
return GetTransport().ServerUri();
}
}
}
38 changes: 19 additions & 19 deletions Assets/Mirror/Runtime/Transport/AsyncMultiplexTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@

namespace Mirror
{
public class AsyncMultiplexTransport : Transport
public class AsyncMultiplexTransport : Transport
{

public Transport[] transports;
public Transport[] transports;

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

public override string Scheme
{
get
{
foreach (Transport transport in transports)
{
try
{
if (transport.Supported)
return transport.Scheme;
}
catch (PlatformNotSupportedException)
{
// try the next transport
}
}
throw new PlatformNotSupportedException("No transport was able to provide scheme");
}
}

private Transport GetTransport()
{
foreach (Transport transport in transports)
{
if (transport.Supported)
return transport;

This comment has been minimized.

Copy link
@uweeby

uweeby Jul 14, 2020

Member

Is it possible that more than 1 transport in the multiplexer could be supported?

}
throw new PlatformNotSupportedException("None of the transports is supported in this platform");
}

public override bool Supported => GetTransport() != null;

public override async Task<IConnection> AcceptAsync()
{
if (Accepters == null)
Expand Down Expand Up @@ -72,18 +78,12 @@ public override async Task<IConnection> AcceptAsync()
}
}

public override async Task<IConnection> ConnectAsync(Uri uri)
public override Task<IConnection> ConnectAsync(Uri uri)
{
foreach (Transport transport in transports)
{
try
{
return await transport.ConnectAsync(uri);
}
catch (ArgumentException)
{
// try the next transport
}
if (transport.Supported && transport.Scheme == uri.Scheme)
return transport.ConnectAsync(uri);
}
throw new ArgumentException($"No transport was able to connect to {uri}");
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/Mirror/Runtime/Transport/AsyncTcp/AsyncTcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using UnityEngine;

namespace Mirror.AsyncTcp
{
Expand All @@ -13,6 +14,8 @@ public class AsyncTcpTransport : Transport

public override string Scheme => "tcp4";

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

public override Task ListenAsync()
{
listener = TcpListener.Create(Port);
Expand Down
6 changes: 6 additions & 0 deletions Assets/Mirror/Runtime/Transport/Transport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public abstract class Transport : MonoBehaviour
/// </summary>
public abstract void Disconnect();

/// <summary>
/// Determines if this transport is supported in the current platform
/// </summary>
/// <returns>true if the transport works in this platform</returns>
public abstract bool Supported { get; }

/// <summary>
/// Connect to a server located at a provided uri
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions Assets/Mirror/Runtime/Transport/Websocket/AsyncWsTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class AsyncWsTransport : Transport

public override string Scheme => "ws";

// supported in all platforms
public override bool Supported => true;

public override async Task<IConnection> AcceptAsync()
{
try
Expand Down
2 changes: 2 additions & 0 deletions Assets/Mirror/Tests/Common/LoopbackTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public override async Task<IConnection> AcceptAsync()

public override string Scheme => "local";

public override bool Supported => true;

public override Task<IConnection> ConnectAsync(Uri uri)
{
(IConnection c1, IConnection c2) = PipeConnection.CreatePipe();
Expand Down
2 changes: 2 additions & 0 deletions Assets/Mirror/Tests/Common/MockTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public override async Task<IConnection> AcceptAsync()

public override string Scheme => "tcp4";

public override bool Supported => true;

public override async Task<IConnection> ConnectAsync(Uri uri)
{
return await ConnectConnections.DequeueAsync();
Expand Down

0 comments on commit 890c6b8

Please sign in to comment.