Skip to content

Commit 862f395

Browse files
committed
Added protocol version support
1 parent a4a8767 commit 862f395

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/cluster/DotNext.Net.Cluster/ExceptionMessages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ internal static string UnknownRaftMessageType<T>(T messageType)
5959

6060
internal static string ConnectionTimedOut => (string)Resources.Get();
6161

62-
internal static string PipeTimedOut => (string)Resources.Get();
62+
internal static string BadProtocolVersion(byte version) => Resources.Get().Format(version);
6363
}

src/cluster/DotNext.Net.Cluster/ExceptionMessages.restext

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ InvalidPartitionFormat=Partition has invalid binary format or broken
2121
StreamRejected=The stream has been rejected by the server due to overflow of its backlog
2222
ConnectionClosed=The underlying connection is closed
2323
ConnectionTimedOut=The data cannot be sent or received on time
24-
PipeTimedOut=The data cannot be written on time
24+
BadProtocolVersion=Multiplexing protocol version is not supported: {0}

src/cluster/DotNext.Net.Cluster/Net/Multiplexing/FrameHeader.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ namespace DotNext.Net.Multiplexing;
1414
[StructLayout(LayoutKind.Auto)]
1515
internal readonly struct FrameHeader(ulong id, FrameControl control, ushort length) : IBinaryFormattable<FrameHeader>
1616
{
17+
private const byte CurrentVersion = 0;
18+
1719
/// <summary>
1820
/// All protocol-specific fragments have ID = 0.
1921
/// </summary>
2022
public const ulong SystemStreamId = 0U;
21-
22-
public const int Size = sizeof(ulong) + sizeof(FrameControl) + sizeof(ushort);
23+
24+
public const int Size = sizeof(byte) + sizeof(ulong) + sizeof(FrameControl) + sizeof(ushort);
2325

2426
public ulong Id => id; // if 0, then the packet is protocol-specific
2527
public FrameControl Control => control;
@@ -32,6 +34,7 @@ internal readonly struct FrameHeader(ulong id, FrameControl control, ushort leng
3234
public void Format(Span<byte> destination)
3335
{
3436
var writer = new SpanWriter<byte>(destination);
37+
writer.Add(CurrentVersion);
3538
writer.WriteLittleEndian(id);
3639
writer.WriteLittleEndian((ushort)control);
3740
writer.WriteLittleEndian(length);
@@ -40,6 +43,11 @@ public void Format(Span<byte> destination)
4043
public static FrameHeader Parse(ReadOnlySpan<byte> source)
4144
{
4245
var reader = new SpanReader<byte>(source);
46+
var version = reader.Read();
47+
48+
if (version > CurrentVersion)
49+
throw new UnsupportedVersionException(version);
50+
4351
return new(
4452
reader.ReadLittleEndian<ulong>(),
4553
(FrameControl)reader.ReadLittleEndian<ushort>(),

src/cluster/DotNext.Net.Cluster/Net/Multiplexing/MultiplexingProtocolException.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ internal StreamRejectedException()
2222
: base(ExceptionMessages.StreamRejected)
2323
{
2424
}
25+
}
26+
27+
/// <summary>
28+
/// Indicates that the protocol version is not supported.
29+
/// </summary>
30+
public sealed class UnsupportedVersionException : MultiplexingProtocolException
31+
{
32+
internal UnsupportedVersionException(byte version)
33+
: base(ExceptionMessages.BadProtocolVersion(version))
34+
{
35+
}
2536
}

0 commit comments

Comments
 (0)