private void MainLoop() { StreamInitialized(); ThreadTimer.Start(); #if !DEBUG try { #endif while (!Token.IsCancellationRequested) { 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); #if PRINT_DEBUG Console.WriteLine($"video {size}"); #endif TransfersPerSecond++; BytesPerSecond += size; CheckPlayStats(); } } #if !DEBUG } catch (Exception ex) { Console.WriteLine("There was an exception: " + ex.ToString()); } #endif Console.WriteLine($"{Kind} thread stopped."); }
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."); }
private async void MainLoop() { cli = new TcpClient(); try { await cli.ConnectAsync(Ip, Port, Token); var stream = cli.GetStream(); Target.InitializeStreaming(); var bin = new BinaryReader(stream); while (!Token.IsCancellationRequested) { int read = 0; { int magicCount = 0; while (magicCount != magicLen && !Token.IsCancellationRequested) { read = await stream.ReadAsync(tempBuffer, 0, 1, Token); if (read <= 0) { return; } if (tempBuffer[0] == magic) { magicCount++; } } if (Token.IsCancellationRequested) { return; } } UInt64 ts = bin.ReadUInt64(); Int32 sz = bin.ReadInt32(); if (sz < 0) { continue; } byte[] data = bin.ReadBytes(sz); Target.SendData(data, 0, sz, ts); #if DEBUG && LOG Console.WriteLine($"[{Kind}] received {sz} ts {ts}"); #endif } } catch (Exception ex) { if (!Token.IsCancellationRequested) { Console.WriteLine($"Terminating {Kind} thread due to exception: {ex.ToString()}"); } else { Console.WriteLine($"Terminating {Kind} thread"); } } }