diff --git a/src/NATS.Server/Protocol/NatsParser.cs b/src/NATS.Server/Protocol/NatsParser.cs index 85063fc..eb23958 100644 --- a/src/NATS.Server/Protocol/NatsParser.cs +++ b/src/NATS.Server/Protocol/NatsParser.cs @@ -37,7 +37,7 @@ public readonly struct ParsedCommand public sealed class NatsParser { - private static readonly byte[] CrLfBytes = "\r\n"u8.ToArray(); + private static ReadOnlySpan CrLfBytes => "\r\n"u8; private ILogger? _logger; public ILogger? Logger { set => _logger = value; } @@ -85,7 +85,7 @@ public sealed class NatsParser // Look for \r\n to find control line var reader = new SequenceReader(buffer); - if (!reader.TryReadTo(out ReadOnlySequence line, CrLfBytes.AsSpan())) + if (!reader.TryReadTo(out ReadOnlySequence line, CrLfBytes)) return false; // Control line size check @@ -177,7 +177,7 @@ public sealed class NatsParser case (byte)'c': if (b1 == (byte)'o') // CONNECT { - command = ParseConnect(lineSpan); + command = ParseConnect(line); buffer = buffer.Slice(reader.Position); TraceInOp("CONNECT"); return true; @@ -188,7 +188,7 @@ public sealed class NatsParser case (byte)'i': if (b1 == (byte)'n') // INFO { - command = ParseInfo(lineSpan); + command = ParseInfo(line); buffer = buffer.Slice(reader.Position); TraceInOp("INFO"); return true; @@ -430,36 +430,34 @@ public sealed class NatsParser }; } - private static ParsedCommandView ParseConnect(Span line) + private static ParsedCommandView ParseConnect(ReadOnlySequence line) { - // CONNECT {json} -- find first space after command - int spaceIdx = line.IndexOf((byte)' '); - if (spaceIdx < 0) + var reader = new SequenceReader(line); + if (!reader.TryAdvanceTo((byte)' ', advancePastDelimiter: true)) throw new ProtocolViolationException("Invalid CONNECT"); - var json = line[(spaceIdx + 1)..]; + var json = line.Slice(reader.Position); return new ParsedCommandView { Type = CommandType.Connect, Operation = "CONNECT", - Payload = new ReadOnlySequence(json.ToArray()), + Payload = json, MaxMessages = -1, }; } - private static ParsedCommandView ParseInfo(Span line) + private static ParsedCommandView ParseInfo(ReadOnlySequence line) { - // INFO {json} -- find first space after command - int spaceIdx = line.IndexOf((byte)' '); - if (spaceIdx < 0) + var reader = new SequenceReader(line); + if (!reader.TryAdvanceTo((byte)' ', advancePastDelimiter: true)) throw new ProtocolViolationException("Invalid INFO"); - var json = line[(spaceIdx + 1)..]; + var json = line.Slice(reader.Position); return new ParsedCommandView { Type = CommandType.Info, Operation = "INFO", - Payload = new ReadOnlySequence(json.ToArray()), + Payload = json, MaxMessages = -1, }; }