]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Added timeouts on Connect
authorJamJar00 <jamster.30@btinternet.com>
Wed, 30 Nov 2016 00:25:15 +0000 (00:25 +0000)
committerJamJar00 <jamster.30@btinternet.com>
Wed, 30 Nov 2016 00:25:15 +0000 (00:25 +0000)
Hazel/Connection.cs
Hazel/Tcp/TcpConnection.cs
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpServerConnection.cs

index 3f12dc0236a632b564373b13876a72165f1cd626..45058c1d11ec2fd840c351fddb7036ac700909b2 100644 (file)
@@ -162,12 +162,13 @@ namespace Hazel
         ///     Connects the connection to a server and begins listening.
         /// </summary>
         /// <param name="bytes">The bytes of data to send in the handshake.</param>
+        /// <param name="timeout">The number of milliseconds to wait before giving up on the connect attempt.</param>
         /// <remarks>
         ///     Calling Connect makes the connection attempt to connect to the end point that's specified in the 
         ///     constructor. This method will block until the connection attempt completes and will throw a 
         ///     <see cref="HazelException"/> if there is a problem connecting.
         /// </remarks>
-        public abstract void Connect(byte[] bytes = null);
+        public abstract void Connect(byte[] bytes = null, int timeout = 5000);
 
         /// <summary>
         ///     Invokes the DataReceived event.
@@ -213,14 +214,15 @@ namespace Hazel
         /// <summary>
         ///     Blocks until the Connection is connected.
         /// </summary>
+        /// <param name="timeout">The number of milliseconds to wait before timing out.</param>
         /// <remarks>
         ///     This is a helper method for waiting until the connection is connected. It will block until the 
         ///     <see cref="State"/> property is set to <see cref="ConnectionState.Connected"/> allowing the main thread to 
         ///     wait until specific data is received etc. before returning to the user's code.
         /// </remarks>
-        protected void WaitOnConnect()
+        protected bool WaitOnConnect(int timeout)
         {
-            connectWaitLock.WaitOne();
+            return connectWaitLock.WaitOne(timeout);
         }
 
         /// <summary>
index d0c2a8fa65d6a13b406fc77ae8ead2a394c03403..c64190761db4934a45da5f8a8b60fba7751f024d 100644 (file)
@@ -77,7 +77,7 @@ namespace Hazel.Tcp
         }
 
         /// <inheritdoc />
-        public override void Connect(byte[] bytes = null)
+        public override void Connect(byte[] bytes = null, int timeout = 5000)
         {
             lock(socketLock)
             {
@@ -86,7 +86,11 @@ namespace Hazel.Tcp
 
                 try
                 {
-                    socket.Connect(RemoteEndPoint);
+                    IAsyncResult result = socket.BeginConnect(RemoteEndPoint, null, null);
+
+                    result.AsyncWaitHandle.WaitOne(timeout);
+
+                    socket.EndConnect(result);
                 }
                 catch (Exception e)
                 {
index afcc12492f414f4e9038aba0df37cd2c0eac84f4..4f2e33fc6e686d71c9d6df0301fb2ba821a0562d 100644 (file)
@@ -102,7 +102,7 @@ namespace Hazel.Udp
         }
 
         /// <inheritdoc />
-        public override void Connect(byte[] bytes = null)
+        public override void Connect(byte[] bytes = null, int timeout = 5000)
         {
             lock(socketLock)
             {
@@ -121,6 +121,7 @@ namespace Hazel.Udp
                 }
                 catch (SocketException e)
                 {
+                    State = ConnectionState.NotConnected;
                     throw new HazelException("A socket exception occured while binding to the port.", e);
                 }
 
@@ -137,6 +138,7 @@ namespace Hazel.Udp
                 }
                 catch (SocketException e)
                 {
+                    Dispose();
                     throw new HazelException("A Socket exception occured while initiating a receive operation.", e);
                 }
             }
@@ -146,7 +148,14 @@ namespace Hazel.Udp
             SendHello(bytes, () => { lock (socketLock) State = ConnectionState.Connected; });
 
             //Wait till hello packet is acknowledged and the state is set to Connected
-            WaitOnConnect();
+            bool timedOut = !WaitOnConnect(timeout);
+
+            //If we timed out raise an exception
+            if (timedOut)
+            {
+                Dispose();
+                throw new HazelException("Connection attempt timed out.");
+            }
         }
 
         /// <summary>
index 98a204776e0c3e1378126e4db661c3ef109be661..ff19df411680689c1f90a19f8597e6a6de7e9af1 100644 (file)
@@ -60,7 +60,7 @@ namespace Hazel.Udp
         /// <remarks>
         ///     This will always throw a HazelException.
         /// </remarks>
-        public override void Connect(byte[] bytes)
+        public override void Connect(byte[] bytes = null, int timeout = 5000)
         {
             throw new HazelException("Cannot manually connect a UdpServerConnection, did you mean to use UdpClientConnection?");
         }