From e2de9f5917aae298b045e10ab54fa1afc5ce2924 Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Wed, 22 Sep 2021 15:51:53 +0200 Subject: [PATCH] add retry logic on disconnect, silently delete chat if disconnected after connect was succesfull --- PolyChat/Controller.cs | 13 +++++++++++-- PolyChat/MainPage.xaml.cs | 33 ++++++++++++++++++++++++++------- PolyChat/Models/Connection.cs | 12 ++++++++---- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 099fada..8a48a6d 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -37,7 +37,7 @@ namespace PolyChat public void Connect(string ip) { Debug.WriteLine("--- Controller.Connect ---"); - Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data))); + Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat)); } private void Serve() @@ -59,7 +59,7 @@ namespace PolyChat Debug.WriteLine("--- initial packet received ---"); string ForeignIp = data[0].ToString(); //Todo deserialize inital packet and extract ip address - Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data))); + Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat)); UIController.OnIncomingConnection(ForeignIp); }); }); @@ -89,6 +89,15 @@ namespace PolyChat else Debug.WriteLine("Undefined: " + data); } + public void CloseChat(string IP, bool wasConnected = true) + { + Connections[IP].Close(); + Connections.Remove(IP); + UIController.OnChatPartnerDeleted(IP); + if(!wasConnected) + UIController.ShowConnectionError(IP, $"Connection to {IP} failed..."); + } + public string getIP() { IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 8f51424..0dc0f43 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -36,15 +36,22 @@ namespace PolyChat updateSendButtonEnabled(); } - public async void ShowConnectionError(string message) + public async void ShowConnectionError(string code, string message) { - ConnectionFailedDialog dialog = new ConnectionFailedDialog(message); - var result = await dialog.ShowAsync(); - if (result == ContentDialogResult.Primary) + await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { - //retry - } - // else abort -> del chat + ConnectionFailedDialog dialog = new ConnectionFailedDialog(message); + var result = await dialog.ShowAsync(); + if (result == ContentDialogResult.Primary) + { + Controller.Connect(code); + Partners.Add(new ChatPartner( + "Connecting...", + code + )); + updateNoChatsPlaceholder(); + } + }); } // EVENTS @@ -113,10 +120,22 @@ namespace PolyChat private void OnDeleteChat(object sender = null, RoutedEventArgs e = null) { + Controller.CloseChat(selectedPartner.Code); Partners.Remove(selectedPartner); updateNoChatsPlaceholder(); updateNoChatSelected(); } + + public async void OnChatPartnerDeleted(string code) + { + await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + Partners.Remove(Partners.First(p => p.Code == code)); + selectedPartner = null; + updateNoChatsPlaceholder(); + updateNoChatSelected(); + }); + } public void OnChatPartnerSelected(object sender, RoutedEventArgs e) { string code = ((RadioButton)sender).Tag.ToString(); diff --git a/PolyChat/Models/Connection.cs b/PolyChat/Models/Connection.cs index 6700330..971e6af 100644 --- a/PolyChat/Models/Connection.cs +++ b/PolyChat/Models/Connection.cs @@ -15,11 +15,13 @@ namespace PolyChat.Models private SocketIOSocket Socket; private bool Connected = false; private readonly string IP; + private Action DeleteConnection; - public Connection(string ip, ushort port, Action onMessage) + public Connection(string ip, ushort port, Action onMessage, Action onClose) { Debug.WriteLine("! CONNECTING TO SERVER !"); IP = ip; + DeleteConnection = onClose; // establish connection Client = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, port)); Client.Connect(); @@ -29,10 +31,11 @@ namespace PolyChat.Models Client.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data)); Client.On("message", (Action) onMessage); } - - public Connection(SocketIOSocket socket, Action onMessage) + + public Connection(SocketIOSocket socket, Action onMessage, Action onClose) { Socket = socket; + DeleteConnection = onClose; Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect); Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data)); Socket.On("message", (Action) onMessage); @@ -62,8 +65,9 @@ namespace PolyChat.Models private void OnDisconnect() { Debug.WriteLine("--- Disconnected! ---"); + Debug.WriteLine($"--- Deleting Connection with IP: {IP}---"); + DeleteConnection(IP, IsConnected()); Connected = false; - Close(); } private void OnError(JToken[] data) {