From 92939c82306dd835f26c62ad682a708fb49ef859 Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Thu, 23 Sep 2021 09:12:03 +0200 Subject: [PATCH 1/3] stop connection if we are already connected --- PolyChat/Controller.cs | 36 ++++++++++++++++++++++++++++++----- PolyChat/Models/Connection.cs | 5 +++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index b272f6a..87c9a9e 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -39,7 +39,14 @@ namespace PolyChat public void Connect(string ip) { Debug.WriteLine("--- Controller.Connect ---"); - Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat)); + if (isInConnections(ip)) + { + Debug.WriteLine("---- We have an active connection to this client. ABORT! ----"); + CloseChatUI(ip); + //Todo show error! + } + else + Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat)); } private void Serve() @@ -58,9 +65,16 @@ 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), CloseChat)); - UIController.OnIncomingConnection(ForeignIp); + if (isInConnections(ForeignIp)) + { + Debug.WriteLine("---- We have an active connection to this client. ABORT! ----");//Todo show error! + CloseChatUI(ForeignIp); + } + else + { + Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat)); + UIController.OnIncomingConnection(ForeignIp); + } }); }); } @@ -93,11 +107,23 @@ namespace PolyChat { Connections[IP].Close(); Connections.Remove(IP); + CloseChatUI(IP,wasConnected); + } + + private void CloseChatUI(string IP, bool wasConnected = true) + { UIController.OnChatPartnerDeleted(IP); - if(!wasConnected) + if (!wasConnected) UIController.ShowConnectionError("Connection close", IP, $"Connection to {IP} failed..."); } + private bool isInConnections(string IP) + { + if(Connections.ContainsKey(IP)) + return true; + return false; + } + public string getIP() { IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); diff --git a/PolyChat/Models/Connection.cs b/PolyChat/Models/Connection.cs index 971e6af..b1d8600 100644 --- a/PolyChat/Models/Connection.cs +++ b/PolyChat/Models/Connection.cs @@ -92,5 +92,10 @@ namespace PolyChat.Models { return Connected; } + + public string getIP() + { + return IP; + } } } From cd26ac11c389da2bd711b967ccfddcb32cdfc8e4 Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Thu, 23 Sep 2021 09:41:56 +0200 Subject: [PATCH 2/3] allow deletion of chats which are not started by me --- PolyChat/Controller.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 87c9a9e..56c1460 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -105,8 +105,12 @@ namespace PolyChat public void CloseChat(string IP, bool wasConnected = true) { - Connections[IP].Close(); - Connections.Remove(IP); + Debug.WriteLine($"Deleting connection with IP:{IP}"); + if (IP != null && Connections.ContainsKey(IP)) + { + Connections[IP].Close(); + Connections.Remove(IP); + } CloseChatUI(IP,wasConnected); } From c22987d86f3eb697a7bd78d027bd2f2b9288c393 Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Thu, 23 Sep 2021 09:52:54 +0200 Subject: [PATCH 3/3] fix ip in intial packet (send own ip, not foreign) --- PolyChat/Controller.cs | 3 ++- PolyChat/Models/Connection.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 56c1460..9a187b3 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -65,6 +65,7 @@ namespace PolyChat { Debug.WriteLine("--- initial packet received ---"); string ForeignIp = data[0].ToString(); + Debug.WriteLine($"--- this ip was in the inital packet: {ForeignIp} ---"); if (isInConnections(ForeignIp)) { Debug.WriteLine("---- We have an active connection to this client. ABORT! ----");//Todo show error! @@ -128,7 +129,7 @@ namespace PolyChat return false; } - public string getIP() + public static string getIP() { IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] addrList = ipEntry.AddressList; diff --git a/PolyChat/Models/Connection.cs b/PolyChat/Models/Connection.cs index b1d8600..9c985b6 100644 --- a/PolyChat/Models/Connection.cs +++ b/PolyChat/Models/Connection.cs @@ -58,7 +58,7 @@ namespace PolyChat.Models private void OnConnect() { Debug.WriteLine("--- Sending initial packet to server ---"); - Client.Emit("initial", IP); + Client.Emit("initial", Controller.getIP()); Debug.WriteLine("--- Connection successfull ---"); Connected = true; }