public async Task <Stream> Initialize(TivoEndPoint endPoint)
        {
            if (this.client != null)
            {
                throw new InvalidOperationException("Cannot open the same connection twice.");
            }

            var ep = new DnsEndPoint(endPoint.Address, endPoint.Port);

            // Create a TCP/IP connection to the TiVo.
            this.client = await ConnectSocketAsync(ep).ConfigureAwait(false);

            ////Debug.WriteLine("Client connected.");

            try
            {
                // Create an SSL stream that will close the client's stream.
                var tivoTlsClient = new TivoTlsClient(endPoint.Certificate, endPoint.Password);

                this.tlsProtocolHandler = new TlsProtocolHandler(new NetworkStream(this.client)
                {
                    ReadTimeout = Timeout.Infinite
                });
                this.tlsProtocolHandler.Connect(tivoTlsClient);
            }
            catch (IOException e)
            {
                Debug.WriteLine("Authentication failed - closing the connection.");

                Debug.WriteLine("Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Debug.WriteLine("Inner exception: {0}", e.InnerException.Message);
                }

                this.client.Dispose();
                this.client = null;

                this.tlsProtocolHandler.Close();
                this.tlsProtocolHandler = null;

                throw;
            }

            return(this.tlsProtocolHandler.Stream);
        }
Example #2
0
 public TivoTlsAuthentication(TivoTlsClient client)
 {
     this.client = client;
 }