Skip to content

Commit

Permalink
fix: AsyncTcp now exits normally when client disconnects (#141)
Browse files Browse the repository at this point in the history
* Transport should return normally on disconnect

* fix bug disconnecting
  • Loading branch information
paulpach committed Mar 31, 2020
1 parent c762213 commit 8896c4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Assets/Mirror/Runtime/Transport/AsyncTcp/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,29 @@ public TcpConnection(TcpClient client)
#region Receiving
public async Task<bool> ReceiveAsync(MemoryStream buffer)
{
long position = buffer.Position;
try
{
buffer.SetLength(0);
long position = buffer.Position;

// read message size
if (!await ReadExactlyAsync(stream, buffer, 4))
return false;
// read message size
if (!await ReadExactlyAsync(stream, buffer, 4))
return false;

// rewind so that we read it
buffer.Position = position;
// rewind so that we read it
buffer.Position = position;

int length = ReadInt(buffer);
int length = ReadInt(buffer);

// now read the message
buffer.Position = position;
// now read the message
buffer.Position = position;

return await ReadExactlyAsync(stream, buffer, length);
return await ReadExactlyAsync(stream, buffer, length);
}
catch (ObjectDisposedException)
{
return false;
}
}


Expand Down
16 changes: 16 additions & 0 deletions Assets/Mirror/Tests/Editor/Transport/AsyncTcpTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ public IEnumerator DisconnectClientTest()
});
}

[UnityTest]
public IEnumerator DisconnectClientTest2()
{
return RunAsync(async () =>
{
await transport.ListenAsync();
IConnection clientConnection = await transport.ConnectAsync(new Uri("tcp4://localhost:8798"));
IConnection serverConnection = await transport.AcceptAsync();
clientConnection.Disconnect();
var stream = new MemoryStream();
Assert.That(await clientConnection.ReceiveAsync(stream), Is.False);
});
}

[Test]
public void TestServerUri()
{
Expand Down

0 comments on commit 8896c4a

Please sign in to comment.