diff --git a/YJMPD-UWP/Model/GameHandler.cs b/YJMPD-UWP/Model/GameHandler.cs index 82f729c..5028aa4 100644 --- a/YJMPD-UWP/Model/GameHandler.cs +++ b/YJMPD-UWP/Model/GameHandler.cs @@ -78,6 +78,8 @@ namespace YJMPD_UWP.Model Selected = false; Players.Clear(); UpdateGamePlayers(null); + + App.Navigate(typeof(MatchView)); } public void UpdatePlayer(string username, double pointstotal, double points) diff --git a/YJMPD-UWP/Model/NetworkHandler.cs b/YJMPD-UWP/Model/NetworkHandler.cs index 42fb893..74d1f78 100644 --- a/YJMPD-UWP/Model/NetworkHandler.cs +++ b/YJMPD-UWP/Model/NetworkHandler.cs @@ -44,12 +44,15 @@ namespace YJMPD_UWP.Model private async Task Read() { + if (Status != NetworkStatus.CONNECTED) + return "error"; + return await din.ReadLineAsync(); } public async Task Write(string data) { - if (client.InputStream == null || client.OutputStream == null) + if (Status != NetworkStatus.CONNECTED) return false; dout.WriteString(data + Environment.NewLine); @@ -86,20 +89,30 @@ namespace YJMPD_UWP.Model BackgroundReader = Windows.System.Threading.ThreadPool.RunAsync(async (workItem) => { - while (workItem.Status != AsyncStatus.Canceled) - { - Debug.WriteLine("Awaiting incoming data..."); + bool running = true; - if(client.InputStream != null && client.OutputStream != null) + while (running) + { + Debug.WriteLine("Awaiting incoming data... " + workItem.Status.ToString()); + + string data = ""; + + try { - string data = await Read(); - HandleMessage(data); - await Task.Delay(TimeSpan.FromMilliseconds(50)); + data = await Read(); } - else + catch (Exception) + { + data = null; + } + + if (data == null) { Disconnect(); - workItem.Cancel(); + running = false; + } + else { + HandleMessage(data); } } }); @@ -109,19 +122,25 @@ namespace YJMPD_UWP.Model public async Task Disconnect() { - if(App.Game.Status != GameHandler.GameStatus.STOPPED) - await App.Api.LeaveGame(); + Debug.WriteLine("Disconnecting..."); + + if (App.Game.Status != GameHandler.GameStatus.STOPPED) + await App.Game.Stop(); UpdateNetworkStatus(NetworkStatus.DISCONNECTED); if (BackgroundReader != null) + { BackgroundReader.Cancel(); + BackgroundReader = null; + } din.Dispose(); dout.Dispose(); client.Dispose(); + Debug.WriteLine("Disconnected..."); return true; } diff --git a/YJMPD-UWP/Model/PhotoHandler.cs b/YJMPD-UWP/Model/PhotoHandler.cs index 5383a00..8dccad1 100644 --- a/YJMPD-UWP/Model/PhotoHandler.cs +++ b/YJMPD-UWP/Model/PhotoHandler.cs @@ -46,7 +46,6 @@ namespace YJMPD_UWP.Model if (photo == null) { - // User cancelled photo capture return; }