feat: add byte-oriented parser view contract
This commit is contained in:
@@ -38,22 +38,20 @@ public readonly struct ParsedCommand
|
|||||||
public sealed class NatsParser
|
public sealed class NatsParser
|
||||||
{
|
{
|
||||||
private static readonly byte[] CrLfBytes = "\r\n"u8.ToArray();
|
private static readonly byte[] CrLfBytes = "\r\n"u8.ToArray();
|
||||||
private readonly int _maxPayload;
|
|
||||||
private ILogger? _logger;
|
private ILogger? _logger;
|
||||||
public ILogger? Logger { set => _logger = value; }
|
public ILogger? Logger { set => _logger = value; }
|
||||||
|
|
||||||
// State for split-packet payload reading
|
// State for split-packet payload reading
|
||||||
private bool _awaitingPayload;
|
private bool _awaitingPayload;
|
||||||
private int _expectedPayloadSize;
|
private int _expectedPayloadSize;
|
||||||
private string? _pendingSubject;
|
private byte[]? _pendingSubject;
|
||||||
private string? _pendingReplyTo;
|
private byte[]? _pendingReplyTo;
|
||||||
private int _pendingHeaderSize;
|
private int _pendingHeaderSize;
|
||||||
private CommandType _pendingType;
|
private CommandType _pendingType;
|
||||||
private string _pendingOperation = string.Empty;
|
private string _pendingOperation = string.Empty;
|
||||||
|
|
||||||
public NatsParser(int maxPayload = NatsProtocol.MaxPayloadSize, ILogger? logger = null)
|
public NatsParser(int maxPayload = NatsProtocol.MaxPayloadSize, ILogger? logger = null)
|
||||||
{
|
{
|
||||||
_maxPayload = maxPayload;
|
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +69,17 @@ public sealed class NatsParser
|
|||||||
{
|
{
|
||||||
command = default;
|
command = default;
|
||||||
|
|
||||||
|
if (!TryParseView(ref buffer, out var view))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
command = view.Materialize();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal bool TryParseView(ref ReadOnlySequence<byte> buffer, out ParsedCommandView command)
|
||||||
|
{
|
||||||
|
command = default;
|
||||||
|
|
||||||
if (_awaitingPayload)
|
if (_awaitingPayload)
|
||||||
return TryReadPayload(ref buffer, out command);
|
return TryReadPayload(ref buffer, out command);
|
||||||
|
|
||||||
@@ -114,7 +123,7 @@ public sealed class NatsParser
|
|||||||
case (byte)'p':
|
case (byte)'p':
|
||||||
if (b1 == (byte)'i') // PING
|
if (b1 == (byte)'i') // PING
|
||||||
{
|
{
|
||||||
command = ParsedCommand.Simple(CommandType.Ping, "PING");
|
command = ParsedCommandView.Simple(CommandType.Ping, "PING");
|
||||||
buffer = buffer.Slice(reader.Position);
|
buffer = buffer.Slice(reader.Position);
|
||||||
TraceInOp("PING");
|
TraceInOp("PING");
|
||||||
return true;
|
return true;
|
||||||
@@ -122,7 +131,7 @@ public sealed class NatsParser
|
|||||||
|
|
||||||
if (b1 == (byte)'o') // PONG
|
if (b1 == (byte)'o') // PONG
|
||||||
{
|
{
|
||||||
command = ParsedCommand.Simple(CommandType.Pong, "PONG");
|
command = ParsedCommandView.Simple(CommandType.Pong, "PONG");
|
||||||
buffer = buffer.Slice(reader.Position);
|
buffer = buffer.Slice(reader.Position);
|
||||||
TraceInOp("PONG");
|
TraceInOp("PONG");
|
||||||
return true;
|
return true;
|
||||||
@@ -188,13 +197,13 @@ public sealed class NatsParser
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case (byte)'+': // +OK
|
case (byte)'+': // +OK
|
||||||
command = ParsedCommand.Simple(CommandType.Ok, "+OK");
|
command = ParsedCommandView.Simple(CommandType.Ok, "+OK");
|
||||||
buffer = buffer.Slice(reader.Position);
|
buffer = buffer.Slice(reader.Position);
|
||||||
TraceInOp("+OK");
|
TraceInOp("+OK");
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case (byte)'-': // -ERR
|
case (byte)'-': // -ERR
|
||||||
command = ParsedCommand.Simple(CommandType.Err, "-ERR");
|
command = ParsedCommandView.Simple(CommandType.Err, "-ERR");
|
||||||
buffer = buffer.Slice(reader.Position);
|
buffer = buffer.Slice(reader.Position);
|
||||||
TraceInOp("-ERR");
|
TraceInOp("-ERR");
|
||||||
return true;
|
return true;
|
||||||
@@ -227,7 +236,7 @@ public sealed class NatsParser
|
|||||||
Span<byte> line,
|
Span<byte> line,
|
||||||
ref ReadOnlySequence<byte> buffer,
|
ref ReadOnlySequence<byte> buffer,
|
||||||
SequencePosition afterLine,
|
SequencePosition afterLine,
|
||||||
out ParsedCommand command)
|
out ParsedCommandView command)
|
||||||
{
|
{
|
||||||
command = default;
|
command = default;
|
||||||
|
|
||||||
@@ -236,19 +245,19 @@ public sealed class NatsParser
|
|||||||
var argsSpan = line[4..];
|
var argsSpan = line[4..];
|
||||||
int argCount = SplitArgs(argsSpan, ranges);
|
int argCount = SplitArgs(argsSpan, ranges);
|
||||||
|
|
||||||
string subject;
|
byte[] subject;
|
||||||
string? reply = null;
|
byte[]? reply = null;
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
if (argCount == 2)
|
if (argCount == 2)
|
||||||
{
|
{
|
||||||
subject = Encoding.ASCII.GetString(argsSpan[ranges[0]]);
|
subject = argsSpan[ranges[0]].ToArray();
|
||||||
size = ParseSize(argsSpan[ranges[1]]);
|
size = ParseSize(argsSpan[ranges[1]]);
|
||||||
}
|
}
|
||||||
else if (argCount == 3)
|
else if (argCount == 3)
|
||||||
{
|
{
|
||||||
subject = Encoding.ASCII.GetString(argsSpan[ranges[0]]);
|
subject = argsSpan[ranges[0]].ToArray();
|
||||||
reply = Encoding.ASCII.GetString(argsSpan[ranges[1]]);
|
reply = argsSpan[ranges[1]].ToArray();
|
||||||
size = ParseSize(argsSpan[ranges[2]]);
|
size = ParseSize(argsSpan[ranges[2]]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -277,7 +286,7 @@ public sealed class NatsParser
|
|||||||
Span<byte> line,
|
Span<byte> line,
|
||||||
ref ReadOnlySequence<byte> buffer,
|
ref ReadOnlySequence<byte> buffer,
|
||||||
SequencePosition afterLine,
|
SequencePosition afterLine,
|
||||||
out ParsedCommand command)
|
out ParsedCommandView command)
|
||||||
{
|
{
|
||||||
command = default;
|
command = default;
|
||||||
|
|
||||||
@@ -286,20 +295,20 @@ public sealed class NatsParser
|
|||||||
var argsSpan = line[5..];
|
var argsSpan = line[5..];
|
||||||
int argCount = SplitArgs(argsSpan, ranges);
|
int argCount = SplitArgs(argsSpan, ranges);
|
||||||
|
|
||||||
string subject;
|
byte[] subject;
|
||||||
string? reply = null;
|
byte[]? reply = null;
|
||||||
int hdrSize, totalSize;
|
int hdrSize, totalSize;
|
||||||
|
|
||||||
if (argCount == 3)
|
if (argCount == 3)
|
||||||
{
|
{
|
||||||
subject = Encoding.ASCII.GetString(argsSpan[ranges[0]]);
|
subject = argsSpan[ranges[0]].ToArray();
|
||||||
hdrSize = ParseSize(argsSpan[ranges[1]]);
|
hdrSize = ParseSize(argsSpan[ranges[1]]);
|
||||||
totalSize = ParseSize(argsSpan[ranges[2]]);
|
totalSize = ParseSize(argsSpan[ranges[2]]);
|
||||||
}
|
}
|
||||||
else if (argCount == 4)
|
else if (argCount == 4)
|
||||||
{
|
{
|
||||||
subject = Encoding.ASCII.GetString(argsSpan[ranges[0]]);
|
subject = argsSpan[ranges[0]].ToArray();
|
||||||
reply = Encoding.ASCII.GetString(argsSpan[ranges[1]]);
|
reply = argsSpan[ranges[1]].ToArray();
|
||||||
hdrSize = ParseSize(argsSpan[ranges[2]]);
|
hdrSize = ParseSize(argsSpan[ranges[2]]);
|
||||||
totalSize = ParseSize(argsSpan[ranges[3]]);
|
totalSize = ParseSize(argsSpan[ranges[3]]);
|
||||||
}
|
}
|
||||||
@@ -324,7 +333,7 @@ public sealed class NatsParser
|
|||||||
return TryReadPayload(ref buffer, out command);
|
return TryReadPayload(ref buffer, out command);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryReadPayload(ref ReadOnlySequence<byte> buffer, out ParsedCommand command)
|
private bool TryReadPayload(ref ReadOnlySequence<byte> buffer, out ParsedCommandView command)
|
||||||
{
|
{
|
||||||
command = default;
|
command = default;
|
||||||
|
|
||||||
@@ -333,10 +342,7 @@ public sealed class NatsParser
|
|||||||
if (buffer.Length < needed)
|
if (buffer.Length < needed)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Extract payload
|
|
||||||
var payloadSlice = buffer.Slice(0, _expectedPayloadSize);
|
var payloadSlice = buffer.Slice(0, _expectedPayloadSize);
|
||||||
var payload = new byte[_expectedPayloadSize];
|
|
||||||
payloadSlice.CopyTo(payload);
|
|
||||||
|
|
||||||
// Verify \r\n after payload
|
// Verify \r\n after payload
|
||||||
var trailer = buffer.Slice(_expectedPayloadSize, 2);
|
var trailer = buffer.Slice(_expectedPayloadSize, 2);
|
||||||
@@ -345,23 +351,25 @@ public sealed class NatsParser
|
|||||||
if (trailerBytes[0] != (byte)'\r' || trailerBytes[1] != (byte)'\n')
|
if (trailerBytes[0] != (byte)'\r' || trailerBytes[1] != (byte)'\n')
|
||||||
throw new ProtocolViolationException("Expected \\r\\n after payload");
|
throw new ProtocolViolationException("Expected \\r\\n after payload");
|
||||||
|
|
||||||
command = new ParsedCommand
|
command = new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = _pendingType,
|
Type = _pendingType,
|
||||||
Operation = _pendingOperation,
|
Operation = _pendingOperation,
|
||||||
Subject = _pendingSubject,
|
Subject = _pendingSubject,
|
||||||
ReplyTo = _pendingReplyTo,
|
ReplyTo = _pendingReplyTo,
|
||||||
Payload = payload,
|
Payload = payloadSlice,
|
||||||
HeaderSize = _pendingHeaderSize,
|
HeaderSize = _pendingHeaderSize,
|
||||||
MaxMessages = -1,
|
MaxMessages = -1,
|
||||||
};
|
};
|
||||||
|
|
||||||
buffer = buffer.Slice(buffer.GetPosition(needed));
|
buffer = buffer.Slice(buffer.GetPosition(needed));
|
||||||
_awaitingPayload = false;
|
_awaitingPayload = false;
|
||||||
|
_pendingSubject = null;
|
||||||
|
_pendingReplyTo = null;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ParsedCommand ParseSub(Span<byte> line)
|
private static ParsedCommandView ParseSub(Span<byte> line)
|
||||||
{
|
{
|
||||||
// SUB subject [queue] sid -- skip "SUB "
|
// SUB subject [queue] sid -- skip "SUB "
|
||||||
if (line.Length < 5)
|
if (line.Length < 5)
|
||||||
@@ -372,28 +380,28 @@ public sealed class NatsParser
|
|||||||
|
|
||||||
return argCount switch
|
return argCount switch
|
||||||
{
|
{
|
||||||
2 => new ParsedCommand
|
2 => new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = CommandType.Sub,
|
Type = CommandType.Sub,
|
||||||
Operation = "SUB",
|
Operation = "SUB",
|
||||||
Subject = Encoding.ASCII.GetString(argsSpan[ranges[0]]),
|
Subject = CopyBytes(argsSpan[ranges[0]]),
|
||||||
Sid = Encoding.ASCII.GetString(argsSpan[ranges[1]]),
|
Sid = CopyBytes(argsSpan[ranges[1]]),
|
||||||
MaxMessages = -1,
|
MaxMessages = -1,
|
||||||
},
|
},
|
||||||
3 => new ParsedCommand
|
3 => new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = CommandType.Sub,
|
Type = CommandType.Sub,
|
||||||
Operation = "SUB",
|
Operation = "SUB",
|
||||||
Subject = Encoding.ASCII.GetString(argsSpan[ranges[0]]),
|
Subject = CopyBytes(argsSpan[ranges[0]]),
|
||||||
Queue = Encoding.ASCII.GetString(argsSpan[ranges[1]]),
|
Queue = CopyBytes(argsSpan[ranges[1]]),
|
||||||
Sid = Encoding.ASCII.GetString(argsSpan[ranges[2]]),
|
Sid = CopyBytes(argsSpan[ranges[2]]),
|
||||||
MaxMessages = -1,
|
MaxMessages = -1,
|
||||||
},
|
},
|
||||||
_ => throw new ProtocolViolationException("Invalid SUB arguments"),
|
_ => throw new ProtocolViolationException("Invalid SUB arguments"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ParsedCommand ParseUnsub(Span<byte> line)
|
private static ParsedCommandView ParseUnsub(Span<byte> line)
|
||||||
{
|
{
|
||||||
// UNSUB sid [max_msgs] -- skip "UNSUB "
|
// UNSUB sid [max_msgs] -- skip "UNSUB "
|
||||||
if (line.Length < 7)
|
if (line.Length < 7)
|
||||||
@@ -404,25 +412,25 @@ public sealed class NatsParser
|
|||||||
|
|
||||||
return argCount switch
|
return argCount switch
|
||||||
{
|
{
|
||||||
1 => new ParsedCommand
|
1 => new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = CommandType.Unsub,
|
Type = CommandType.Unsub,
|
||||||
Operation = "UNSUB",
|
Operation = "UNSUB",
|
||||||
Sid = Encoding.ASCII.GetString(argsSpan[ranges[0]]),
|
Sid = CopyBytes(argsSpan[ranges[0]]),
|
||||||
MaxMessages = -1,
|
MaxMessages = -1,
|
||||||
},
|
},
|
||||||
2 => new ParsedCommand
|
2 => new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = CommandType.Unsub,
|
Type = CommandType.Unsub,
|
||||||
Operation = "UNSUB",
|
Operation = "UNSUB",
|
||||||
Sid = Encoding.ASCII.GetString(argsSpan[ranges[0]]),
|
Sid = CopyBytes(argsSpan[ranges[0]]),
|
||||||
MaxMessages = ParseSize(argsSpan[ranges[1]]),
|
MaxMessages = ParseSize(argsSpan[ranges[1]]),
|
||||||
},
|
},
|
||||||
_ => throw new ProtocolViolationException("Invalid UNSUB arguments"),
|
_ => throw new ProtocolViolationException("Invalid UNSUB arguments"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ParsedCommand ParseConnect(Span<byte> line)
|
private static ParsedCommandView ParseConnect(Span<byte> line)
|
||||||
{
|
{
|
||||||
// CONNECT {json} -- find first space after command
|
// CONNECT {json} -- find first space after command
|
||||||
int spaceIdx = line.IndexOf((byte)' ');
|
int spaceIdx = line.IndexOf((byte)' ');
|
||||||
@@ -430,16 +438,16 @@ public sealed class NatsParser
|
|||||||
throw new ProtocolViolationException("Invalid CONNECT");
|
throw new ProtocolViolationException("Invalid CONNECT");
|
||||||
|
|
||||||
var json = line[(spaceIdx + 1)..];
|
var json = line[(spaceIdx + 1)..];
|
||||||
return new ParsedCommand
|
return new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = CommandType.Connect,
|
Type = CommandType.Connect,
|
||||||
Operation = "CONNECT",
|
Operation = "CONNECT",
|
||||||
Payload = json.ToArray(),
|
Payload = new ReadOnlySequence<byte>(json.ToArray()),
|
||||||
MaxMessages = -1,
|
MaxMessages = -1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ParsedCommand ParseInfo(Span<byte> line)
|
private static ParsedCommandView ParseInfo(Span<byte> line)
|
||||||
{
|
{
|
||||||
// INFO {json} -- find first space after command
|
// INFO {json} -- find first space after command
|
||||||
int spaceIdx = line.IndexOf((byte)' ');
|
int spaceIdx = line.IndexOf((byte)' ');
|
||||||
@@ -447,15 +455,18 @@ public sealed class NatsParser
|
|||||||
throw new ProtocolViolationException("Invalid INFO");
|
throw new ProtocolViolationException("Invalid INFO");
|
||||||
|
|
||||||
var json = line[(spaceIdx + 1)..];
|
var json = line[(spaceIdx + 1)..];
|
||||||
return new ParsedCommand
|
return new ParsedCommandView
|
||||||
{
|
{
|
||||||
Type = CommandType.Info,
|
Type = CommandType.Info,
|
||||||
Operation = "INFO",
|
Operation = "INFO",
|
||||||
Payload = json.ToArray(),
|
Payload = new ReadOnlySequence<byte>(json.ToArray()),
|
||||||
MaxMessages = -1,
|
MaxMessages = -1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ReadOnlyMemory<byte> CopyBytes(ReadOnlySpan<byte> value) =>
|
||||||
|
value.IsEmpty ? ReadOnlyMemory<byte>.Empty : value.ToArray();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parse a decimal integer from ASCII bytes. Returns -1 on error.
|
/// Parse a decimal integer from ASCII bytes. Returns -1 on error.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
45
src/NATS.Server/Protocol/ParsedCommandView.cs
Normal file
45
src/NATS.Server/Protocol/ParsedCommandView.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System.Buffers;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace NATS.Server.Protocol;
|
||||||
|
|
||||||
|
public readonly struct ParsedCommandView
|
||||||
|
{
|
||||||
|
public CommandType Type { get; init; }
|
||||||
|
public string? Operation { get; init; }
|
||||||
|
public ReadOnlyMemory<byte> Subject { get; init; }
|
||||||
|
public ReadOnlyMemory<byte> ReplyTo { get; init; }
|
||||||
|
public ReadOnlyMemory<byte> Queue { get; init; }
|
||||||
|
public ReadOnlyMemory<byte> Sid { get; init; }
|
||||||
|
public int MaxMessages { get; init; }
|
||||||
|
public int HeaderSize { get; init; }
|
||||||
|
public ReadOnlySequence<byte> Payload { get; init; }
|
||||||
|
|
||||||
|
public static ParsedCommandView Simple(CommandType type, string operation) =>
|
||||||
|
new() { Type = type, Operation = operation, MaxMessages = -1 };
|
||||||
|
|
||||||
|
public ParsedCommand Materialize() =>
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Type = Type,
|
||||||
|
Operation = Operation,
|
||||||
|
Subject = DecodeAsciiOrNull(Subject),
|
||||||
|
ReplyTo = DecodeAsciiOrNull(ReplyTo),
|
||||||
|
Queue = DecodeAsciiOrNull(Queue),
|
||||||
|
Sid = DecodeAsciiOrNull(Sid),
|
||||||
|
MaxMessages = MaxMessages,
|
||||||
|
HeaderSize = HeaderSize,
|
||||||
|
Payload = MaterializePayload(),
|
||||||
|
};
|
||||||
|
|
||||||
|
private ReadOnlyMemory<byte> MaterializePayload()
|
||||||
|
{
|
||||||
|
if (Payload.IsEmpty)
|
||||||
|
return ReadOnlyMemory<byte>.Empty;
|
||||||
|
|
||||||
|
return Payload.IsSingleSegment ? Payload.First : Payload.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string? DecodeAsciiOrNull(ReadOnlyMemory<byte> value) =>
|
||||||
|
value.IsEmpty ? null : Encoding.ASCII.GetString(value.Span);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user