Skip to content

Commit

Permalink
Server Disconnect is now an event not a message (#121)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: OnServerDisconnect is gone
  • Loading branch information
paulpach committed Mar 26, 2020
1 parent 44a538f commit 82ebd71
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 107 deletions.
21 changes: 2 additions & 19 deletions Assets/Mirror/Components/NetworkRoomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void Awake()
client.Authenticated.AddListener(OnAuthenticated);
server.Stopped.AddListener(Stopped);
client.Disconnected.AddListener(Disconnected);
server.Disconnected.AddListener(OnServerDisconnect);
}

public override void Start()
Expand Down Expand Up @@ -253,7 +254,7 @@ public override void OnServerConnect(NetworkConnection conn)
/// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerDisconnect(NetworkConnection conn)
public void OnServerDisconnect(NetworkConnection conn)
{
if (conn.identity != null)
{
Expand All @@ -274,7 +275,6 @@ public override void OnServerDisconnect(NetworkConnection conn)
if (SceneManager.GetActiveScene().name == RoomScene)
RecalculateRoomPlayerIndices();

base.OnServerDisconnect(conn);
OnRoomServerDisconnect(conn);
}

Expand Down Expand Up @@ -426,17 +426,6 @@ public void OnAuthenticated(NetworkConnection conn)
CallOnClientEnterRoom();
}

/// <summary>
/// Called on clients when disconnected from a server.
/// <para>This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects.</para>
/// </summary>
/// <param name="conn">Connection to the server.</param>
public override void OnClientDisconnect(NetworkConnection conn)
{
OnRoomClientDisconnect(conn);
base.OnClientDisconnect(conn);
}

/// <summary>
/// This is invoked when a client is stopped.
/// </summary>
Expand Down Expand Up @@ -598,12 +587,6 @@ public virtual void OnRoomServerPlayersReady()
/// <param name="conn">The connection that connected.</param>
public virtual void OnRoomClientConnect(NetworkConnection conn) { }

/// <summary>
/// This is called on the client when disconnected from a server.
/// </summary>
/// <param name="conn">The connection that disconnected.</param>
public virtual void OnRoomClientDisconnect(NetworkConnection conn) { }

/// <summary>
/// This is called on the client when the client stops.
/// </summary>
Expand Down
10 changes: 6 additions & 4 deletions Assets/Mirror/Examples/Pong/Scripts/NetworkManagerPong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class NetworkManagerPong : NetworkManager
public Transform rightRacketSpawn;
GameObject ball;

public void Awake()
{
server.Disconnected.AddListener(OnServerDisconnect);
}

public override void OnServerAddPlayer(NetworkConnection conn)
{
// add player at correct spawn position
Expand All @@ -27,14 +32,11 @@ public override void OnServerAddPlayer(NetworkConnection conn)
}
}

public override void OnServerDisconnect(NetworkConnection conn)
public void OnServerDisconnect(NetworkConnection conn)
{
// destroy ball
if (ball != null)
server.Destroy(ball);

// call base functionality (actually destroys the player)
base.OnServerDisconnect(conn);
}
}
}
2 changes: 1 addition & 1 deletion Assets/Mirror/Runtime/LocalConnections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal void DisconnectInternal()

// TODO: this does not work if there is no player yet
if (identity != null)
identity.client.HandleClientDisconnect(this);
identity.client.HandleClientDisconnect();
}

/// <summary>
Expand Down
7 changes: 0 additions & 7 deletions Assets/Mirror/Runtime/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ public struct RemovePlayerMessage : IMessageBase
public void Serialize(NetworkWriter writer) { /* nothing to serialize */ }
}

public struct DisconnectMessage : IMessageBase
{
public void Deserialize(NetworkReader reader) { /* nothing to serialize */ }

public void Serialize(NetworkWriter writer) { /* nothing to serialize */ }
}

public struct SceneMessage : IMessageBase
{
public string sceneName;
Expand Down
17 changes: 7 additions & 10 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ void OnError(Exception exception)
void OnDisconnected()
{
connectState = ConnectState.Disconnected;
connection?.InvokeHandler(new DisconnectMessage(), -1);
HandleClientDisconnect(connection);
HandleClientDisconnect();
}

/// <summary>
Expand Down Expand Up @@ -279,7 +278,7 @@ public void Disconnect()
{
if (isConnected)
{
hostServer.localConnection.Send(new DisconnectMessage());
hostServer.Disconnected.Invoke(hostServer.localConnection);
}
hostServer.RemoveLocalConnection();
}
Expand All @@ -291,7 +290,7 @@ public void Disconnect()
RemoveTransportHandlers();
}
}
HandleClientDisconnect(connection);
HandleClientDisconnect();
}

void RemoveTransportHandlers()
Expand Down Expand Up @@ -523,13 +522,11 @@ internal void InternalAddPlayer(NetworkIdentity identity)
}
}

internal void HandleClientDisconnect(NetworkConnection conn)
internal void HandleClientDisconnect()
{
if (connection == conn && ready)
{
ready = false;
connection = null;
}
DestroyAllClientObjects();
ready = false;
connection = null;

Disconnected.Invoke();
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Runtime/NetworkConnectionToServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override void Disconnect()
isReady = false;
// TODO: This does not work if there is no player yet
if (identity != null)
identity.client.HandleClientDisconnect(this);
identity.client.HandleClientDisconnect();
Transport.activeTransport.ClientDisconnect();
}
}
Expand Down
44 changes: 0 additions & 44 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ public void StopServer()
{
ServerChangeScene(offlineScene);
}
CleanupNetworkIdentities();

startPositionIndex = 0;
}
Expand Down Expand Up @@ -500,7 +499,6 @@ public void StopClient()
ClientChangeScene(offlineScene, SceneOperation.Normal);
}

CleanupNetworkIdentities();
}

/// <summary>
Expand Down Expand Up @@ -581,14 +579,6 @@ void Initialize()
client.Authenticated.AddListener(OnClientAuthenticated);
}

void CleanupNetworkIdentities()
{
foreach (NetworkIdentity identity in Resources.FindObjectsOfTypeAll<NetworkIdentity>())
{
identity.MarkForReset();
}
}

/// <summary>
/// virtual so that inheriting classes' OnDestroy() can call base.OnDestroy() too
/// </summary>
Expand Down Expand Up @@ -903,7 +893,6 @@ public void UnRegisterStartPosition(Transform start)

void RegisterServerMessages(NetworkConnection connection)
{
connection.RegisterHandler<NetworkConnectionToClient, DisconnectMessage>(OnServerDisconnectInternal, false);
connection.RegisterHandler<NetworkConnectionToClient, ReadyMessage>(OnServerReadyMessageInternal);
connection.RegisterHandler<NetworkConnectionToClient, AddPlayerMessage>(OnServerAddPlayerInternal);
connection.RegisterHandler<NetworkConnectionToClient, RemovePlayerMessage>(OnServerRemovePlayerMessageInternal);
Expand All @@ -928,12 +917,6 @@ void OnServerAuthenticated(NetworkConnectionToClient conn)
OnServerConnect(conn);
}

void OnServerDisconnectInternal(NetworkConnection conn, DisconnectMessage msg)
{
if (LogFilter.Debug) Debug.Log("NetworkManager.OnServerDisconnectInternal");
OnServerDisconnect(conn);
}

void OnServerReadyMessageInternal(NetworkConnection conn, ReadyMessage msg)
{
if (LogFilter.Debug) Debug.Log("NetworkManager.OnServerReadyMessageInternal");
Expand Down Expand Up @@ -988,7 +971,6 @@ void OnServerErrorInternal(NetworkConnection conn, ErrorMessage msg)

void RegisterClientMessages(NetworkConnection connection)
{
connection.RegisterHandler<NetworkConnectionToServer, DisconnectMessage>(OnClientDisconnectInternal, false);
connection.RegisterHandler<NetworkConnectionToServer, NotReadyMessage>(OnClientNotReadyMessageInternal);
connection.RegisterHandler<NetworkConnectionToServer, ErrorMessage>(OnClientErrorInternal, false);
connection.RegisterHandler<NetworkConnectionToServer, SceneMessage>(OnClientSceneInternal, false);
Expand All @@ -1015,12 +997,6 @@ void OnClientAuthenticated(NetworkConnectionToServer conn)
}
}

void OnClientDisconnectInternal(NetworkConnection conn, DisconnectMessage msg)
{
if (LogFilter.Debug) Debug.Log("NetworkManager.OnClientDisconnectInternal");
OnClientDisconnect(conn);
}

void OnClientNotReadyMessageInternal(NetworkConnection conn, NotReadyMessage msg)
{
if (LogFilter.Debug) Debug.Log("NetworkManager.OnClientNotReadyMessageInternal");
Expand Down Expand Up @@ -1058,16 +1034,6 @@ void OnClientSceneInternal(NetworkConnection conn, SceneMessage msg)
/// <param name="conn">Connection from client.</param>
public virtual void OnServerConnect(NetworkConnection conn) { }

/// <summary>
/// Called on the server when a client disconnects.
/// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public virtual void OnServerDisconnect(NetworkConnection conn)
{
server.DestroyPlayerForConnection(conn);
if (LogFilter.Debug) Debug.Log("OnServerDisconnect: Client disconnected.");
}

/// <summary>
/// Called on the server when a client is ready.
Expand Down Expand Up @@ -1183,16 +1149,6 @@ public void OnAuthenticated(NetworkConnectionToServer conn)
}
}

/// <summary>
/// Called on clients when disconnected from a server.
/// <para>This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects.</para>
/// </summary>
/// <param name="conn">Connection to the server.</param>
public virtual void OnClientDisconnect(NetworkConnection conn)
{
StopClient();
}

/// <summary>
/// Called on clients when a network error occurs.
/// </summary>
Expand Down
15 changes: 6 additions & 9 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class NetworkServer : MonoBehaviour

public NetworkConnectionEvent Connected = new NetworkConnectionEvent();
public NetworkConnectionEvent Authenticated = new NetworkConnectionEvent();
public NetworkConnectionEvent Disconnected = new NetworkConnectionEvent();
public UnityEvent Stopped = new UnityEvent();

[Header("Authentication")]
Expand Down Expand Up @@ -337,12 +338,12 @@ public void DisconnectAll()
/// </summary>
public void DisconnectAllConnections()
{
foreach (NetworkConnection conn in connections.Values)
foreach (NetworkConnectionToClient conn in connections.Values)
{
conn.Disconnect();
// call OnDisconnected unless local player in host mode
if (conn.connectionId != 0)
OnDisconnected(conn);
Disconnected.Invoke(conn);
conn.Dispose();
}
connections.Clear();
Expand Down Expand Up @@ -428,14 +429,10 @@ void OnDisconnected(int connectionId)
RemoveConnection(connectionId);
if (LogFilter.Debug) Debug.Log("Server lost client:" + connectionId);

OnDisconnected(conn);
}
}
DestroyPlayerForConnection(conn);

void OnDisconnected(NetworkConnection conn)
{
conn.InvokeHandler(new DisconnectMessage(), -1);
if (LogFilter.Debug) Debug.Log("Server lost client:" + conn);
Disconnected.Invoke(conn);
}
}

internal void OnAuthenticated(NetworkConnectionToClient conn)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Tests/Editor/MessagePackerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void UnpackWrongMessage()

Assert.Throws<FormatException>(() =>
{
DisconnectMessage unpacked = MessagePacker.Unpack<DisconnectMessage>(data);
AddPlayerMessage unpacked = MessagePacker.Unpack<AddPlayerMessage>(data);
});
}

Expand Down
6 changes: 0 additions & 6 deletions Assets/Mirror/Tests/Editor/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ private void TestSerializeDeserialize<T>(T message) where T : IMessageBase, new(
Assert.That(fresh, Is.EqualTo(message));
}

[Test]
public void DisconnectMessageTest()
{
TestSerializeDeserialize(new DisconnectMessage());
}

[Test]
public void ErrorMessageTest()
{
Expand Down
8 changes: 3 additions & 5 deletions Assets/Mirror/Tests/Runtime/NetworkServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,13 @@ public void MaxConnectionsTest()
Assert.That(server.connections.Count, Is.EqualTo(1));
}

/*

[Test]
public void DisconnectMessageHandlerTest()
{
// message handlers
bool disconnectCalled = false;
server.RegisterHandler<DisconnectMessage>((conn, msg) => { disconnectCalled = true; }, false);
server.RegisterHandler<ErrorMessage>((conn, msg) => {}, false);
server.Disconnected.AddListener(conn => { disconnectCalled = true; });

// listen
server.Listen();
Expand All @@ -172,7 +171,7 @@ public void DisconnectMessageHandlerTest()
// disconnect
Transport.activeTransport.OnServerDisconnected.Invoke(42);
Assert.That(disconnectCalled, Is.True);
}*/
}

[Test]
public void ConnectionsDictTest()
Expand Down Expand Up @@ -595,7 +594,6 @@ public void RegisterUnregisterClearHandlerTest()
// unregister second handler via ClearHandlers to test that one too. send, should fail
connection.ClearHandlers();
// (only add this one to avoid disconnect error)
connection.RegisterHandler<DisconnectMessage>(msg => { }, false);
writer = new NetworkWriter();
MessagePacker.Pack(new TestMessage(), writer);
// log error messages are expected
Expand Down

0 comments on commit 82ebd71

Please sign in to comment.