/// <summary>
/// Creates a new <see cref="ClientConnection"/> instance.
/// </summary>
/// <param name="parent">Parent data publisher.</param>
/// <param name="clientID">Client ID of associated connection.</param>
/// <param name="commandChannel"><see cref="TcpServer"/> command channel used to lookup connection information.</param>
public ClientConnection(DataPublisher parent, Guid clientID, IServer commandChannel)
{
m_parent = parent;
m_clientID = clientID;
m_commandChannel = commandChannel;
m_subscriberID = clientID;
m_keyIVs = null;
m_cipherIndex = 0;
// Setup ping timer
m_pingTimer = Common.TimerScheduler.CreateTimer(5000);
m_pingTimer.AutoReset = true;
m_pingTimer.Elapsed += m_pingTimer_Elapsed;
m_pingTimer.Start();
// Setup reconnect timer
m_reconnectTimer = Common.TimerScheduler.CreateTimer(1000);
m_reconnectTimer.AutoReset = false;
m_reconnectTimer.Elapsed += m_reconnectTimer_Elapsed;
// Attempt to lookup remote connection identification for logging purposes
try
{
Socket commandChannelSocket = GetCommandChannelSocket();
IPEndPoint remoteEndPoint = null;
if ((object)commandChannel != null)
remoteEndPoint = commandChannelSocket.RemoteEndPoint as IPEndPoint;
if ((object)remoteEndPoint != null)
{
m_ipAddress = remoteEndPoint.Address;
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
m_connectionID = "[" + m_ipAddress + "]:" + remoteEndPoint.Port;
else
m_connectionID = m_ipAddress + ":" + remoteEndPoint.Port;
try
{
IPHostEntry ipHost = Dns.GetHostEntry(remoteEndPoint.Address);
if (!string.IsNullOrWhiteSpace(ipHost.HostName))
{
m_hostName = ipHost.HostName;
m_connectionID = m_hostName + " (" + m_connectionID + ")";
}
}
// Just ignoring possible DNS lookup failures...
catch (ArgumentNullException)
{
// The hostNameOrAddress parameter is null.
}
catch (ArgumentOutOfRangeException)
{
// The length of hostNameOrAddress parameter is greater than 255 characters.
}
catch (ArgumentException)
{
// The hostNameOrAddress parameter is an invalid IP address.
}
catch (SocketException)
{
// An error was encountered when resolving the hostNameOrAddress parameter.
}
}
}
catch
{
// At worst we'll just use the client GUID for identification
m_connectionID = m_subscriberID == Guid.Empty ? clientID.ToString() : m_subscriberID.ToString();
}
if (string.IsNullOrWhiteSpace(m_connectionID))
m_connectionID = "unavailable";
if (string.IsNullOrWhiteSpace(m_hostName))
{
if ((object)m_ipAddress != null)
m_hostName = m_ipAddress.ToString();
else
m_hostName = m_connectionID;
}
if ((object)m_ipAddress == null)
m_ipAddress = IPAddress.None;
}