From: Forest Date: Thu, 9 Aug 2018 18:29:04 +0000 (-0700) Subject: Make disconnect send synchronous so it's more likely to actually send X-Git-Tag: 1.0.0~81 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=48ffc3761a6e3168ac1520b95b1739bd05666412;p=rhonda%2Fimpostor.hazel.git Make disconnect send synchronous so it's more likely to actually send --- diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index 5b59197..05f4513 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -225,7 +225,12 @@ namespace Hazel /// if there is a problem connecting. /// public abstract void ConnectAsync(byte[] bytes = null, int timeout = 5000); - + + /// + /// Sends a disconnect message to the end point. + /// + public abstract void SendDisconnect(); + /// /// Invokes the DataReceived event. /// diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs index 7aef3e4..21f2f45 100644 --- a/Hazel/NetworkConnection.cs +++ b/Hazel/NetworkConnection.cs @@ -26,5 +26,10 @@ namespace Hazel /// The IPMode the client is connected using. /// public IPMode IPMode { get; protected set; } + + public long GetIP4Address() + { + return ((IPEndPoint)this.RemoteEndPoint).Address.Address; + } } } diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 89f408c..32fa7c3 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -109,6 +109,40 @@ namespace Hazel.Udp } } + /// + protected override void WriteBytesToConnectionSync(byte[] bytes, int length) + { + InvokeDataSentRaw(bytes, length); + + lock (stateLock) + { + if (State != ConnectionState.Connected && State != ConnectionState.Connecting) + throw new InvalidOperationException("Could not send data as this Connection is not connected and is not connecting. Did you disconnect?"); + } + + try + { + socket.SendTo( + bytes, + 0, + length, + SocketFlags.None, + RemoteEndPoint + ); + } + catch (ObjectDisposedException) + { + //User probably called Disconnect in between this method starting and here so report the issue + throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); + } + catch (SocketException e) + { + HazelException he = new HazelException("Could not send data as a SocketException occured.", e); + HandleDisconnect(he); + throw he; + } + } + /// public override void Connect(byte[] bytes = null, int timeout = 5000) { diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index f705bff..d18c03e 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -29,6 +29,12 @@ namespace Hazel.Udp /// The bytes to write. protected abstract void WriteBytesToConnection(byte[] bytes, int length); + /// + /// Writes the given bytes to the connection synchronously. + /// + /// The bytes to write. + protected abstract void WriteBytesToConnectionSync(byte[] bytes, int length); + /// public override void Send(MessageWriter msg) { @@ -282,9 +288,9 @@ namespace Hazel.Udp /// /// Sends a disconnect message to the end point. /// - protected void SendDisconnect() + public override void SendDisconnect() { - HandleSend(new byte[0], (byte)UdpSendOption.Disconnect); //TODO Should disconnect wait for an ack? + WriteBytesToConnectionSync(new byte[] { (byte)UdpSendOption.Disconnect }, 1); } /// diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index e1cacd6..26d95ee 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -216,6 +216,34 @@ namespace Hazel.Udp } } + /// + /// Sends data from the listener socket. + /// + /// The bytes to send. + /// The endpoint to send to. + internal void SendDataSync(byte[] bytes, int length, EndPoint endPoint) + { + try + { + listener.SendTo( + bytes, + 0, + length, + SocketFlags.None, + endPoint + ); + } + catch (SocketException e) + { + throw new HazelException("Could not send data as a SocketException occured.", e); + } + catch (ObjectDisposedException) + { + //Keep alive timer probably ran, ignore + return; + } + } + /// /// Removes a virtual connection from the list. /// diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index 3bfae30..85b6e22 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -58,6 +58,20 @@ namespace Hazel.Udp Listener.SendData(bytes, length, RemoteEndPoint); } + /// + protected override void WriteBytesToConnectionSync(byte[] bytes, int length) + { + InvokeDataSentRaw(bytes, length); + + lock (stateLock) + { + if (State != ConnectionState.Connected) + throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); + } + + Listener.SendDataSync(bytes, length, RemoteEndPoint); + } + /// /// /// This will always throw a HazelException.