Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.Net.Sockets;
using System.Threading;

namespace Elasticsearch.Net.Connection.Thrift.Transport
{
Expand All @@ -33,6 +34,55 @@ public class TSocket : TStreamTransport
private TcpClient client;
private int timeout;

private bool isConnectionSuccessful = false;
private Exception socketexception;
private ManualResetEvent timeoutObject = new ManualResetEvent(false);

public TcpClient Connect()
{
timeoutObject.Reset();
socketexception = null;

client.BeginConnect(host, port, new AsyncCallback(CallBackMethod), client);

if (timeoutObject.WaitOne(timeout, false))
{
if (isConnectionSuccessful)
{
return client;
}

throw socketexception ?? new Exception("Socket exception should not be null.");
}

client.Close();
throw new TimeoutException("TimeOut Exception");
}

private void CallBackMethod(IAsyncResult asyncresult)
{
try
{
isConnectionSuccessful = false;
var tcpclient = asyncresult.AsyncState as TcpClient;

if (tcpclient != null && tcpclient.Client != null)
{
tcpclient.EndConnect(asyncresult);
isConnectionSuccessful = true;
}
}
catch (Exception ex)
{
isConnectionSuccessful = false;
socketexception = ex;
}
finally
{
timeoutObject.Set();
}
}

public TSocket(TcpClient client)
{
this.client = client;
Expand Down Expand Up @@ -121,14 +171,7 @@ public override void Open()
InitSocket();
}


var connectionRequest = client.BeginConnect(host, port, null, null);
var connected = connectionRequest.AsyncWaitHandle.WaitOne(this.ConnectTimeout);

if (!connected)
{
throw new TTransportException("Failed to connect");
}
client = Connect();

inputStream = client.GetStream();
outputStream = client.GetStream();
Expand Down