public virtual void Connect(string hostname, int port, bool ssl, System.Net.Security.RemoteCertificateValidationCallback validateCertificate) { try { Host = hostname; Port = port; Ssl = ssl; _Connection = new TcpClient(hostname, port); _Stream = _Connection.GetStream(); if (ssl) { System.Net.Security.SslStream sslStream; if (validateCertificate != null) { sslStream = new System.Net.Security.SslStream(_Stream, false, validateCertificate); } else { sslStream = new System.Net.Security.SslStream(_Stream, false); } _Stream = sslStream; sslStream.AuthenticateAsClient(hostname); } OnConnected(GetResponse()); IsConnected = true; Host = hostname; } catch (Exception) { IsConnected = false; Utilities.TryDispose(ref _Stream); throw; } }
public void Disconnect() { Logout(); Utilities.TryDispose(ref _Reader); Utilities.TryDispose(ref _Stream); Utilities.TryDispose(ref _Connection); }
public virtual void Disconnect() { if (IsAuthenticated) { Logout(); } Utilities.TryDispose(ref _Stream); Utilities.TryDispose(ref _Connection); }
public virtual void Disconnect() { if (!IsConnected) { return; } if (IsAuthenticated) { Logout(); } IsConnected = false; Utilities.TryDispose(ref _Stream); Utilities.TryDispose(ref _Connection); }
public virtual void Connect(string hostname, int port, bool ssl, System.Net.Security.RemoteCertificateValidationCallback validateCertificate) { try { Host = hostname; Port = port; Ssl = ssl; _Connection.SendTimeout = this.Timeout; _Connection.ReceiveTimeout = this.Timeout; IAsyncResult ar = _Connection.BeginConnect(hostname, port, null, null); System.Threading.WaitHandle wh = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(this.Timeout), true)) { throw new TimeoutException(string.Format("Could not connect to {0} on port {1}.", hostname, port)); } _Connection.EndConnect(ar); } finally { wh.Close(); } _Stream = _Connection.GetStream(); if (ssl) { System.Net.Security.SslStream sslStream; if (validateCertificate != null) { sslStream = new System.Net.Security.SslStream(_Stream, false, validateCertificate); } else { sslStream = new System.Net.Security.SslStream(_Stream, false); } _Stream = sslStream; sslStream.AuthenticateAsClient(hostname); } OnConnected(GetResponse()); IsConnected = true; Host = hostname; } catch (Exception) { IsConnected = false; Utilities.TryDispose(ref _Stream); throw; } }
public void Connect(string hostname, int port, bool ssl, bool skipSslValidation) { try { Host = hostname; Port = port; Ssl = ssl; _Connection = new TcpClient(hostname, port); _Stream = _Connection.GetStream(); if (ssl) { System.Net.Security.SslStream sslStream; if (skipSslValidation) { sslStream = new System.Net.Security.SslStream(_Stream, false, (sender, cert, chain, err) => true); } else { sslStream = new System.Net.Security.SslStream(_Stream, false); } _Stream = sslStream; sslStream.AuthenticateAsClient(hostname); } _Reader = new StreamReader(_Stream); string info = _Reader.ReadLine(); OnConnected(info); IsConnected = true; Host = hostname; } catch (Exception) { IsConnected = false; Utilities.TryDispose(ref _Reader); Utilities.TryDispose(ref _Stream); throw; } }