Handle server crash and disconnecting

This commit is contained in:
2016-01-19 22:39:58 +01:00
parent 6f090062e3
commit 9b0a982fb1
3 changed files with 33 additions and 13 deletions
+2
View File
@@ -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)
+31 -12
View File
@@ -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)
{
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<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;
}
-1
View File
@@ -46,7 +46,6 @@ namespace YJMPD_UWP.Model
if (photo == null)
{
// User cancelled photo capture
return;
}