From 0cb36a24d5752b3a887517e8be7fd57e8d1271a6 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Tue, 21 Sep 2021 21:06:08 +0200 Subject: [PATCH] Some cleanups --- .../IO/TestSceneGameplayEventBroadcaster.cs | 9 +++++---- .../IO/GameplayEventBroadcaster.cs | 18 +++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/osu.Game.Rulesets.Sentakki.Tests/IO/TestSceneGameplayEventBroadcaster.cs b/osu.Game.Rulesets.Sentakki.Tests/IO/TestSceneGameplayEventBroadcaster.cs index 0b2a715e3..e372e50cb 100644 --- a/osu.Game.Rulesets.Sentakki.Tests/IO/TestSceneGameplayEventBroadcaster.cs +++ b/osu.Game.Rulesets.Sentakki.Tests/IO/TestSceneGameplayEventBroadcaster.cs @@ -32,6 +32,7 @@ public TestSceneGameplayEventBroadcaster() AddStep("Send message 3", () => broadcaster.Broadcast(new TransmissionData(TransmissionData.InfoType.Miss, 3))); AddAssert("Client received message 3", () => text.Text == new TransmissionData(TransmissionData.InfoType.Miss, 3).ToString()); AddStep("Dispose broadcaster", () => broadcaster.Dispose()); + AddStep("Dispose client", () => client?.Dispose()); } protected override void Dispose(bool isDisposing) @@ -42,15 +43,15 @@ protected override void Dispose(bool isDisposing) private class TestBroadcastClient : IDisposable { - private NamedPipeClientStream pipeServer; + private readonly NamedPipeClientStream pipeServer; - private SpriteText text; + private readonly SpriteText text; private bool running = true; public bool IsClientConnected => pipeServer.IsConnected; - private Thread readThread; + private readonly Thread readThread; public TestBroadcastClient(SpriteText outputText) { @@ -63,7 +64,7 @@ public TestBroadcastClient(SpriteText outputText) readThread.Start(); } - private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); private CancellationToken cancellationToken => cancellationTokenSource.Token; private void clientLoop() diff --git a/osu.Game.Rulesets.Sentakki/IO/GameplayEventBroadcaster.cs b/osu.Game.Rulesets.Sentakki/IO/GameplayEventBroadcaster.cs index 95432ab8a..b6ccdbe72 100644 --- a/osu.Game.Rulesets.Sentakki/IO/GameplayEventBroadcaster.cs +++ b/osu.Game.Rulesets.Sentakki/IO/GameplayEventBroadcaster.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.IO.Pipes; namespace osu.Game.Rulesets.Sentakki.IO @@ -35,18 +36,16 @@ private void waitForConnectionCallBack(IAsyncResult result) { pipeServer.EndWaitForConnection(result); isWaitingForClient = false; - - if (queuedData != TransmissionData.Empty) - Broadcast(queuedData); } - catch + catch (IOException) { - // If the pipe is closed before a client ever connects, - // EndWaitForConnection() will throw an exception. - - // If we are in here that is probably the case so just return. + // The server has been disposed, abort wait return; } + + if (queuedData != TransmissionData.Empty) + Broadcast(queuedData); + } public void Broadcast(TransmissionData packet) @@ -66,8 +65,9 @@ private bool connectionValid() if (isWaitingForClient) return false; try { pipeServer.WriteByte(0); } - catch + catch (IOException) { + // The client has suddenly disconnected, we must disconnect on our end, and wait for a new connection. pipeServer.Disconnect(); onClientDisconnected();