async Task EasyRecvDataOneLineRecvCallbackAsync(PipeStream st) { bool returnedFalseOnce = false; var r = new BinaryLineReader(st); while (true) { List <Tuple <Memory <byte>, bool> >?lines = await r.ReadLinesWithTimeoutAsync(1000); if (lines == null) { break; } foreach (var line in lines) { string lineDecoded = line.Item1._GetString(this.OutputEncoding); if (returnedFalseOnce == false) { if (this.Options.EasyOneLineRecvCallbackAsync != null) { try { bool ret = await this.Options.EasyOneLineRecvCallbackAsync(lineDecoded); if (ret == false) { returnedFalseOnce = true; } } catch (Exception ex) { ex._Debug(); returnedFalseOnce = true; } if (returnedFalseOnce) { TaskUtil.StartAsyncTaskAsync(async() => { await Task.Yield(); KillProcessInternal(); await Task.CompletedTask; })._LaissezFaire(true); } } } } } }
public async Task <IReadOnlyList <SslCertCollectorItem> > ExecuteAsync(CancellationToken cancel = default) { int totalCount = Queue.Count; StartFlag.FirstCallOrThrowException(); List <Task> taskList = new List <Task>(); for (int i = 0; i < this.MaxConcurrentTasks; i++) { Task t = WorkerTaskAsync(cancel); taskList.Add(t); } CancellationTokenSource done = new CancellationTokenSource(); TaskUtil.StartAsyncTaskAsync(async() => { CancellationToken doneCancel = done.Token; while (doneCancel.IsCancellationRequested == false) { await doneCancel._WaitUntilCanceledAsync(250); int completed = totalCount - Queue.Count; Con.WriteLine("{0} / {1}", completed._ToString3(), totalCount._ToString3()); } })._LaissezFaire(true); foreach (Task t in taskList) { await t._TryWaitAsync(true); } done._TryCancelNoBlock(); return(ResultList.ToList()); }
// リアルタイムバッファ監視タスク async Task EasyRealtimeBufferMainteTaskAsync(CancellationToken cancel) { // delay time int delay = this.Options.EasyRealtimeRecvBufCallbackDelayTickMsecs; delay = Math.Max(delay, 10); int interval = Math.Max(delay / 10, 1); long lastHash = Secure.RandSInt64_Caution(); long lastStableTick = 0; while (cancel.IsCancellationRequested == false && Proc.HasExited == false) { long currentHash; long currentTick; ReadOnlyMemory <byte> currentMemOutput; ReadOnlyMemory <byte> currentMemErr; lock (this.EasyRealtimeBufLock) { MemoryBuffer <byte> tmp = new MemoryBuffer <byte>(); tmp.Write(this.EasyErrRealtimeBuf.Span); tmp.WriteStr("----test----"); tmp.Write(this.EasyOutputRealtimeBuf.Span); currentHash = Secure.HashSHA1AsLong(tmp.Span); currentTick = this.EasyRealtimeBufLastWriteTick; currentMemOutput = this.EasyOutputRealtimeBuf.Clone(); currentMemErr = this.EasyErrRealtimeBuf.Clone(); } bool exit = false; if (lastHash != currentHash) { //Dbg.Where(); lastHash = currentHash; lastStableTick = currentTick; } else { //Dbg.Where($"lastStableTick = {lastStableTick}, currentTick = {Time.Tick64}, lastHash = {lastHash}"); if ((lastStableTick + delay) <= Time.Tick64) { //Dbg.Where("**********"); try { bool ret = await this.Options.EasyRealtimeRecvBufCallbackAsync !(currentMemOutput, currentMemErr); exit = ret; } catch (Exception ex) { ex._Debug(); exit = true; } } } if (exit) { //Dbg.Where(); TaskUtil.StartAsyncTaskAsync(async() => { await Task.Yield(); KillProcessInternal(); await Task.CompletedTask; })._LaissezFaire(true); return; } await cancel._WaitUntilCanceledAsync(interval); } }
static void StartUpdateLoop() { UpdateMainLoopTaskCancel = new CancellationTokenSource(); UpdateMainLoopTask = TaskUtil.StartAsyncTaskAsync(UpdateRepositoryMainLoopAsync); }
public void Show() { HiveData.SyncWithStorage(HiveSyncFlags.LoadFromFile, true); long pid = HiveData.ManagedData.Pid; int port = HiveData.ManagedData.LocalLogWatchPort; if (pid != 0 && port != 0) { Con.WriteLine("Starting the real-time log session."); Con.WriteLine("Pressing Ctrl + D or Ctrl + Q to disconnect the session."); Con.WriteLine(); Con.WriteLine($"Connecting to localhost:{port} ..."); CancellationTokenSource cancelSource = new CancellationTokenSource(); CancellationToken cancel = cancelSource.Token; Task task = TaskUtil.StartAsyncTaskAsync(async() => { try { using (var sock = await LocalNet.ConnectAsync(new TcpConnectParam(IPAddress.Loopback, port), cancel)) using (var stream = sock.GetStream()) using (MemoryHelper.FastAllocMemoryWithUsing(65536, out Memory <byte> tmp)) { Con.WriteLine("The real-time log session is connected."); Con.WriteLine(); try { while (true) { int r = await stream.ReadAsync(tmp, cancel); if (r <= 0) { break; } ReadOnlyMemory <byte> data = tmp.Slice(0, r); string s = Str.Utf8Encoding.GetString(data.Span); Console.Write(s); } } catch { } Con.WriteLine(); Con.WriteLine("The real-time log session is disconnected."); } } catch (Exception ex) { Con.WriteError(ex.Message); } }); try { while (true) { var key = Console.ReadKey(); if ((key.Key == ConsoleKey.D || key.Key == ConsoleKey.Q) && key.Modifiers == ConsoleModifiers.Control) { break; } } } catch { } cancelSource._TryCancelNoBlock(); task._TryWait(true); } else { Con.WriteLine($"The daemon \"{Name}\" is not running."); } }