Skip to content

Commit

Permalink
feat: add events to interfaces (MirageNet#614)
Browse files Browse the repository at this point in the history
* fix: move interfaces into their own files

* feat: add events to interfaces

* add tests

* fix serialization
  • Loading branch information
uweeby authored Feb 19, 2021
1 parent 7156f2d commit 4d1a772
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 27 deletions.
12 changes: 6 additions & 6 deletions Assets/Mirage/Runtime/ClientObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Cysharp.Threading.Tasks;
using Mirage.RemoteCalls;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace Mirage
Expand All @@ -25,19 +24,20 @@ public class ClientObjectManager : MonoBehaviour, IClientObjectManager, IObjectL
internal readonly Dictionary<Guid, SpawnHandlerDelegate> spawnHandlers = new Dictionary<Guid, SpawnHandlerDelegate>();
internal readonly Dictionary<Guid, UnSpawnDelegate> unspawnHandlers = new Dictionary<Guid, UnSpawnDelegate>();

[System.Serializable]
public class SpawnEvent : UnityEvent<NetworkIdentity> { }

[Header("Events")]
/// <summary>
/// Raised when the client spawns an object
/// </summary>
public SpawnEvent Spawned = new SpawnEvent();
[FormerlySerializedAs("Spawned")]
[SerializeField] SpawnEvent _spawned = new SpawnEvent();
public SpawnEvent Spawned => _spawned;

/// <summary>
/// Raised when the client unspawns an object
/// </summary>
public SpawnEvent UnSpawned = new SpawnEvent();
[FormerlySerializedAs("UnSpawned")]
[SerializeField] SpawnEvent _unSpawned = new SpawnEvent();
public SpawnEvent UnSpawned => _unSpawned;

[Header("Prefabs")]
/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions Assets/Mirage/Runtime/IClientObjectManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using UnityEngine;
using UnityEngine.Events;

namespace Mirage
{
Expand All @@ -7,8 +9,21 @@ namespace Mirage
// Handles requests to unspawn objects on the client
public delegate void UnSpawnDelegate(NetworkIdentity spawned);

[Serializable]
public class SpawnEvent : UnityEvent<NetworkIdentity> { }

public interface IClientObjectManager
{
/// <summary>
/// Raised when the client spawns an object
/// </summary>
SpawnEvent Spawned { get; }

/// <summary>
/// Raised when the client unspawns an object
/// </summary>
SpawnEvent UnSpawned { get; }

NetworkIdentity GetPrefab(Guid assetId);

void RegisterPrefab(NetworkIdentity identity);
Expand Down
17 changes: 17 additions & 0 deletions Assets/Mirage/Runtime/INetworkClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
using Cysharp.Threading.Tasks;
using UnityEngine.Events;

namespace Mirage
{
public interface INetworkClient
{

/// <summary>
/// Event fires once the Client has connected its Server.
/// </summary>
NetworkConnectionEvent Connected { get; }

/// <summary>
/// Event fires after the Client connection has sucessfully been authenticated with its Server.
/// </summary>
NetworkConnectionEvent Authenticated { get; }

/// <summary>
/// Event fires after the Client has disconnected from its Server and Cleanup has been called.
/// </summary>
UnityEvent Disconnected { get; }

void Disconnect();

void Send<T>(T message, int channelId = Channel.Reliable);
Expand Down
20 changes: 20 additions & 0 deletions Assets/Mirage/Runtime/INetworkSceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ public enum SceneOperation : byte

public interface INetworkSceneManager
{
/// <summary>
/// Event fires when the Client starts changing scene.
/// </summary>
ClientSceneChangeEvent ClientChangeScene { get; }

/// <summary>
/// Event fires after the Client has completed its scene change.
/// </summary>
ClientSceneChangeEvent ClientSceneChanged { get; }

/// <summary>
/// Event fires before Server changes scene.
/// </summary>
ClientSceneChangeEvent ServerChangeScene { get; }

/// <summary>
/// Event fires after Server has completed scene change.
/// </summary>
ClientSceneChangeEvent ServerSceneChanged { get; }

void ChangeServerScene(string scenePath, SceneOperation sceneOperation = SceneOperation.Normal);
}
}
35 changes: 35 additions & 0 deletions Assets/Mirage/Runtime/INetworkServer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
using UnityEngine.Events;

namespace Mirage
{
public interface INetworkServer
{
/// <summary>
/// This is invoked when a server is started - including when a host is started.
/// </summary>
UnityEvent Started { get; }

/// <summary>
/// Event fires once a new Client has connect to the Server.
/// </summary>
NetworkConnectionEvent Connected { get; }

/// <summary>
/// Event fires once a new Client has passed Authentication to the Server.
/// </summary>
NetworkConnectionEvent Authenticated { get; }

/// <summary>
/// Event fires once a Client has Disconnected from the Server.
/// </summary>
NetworkConnectionEvent Disconnected { get; }

UnityEvent Stopped { get; }

/// <summary>
/// This is invoked when a host is started.
/// <para>StartHost has multiple signatures, but they all cause this hook to be called.</para>
/// </summary>
UnityEvent OnStartHost { get; }

/// <summary>
/// This is called when a host is stopped.
/// </summary>
UnityEvent OnStopHost { get; }

void Disconnect();

void AddConnection(INetworkConnection conn);
Expand Down
11 changes: 11 additions & 0 deletions Assets/Mirage/Runtime/IServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ namespace Mirage
{
public interface IServerObjectManager
{

/// <summary>
/// Raised when the client spawns an object
/// </summary>
SpawnEvent Spawned { get; }

/// <summary>
/// Raised when the client unspawns an object
/// </summary>
SpawnEvent UnSpawned { get; }

bool AddPlayerForConnection(INetworkConnection conn, GameObject player);

bool AddPlayerForConnection(INetworkConnection conn, GameObject player, Guid assetId);
Expand Down
13 changes: 10 additions & 3 deletions Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace Mirage
{
Expand Down Expand Up @@ -35,17 +36,23 @@ public class NetworkClient : MonoBehaviour, INetworkClient
/// <summary>
/// Event fires once the Client has connected its Server.
/// </summary>
public NetworkConnectionEvent Connected = new NetworkConnectionEvent();
[FormerlySerializedAs("Connected")]
[SerializeField] NetworkConnectionEvent _connected = new NetworkConnectionEvent();
public NetworkConnectionEvent Connected => _connected;

/// <summary>
/// Event fires after the Client connection has sucessfully been authenticated with its Server.
/// </summary>
public NetworkConnectionEvent Authenticated = new NetworkConnectionEvent();
[FormerlySerializedAs("Authenticated")]
[SerializeField] NetworkConnectionEvent _authenticated = new NetworkConnectionEvent();
public NetworkConnectionEvent Authenticated => _authenticated;

/// <summary>
/// Event fires after the Client has disconnected from its Server and Cleanup has been called.
/// </summary>
public UnityEvent Disconnected = new UnityEvent();
[FormerlySerializedAs("Disconnected")]
[SerializeField] UnityEvent _disconnected = new UnityEvent();
public UnityEvent Disconnected => _disconnected;

/// <summary>
/// The NetworkConnection object this client is using.
Expand Down
17 changes: 12 additions & 5 deletions Assets/Mirage/Runtime/NetworkSceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;

Expand Down Expand Up @@ -33,22 +32,30 @@ public class NetworkSceneManager : MonoBehaviour, INetworkSceneManager
/// <summary>
/// Event fires when the Client starts changing scene.
/// </summary>
public ClientSceneChangeEvent ClientChangeScene = new ClientSceneChangeEvent();
[FormerlySerializedAs("ClientChangeScene")]
[SerializeField] ClientSceneChangeEvent _clientChangeScene = new ClientSceneChangeEvent();
public ClientSceneChangeEvent ClientChangeScene => _clientChangeScene;

/// <summary>
/// Event fires after the Client has completed its scene change.
/// </summary>
public ClientSceneChangeEvent ClientSceneChanged = new ClientSceneChangeEvent();
[FormerlySerializedAs("ClientSceneChanged")]
[SerializeField] ClientSceneChangeEvent _clientSceneChanged = new ClientSceneChangeEvent();
public ClientSceneChangeEvent ClientSceneChanged => _clientSceneChanged;

/// <summary>
/// Event fires before Server changes scene.
/// </summary>
public ClientSceneChangeEvent ServerChangeScene = new ClientSceneChangeEvent();
[FormerlySerializedAs("ServerChangeScene")]
[SerializeField] ClientSceneChangeEvent _serverChangeScene = new ClientSceneChangeEvent();
public ClientSceneChangeEvent ServerChangeScene => _serverChangeScene;

/// <summary>
/// Event fires after Server has completed scene change.
/// </summary>
public ClientSceneChangeEvent ServerSceneChanged = new ClientSceneChangeEvent();
[FormerlySerializedAs("ServerSceneChanged")]
[SerializeField] ClientSceneChangeEvent _serverSceneChanged = new ClientSceneChangeEvent();
public ClientSceneChangeEvent ServerSceneChanged => _serverSceneChanged;

/// <summary>
/// The path of the current network scene.
Expand Down
26 changes: 19 additions & 7 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace Mirage
{
Expand Down Expand Up @@ -45,35 +46,46 @@ public class NetworkServer : MonoBehaviour, INetworkServer
/// <summary>
/// This is invoked when a server is started - including when a host is started.
/// </summary>
public UnityEvent Started = new UnityEvent();
[FormerlySerializedAs("Started")]
[SerializeField] UnityEvent _started = new UnityEvent();
public UnityEvent Started => _started;

/// <summary>
/// Event fires once a new Client has connect to the Server.
/// </summary>
public NetworkConnectionEvent Connected = new NetworkConnectionEvent();
[FormerlySerializedAs("Connected")]
[SerializeField] NetworkConnectionEvent _connected = new NetworkConnectionEvent();
public NetworkConnectionEvent Connected => _connected;

/// <summary>
/// Event fires once a new Client has passed Authentication to the Server.
/// </summary>
public NetworkConnectionEvent Authenticated = new NetworkConnectionEvent();
[FormerlySerializedAs("Authenticated")]
[SerializeField] NetworkConnectionEvent _authenticated = new NetworkConnectionEvent();
public NetworkConnectionEvent Authenticated => _authenticated;

/// <summary>
/// Event fires once a Client has Disconnected from the Server.
/// </summary>
public NetworkConnectionEvent Disconnected = new NetworkConnectionEvent();
[FormerlySerializedAs("Disconnected")]
[SerializeField] NetworkConnectionEvent _disconnected = new NetworkConnectionEvent();
public NetworkConnectionEvent Disconnected => _disconnected;

public UnityEvent Stopped = new UnityEvent();
[SerializeField] UnityEvent _stopped = new UnityEvent();
public UnityEvent Stopped => _stopped;

/// <summary>
/// This is invoked when a host is started.
/// <para>StartHost has multiple signatures, but they all cause this hook to be called.</para>
/// </summary>
public UnityEvent OnStartHost = new UnityEvent();
[SerializeField] UnityEvent _onStartHost = new UnityEvent();
public UnityEvent OnStartHost => _onStartHost;

/// <summary>
/// This is called when a host is stopped.
/// </summary>
public UnityEvent OnStopHost = new UnityEvent();
[SerializeField] UnityEvent _onStopHost = new UnityEvent();
public UnityEvent OnStopHost => _onStopHost;

/// <summary>
/// The connection to the host mode client (if any).
Expand Down
12 changes: 6 additions & 6 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using Mirage.RemoteCalls;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace Mirage
Expand All @@ -28,19 +27,20 @@ public class ServerObjectManager : MonoBehaviour, IServerObjectManager, IObjectL
[FormerlySerializedAs("networkSceneManager")]
public NetworkSceneManager NetworkSceneManager;

[System.Serializable]
public class SpawnEvent : UnityEvent<NetworkIdentity> { }

[Header("Events")]
/// <summary>
/// Raised when the client spawns an object
/// </summary>
public SpawnEvent Spawned = new SpawnEvent();
[FormerlySerializedAs("Spawned")]
[SerializeField] SpawnEvent _spawned = new SpawnEvent();
public SpawnEvent Spawned => _spawned;

/// <summary>
/// Raised when the client unspawns an object
/// </summary>
public SpawnEvent UnSpawned = new SpawnEvent();
[FormerlySerializedAs("UnSpawned")]
[SerializeField] SpawnEvent _unSpawned = new SpawnEvent();
public SpawnEvent UnSpawned => _unSpawned;

uint nextNetworkId = 1;
uint GetNextNetworkId() => nextNetworkId++;
Expand Down
12 changes: 12 additions & 0 deletions Assets/Tests/Runtime/Host/ClientObjectManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,17 @@ public void SpawnSceneObjectTest()

Object.Destroy(prefabObject);
}

[Test]
public void SpawnedNotNullTest()
{
Assert.That(clientObjectManager.Spawned, Is.Not.Null);
}

[Test]
public void UnSpawnedNotNullTest()
{
Assert.That(clientObjectManager.UnSpawned, Is.Not.Null);
}
}
}
18 changes: 18 additions & 0 deletions Assets/Tests/Runtime/Host/NetworkClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,23 @@ public IEnumerator IsLocalClientShutdownTest() => UniTask.ToCoroutine(async () =
await AsyncUtil.WaitUntilWithTimeout(() => !client.IsLocalClient);
});

[Test]
public void ConnectedNotNullTest()
{
Assert.That(client.Connected, Is.Not.Null);
}

[Test]
public void AuthenticatedNotNullTest()
{
Assert.That(client.Authenticated, Is.Not.Null);
}

[Test]
public void DisconnectedNotNullTest()
{
Assert.That(client.Disconnected, Is.Not.Null);
}
}
}
Loading

0 comments on commit 4d1a772

Please sign in to comment.