-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAfterburnerMeasurementsProvider.cs
196 lines (161 loc) · 5.67 KB
/
AfterburnerMeasurementsProvider.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
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
using Config.Net;
using System.Diagnostics;
using System.Text;
using System.Threading;
namespace AfterburnerViewerServerWin
{
public class AfterburnerMeasurementsProvider : IMeasurementsProvider, IDisposable
{
public event EventHandler<List<AfterburnerMeasurement>>? OnNewMeasurements;
public event EventHandler<string>? OnError;
public string Source { get; private set; } = string.Empty;
public List<MeasurementType>? MeasurementTypes { get; private set; }
private const int SourceReadMinDelayMs = 100;
private const int SourceReadExceptionDelayMs = 1000;
private volatile bool isRunning;
private FileSystemWatcher? sourceWatcher;
private static readonly SemaphoreSlim _semaphore = new(1, 1);
private bool disposedValue;
public bool Start(string source)
{
try
{
if (!IsValidSource(source))
throw new InvalidOperationException("Invalid source file");
if (isRunning)
throw new InvalidOperationException("Already started");
Source = source;
InitSourceMonitoring();
isRunning = true;
}
catch (Exception e)
{
OnError?.Invoke(this, e.Message);
return false;
}
return true;
}
public bool Stop(bool notifyOnError = true)
{
try
{
if (!isRunning)
throw new InvalidOperationException("Cannot stop, not running");
DestroySourceMonitoring();
isRunning = false;
}
catch (Exception e)
{
if (notifyOnError)
OnError?.Invoke(this, e.Message);
return false;
}
return true;
}
private void InitSourceMonitoring()
{
if (sourceWatcher != null)
throw new InvalidOperationException("Already monitoring");
Debug.Assert(Source != null);
sourceWatcher = new FileSystemWatcher(Path.GetDirectoryName(Source)
?? throw new InvalidOperationException("Invalid source file path"))
{
Filter = Path.GetFileName(Source),
NotifyFilter = NotifyFilters.LastWrite
};
sourceWatcher.Changed += OnSourceWatcher_Changed;
sourceWatcher.EnableRaisingEvents = true;
}
private void DestroySourceMonitoring()
{
if (sourceWatcher == null)
throw new InvalidOperationException("Not monitoring");
sourceWatcher.EnableRaisingEvents = false;
sourceWatcher.Changed -= OnSourceWatcher_Changed;
sourceWatcher.Dispose();
sourceWatcher = null;
}
private async void OnSourceWatcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
if (!await _semaphore.WaitAsync(0))
{
#if DEBUG
OnError?.Invoke(this, "Already reading, skipping this read.");
#endif
return;
}
} catch { return; }
try
{
Thread.Sleep(SourceReadMinDelayMs);
#if DEBUG
OnError?.Invoke(this, "Reading...");
#endif
const int defaultBuffSize = 4096;
using var fs = new FileStream(Source, FileMode.Open, FileAccess.Read, FileShare.Read, defaultBuffSize);
using var sr = new StreamReader(fs, Encoding.Latin1, true, defaultBuffSize);
fs.Seek(0, SeekOrigin.Begin);
MeasurementTypes = AfterburnerParser.ReadMeasurementTypes(sr);
if (MeasurementTypes == null || MeasurementTypes.Count == 0)
return;
fs.Seek(-defaultBuffSize, SeekOrigin.End);
string? lastLine = await getLastLineAsync();
if (string.IsNullOrEmpty(lastLine))
return;
OnNewMeasurements?.Invoke(this,
AfterburnerParser.ExtractMeasurements(lastLine, MeasurementTypes));
async Task<string?> getLastLineAsync()
{
string? line = null;
while (!sr.EndOfStream)
line = await sr.ReadLineAsync();
line = string.IsNullOrWhiteSpace(line)
? null
: line;
return line;
}
}
catch (Exception ex)
{
#if DEBUG
OnError?.Invoke(this, ex.Message);
#endif
Thread.Sleep(SourceReadExceptionDelayMs);
}
finally
{
try
{
_semaphore.Release();
}
catch { }
}
}
public bool IsValidSource(string? source)
{
return !string.IsNullOrWhiteSpace(source) && File.Exists(source);
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Stop();
OnNewMeasurements = null;
OnError = null;
_semaphore.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}