Beispiel #1
0
        public async Task ChangeChannel(IHdHomerunChannelCommands commands, CancellationToken cancellationToken)
        {
            if (!_lockkey.HasValue)
            {
                return;
            }

            using (var tcpClient = _socketFactory.CreateTcpSocket(_remoteIp, HdHomeRunPort))
            {
                var commandList   = commands.GetCommands();
                var receiveBuffer = new byte[8192];

                foreach (Tuple <string, string> command in commandList)
                {
                    var channelMsg = CreateSetMessage(_activeTuner, command.Item1, command.Item2, _lockkey);
                    await tcpClient.SendToAsync(channelMsg, 0, channelMsg.Length, new IpEndPointInfo(_remoteIp, HdHomeRunPort), cancellationToken).ConfigureAwait(false);

                    var response = await tcpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);

                    // parse response to make sure it worked
                    string returnVal;
                    if (!ParseReturnMessage(response.Buffer, response.ReceivedBytes, out returnVal))
                    {
                        return;
                    }
                }
            }
        }
Beispiel #2
0
        public async Task ChangeChannel(IHdHomerunChannelCommands commands, CancellationToken cancellationToken)
        {
            if (!_lockkey.HasValue)
            {
                return;
            }

            using (var tcpClient = new TcpClient(_remoteEndPoint))
                using (var stream = tcpClient.GetStream())
                {
                    var    commandList = commands.GetCommands();
                    byte[] buffer      = ArrayPool <byte> .Shared.Rent(8192);

                    try
                    {
                        foreach (var command in commandList)
                        {
                            var channelMsg = CreateSetMessage(_activeTuner, command.Item1, command.Item2, _lockkey);
                            await stream.WriteAsync(channelMsg, 0, channelMsg.Length, cancellationToken).ConfigureAwait(false);

                            int receivedBytes = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

                            // parse response to make sure it worked
                            if (!ParseReturnMessage(buffer, receivedBytes, out _))
                            {
                                return;
                            }
                        }
                    }
                    finally
                    {
                        ArrayPool <byte> .Shared.Return(buffer);
                    }
                }
        }
Beispiel #3
0
        public async Task ChangeChannel(IHdHomerunChannelCommands commands, CancellationToken cancellationToken)
        {
            if (!_lockkey.HasValue)
            {
                return;
            }

            using var tcpClient = new TcpClient();
            await tcpClient.ConnectAsync(_remoteEndPoint, cancellationToken).ConfigureAwait(false);

            using var stream = tcpClient.GetStream();
            var commandList = commands.GetCommands();

            byte[] buffer = ArrayPool <byte> .Shared.Rent(8192);

            try
            {
                foreach (var command in commandList)
                {
                    var channelMsgLen = WriteSetMessage(buffer, _activeTuner, command.CommandName, command.CommandValue, _lockkey);
                    await stream.WriteAsync(buffer.AsMemory(0, channelMsgLen), cancellationToken).ConfigureAwait(false);

                    int receivedBytes = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);

                    // parse response to make sure it worked
                    if (!TryGetReturnValueOfGetSet(buffer.AsSpan(0, receivedBytes), out _))
                    {
                        return;
                    }
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
Beispiel #4
0
        public async Task StartStreaming(IPAddress remoteIp, IPAddress localIp, int localPort, IHdHomerunChannelCommands commands, int numTuners, CancellationToken cancellationToken)
        {
            _remoteEndPoint = new IPEndPoint(remoteIp, HdHomeRunPort);

            _tcpClient = new TcpClient();
            _tcpClient.Connect(_remoteEndPoint);

            if (!_lockkey.HasValue)
            {
                var rand = new Random();
                _lockkey = (uint)rand.Next();
            }

            var lockKeyValue = _lockkey.Value;
            var stream       = _tcpClient.GetStream();

            byte[] buffer = ArrayPool <byte> .Shared.Rent(8192);

            try
            {
                for (int i = 0; i < numTuners; ++i)
                {
                    if (!await CheckTunerAvailability(stream, i, cancellationToken).ConfigureAwait(false))
                    {
                        continue;
                    }

                    _activeTuner = i;
                    var lockKeyString = string.Format(CultureInfo.InvariantCulture, "{0:d}", lockKeyValue);
                    var lockkeyMsgLen = WriteSetMessage(buffer, i, "lockkey", lockKeyString, null);
                    await stream.WriteAsync(buffer.AsMemory(0, lockkeyMsgLen), cancellationToken).ConfigureAwait(false);

                    int receivedBytes = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);

                    // parse response to make sure it worked
                    if (!TryGetReturnValueOfGetSet(buffer.AsSpan(0, receivedBytes), out _))
                    {
                        continue;
                    }

                    foreach (var command in commands.GetCommands())
                    {
                        var channelMsgLen = WriteSetMessage(buffer, i, command.Item1, command.Item2, lockKeyValue);
                        await stream.WriteAsync(buffer.AsMemory(0, channelMsgLen), cancellationToken).ConfigureAwait(false);

                        receivedBytes = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);

                        // parse response to make sure it worked
                        if (!TryGetReturnValueOfGetSet(buffer.AsSpan(0, receivedBytes), out _))
                        {
                            await ReleaseLockkey(_tcpClient, lockKeyValue).ConfigureAwait(false);

                            continue;
                        }
                    }

                    var targetValue  = string.Format(CultureInfo.InvariantCulture, "rtp://{0}:{1}", localIp, localPort);
                    var targetMsgLen = WriteSetMessage(buffer, i, "target", targetValue, lockKeyValue);

                    await stream.WriteAsync(buffer.AsMemory(0, targetMsgLen), cancellationToken).ConfigureAwait(false);

                    receivedBytes = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);

                    // parse response to make sure it worked
                    if (!TryGetReturnValueOfGetSet(buffer.AsSpan(0, receivedBytes), out _))
                    {
                        await ReleaseLockkey(_tcpClient, lockKeyValue).ConfigureAwait(false);

                        continue;
                    }

                    return;
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }

            _activeTuner = -1;
            throw new LiveTvConflictException();
        }
Beispiel #5
0
        public async Task StartStreaming(IpAddressInfo remoteIp, IPAddress localIp, int localPort, IHdHomerunChannelCommands commands, int numTuners, CancellationToken cancellationToken)
        {
            _remoteIp = remoteIp;

            var tcpClient = _socketFactory.CreateTcpSocket(_remoteIp, HdHomeRunPort);

            _currentTcpSocket = tcpClient;

            var receiveBuffer = new byte[8192];

            if (!_lockkey.HasValue)
            {
                var rand = new Random();
                _lockkey = (uint)rand.Next();
            }

            var lockKeyValue = _lockkey.Value;

            var ipEndPoint = new IpEndPointInfo(_remoteIp, HdHomeRunPort);

            for (int i = 0; i < numTuners; ++i)
            {
                if (!await CheckTunerAvailability(tcpClient, _remoteIp, i, cancellationToken).ConfigureAwait(false))
                {
                    continue;
                }

                _activeTuner = i;
                var lockKeyString = String.Format("{0:d}", lockKeyValue);
                var lockkeyMsg    = CreateSetMessage(i, "lockkey", lockKeyString, null);
                await tcpClient.SendToAsync(lockkeyMsg, 0, lockkeyMsg.Length, ipEndPoint, cancellationToken).ConfigureAwait(false);

                var response = await tcpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);

                string returnVal;
                // parse response to make sure it worked
                if (!ParseReturnMessage(response.Buffer, response.ReceivedBytes, out returnVal))
                {
                    continue;
                }

                var commandList = commands.GetCommands();
                foreach (Tuple <string, string> command in commandList)
                {
                    var channelMsg = CreateSetMessage(i, command.Item1, command.Item2, lockKeyValue);
                    await tcpClient.SendToAsync(channelMsg, 0, channelMsg.Length, ipEndPoint, cancellationToken).ConfigureAwait(false);

                    response = await tcpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);

                    // parse response to make sure it worked
                    if (!ParseReturnMessage(response.Buffer, response.ReceivedBytes, out returnVal))
                    {
                        await ReleaseLockkey(tcpClient, lockKeyValue).ConfigureAwait(false);

                        continue;
                    }
                }

                var targetValue = String.Format("rtp://{0}:{1}", localIp, localPort);
                var targetMsg   = CreateSetMessage(i, "target", targetValue, lockKeyValue);

                await tcpClient.SendToAsync(targetMsg, 0, targetMsg.Length, ipEndPoint, cancellationToken).ConfigureAwait(false);

                response = await tcpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);

                // parse response to make sure it worked
                if (!ParseReturnMessage(response.Buffer, response.ReceivedBytes, out returnVal))
                {
                    await ReleaseLockkey(tcpClient, lockKeyValue).ConfigureAwait(false);

                    continue;
                }

                return;
            }

            _activeTuner = -1;
            throw new LiveTvConflictException();
        }