add retry logic on disconnect, silently delete chat if disconnected after connect was succesfull

This commit is contained in:
Felix Hartmann (PEA3-Fe-FI)
2021-09-22 15:51:53 +02:00
parent 6f2c442e94
commit e2de9f5917
3 changed files with 45 additions and 13 deletions

View File

@@ -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());