Преглед на файлове

Handle server crash and disconnecting

Kenneth van Ewijk преди 10 години
родител
ревизия
9b0a982fb1
променени са 3 файла, в които са добавени 32 реда и са изтрити 12 реда
  1. 2 0
      YJMPD-UWP/Model/GameHandler.cs
  2. 30 11
      YJMPD-UWP/Model/NetworkHandler.cs
  3. 0 1
      YJMPD-UWP/Model/PhotoHandler.cs

+ 2 - 0
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)

+ 30 - 11
YJMPD-UWP/Model/NetworkHandler.cs

@@ -44,12 +44,15 @@ namespace YJMPD_UWP.Model
 
         private async Task<string> Read()
         {
+            if (Status != NetworkStatus.CONNECTED)
+                return "error";
+
             return await din.ReadLineAsync();
         }
 
         public async Task<bool> 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)
+                bool running = true;
+
+                while (running)
                 {
-                    Debug.WriteLine("Awaiting incoming data...");
+                    Debug.WriteLine("Awaiting incoming data... " + workItem.Status.ToString());
+
+                    string data = "";
 
-                    if(client.InputStream != null && client.OutputStream != null)
+                    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<bool> 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;
         }
 

+ 0 - 1
YJMPD-UWP/Model/PhotoHandler.cs

@@ -46,7 +46,6 @@ namespace YJMPD_UWP.Model
 
             if (photo == null)
             {
-                // User cancelled photo capture
                 return;
             }