📄
lib/src/DltReader.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
197
198
199
200
201
202
203
using System; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using SharpDlt.Headers; namespace SharpDlt; /// <summary> /// Reads DLT messages from a stream or a DLT file. /// </summary> /// <example> /// <code> /// foreach (var message in DltReader.ReadMessages("trace.dlt")) /// { /// Console.WriteLine(message); /// } /// </code> /// </example> public static class DltReader { /// <summary> /// Reads all DLT messages from <paramref name="stream"/>. /// </summary> /// <param name="stream">The stream to read from. The caller keeps ownership of the stream.</param> /// <param name="options">Options controlling the parsing, <see cref="DltReaderOptions.Default"/> if omitted.</param> /// <returns>A lazily evaluated sequence of DLT messages.</returns> /// <remarks> /// Reading stops silently as soon as the remaining data no longer contains a complete message. /// </remarks> /// <exception cref="DltParseException">The data of a message is malformed.</exception> public static IEnumerable<DltMessage> ReadMessages(Stream stream, DltReaderOptions? options = null) { ArgumentNullException.ThrowIfNull(stream); return Iterate(stream, options ?? DltReaderOptions.Default); static IEnumerable<DltMessage> Iterate(Stream stream, DltReaderOptions options) { var prefixLength = GetPrefixLength(options); var prefix = new byte[prefixLength]; while (true) { if (stream.ReadAtLeast(prefix, prefixLength, throwOnEndOfStream: false) < prefixLength) { yield break; } var buffer = CreateMessageBuffer(prefix, options, out var remaining); if ( remaining > 0 && stream.ReadAtLeast(buffer.AsSpan(prefixLength), remaining, throwOnEndOfStream: false) < remaining ) { yield break; } yield return DltMessage.Parse(buffer, options); } } } /// <summary> /// Opens the DLT file at <paramref name="path"/> and reads all DLT messages from it. /// </summary> /// <param name="path">Path of the DLT file.</param> /// <param name="options">Options controlling the parsing, <see cref="DltReaderOptions.Default"/> if omitted.</param> /// <returns>A lazily evaluated sequence of DLT messages. The file is closed when enumeration ends.</returns> /// <exception cref="DltParseException">The data of a message is malformed.</exception> public static IEnumerable<DltMessage> ReadMessages(string path, DltReaderOptions? options = null) { ArgumentException.ThrowIfNullOrEmpty(path); return Iterate(path, options); static IEnumerable<DltMessage> Iterate(string path, DltReaderOptions? options) { using var stream = OpenFile(path); foreach (var message in ReadMessages(stream, options)) { yield return message; } } } /// <summary> /// Asynchronously reads all DLT messages from <paramref name="stream"/>. /// </summary> /// <param name="stream">The stream to read from. The caller keeps ownership of the stream.</param> /// <param name="options">Options controlling the parsing, <see cref="DltReaderOptions.Default"/> if omitted.</param> /// <param name="cancellationToken">Token to cancel the enumeration.</param> /// <returns>A lazily evaluated asynchronous sequence of DLT messages.</returns> /// <exception cref="DltParseException">The data of a message is malformed.</exception> public static IAsyncEnumerable<DltMessage> ReadMessagesAsync( Stream stream, DltReaderOptions? options = null, CancellationToken cancellationToken = default ) { ArgumentNullException.ThrowIfNull(stream); return Iterate(stream, options ?? DltReaderOptions.Default, cancellationToken); static async IAsyncEnumerable<DltMessage> Iterate( Stream stream, DltReaderOptions options, [EnumeratorCancellation] CancellationToken cancellationToken ) { var prefixLength = GetPrefixLength(options); var prefix = new byte[prefixLength]; while (true) { var read = await stream .ReadAtLeastAsync(prefix, prefixLength, false, cancellationToken) .ConfigureAwait(false); if (read < prefixLength) { yield break; } var buffer = CreateMessageBuffer(prefix, options, out var remaining); if (remaining > 0) { read = await stream .ReadAtLeastAsync(buffer.AsMemory(prefixLength), remaining, false, cancellationToken) .ConfigureAwait(false); if (read < remaining) { yield break; } } yield return DltMessage.Parse(buffer, options); } } } /// <summary> /// Opens the DLT file at <paramref name="path"/> and asynchronously reads all DLT messages from it. /// </summary> /// <param name="path">Path of the DLT file.</param> /// <param name="options">Options controlling the parsing, <see cref="DltReaderOptions.Default"/> if omitted.</param> /// <param name="cancellationToken">Token to cancel the enumeration.</param> /// <returns>A lazily evaluated asynchronous sequence of DLT messages. The file is closed when enumeration ends.</returns> /// <exception cref="DltParseException">The data of a message is malformed.</exception> public static IAsyncEnumerable<DltMessage> ReadMessagesAsync( string path, DltReaderOptions? options = null, CancellationToken cancellationToken = default ) { ArgumentException.ThrowIfNullOrEmpty(path); return Iterate(path, options, cancellationToken); static async IAsyncEnumerable<DltMessage> Iterate( string path, DltReaderOptions? options, [EnumeratorCancellation] CancellationToken cancellationToken ) { await using var stream = OpenFile(path, useAsync: true); await foreach (var message in ReadMessagesAsync(stream, options, cancellationToken).ConfigureAwait(false)) { yield return message; } } } private static FileStream OpenFile(string path, bool useAsync = false) => new( path, new FileStreamOptions { Mode = FileMode.Open, Access = FileAccess.Read, Share = FileShare.Read, Options = FileOptions.SequentialScan | (useAsync ? FileOptions.Asynchronous : 0), } ); private static int GetPrefixLength(DltReaderOptions options) => (options.HasStorageHeader ? StorageHeader.Length : 0) + StandardHeader.MinimumLength; private static byte[] CreateMessageBuffer(byte[] prefix, DltReaderOptions options, out int remaining) { var storageHeaderLength = options.HasStorageHeader ? StorageHeader.Length : 0; var length = StandardHeader.ReadMessageLength(prefix.AsSpan(storageHeaderLength)); if (length < StandardHeader.MinimumLength) { throw new DltParseException( $"Invalid length of a DLT message: {length} / it must be " + $"{StandardHeader.MinimumLength} or more." ); } var buffer = new byte[storageHeaderLength + length]; prefix.CopyTo(buffer.AsSpan()); remaining = buffer.Length - prefix.Length; return buffer; } }