From: Matthew Endsley Date: Fri, 5 Mar 2021 22:09:07 +0000 (-0800) Subject: Add a synchronous WriteBytesToConnection to handle disconnect packets X-Git-Tag: 1.0.0~19^2 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=416beed4536cc6c168acf40f1e3fc3668e597667;p=rhonda%2Fimpostor.hazel.git Add a synchronous WriteBytesToConnection to handle disconnect packets --- diff --git a/Hazel/Dtls/DtlsUnityConnection.cs b/Hazel/Dtls/DtlsUnityConnection.cs index 6be3b19..0c09e3d 100644 --- a/Hazel/Dtls/DtlsUnityConnection.cs +++ b/Hazel/Dtls/DtlsUnityConnection.cs @@ -274,8 +274,18 @@ namespace Hazel.Dtls this.queuedApplicationData.Clear(); } - /// - protected override void WriteBytesToConnection(byte[] bytes, int length) + /// + /// Request from the application to write data to the DTLS + /// stream. If appropriate, returns a byte span to send to + /// the wire. + /// + /// Plaintext bytes to write + /// Length of the bytes to write + /// + /// Encrypted data to put on the wire if appropriate, + /// otherwise an empty span + /// + private ByteSpan WriteBytesToConnectionInternal(byte[] bytes, int length) { lock (this.syncRoot) { @@ -286,7 +296,7 @@ namespace Hazel.Dtls new ByteSpan(bytes, 0, length).CopyTo(copyOfSpan); this.queuedApplicationData.Add(copyOfSpan); - return; + return ByteSpan.Empty; } // Send any queued application data now @@ -313,7 +323,29 @@ namespace Hazel.Dtls , ref outgoinRecord ); - base.WriteBytesToConnection(packet.GetUnderlyingArray(), packet.Length); + return packet; + } + } + + /// + protected override void WriteBytesToConnection(byte[] bytes, int length) + { + ByteSpan wireData = this.WriteBytesToConnectionInternal(bytes, length); + if (wireData.Length > 0) + { + Debug.Assert(wireData.Offset == 0, "Got a non-zero write data offset"); + base.WriteBytesToConnection(wireData.GetUnderlyingArray(), wireData.Length); + } + } + + /// + protected override void WriteBytesToConnectionSync(byte[] bytes, int length) + { + ByteSpan wireData = this.WriteBytesToConnectionInternal(bytes, length); + if (wireData.Length > 0) + { + Debug.Assert(wireData.Offset == 0, "Got a non-zero write data offset"); + base.WriteBytesToConnectionSync(wireData.GetUnderlyingArray(), wireData.Length); } } diff --git a/Hazel/Udp/UnityUdpClientConnection.cs b/Hazel/Udp/UnityUdpClientConnection.cs index 25eb4db..8309602 100644 --- a/Hazel/Udp/UnityUdpClientConnection.cs +++ b/Hazel/Udp/UnityUdpClientConnection.cs @@ -14,7 +14,6 @@ namespace Hazel.Udp public class UnityUdpClientConnection : UdpConnection { private Socket socket; - private bool sendSynchronously = false; public UnityUdpClientConnection(IPEndPoint remoteEndPoint, IPMode ipMode = IPMode.IPv4) : base() @@ -50,26 +49,40 @@ namespace Hazel.Udp { try { - if (this.sendSynchronously) - { - socket.SendTo( - bytes, - 0, - length, - SocketFlags.None, - EndPoint); - } - else - { - socket.BeginSendTo( - bytes, - 0, - length, - SocketFlags.None, - EndPoint, - HandleSendTo, - null); - } + socket.BeginSendTo( + bytes, + 0, + length, + SocketFlags.None, + EndPoint, + HandleSendTo, + null); + } + catch (NullReferenceException) { } + catch (ObjectDisposedException) + { + // Already disposed and disconnected... + } + catch (SocketException ex) + { + DisconnectInternal(HazelInternalErrors.SocketExceptionSend, "Could not send data as a SocketException occurred: " + ex.Message); + } + } + + /// + /// Synchronously writes the given bytes to the connection. + /// + /// The bytes to write. + protected virtual void WriteBytesToConnectionSync(byte[] bytes, int length) + { + try + { + socket.SendTo( + bytes, + 0, + length, + SocketFlags.None, + EndPoint); } catch (NullReferenceException) { } catch (ObjectDisposedException) @@ -249,7 +262,7 @@ namespace Hazel.Udp try { - this.WriteBytesToConnection(bytes, bytes.Length); + this.WriteBytesToConnectionSync(bytes, bytes.Length); } catch { } @@ -259,8 +272,6 @@ namespace Hazel.Udp /// protected override void Dispose(bool disposing) { - this.sendSynchronously = true; - if (disposing) { SendDisconnect();