]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Add a synchronous WriteBytesToConnection to handle disconnect packets
authorMatthew Endsley <mendsley@gmail.com>
Fri, 5 Mar 2021 22:09:07 +0000 (14:09 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Fri, 5 Mar 2021 22:22:10 +0000 (14:22 -0800)
Hazel/Dtls/DtlsUnityConnection.cs
Hazel/Udp/UnityUdpClientConnection.cs

index 6be3b19246f1101e862ed509741e69ab57037454..0c09e3d0e599f6021bf5ccc2fe633e031e552260 100644 (file)
@@ -274,8 +274,18 @@ namespace Hazel.Dtls
             this.queuedApplicationData.Clear();
         }
 
-        /// <inheritdoc />
-        protected override void WriteBytesToConnection(byte[] bytes, int length)
+        /// <summary>
+        /// Request from the application to write data to the DTLS
+        /// stream. If appropriate, returns a byte span to send to
+        /// the wire.
+        /// </summary>
+        /// <param name="bytes">Plaintext bytes to write</param>
+        /// <param name="length">Length of the bytes to write</param>
+        /// <returns>
+        /// Encrypted data to put on the wire if appropriate,
+        /// otherwise an empty span
+        /// </returns>
+        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;
+            }
+        }
+
+        /// <inheritdoc />
+        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);
+            }
+        }
+
+        /// <inheritdoc />
+        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);
             }
         }
 
index 25eb4db92eb0fb51daabf1318dd769fd942271ec..83096020d4afd2b7325c267a03ee9a06a1ad4d25 100644 (file)
@@ -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);
+            }
+        }
+
+        /// <summary>
+        ///     Synchronously writes the given bytes to the connection.
+        /// </summary>
+        /// <param name="bytes">The bytes to write.</param>
+        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
         /// <inheritdoc />
         protected override void Dispose(bool disposing)
         {
-            this.sendSynchronously = true;
-
             if (disposing)
             {
                 SendDisconnect();