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); }
public async Task <JObject> Connect(TivoEndPoint endPoint, IDictionary <string, object> authMessage, int authMessageSchemaVersion) { this.connectionMode = endPoint.ConnectionMode; this.headerInfo = (IMindRpcHeaderInfo)endPoint; this.sslStream = await this.networkInterface.Initialize(endPoint).ConfigureAwait(false); this.receiveSubject = new Subject <Tuple <int, JObject> >(); // Send authentication message to the TiVo. var authTask = SendRequest(authMessage, authMessageSchemaVersion); // Start listening on the socket *after* the first send operation. // This stops errors occuring on WP7 StartReceiveThread(); return(await authTask); }