Example #1
0
        public async Task Connect(string host, int port,
                                  ConnectionChannel connectionChannel,
                                  HeartbeatChannel heartbeatChannel,
                                  Func <Stream, bool, CancellationToken, Task> packetReader,
                                  CancellationToken token)
        {
            await clientConnectLock.WaitAsync(token).ConfigureAwait(false);

            try
            {
                if (client != null)
                {
                    throw new Exception("Already set");
                }
                client                  = new ChromecastTcpClient();
                stopTokenSource         = new CancellationTokenSource();
                combinedStopTokenSource = CancellationTokenSource.CreateLinkedTokenSource(stopTokenSource.Token, token);
                CancellationToken combinedToken = combinedStopTokenSource.Token;
                await client.ConnectAsync(host, port, combinedToken).ConfigureAwait(false);

                readTask = ProcessRead(packetReader, combinedToken);

                connectionChannel.OpenConnection(combinedStopTokenSource.Token);
                heartbeatChannel.StartHeartbeat(combinedStopTokenSource.Token);
            }
            finally
            {
                clientConnectLock.Release();
            }
        }
Example #2
0
        public async Task Connect(string host, int port,
                                  ConnectionChannel connectionChannel,
                                  HeartbeatChannel heartbeatChannel,
                                  Func <Stream, CancellationToken, Task> packetReader,
                                  CancellationToken token)
        {
            using (var sync = await clientConnectLock.LockAsync(token).ConfigureAwait(false))
            {
                if (client != null)
                {
                    throw new Exception("Already set");
                }
                client = new ChromecastTcpClient();
                combinedStopTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
                CancellationToken combinedToken = combinedStopTokenSource.Token;
                await client.ConnectAsync(host, port, combinedToken).ConfigureAwait(false);

                readTask = TaskHelper.StartAsync(() => ProcessRead(packetReader, combinedToken), combinedToken);

                connectionChannel.OpenConnection(combinedToken);
                heartbeatChannel.StartHeartbeat(combinedToken);
            }
        }