public static async Task Read(RosBagReader reader, RosBagNodeType nodeType) { bool read = await reader.ReadAsync().ConfigureAwait(false); Assert.IsTrue(read, "Unexpected end of stream"); Assert.AreEqual(nodeType, reader.NodeType); }
public async Task IntegrationTest() { using var stream = Assembly.GetExecutingAssembly() .GetManifestResourceStream(typeof(ReadingTests), name: "testdata.dataset.bag"); using var reader = new RosBagReader(stream); Assert.IsTrue(await reader.ReadAsync().ConfigureAwait(false)); Assert.AreEqual("#ROSBAG V2.0", reader.Text); uint expectedBytes = 69; Assert.IsTrue(await reader.ReadAsync().ConfigureAwait(false)); Assert.AreEqual(RosBagNodeType.RecordHeader, reader.NodeType); Assert.AreEqual(expectedBytes, reader.RemainingEntryBytes); await ReaderAssert.HeaderFieldName(reader, "chunk_count").ConfigureAwait(false); await ReaderAssert.HeaderFieldValue <uint>(reader, 5, ToUInt32).ConfigureAwait(false); expectedBytes -= FieldSize("chunk_count", 4); await ReaderAssert.HeaderFieldName(reader, "conn_count").ConfigureAwait(false); await ReaderAssert.HeaderFieldValue <uint>(reader, 2, ToUInt32).ConfigureAwait(false); expectedBytes -= FieldSize("conn_count", 4); reader.Close(); }
public static async Task HeaderFieldValue <T>(RosBagReader reader, T expectedValue, Func <byte[], T> decoder) { await HeaderFieldValue(reader).ConfigureAwait(false); var decoded = decoder(reader.Bytes.ToArray()); Assert.AreEqual(expectedValue, decoded); }
public static async Task HeaderFieldName(RosBagReader reader, string expectedName = null) { await reader.ReadAsync().ConfigureAwait(false); Assert.AreEqual(RosBagNodeType.HeaderFieldName, reader.NodeType); if (expectedName != null) { Assert.AreEqual(expectedName, reader.Text); } }
public static async Task RecordHeader(RosBagReader reader, uint?expectedBytes = null) { bool result = await reader.ReadAsync().ConfigureAwait(false); Assert.IsTrue(result, "Unexpected end of stream"); Assert.AreEqual(RosBagNodeType.RecordHeader, reader.NodeType); if (expectedBytes != null) { Assert.AreEqual(expectedBytes, reader.RemainingEntryBytes); } }
public static async Task <int> RecordData(RosBagReader reader, int?expectedBytes = null) { await Read(reader, RosBagNodeType.RecordDataHeader).ConfigureAwait(false); await Read(reader, RosBagNodeType.RecordData).ConfigureAwait(false); if (expectedBytes != null) { Assert.AreEqual(expectedBytes, reader.Bytes.Length); } return(reader.Bytes.Length); }
public static async ValueTask <RosBagHeader> ReadBagHeader(this RosBagReader reader, CancellationToken cancellation = default) { if (reader is null) { throw new ArgumentNullException(nameof(reader)); } if (!await reader.ReadRecord(cancellation).ConfigureAwait(false)) { throw new RosBagException(RosBagReader.EndOfStream); } if (reader.NodeType != RosBagNodeType.RecordHeader) { throw new RosBagException(HeaderMissing); } ulong firstRecordOffset = 0; uint connectionCount = 0; uint chunkCount = 0; var fields = RosBagHeader.Fields.None; while (fields != RosBagHeader.Fields.All) { if (!await reader.ReadAsync(cancellation).ConfigureAwait(false)) { throw new RosBagException(RosBagReader.EndOfStream); } if (reader.NodeType != RosBagNodeType.HeaderFieldName) { throw new RosBagException(RequiredFieldMissing); } switch (reader.Text) { case "index_pos": if (fields.HasFlag(RosBagHeader.Fields.FirstRecordOffset)) { throw new RosBagException(RosBagReader.DuplicateField); } firstRecordOffset = await reader.ReadUInt64(cancellation).ConfigureAwait(false); fields |= RosBagHeader.Fields.FirstRecordOffset; break; case "conn_count": if (fields.HasFlag(RosBagHeader.Fields.ConnectionCount)) { throw new RosBagException(RosBagReader.DuplicateField); } connectionCount = await reader.ReadUInt32(cancellation).ConfigureAwait(false); fields |= RosBagHeader.Fields.ConnectionCount; break; case "chunk_count": if (fields.HasFlag(RosBagHeader.Fields.ChunkCount)) { throw new RosBagException(RosBagReader.DuplicateField); } chunkCount = await reader.ReadUInt32(cancellation).ConfigureAwait(false); fields |= RosBagHeader.Fields.ChunkCount; break; case "op": if (!await reader.ReadAsync(cancellation).ConfigureAwait(false)) { throw new RosBagException(RosBagReader.EndOfStream); } if (reader.CurrentRecordType != RosBagRecordType.BagHeader) { throw new RosBagException(HeaderMissing); } fields |= RosBagHeader.Fields.Op; break; default: Debug.WriteLine($"unknown bag header field: {reader.Text}"); if (!await reader.ReadAsync(cancellation).ConfigureAwait(false)) { throw new RosBagException(RosBagReader.EndOfStream); } break; } } Debug.WriteLine("Extra headers in bag header. Size: " + reader.RemainingEntryBytes); // skip remaining fields await reader.Skip(cancellation).ConfigureAwait(false); // skip data if (!await reader.ReadAsync(cancellation).ConfigureAwait(false)) { throw new RosBagException(RosBagReader.EndOfStream); } return(new RosBagHeader( firstRecordOffset: firstRecordOffset, connectionCount: connectionCount, chunkCount: chunkCount)); }
public static async Task HeaderFieldValue(RosBagReader reader, ulong expectedValue) { ulong actual = await reader.ReadUInt64().ConfigureAwait(false); Assert.AreEqual(expectedValue, actual); }
public static async Task HeaderFieldValue(RosBagReader reader) { await reader.ReadAsync().ConfigureAwait(false); Assert.AreEqual(RosBagNodeType.HeaderFieldValue, reader.NodeType); }