]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Fixed bugs in TCP exception handling and improved some exception messages.
authorJamJar00 <jamster.30@btinternet.com>
Tue, 27 Sep 2016 00:02:05 +0000 (01:02 +0100)
committerJamJar00 <jamster.30@btinternet.com>
Tue, 27 Sep 2016 00:02:05 +0000 (01:02 +0100)
Previously most exceptions from Socket.BeginReceive in TCP were unhandled when they should have marked a disconnection, this is fixed and now more obvious.

Also TcpConnection.StartListening is no longer needed, simply bootstrap it using StartWaitingForHeader.

Hazel/Tcp/TcpConnection.cs
Hazel/Tcp/TcpConnectionListener.cs

index 2507870d2673dc3ab7c93a183be8a8f82b5fe55c..b2fa6167ed0196f1fa5729de4e42cade798ef838 100644 (file)
@@ -76,22 +76,6 @@ namespace Hazel.Tcp
             }
         }
 
-        /// <summary>
-        ///     Internal call to start listening once this socket has been constructed and is ready.
-        /// </summary>
-        internal void StartListening()
-        {
-            //Start receiving data
-            try
-            {
-                StartWaitingForHeader();
-            }
-            catch (SocketException e)
-            {
-                throw new HazelException("A Socket exception occured while initiating a receive operation.", e);
-            }
-        }
-
         /// <inheritdoc />
         public override void Connect()
         {
@@ -110,10 +94,17 @@ namespace Hazel.Tcp
                 }
 
                 //Start receiving data
-                StartListening();
+                try
+                {
+                    StartListening();
+                }
+                catch (SocketException e)
+                {
+                    throw new HazelException("A Socket exception occured while initiating the first receive operation.", e);
+                }
 
-                //Set connected
-                State = ConnectionState.Connected;
+            //Set connected
+            State = ConnectionState.Connected;
             }
         }
 
@@ -167,7 +158,7 @@ namespace Hazel.Tcp
             }
             catch (SocketException e)
             {
-                HandleDisconnect(new HazelException("A Socket exception occured while initiating a receive operation.", e));
+                HandleDisconnect(new HazelException("A Socket exception occured while initiating a body receive operation.", e));
             }
         }
 
@@ -178,7 +169,14 @@ namespace Hazel.Tcp
         void BodyReadCallback(byte[] bytes)
         {
             //Begin receiving from the start
-            StartWaitingForHeader();
+            try
+            {
+                StartWaitingForHeader();
+            }
+            catch (SocketException e)
+            {
+                HandleDisconnect(new HazelException("A Socket exception occured while initiating a body receive operation.", e));
+            }
 
             Statistics.LogReceive(bytes.Length, bytes.Length + 4);
 
@@ -189,7 +187,7 @@ namespace Hazel.Tcp
         /// <summary>
         ///     Starts this connections waiting for the header.
         /// </summary>
-        void StartWaitingForHeader()
+        internal void StartWaitingForHeader()
         {
             StartWaitingForBytes(4, HeaderReadCallback);
         }
@@ -267,7 +265,7 @@ namespace Hazel.Tcp
                 }
                 catch (SocketException e)
                 {
-                    HandleDisconnect(new HazelException("A Socket exception occured while initiating a receive operation.", e));
+                    HandleDisconnect(new HazelException("A Socket exception occured while initiating a chunk receive operation.", e));
                     return;
                 }
             }
index 78b7fa0c3e20cf0416142d5c166773651ec40728..9e174b99c8168c73320b72cf95259d923c77b6b1 100644 (file)
@@ -101,7 +101,15 @@ namespace Hazel.Tcp
                 //Invoke
                 InvokeNewConnection(tcpConnection);
 
-                tcpConnection.StartListening();
+                try
+                {
+                    tcpConnection.StartWaitingForHeader();
+                }
+                catch (SocketException)
+                {
+                    //Receive operation couldn't be started, there's nothing we can do to save it
+                    return;
+                }
             }
         }