forked from TheAirBlow/HyperSploit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventOutputReceiver.cs
63 lines (54 loc) · 1.6 KB
/
EventOutputReceiver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using AdvancedSharpAdbClient.Receivers;
namespace HyperSploit;
/// <summary>
/// Event-based output receiver
/// </summary>
public class EventOutputReceiver : IShellOutputReceiver {
/// <summary>
/// Output event handler delegate
/// </summary>
public delegate void OutputEventHandler(EventOutputReceiver sender, string line);
/// <summary>
/// On output line event
/// </summary>
public event OutputEventHandler? OnOutput;
/// <summary>
/// Should output be terminated
/// </summary>
private bool _terminate;
/// <summary>
/// Add output line
/// </summary>
/// <param name="line">Line</param>
/// <returns>True on success</returns>
public bool AddOutput(string line) {
if (_terminate) return false;
OnOutput?.Invoke(this, line);
return !_terminate;
}
/// <summary>
/// Flush output
/// </summary>
public void Flush() {
// Do nothing
}
/// <summary>
/// Should output handling be terminated
/// </summary>
public void Terminate()
=> _terminate = true;
/// <summary>
/// Add output line
/// </summary>
/// <param name="line">Line</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>True on success</returns>
public Task<bool> AddOutputAsync(string line, CancellationToken cancellationToken)
=> Task.FromResult(AddOutput(line));
/// <summary>
/// Flush output
/// </summary>
public Task FlushAsync(CancellationToken cancellationToken) {
Flush(); return Task.CompletedTask;
}
}