dis-/connect bugfix

This commit is contained in:
waltem01 2022-07-01 21:40:25 +02:00
parent 066422e796
commit 8a6cc04caf

View File

@ -21,8 +21,6 @@ public static class Connection {
port = _port;
buffer = new byte[1024];
messageEvents = new List<MessageEvent>();
disconnectMethod = new Action<Socket>((other)=>{});
connectMethod = new Action<Socket>((other)=>{});
// Get computers' ipv4 address
ipAddress = "127.0.0.1";
@ -72,6 +70,9 @@ public static class Connection {
self.Connect(new IPEndPoint(IPAddress.Parse(ipAddr), _port));
self.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), self);
if (connectMethod != null)
connectMethod.Invoke(self);
}
public static void ClientListen() {
@ -110,12 +111,9 @@ public static class Connection {
// Begin listening for more messages
other.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), other);
} catch (Exception) {
// Potentially detected Disconnect
if (other.Connected) {
// Close client connection
disconnectMethod.Invoke(other);
other.Close();
}
// Close client connection
disconnectMethod.Invoke(other);
other.Close();
}
}
@ -127,7 +125,8 @@ public static class Connection {
other.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), other);
if (self == null)
self = other;
connectMethod.Invoke(other);
if (connectMethod != null)
connectMethod.Invoke(other);
}
// Handle sending messages