public static LiveClient FromTcpClient(TcpClient tcpClient, bool bUseSsl, X509Certificate2 certificate) { ExcpHelper.ThrowIf(tcpClient == null, "Parameter tcpClient is Null"); LiveClient lcNew = new LiveClient(bUseSsl, certificate); return(lcNew.Connect(tcpClient) ? lcNew : null); }
private static void ProcessEvent(eAction action, DelegateThread dt, ThreadContext tc) { if (dt != null) { lock (m_oLocker) { switch (action) { case eAction.Started: ExcpHelper.ThrowIf(m_diIdToContext.ContainsKey(tc.ManagedThreadId), "Thread is running already {0}", tc); ExcpHelper.ThrowIf(m_diNameToContext.ContainsKey(tc.ThreadName), "Thread is running already {0}", tc); m_diIdToContext.Add(tc.ManagedThreadId, tc); m_diNameToContext.Add(tc.ThreadName, tc); Debug.Assert(m_diIdToContext.Count == m_diNameToContext.Count); break; case eAction.Completed: ExcpHelper.ThrowIf(!m_diIdToContext.ContainsKey(tc.ManagedThreadId), "Thread is not running {0}", tc); ExcpHelper.ThrowIf(!m_diNameToContext.ContainsKey(tc.ThreadName), "Thread is not running {0}", tc); m_diIdToContext.Remove(tc.ManagedThreadId); m_diNameToContext.Remove(tc.ThreadName); Debug.Assert(m_diIdToContext.Count == m_diNameToContext.Count); break; case eAction.Error: Debug.Assert(m_diIdToContext.Count == m_diNameToContext.Count); break; default: Debug.Assert(false); break; } try { if (dt != null) { dt(tc); } } catch (Exception excp) { m_logger.Error(ExcpHelper.FormatException(excp, "ProcessEvent {0} ERROR for {1}:\r\n{2}\r\n{3}", action, tc, excp.Message, excp.StackTrace), excp); } } } }
public void WaitUntillReadyOrThrow(int iTimeOutMilliseconds) { int iStart = 0; while (!this.IsAlive) { System.Threading.Thread.Sleep(WAITFOR_TIMEOUT); iStart += WAITFOR_TIMEOUT; ExcpHelper.ThrowIf(iStart >= iTimeOutMilliseconds, "LiveClient is not ready too long time (Requested TimeOut = {0})", iTimeOutMilliseconds); } }
// DK - if param tcpClient is specified then it is Server Side and tcpClient came from method listener.AcceptTcpClient() // Else it is new ClientSide connection private bool Connect(TcpClient tcpClient) { try { lock (m_objLocker) { if (tcpClient != null) { // Server m_tcpClient = tcpClient; IPEndPoint ep = tcpClient.Client.RemoteEndPoint as IPEndPoint; m_sHost = ep == null ? "Undefined" : ep.Address.ToString(); } else { // Client _logger.InfoFormat("Connectiong to {0}:{1}{2}", m_sHost, m_iPort, m_bUseSsl ? " using SSL" : string.Empty); m_tcpClient = new TcpClient(m_sHost, m_iPort); } m_tcpClient.ReceiveTimeout = DEFAULT_TIMEOUT; m_tcpClient.SendTimeout = DEFAULT_TIMEOUT; _logger.DebugFormat("LiveClient connected to Server {0}:{1}", m_sHost, m_iPort); if (m_bUseSsl) { // DK - use certmgr.msc to check local certificates _logger.Info("Authenticating..."); SslStream m_sslStream = null; if (tcpClient != null) { // Server ExcpHelper.ThrowIf(m_certificate == null, "LiveClient.Connect() ERROR. UseSSL is specified but certificate is null."); m_sslStream = new SslStream(m_tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); m_sslStream.AuthenticateAsServer(m_certificate, true, SslProtocols.Default, false); } else { // Client m_sslStream = new SslStream(m_tcpClient.GetStream()); if (m_certificate == null) { m_sslStream.AuthenticateAsClient(m_sRemoteSecureHostName); } else { m_sslStream.AuthenticateAsClient(m_sRemoteSecureHostName, new X509Certificate2Collection() { m_certificate }, SslProtocols.Default, false); } _logger.DebugFormat("LiveClient authenticated (SSL) at Server {0}:{1}", m_sHost, m_iPort); } m_sr.SslStream = m_sslStream; //m_sw = new StreamWriter(m_sslStream); return(m_sslStream.IsAuthenticated); } _logger.Info("No SSL"); m_sr.NetworkStream = m_tcpClient.GetStream(); //m_sw = new StreamWriter(m_tcpClient.GetStream()); return(m_tcpClient.Connected); } } catch (Exception excp) { m_tcpClient = null; _logger.WarnFormat("LiveClient COULD NOT establish connection to Server {0}:{1}\r\n{2}\r\n{3}", m_sHost, m_iPort, excp.Message, excp.StackTrace); } return(false); }