Merged controller Dialog Heading

This commit is contained in:
Patrick Hellebrand
2021-09-23 10:22:49 +02:00
2 changed files with 44 additions and 8 deletions

View File

@@ -39,6 +39,13 @@ namespace PolyChat
public void Connect(string ip) public void Connect(string ip)
{ {
Debug.WriteLine("--- Controller.Connect ---"); Debug.WriteLine("--- Controller.Connect ---");
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)); Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat));
} }
@@ -58,9 +65,17 @@ namespace PolyChat
{ {
Debug.WriteLine("--- initial packet received ---"); Debug.WriteLine("--- initial packet received ---");
string ForeignIp = data[0].ToString(); string ForeignIp = data[0].ToString();
//Todo deserialize inital packet and extract ip address 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!
CloseChatUI(ForeignIp);
}
else
{
Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat)); Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat));
UIController.OnIncomingConnection(ForeignIp); UIController.OnIncomingConnection(ForeignIp);
}
}); });
}); });
} }
@@ -89,15 +104,31 @@ namespace PolyChat
} }
public void CloseChat(string IP, bool wasConnected = true) public void CloseChat(string IP, bool wasConnected = true)
{
Debug.WriteLine($"Deleting connection with IP:{IP}");
if (IP != null && Connections.ContainsKey(IP))
{ {
Connections[IP].Close(); Connections[IP].Close();
Connections.Remove(IP); Connections.Remove(IP);
}
CloseChatUI(IP,wasConnected);
}
private void CloseChatUI(string IP, bool wasConnected = true)
{
UIController.OnChatPartnerDeleted(IP); UIController.OnChatPartnerDeleted(IP);
string heading = wasConnected ? "Connection Closed" : "Connection Failed"; string heading = wasConnected ? "Connection Closed" : "Connection Failed";
UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed..."); UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed...");
} }
public string getIP() private bool isInConnections(string IP)
{
if(Connections.ContainsKey(IP))
return true;
return false;
}
public static string getIP()
{ {
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] addrList = ipEntry.AddressList; IPAddress[] addrList = ipEntry.AddressList;

View File

@@ -58,7 +58,7 @@ namespace PolyChat.Models
private void OnConnect() private void OnConnect()
{ {
Debug.WriteLine("--- Sending initial packet to server ---"); Debug.WriteLine("--- Sending initial packet to server ---");
Client.Emit("initial", IP); Client.Emit("initial", Controller.getIP());
Debug.WriteLine("--- Connection successfull ---"); Debug.WriteLine("--- Connection successfull ---");
Connected = true; Connected = true;
} }
@@ -92,5 +92,10 @@ namespace PolyChat.Models
{ {
return Connected; return Connected;
} }
public string getIP()
{
return IP;
}
} }
} }