### OnNext method signature for INmeaAisMessageStreamProcessor Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Signature for the OnNext method required by INmeaAisMessageStreamProcessor. Use the asciiPayload and padding arguments to access the full payload, as firstLine only provides the first sentence for fragmented messages. ```csharp void OnNext( in NmeaLineParser firstLine, in ReadOnlySpan asciiPayload, uint padding); ``` -------------------------------- ### Convert AIS text field to string using NmeaAisTextFieldParser Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Shows how to retrieve a text field (e.g., vessel name) as a string by writing it into a stack-allocated Span and then converting to ASCII. The comment warns that this causes an allocation and should be avoided unless necessary. ```csharp var parser = new NmeaAisStaticAndVoyageRelatedDataParser( lineParser.Payload, lineParser.Padding); Span vesselNameAscii = stackalloc byte[(int)parser.VesselName.CharacterCount]; parser.VesselName.WriteAsAscii(vesselNameAscii); // CAUTION: this will cause an allocation. Don't do this unless you have to string vesselName = Encoding.ASCII.GetString(vesselNameAscii); ``` -------------------------------- ### Parse AIS position with NmeaAisPositionReportClassAParser Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Shows how to use NmeaAisPositionReportClassAParser in a synchronous method. The parser is a ref struct, so it can only live on the stack; this method returns a tuple of latitude and longitude. ```csharp public static (double Latitude, double Longitude) GetPosition(byte[] asciiNmeaLine) { var parser = new NmeaAisPositionReportClassAParser(asciiNmeaLine); return (parser.Latitude10000thMins / 600000, parser.Longitude10000thMins / 600000); } ``` -------------------------------- ### Parse AIS messages from a file with NmeaStreamParser.ParseFileAsync Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Call NmeaStreamParser.ParseFileAsync with a processor implementing INmeaAisMessageStreamProcessor to process messages from a file. The processor's OnNext method is invoked for each complete AIS message. ```csharp public static async Task ProcessMessagesFromFile(string path) { await NmeaStreamParser.ParseFileAsync(path, Processor).ConfigureAwait(false); } ``` -------------------------------- ### OnError method signature in INmeaLineStreamProcessor/INmeaAisMessageStreamProcessor Source: https://github.com/ais-dotnet/ais.net/blob/main/docs/adr/0001-error-handling-strategy.md This method is required by both INmeaLineStreamProcessor and INmeaAisMessageStreamProcessor interfaces. It receives the problematic line, the exception, and the line number. Use it to handle errors without stopping the stream. ```csharp void OnError(in ReadOnlySpan line, Exception error, int lineNumber); ``` -------------------------------- ### Use ref struct parser in async method via nested method Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Shows how to use a ref struct parser inside an async method by placing it in a nested non-async method. The nested method is allowed to have local ref struct variables, while the containing async method is not. ```csharp public static async Task<(double latitude, double longitude)> GetPositionAsync() { ReadOnlyMemory line = await GetLineAsync(); return ParseLine(); (double latitude, double longitude) ParseLine() { var parser = new NmeaAisPositionReportClassAParser(line.Span, 0); return (parser.Latitude10000thMins / 60000.0, parser.Longitude10000thMins / 60000.0); } } ``` -------------------------------- ### Peek AIS message type with NmeaPayloadParser.PeekMessageType Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Determine the AIS message type from the payload using NmeaPayloadParser.PeekMessageType. The returned integer can be used to select the appropriate parser for the message. ```csharp int messageType = NmeaPayloadParser.PeekMessageType(asciiPayload, padding); ``` -------------------------------- ### Compiler error: ref struct field in non-ref-struct Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Illustrates a compiler error: a ref struct (NmeaAisPositionReportClassAParser) cannot be used as a field in a non-ref-struct type. This code will not compile. ```csharp public class PositionExtractor { // This will produce a compiler error, because you cannot have a ref struct // a field in a non-ref-struct type. private readonly NmeaAisPositionReportClassAParser parser; public PositionExtractor(byte[] asciiNmeaLine) { this.parser = new NmeaAisPositionReportClassAParser(asciiNmeaLine); } } ``` -------------------------------- ### Parse AIS position report and output coordinates Source: https://github.com/ais-dotnet/ais.net/blob/main/Documentation/articles/intro.md Parses an AIS position report (message types 1-3) using NmeaAisPositionReportClassAParser and prints the latitude and longitude in decimal degrees. The parser is constructed with the ASCII payload and padding. ```csharp if (messageType >= 1 && messageType <= 3) { var parser = new NmeaAisPositionReportClassAParser(asciiPayload, padding); Console.WriteLine($"Position: {parser.Latitude10000thMins / 60000.0},{parser.Longitude10000thMins / 60000.0}"); } ```