protected virtual async Task CreateSSLConnectionAsync() { // Establish the remote endpoint for the socket. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; var client = new TcpClient(_parameters.Uri, _parameters.Port) { ReceiveTimeout = 60000, }; var sslStream = new SslStream(client.GetStream()); await sslStream.AuthenticateAsClientAsync(_parameters.Uri); if (sslStream.IsAuthenticated && sslStream.IsEncrypted) { var reader = new StreamReader(sslStream); var writer = new StreamWriter(sslStream) { AutoFlush = true, NewLine = _parameters.EndOfLineCharacters }; _connection = new ConnectionTcp { Client = client, Reader = reader, Writer = writer }; } else { throw new Exception("Could not create connection - SSL cert has validation problem."); } }
public virtual async Task <bool> DisconnectAsync() { try { if (_connection != null) { if (_connection != null && _connection.Writer != null) { _connection.Writer.Dispose(); } if (_connection != null && _connection.Reader != null) { _connection.Reader.Dispose(); } if (_connection != null && _connection.Client != null) { _connection.Client.Close(); _connection.Client.Dispose(); } await FireEventAsync(this, new TcpConnectionClientEventArgs { ConnectionEventType = ConnectionEventType.Disconnect, Connection = _connection }); _connection = null; return(true); } } catch (Exception ex) { await FireEventAsync(this, new TcpErrorClientEventArgs { Connection = _connection, Exception = ex, Message = ex.Message }); } return(false); }
protected virtual Task CreateConnectionAsync() { // Establish the remote endpoint for the socket. var client = new TcpClient(_parameters.Uri, _parameters.Port) { ReceiveTimeout = 60000 }; var reader = new StreamReader(client.GetStream()); var writer = new StreamWriter(client.GetStream()) { AutoFlush = true, NewLine = _parameters.EndOfLineCharacters }; _connection = new ConnectionTcp { Client = client, Reader = reader, Writer = writer }; return(Task.CompletedTask); }