Skip to content

Commit

Permalink
feat: adding Peer config properties
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed May 27, 2021
1 parent f11ef9a commit 9fd8a05
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
7 changes: 6 additions & 1 deletion Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class NetworkClient : MonoBehaviour, INetworkClient
public bool EnablePeerMetrics;
public Metrics Metrics { get; private set; }

/// <summary>
/// Config for peer, if not set will use default settings
/// </summary>
public Config PeerConfig { get; set; }

[Tooltip("Creates Socket for Peer to use")]
public SocketFactory SocketFactory;

Expand Down Expand Up @@ -109,7 +114,7 @@ public void Connect(string address = null, ushort? port = null)
ISocket socket = SocketFactory.CreateClientSocket();
var dataHandler = new DataHandler();
Metrics = EnablePeerMetrics ? new Metrics() : null;
peer = new Peer(socket, dataHandler, logger: LogFactory.GetLogger<Peer>(), metrics: Metrics);
peer = new Peer(socket, dataHandler, PeerConfig, LogFactory.GetLogger<Peer>(), Metrics);

peer.OnConnected += Peer_OnConnected;
peer.OnConnectionFailed += Peer_OnConnectionFailed;
Expand Down
12 changes: 8 additions & 4 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ public class NetworkServer : MonoBehaviour, INetworkServer
public bool EnablePeerMetrics;
public Metrics Metrics { get; private set; }

/// <summary>
/// Config for peer, if not set will use default settings
/// </summary>
public Config PeerConfig { get; set; }

/// <summary>
/// The maximum number of concurrent network connections to support.
/// <para>This effects the memory usage of the network layer.</para>
/// <para>This field is only used if the <see cref="PeerConfig"/> property is null</para>
/// </summary>
[Tooltip("Maximum number of concurrent connections.")]
[Min(1)]
Expand Down Expand Up @@ -196,15 +201,14 @@ public void StartServer(NetworkClient localClient = null)
World = new NetworkWorld();

ISocket socket = SocketFactory.CreateServerSocket();
ILogger peerLogger = LogFactory.GetLogger<Peer>();
var dataHandler = new DataHandler(connections);
Metrics = EnablePeerMetrics ? new Metrics() : null;
var config = new Config
Config config = PeerConfig ?? new Config
{
MaxConnections = MaxConnections,
};

peer = new Peer(socket, dataHandler, logger: peerLogger, config: config, metrics: Metrics);
peer = new Peer(socket, dataHandler, config, LogFactory.GetLogger<Peer>(), Metrics);
peer.OnConnected += Peer_OnConnected;
peer.OnDisconnected += Peer_OnDisconnected;

Expand Down
6 changes: 6 additions & 0 deletions Assets/Tests/Runtime/ClientServer/ClientServerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Linq;
using Cysharp.Threading.Tasks;
using Mirage.SocketLayer;
using UnityEngine;
using UnityEngine.TestTools;

Expand Down Expand Up @@ -38,6 +39,8 @@ public class ClientServerSetup<T> where T : NetworkBehaviour
public virtual void ExtraSetup() { }

protected virtual bool AutoConnectClient => true;
protected virtual Config ServerConfig => null;
protected virtual Config ClientConfig => null;

[UnitySetUp]
public IEnumerator Setup() => UniTask.ToCoroutine(async () =>
Expand All @@ -51,6 +54,9 @@ public class ClientServerSetup<T> where T : NetworkBehaviour
server = serverGo.GetComponent<NetworkServer>();
client = clientGo.GetComponent<NetworkClient>();
if (ServerConfig != null) server.PeerConfig = ServerConfig;
if (ClientConfig != null) client.PeerConfig = ClientConfig;
server.SocketFactory = socketFactory;
client.SocketFactory = socketFactory;
Expand Down
56 changes: 31 additions & 25 deletions Assets/Tests/Runtime/ClientServer/NetworkClientDisconnectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
using UnityEngine;
using UnityEngine.TestTools;

namespace Mirage.Tests.Runtime.ClientServer
namespace Mirage.Tests.Runtime.ClientServer.DisconnectTests
{
public class NetworkClientDisconnectTest : ClientServerSetup<MockComponent>
{
Config config = new Config();

public override void ExtraSetup()
readonly Config config = new Config()
{
// todo set config timeout here
}
// lower timeout so tests doesn't wait too long
TimeoutDuration = 2,
};
protected override Config ClientConfig => config;

public override void ExtraTearDown()
{
Expand Down Expand Up @@ -77,10 +77,32 @@ public IEnumerator DisconnectEventWhenTimeout()
}
}

public class NetworkClientConnectFailedTest : ClientServerSetup<MockComponent>
public class NetworkClientConnectFailedFullServerTest : ClientServerSetup<MockComponent>
{
Config config = new Config();
protected override Config ServerConfig => new Config { MaxConnections = 0 };
protected override bool AutoConnectClient => false;

[UnityTest]
public IEnumerator DisconnectEventWhenFull()
{
client.Connect("localhost");

int called = 0;
client.Disconnected.AddListener((reason) =>
{
called++;
Assert.That(reason, Is.EqualTo(ClientStoppedReason.ServerFull));
});

// wait 2 frames so that messages can go from client->server->client
yield return null;
yield return null;

Assert.That(called, Is.EqualTo(1));
}
}
public class NetworkClientConnectFailedTest : ClientServerSetup<MockComponent>
{
protected override bool AutoConnectClient => false;

public override void ExtraTearDown()
Expand All @@ -102,6 +124,7 @@ public IEnumerator DisconnectEventWhenTimeout()
});

// wait longer than timeout
var config = new Config();
float endTime = Time.time + (config.ConnectAttemptInterval * config.MaxConnectAttempts * 1.5f);
while (Time.time < endTime)
{
Expand All @@ -116,23 +139,6 @@ public IEnumerator DisconnectEventWhenTimeout()
Assert.That(called, Is.EqualTo(1));
}

[UnityTest]
public IEnumerator DisconnectEventWhenFull()
{
// todo set server max connections to 0, then try to connect..
Assert.Ignore("not implemented");
int called = 0;
client.Disconnected.AddListener((reason) =>
{
called++;
Assert.That(reason, Is.EqualTo(ClientStoppedReason.ServerFull));
});

yield return null;

Assert.That(called, Is.EqualTo(1));
}

[UnityTest]
public IEnumerator DisconnectEventWhenCanceled()
{
Expand Down

0 comments on commit 9fd8a05

Please sign in to comment.