Skip to content

Commit

Permalink
push some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lev committed Sep 20, 2024
1 parent 68ca234 commit 70d4c92
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/Asv.IO/ULog/ULogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ public bool TryRead(ref SequenceReader<byte> rdr,out IULogToken? token)
break;
case ReaderState.Corrupted:
corrupted:
// TODO: try to find sync message and switch to DataSection
throw new Exception("Corrupted ULog file. Sync message not implemented.");
if (!InternalReadSyncSequence(ref rdr, ref token))
{
_state = ReaderState.Corrupted;
}

_state = ReaderState.DataSection;
break;
default:
throw new ArgumentOutOfRangeException();
Expand All @@ -105,6 +109,38 @@ public bool TryRead(ref SequenceReader<byte> rdr,out IULogToken? token)
return true;

}

private bool InternalReadSyncSequence(ref SequenceReader<byte> rdr, ref IULogToken? token)
{
token = null;

var buffer = ArrayPool<byte>.Shared.Rent(512);
try
{
var temp = new Span<byte>(buffer);
while (true)
{
if (rdr.TryRead(out var data)) // we have an item to handle
{
// возможно стоит искать токен прям здесь, чтобы в случае обнаружения сразу прекратить чтение
BinSerialize.WriteByte(ref temp, data);
rdr.Advance(sizeof(byte));
}

// попробовать найти хэдер и токен, если получилось найти, то
// вернуть true и сдвинуть каретку на байт после токена

// если не получилось найти
rdr.Rewind(sizeof(ushort) + sizeof(byte) + ULogSynchronizationMessageToken.SyncMagic.Length);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}

return false;
}

private bool InternalReadToken(ref SequenceReader<byte> rdr, ref IULogToken? token)
{
Expand Down

0 comments on commit 70d4c92

Please sign in to comment.