-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommandCollector.cs
More file actions
299 lines (264 loc) · 10.2 KB
/
CommandCollector.cs
File metadata and controls
299 lines (264 loc) · 10.2 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using GTerm.Listeners;
using System.Collections.Concurrent;
namespace GTerm
{
internal class CommandResult
{
public bool Success { get; set; }
public string Command { get; set; } = string.Empty;
public List<OutputLine> Output { get; set; } = [];
public double CollectionDurationMs { get; set; }
public string? Error { get; set; }
}
internal class OutputLine
{
public string Timestamp { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public ColorInfo Color { get; set; } = new();
}
internal class ColorInfo
{
public int R { get; set; }
public int G { get; set; }
public int B { get; set; }
public int A { get; set; }
}
internal class CommandCollector
{
private readonly ILogListener Listener;
private readonly int CollectionWindowMs;
private readonly object Locker = new();
private bool IsCollecting = false;
private bool HasReceivedFirstOutput = false;
private DateTime? CollectionStart;
private readonly ConcurrentQueue<LogEventArgs> CollectedOutput = new();
internal CommandCollector(ILogListener listener, int collectionWindowMs = 1000)
{
this.Listener = listener;
this.CollectionWindowMs = collectionWindowMs;
this.Listener.OnLog += OnLogReceived;
}
private void OnLogReceived(object sender, LogEventArgs args)
{
lock (this.Locker)
{
if (!this.IsCollecting) return;
// First output received - start the timer
if (!this.HasReceivedFirstOutput)
{
this.HasReceivedFirstOutput = true;
this.CollectionStart = DateTime.Now;
LocalLogger.WriteLine($"First output received, starting {this.CollectionWindowMs}ms collection window");
}
// Add to collected output
this.CollectedOutput.Enqueue(args);
}
}
public async Task<CommandResult> CaptureConsoleAsync(int durationMs, CancellationToken cancellationToken = default)
{
lock (this.Locker)
{
if (this.IsCollecting)
{
return new CommandResult
{
Success = false,
Command = $"capture_{durationMs}ms",
Error = "Another command or capture is currently in progress"
};
}
// Start collecting immediately
this.IsCollecting = true;
this.HasReceivedFirstOutput = true; // Skip waiting for first output
this.CollectionStart = DateTime.Now;
this.CollectedOutput.Clear();
}
try
{
LocalLogger.WriteLine($"Capturing console output for {durationMs}ms");
await Task.Delay(durationMs, cancellationToken);
double totalDuration = this.CollectionStart.HasValue
? (DateTime.Now - this.CollectionStart.Value).TotalMilliseconds
: 0;
List<OutputLine> output = [];
while (this.CollectedOutput.TryDequeue(out LogEventArgs? logEvent))
{
if (logEvent != null)
{
output.Add(new OutputLine
{
Timestamp = DateTime.Now.ToString("HH:mm:ss"),
Message = logEvent.Message,
Color = new ColorInfo
{
R = logEvent.Color.R,
G = logEvent.Color.G,
B = logEvent.Color.B,
A = logEvent.Color.A
}
});
}
}
LocalLogger.WriteLine($"Capture completed. Collected {output.Count} output lines in {totalDuration:F0}ms");
return new CommandResult
{
Success = true,
Command = $"capture_{durationMs}ms",
Output = output,
CollectionDurationMs = totalDuration
};
}
catch (Exception ex)
{
return new CommandResult
{
Success = false,
Command = $"capture_{durationMs}ms",
Error = $"Exception: {ex.Message}"
};
}
finally
{
lock (this.Locker)
{
this.IsCollecting = false;
this.HasReceivedFirstOutput = false;
this.CollectionStart = null;
}
}
}
public async Task<CommandResult> ExecuteCommandAsync(string command, int? customCollectionWindowMs = null, CancellationToken cancellationToken = default)
{
if (!this.Listener.IsConnected)
{
return new CommandResult
{
Success = false,
Command = command,
Error = "Not connected to Garry's Mod"
};
}
lock (this.Locker)
{
if (this.IsCollecting)
{
return new CommandResult
{
Success = false,
Command = command,
Error = "Another command is currently being executed"
};
}
// Start collecting
this.IsCollecting = true;
this.HasReceivedFirstOutput = false;
this.CollectionStart = null;
this.CollectedOutput.Clear();
}
try
{
LocalLogger.WriteLine($"Executing command: {command}");
// Send the command
await this.Listener.WriteMessage(command);
// Wait for first output (max 10 seconds timeout)
DateTime startWait = DateTime.Now;
while (!this.HasReceivedFirstOutput)
{
if ((DateTime.Now - startWait).TotalSeconds > 10)
{
return new CommandResult
{
Success = false,
Command = command,
Error = "Timeout waiting for first output (10s)"
};
}
if (cancellationToken.IsCancellationRequested)
{
return new CommandResult
{
Success = false,
Command = command,
Error = "Command execution cancelled"
};
}
await Task.Delay(50, cancellationToken);
}
// Now wait for the collection window
int collectionWindow = customCollectionWindowMs ?? this.CollectionWindowMs;
while (true)
{
lock (this.Locker)
{
if (this.CollectionStart.HasValue)
{
double elapsed = (DateTime.Now - this.CollectionStart.Value).TotalMilliseconds;
if (elapsed >= collectionWindow)
{
break;
}
}
}
if (cancellationToken.IsCancellationRequested)
{
return new CommandResult
{
Success = false,
Command = command,
Error = "Command execution cancelled"
};
}
await Task.Delay(50, cancellationToken);
}
double totalDuration = this.CollectionStart.HasValue
? (DateTime.Now - this.CollectionStart.Value).TotalMilliseconds
: 0;
List<OutputLine> output = [];
while (this.CollectedOutput.TryDequeue(out LogEventArgs? logEvent))
{
if (logEvent != null)
{
output.Add(new OutputLine
{
Timestamp = DateTime.Now.ToString("HH:mm:ss"),
Message = logEvent.Message,
Color = new ColorInfo
{
R = logEvent.Color.R,
G = logEvent.Color.G,
B = logEvent.Color.B,
A = logEvent.Color.A
}
});
}
}
LocalLogger.WriteLine($"Command completed. Collected {output.Count} output lines in {totalDuration:F0}ms");
return new CommandResult
{
Success = true,
Command = command,
Output = output,
CollectionDurationMs = totalDuration
};
}
catch (Exception ex)
{
return new CommandResult
{
Success = false,
Command = command,
Error = $"Exception: {ex.Message}"
};
}
finally
{
lock (this.Locker)
{
this.IsCollecting = false;
this.HasReceivedFirstOutput = false;
this.CollectionStart = null;
}
}
}
}
}