Skip to content

Commit

Permalink
fix: closing socket on application quit
Browse files Browse the repository at this point in the history
Makes sure a disconnect message is sent if applcation is closed. this will stop failed sends from remote connection
  • Loading branch information
James-Frowen committed May 24, 2021
1 parent 4ed12ba commit c37fe7d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
18 changes: 16 additions & 2 deletions Assets/Mirage/Runtime/SocketLayer/Peer.cs
Expand Up @@ -73,8 +73,17 @@ public Peer(ISocket socket, IDataHandler dataHandler, Config config = null, ILog

connectKeyValidator = new ConnectKeyValidator();
bufferPool = new BufferPool(this.config.Mtu, this.config.BufferPoolStartSize, this.config.BufferPoolMaxSize, this.logger);

Application.quitting += Application_quitting;
}

private void Application_quitting()
{
// make sure peer closes itself when applications closes.
// this will make sure that disconnect Command is sent before applications closes
if (active)
Close();
}

public void Bind(EndPoint endPoint)
{
Expand All @@ -100,8 +109,13 @@ public IConnection Connect(EndPoint endPoint)

public void Close()
{
if (!active) throw new InvalidOperationException("Peer is not active");
if (!active)
{
if (logger.IsLogTypeAllowed(LogType.Warning)) logger.Log(LogType.Warning, "Peer is not active");
return;
}
active = false;
Application.quitting -= Application_quitting;

// send disconnect messages
foreach (Connection conn in connections.Values)
Expand Down Expand Up @@ -474,7 +488,7 @@ internal void OnConnectionDisconnected(Connection connection, DisconnectReason r

internal void FailedToConnect(Connection connection, RejectReason reason)
{
if (logger.IsLogTypeAllowed(LogType.Log)) logger.Log($"Connection Failed to connect: {reason}");
if (logger.IsLogTypeAllowed(LogType.Warning)) logger.Log(LogType.Warning, $"Connection Failed to connect: {reason}");

RemoveConnection(connection);

Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/SocketLayer/ByteUtilsTest.cs
Expand Up @@ -101,7 +101,7 @@ public void UlongShift()
[Test]
[Description("this test should fail")]
[Explicit("c# only lets you bit shift by max of 63. It only takes first 6 bits, so will drop any extra bits and try to shift anyway")]
public void UlongShift2()
public void UlongShift_ShouldFail()
{
ulong value = 0xfUL;
ulong shifted = value << 64;
Expand Down
9 changes: 4 additions & 5 deletions Assets/Tests/SocketLayer/PeerTest.cs
Expand Up @@ -3,6 +3,7 @@
using NSubstitute;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace Mirage.SocketLayer.Tests.PeerTests
{
Expand Down Expand Up @@ -49,11 +50,9 @@ public void DoesNotThrowIfLoggerIsNull()
[Test]
public void CloseShouldThrowIfNoActive()
{
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() =>
{
peer.Close();
});
Assert.That(exception, Has.Message.EqualTo("Peer is not active"));
LogAssert.Expect(LogType.Warning, "Peer is not active");
peer.Close();
LogAssert.NoUnexpectedReceived();
}

[Test]
Expand Down

0 comments on commit c37fe7d

Please sign in to comment.