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); } }
public string ReadLine() { WaitUntillReadyOrThrow(DEFAULT_TIMEOUT); byte[] message = new byte[1]; var bytesRead = m_sr.Read(message, 0, message.Length); //string sData = m_sr.Read(message, 0, 4096); StringBuilder sData = new StringBuilder(); sData.Append(encoder.GetString(message, 0, bytesRead)); while (bytesRead > 0) { bytesRead = m_sr.Read(message, 0, message.Length); sData.Append(encoder.GetString(message, 0, bytesRead)); if (message[0] == 10) { return(sData.ToString().TrimEnd('\r', '\n')); } } ExcpHelper.ThrowUp(new Exception("Read line exception"), "LiveClient is not ready too long time (Requested TimeOut = {0})", DEFAULT_TIMEOUT); return(null); }
private static void ThreadHelperThread(object objThreadContext) { ThreadContext tc = objThreadContext as ThreadContext; try { Debug.Assert(tc != null); tc.ManagedThreadId = Thread.CurrentThread.ManagedThreadId; m_logger.InfoFormat("{0} Started", tc); ProcessEvent(eAction.Started, ThreadStarted, tc); tc.SetRunning(true); try { tc.Method(tc); } catch (Exception excp) { tc.Error = excp; m_logger.ErrorFormat("ERROR in {0}:\r\n{1}\r\n{2}", excp, tc, excp.Message, excp.StackTrace); ProcessEvent(eAction.Error, ThreadError, tc); } m_logger.InfoFormat("{0} Completed", tc); tc.SetRunning(false); ProcessEvent(eAction.Completed, ThreadCompleted, tc); } catch (Exception excp) { tc.SetRunning(false); m_logger.Error(ExcpHelper.FormatException(excp, "ThreadHelperThread({0}) General ERROR", objThreadContext), excp); } }
public static ThreadContext RunThread(string sThreadName, DelegateThread dt, object objParam, ThreadPriority priority = ThreadPriority.Normal) { try { ThreadContext tc = null; lock (m_oLocker) { tc = m_diNameToContext.ContainsKey(sThreadName) ? m_diNameToContext[sThreadName] : null; } if (tc == null) { tc = new ThreadContext(sThreadName, dt, objParam); Thread thread = new Thread(ThreadHelperThread); thread.IsBackground = true; thread.Priority = priority; thread.Name = sThreadName; thread.Start(tc); } return(tc); } catch (Exception excp) { m_logger.Error(ExcpHelper.FormatException(excp, "RunThread('{0}', {1}, {2}) General ERROR", sThreadName, dt.Method, objParam), excp); } /* DK - ThreadPool is not suitable for long runnung items. * ThreadPool.QueueUserWorkItem(delegate(object obj) * { * }, objParam); */ return(null); }
// 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); }