stop connection if we are already connected
This commit is contained in:
@@ -37,7 +37,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()
|
||||
@@ -56,9 +63,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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -90,9 +104,22 @@ namespace PolyChat
|
||||
{
|
||||
Connections[IP].Close();
|
||||
Connections.Remove(IP);
|
||||
CloseChatUI(IP,wasConnected);
|
||||
}
|
||||
|
||||
private void CloseChatUI(string IP, bool wasConnected = true)
|
||||
{
|
||||
UIController.OnChatPartnerDeleted(IP);
|
||||
string heading = wasConnected ? "Connection Closed" : "Connection Failed";
|
||||
UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed...");
|
||||
if (!wasConnected)
|
||||
UIController.ShowConnectionError(IP, heading, $"Connecting to {IP} failed...");
|
||||
}
|
||||
|
||||
private bool isInConnections(string IP)
|
||||
{
|
||||
if(Connections.ContainsKey(IP))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public string getIP()
|
||||
|
||||
Reference in New Issue
Block a user