private void OnConnectResult(IAsyncResult ar)
{
try
{
mTcpClient.EndConnect(ar);
OnHandleConnected();
mRetries = 0;
StartHeartbeat();
}
catch (Exception ex)
{
if (mRetries > 0)
{
Logger.InfoLine(string.Format(CultureInfo.InvariantCulture,
"BeginConnect to server with retry {0} failed.", mRetries));
}
mRetries++;
if (mRetries > ConnectRetries)
{
// we have failed to connect to all the IP Addresses,
// connection has failed overall.
OnHandleError(ex);
return;
}
Logger.InfoLine(string.Format(CultureInfo.InvariantCulture,
"Waiting {0} seconds before retrying to connect to server.",
ConnectRetryInterval));
Thread.Sleep(TimeSpan.FromSeconds(ConnectRetryInterval));
BeginConnect();
return;
}
// we are connected successfully and start asyn read operation.
byte[] buffer = new byte[mTcpClient.ReceiveBufferSize];
mTcpClient.GetStream().BeginRead(buffer, 0, buffer.Length, OnReadResult, buffer);
}