stop connection if we are already connected
This commit is contained in:
@@ -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,16 @@ 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
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -93,11 +107,23 @@ namespace PolyChat
|
|||||||
{
|
{
|
||||||
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);
|
||||||
if(!wasConnected)
|
if (!wasConnected)
|
||||||
UIController.ShowConnectionError("Connection close", IP, $"Connection to {IP} failed...");
|
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()
|
public string getIP()
|
||||||
{
|
{
|
||||||
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
|||||||
@@ -92,5 +92,10 @@ namespace PolyChat.Models
|
|||||||
{
|
{
|
||||||
return Connected;
|
return Connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string getIP()
|
||||||
|
{
|
||||||
|
return IP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user