This is an automated email from the ASF dual-hosted git repository.
jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git
The following commit(s) were added to refs/heads/master by this push:
new 1ad08ca THRIFT-3587 TTLSSocket now also uses ConnectHelper to allow
for timeouts when opening the connection. Client: C# Patch: Christoph Herold
1ad08ca is described below
commit 1ad08ca91b62701bf2989f52e73e6642aee1f7f0
Author: Christoph Herold <[email protected]>
AuthorDate: Tue Jan 15 10:23:51 2019 +0100
THRIFT-3587 TTLSSocket now also uses ConnectHelper to allow for timeouts
when opening the connection.
Client: C#
Patch: Christoph Herold
This closes #1698
---
lib/csharp/src/Transport/TTLSSocket.cs | 76 +++++++++++++++++++++++++++++++++-
1 file changed, 75 insertions(+), 1 deletion(-)
diff --git a/lib/csharp/src/Transport/TTLSSocket.cs
b/lib/csharp/src/Transport/TTLSSocket.cs
index fd019c3..06286dc 100644
--- a/lib/csharp/src/Transport/TTLSSocket.cs
+++ b/lib/csharp/src/Transport/TTLSSocket.cs
@@ -295,7 +295,33 @@ 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");
+ }
+ }
setupTLS();
}
@@ -348,6 +374,54 @@ namespace Thrift.Transport
inputStream = this.secureStream;
outputStream = this.secureStream;
}
+
+ 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 (Exception)
+ {
+ // catch that away
+ }
+
+ if (hlp.DoCleanup)
+ {
+ try
+ {
+ asyncres.AsyncWaitHandle.Close();
+ }
+ catch (Exception) { }
+
+ try
+ {
+ if (hlp.Client is IDisposable)
+ ((IDisposable)hlp.Client).Dispose();
+ }
+ catch (Exception) { }
+ 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;
+ }
+ }
/// <summary>
/// Closes the SSL Socket