Updated Branches:
  refs/heads/master ffbfd03cc -> 866c23b5d

THRIFT-2081 Specified timeout should be used in TSocket.Open()

Patch: Jens Geyer


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/866c23b5
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/866c23b5
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/866c23b5

Branch: refs/heads/master
Commit: 866c23b5d134f9d782114cb66638d71661937532
Parents: ffbfd03
Author: Jens Geyer <[email protected]>
Authored: Fri Jul 5 19:20:27 2013 +0200
Committer: Jens Geyer <[email protected]>
Committed: Fri Jul 5 19:20:27 2013 +0200

----------------------------------------------------------------------
 lib/csharp/src/Transport/TSocket.cs | 69 +++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/866c23b5/lib/csharp/src/Transport/TSocket.cs
----------------------------------------------------------------------
diff --git a/lib/csharp/src/Transport/TSocket.cs 
b/lib/csharp/src/Transport/TSocket.cs
index c05b6c2..1d50968 100644
--- a/lib/csharp/src/Transport/TSocket.cs
+++ b/lib/csharp/src/Transport/TSocket.cs
@@ -131,11 +131,78 @@ namespace Thrift.Transport
                                InitSocket();
                        }
 
-                       client.Connect(host, port);
+                       if( timeout == 0)                       // no timeout 
-> infinite
+                       {
+                               client.Connect(host, port);
+                       }
+                       else                        // we have a timeout -> use 
it
+                       {
+                ConnectHelper hlp = new ConnectHelper(client);
+                IAsyncResult asyncres = client.BeginConnect(host, port, new 
AsyncCallback(ConnectCallback), hlp);
+                bool bConnected = asyncres.AsyncWaitHandle.WaitOne(timeout) && 
client.Connected;
+                if (!bConnected)
+                {
+                    lock (hlp.Mutex)
+                    {
+                        if( hlp.CallbackDone)
+                        {
+                            asyncres.AsyncWaitHandle.Close();
+                            client.Close();
+                        }
+                        else
+                        {
+                            hlp.DoCleanup = true;
+                            client = null;
+                        }
+                    }
+                    throw new 
TTransportException(TTransportException.ExceptionType.TimedOut, "Connect timed 
out");
+                }
+                       }
+
                        inputStream = client.GetStream();
                        outputStream = client.GetStream();
                }
 
+
+        static void ConnectCallback(IAsyncResult asyncres)
+        {
+            ConnectHelper hlp = asyncres.AsyncState as ConnectHelper;
+            lock (hlp.Mutex)
+            {
+                hlp.CallbackDone = true;
+                    
+                try
+                {
+                    if( hlp.Client.Client != null)
+                        hlp.Client.EndConnect(asyncres);
+                }
+                catch (SocketException)
+                {
+                    // catch that away
+                }
+
+                if (hlp.DoCleanup) 
+                {
+                    asyncres.AsyncWaitHandle.Close();
+                    if (hlp.Client is IDisposable)
+                        ((IDisposable)hlp.Client).Dispose();
+                    hlp.Client = null;
+                }
+            }
+        }
+
+        private class ConnectHelper
+        {
+            public object Mutex = new object();
+            public bool DoCleanup = false;
+            public bool CallbackDone = false;
+            public TcpClient Client;
+            public ConnectHelper(TcpClient client)
+            {
+                Client = client;
+            }
+        }
+
                public override void Close()
                {
                        base.Close();

Reply via email to