Example #1
0
        private void MainLoop()
        {
            Target.ClientConnected += StreamInitialized;
            Target.InitializeStreaming();
            ThreadTimer.Start();

#if BENCHMARK
            Stopwatch benchmark = new Stopwatch();
#endif
#if !DEBUG
            try
            {
#endif
            while (!Token.IsCancellationRequested)
            {
#if BENCHMARK
                benchmark.Restart();
#endif
                bool ShouldBlock = false;
                lock (wait)
                    ShouldBlock = SuspendThread;
                if (ShouldBlock)
                {
                    wait.WaitOne();
                }

                while (Device.WriteWResult(MagicPacket) != MagicPacket.Length)
                {
                    Console.WriteLine($"Warning: Couldn't write data to device ({Kind} thread)");
                    System.Threading.Thread.Sleep(1000);
                    Device.Flush();
                }

                var size = ReadNextPacket();
                if (size > MaxBufSize || size <= 0)
                {
                    Console.WriteLine($"Warning: Discarding packet of size {size} ({Kind} thread)");
                    System.Threading.Thread.Sleep(500);
                    Device.Flush();
                }
                else
                {
                    Target.SendData(Data, 0, (int)size, Timestamp);
#if LOG
                    Console.WriteLine($"video {size}");
#endif
                    TransfersPerSecond++;
                    BytesPerSecond += size;
                    CheckPlayStats();
                }
#if BENCHMARK
                if (Kind == StreamKind.Video)
                {
                    Console.WriteLine(benchmark.ElapsedMilliseconds);
                }
#endif
            }
#if !DEBUG
        }

        catch (Exception ex)
        {
            if (!Token.IsCancellationRequested)
            {
                Console.WriteLine("There was an exception: " + ex.ToString());
            }
        }
#endif
            Console.WriteLine($"{Kind} thread stopped.");
        }