コード例 #1
0
        public async Task Initialize(string host, string port, ConnectionChannel connectionChannel, HeartbeatChannel heartbeatChannel, Action <Stream, bool, CancellationToken> packetReader, CancellationToken cancellationToken)
        {
            if (_client == null)
            {
                _client = new TcpSocketClient();
            }
            await _client.ConnectAsync(host, int.Parse(port), true, cancellationToken, true);

            await connectionChannel.OpenConnection();

            heartbeatChannel.StartHeartbeat();

            await Task.Run(async() =>
            {
                while (true)
                {
                    var sizeBuffer = new byte[4];
                    // First message should contain the size of message
                    await _client.ReadStream.ReadAsync(sizeBuffer, 0, sizeBuffer.Length, cancellationToken);
                    // The message is little-endian (that is, little end first),
                    // reverse the byte array.
                    Array.Reverse(sizeBuffer);
                    //Retrieve the size of message
                    var messageSize   = BitConverter.ToInt32(sizeBuffer, 0);
                    var messageBuffer = new byte[messageSize];
                    await _client.ReadStream.ReadAsync(messageBuffer, 0, messageBuffer.Length, cancellationToken);
                    var answer = new MemoryStream(messageBuffer.Length);
                    await answer.WriteAsync(messageBuffer, 0, messageBuffer.Length, cancellationToken);
                    answer.Position = 0;
                    packetReader(answer, true, cancellationToken);
                }
            }, cancellationToken);
        }
コード例 #2
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();
            }
        }
コード例 #3
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);
            }
        }