private IEnumerable <IToken> ParseInternal(Stream stream, DbEnvironment env) { IFormatToken previousFormatToken = null; while (stream.Position < stream.Length) { var tokenType = (TokenType)stream.ReadByte(); if (Readers.ContainsKey(tokenType)) { var t = Readers[tokenType](stream, env, previousFormatToken); if (t is IFormatToken token) { Logger.Instance?.WriteLine($"**Set new format token**"); previousFormatToken = token; } yield return(t); } else { Logger.Instance?.WriteLine($"!!! Hit unknown token type {tokenType} !!!"); var t = new CatchAllToken(tokenType); t.Read(stream, env, previousFormatToken); yield return(t); } } }
public void ZeroLengthToken_NoBytesRead(string _, byte type) { var t = new CatchAllToken(type); using (var ms = new MemoryStream()) { t.Read(ms, new DbEnvironment(), null); Assert.AreEqual(0, ms.Position); } }
public void ZeroLengthToken_NoBytesRead(string _, byte type) { var t = new CatchAllToken(type); using (var ms = new MemoryStream()) { // All zero-length tokens are not actually zero-length. // They have a length, but it is derived from prior tokens (eg TDS_ALTROW's length is determined by a prior TDS_ALTFMT token) // As such, we cannot process the tokens with Catch-All without corrupting the stream's next-token position Assert.Throws <InvalidOperationException>(() => t.Read(ms, new DbEnvironment(), null)); } }
public void FixedLengthToken_FixedBytesRead(string _, byte type, int dataLength) { var t = new CatchAllToken(type); using (var ms = new MemoryStream()) { ms.Write(new byte[dataLength]); ms.Seek(0, SeekOrigin.Begin); t.Read(ms, new DbEnvironment(), null); Assert.AreEqual(ms.Length, ms.Position); } }
public void VariableLengthToken_UIntLength_VariableBytesRead(string _, byte type) { uint dataLength = 10; var t = new CatchAllToken(type); using (var ms = new MemoryStream()) { ms.WriteUInt(dataLength); ms.Write(new byte[dataLength]); ms.Seek(0, SeekOrigin.Begin); t.Read(ms, new DbEnvironment(), null); Assert.AreEqual(ms.Length, ms.Position); } }
public IEnumerable <IToken> Read(TokenReceiveStream stream, DbEnvironment env) { IFormatToken previousFormatToken = null; while (stream.DataAvailable) { var rawTokenType = (byte)stream.ReadByte(); var tokenType = (TokenType)rawTokenType; if (Readers.ContainsKey(tokenType)) { var t = Readers[tokenType](stream, env, previousFormatToken); if (t is IFormatToken token) { Logger.Instance?.WriteLine($"**Set new format token**"); previousFormatToken = token; } yield return(t); } else { Logger.Instance?.WriteLine($"!!! Hit unknown token type {tokenType} !!!"); var t = new CatchAllToken(rawTokenType); t.Read(stream, env, previousFormatToken); yield return(t); } if (stream.IsCancelled) { Logger.Instance?.WriteLine($"{nameof(TokenReceiveStream)} - received cancel status flag"); yield return (new DoneToken { Count = 0, Status = DoneToken.DoneStatus.TDS_DONE_ATTN }); } } }