-
Notifications
You must be signed in to change notification settings - Fork 1.2k
preserve unread data across detach #831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
interface95
wants to merge
5
commits into
kerryjiang:master
Choose a base branch
from
interface95:fix-kestrel-detach-handoff
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
125f412
preserve unread data across detach
claude 3f2b25e
add Kestrel HTTP2 detach demo
claude ab59f23
include detach demo in samples solution
claude 0c368a9
docs(connection): comment why PipeConnectionBase preserves unread byt…
claude ce9c073
refactor(connection): consolidate two AdvanceTo calls into single adv…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| using System.Buffers; | ||
| using System.Text; | ||
|
|
||
| namespace KestrelHttp2DetachDemo; | ||
|
|
||
| internal enum Http2FrameType : byte | ||
| { | ||
| Data = 0x0, | ||
| Settings = 0x4 | ||
| } | ||
|
|
||
| internal enum Http2FrameFlags : byte | ||
| { | ||
| None = 0x0, | ||
| Ack = 0x1, | ||
| EndStream = 0x1 | ||
| } | ||
|
|
||
| internal sealed class Http2Frame | ||
| { | ||
| public const int HeaderLength = 9; | ||
|
|
||
| public Http2Frame(int length, Http2FrameType type, byte flags, int streamId, byte[] payload) | ||
| { | ||
| Length = length; | ||
| Type = type; | ||
| Flags = flags; | ||
| StreamId = streamId; | ||
| Payload = payload; | ||
| } | ||
|
|
||
| public int Length { get; } | ||
|
|
||
| public Http2FrameType Type { get; } | ||
|
|
||
| public byte Flags { get; } | ||
|
|
||
| public int StreamId { get; } | ||
|
|
||
| public byte[] Payload { get; } | ||
|
|
||
| public bool IsSettingsAck => Type == Http2FrameType.Settings && (Flags & (byte)Http2FrameFlags.Ack) != 0; | ||
|
|
||
| public string PayloadText => Encoding.UTF8.GetString(Payload); | ||
|
|
||
| public static Http2Frame Read(ReadOnlySequence<byte> buffer) | ||
| { | ||
| Span<byte> header = stackalloc byte[HeaderLength]; | ||
| buffer.Slice(0, HeaderLength).CopyTo(header); | ||
|
|
||
| var length = ReadUInt24(header); | ||
| var type = (Http2FrameType)header[3]; | ||
| var flags = header[4]; | ||
| var streamId = ReadInt31(header.Slice(5, 4)); | ||
| var payload = buffer.Slice(HeaderLength, length).ToArray(); | ||
|
|
||
| return new Http2Frame(length, type, flags, streamId, payload); | ||
| } | ||
|
|
||
| public static byte[] Write(Http2FrameType type, byte flags, int streamId, ReadOnlySpan<byte> payload) | ||
| { | ||
| var frame = new byte[HeaderLength + payload.Length]; | ||
| WriteUInt24(frame, payload.Length); | ||
| frame[3] = (byte)type; | ||
| frame[4] = flags; | ||
| WriteInt31(frame.AsSpan(5, 4), streamId); | ||
| payload.CopyTo(frame.AsSpan(HeaderLength)); | ||
| return frame; | ||
| } | ||
|
|
||
| private static int ReadUInt24(ReadOnlySpan<byte> source) | ||
| { | ||
| return (source[0] << 16) | (source[1] << 8) | source[2]; | ||
| } | ||
|
|
||
| private static void WriteUInt24(Span<byte> destination, int value) | ||
| { | ||
| destination[0] = (byte)((value >> 16) & 0xff); | ||
| destination[1] = (byte)((value >> 8) & 0xff); | ||
| destination[2] = (byte)(value & 0xff); | ||
| } | ||
|
|
||
| private static int ReadInt31(ReadOnlySpan<byte> source) | ||
| { | ||
| return ((source[0] & 0x7f) << 24) | (source[1] << 16) | (source[2] << 8) | source[3]; | ||
| } | ||
|
|
||
| private static void WriteInt31(Span<byte> destination, int value) | ||
| { | ||
| destination[0] = (byte)((value >> 24) & 0x7f); | ||
| destination[1] = (byte)((value >> 16) & 0xff); | ||
| destination[2] = (byte)((value >> 8) & 0xff); | ||
| destination[3] = (byte)(value & 0xff); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System.Buffers; | ||
| using System.Text; | ||
| using SuperSocket.ProtoBase; | ||
|
|
||
| namespace KestrelHttp2DetachDemo; | ||
|
|
||
| internal sealed class Http2PipelineFilter : PipelineFilterBase<Http2Frame> | ||
| { | ||
| private static readonly byte[] ClientPreface = Encoding.ASCII.GetBytes("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"); | ||
|
|
||
| private readonly bool _expectClientPreface; | ||
| private bool _clientPrefaceRead; | ||
| private bool _foundHeader; | ||
| private int _totalSize; | ||
|
|
||
| public Http2PipelineFilter(bool expectClientPreface = true) | ||
| { | ||
| _expectClientPreface = expectClientPreface; | ||
| } | ||
|
|
||
| public override Http2Frame Filter(ref SequenceReader<byte> reader) | ||
| { | ||
| if (_expectClientPreface && !_clientPrefaceRead) | ||
| { | ||
| if (reader.Remaining < ClientPreface.Length) | ||
| return null!; | ||
|
|
||
| Span<byte> preface = stackalloc byte[24]; | ||
| reader.Sequence.Slice(reader.Position, ClientPreface.Length).CopyTo(preface); | ||
|
|
||
| if (!preface.SequenceEqual(ClientPreface)) | ||
| throw new ProtocolException("The client did not send a valid HTTP/2 connection preface."); | ||
|
|
||
| reader.Advance(ClientPreface.Length); | ||
| _clientPrefaceRead = true; | ||
| } | ||
|
|
||
| if (!_foundHeader) | ||
| { | ||
| if (reader.Remaining < Http2Frame.HeaderLength) | ||
| return null!; | ||
|
|
||
| var header = reader.Sequence.Slice(reader.Position, Http2Frame.HeaderLength); | ||
| var bodyLength = GetBodyLengthFromHeader(header); | ||
|
|
||
| if (bodyLength < 0) | ||
| throw new ProtocolException("Failed to get body length from the HTTP/2 frame header."); | ||
|
|
||
| _foundHeader = true; | ||
| _totalSize = Http2Frame.HeaderLength + bodyLength; | ||
| } | ||
|
|
||
| if (reader.Remaining < _totalSize) | ||
| return null!; | ||
|
|
||
| var package = reader.Sequence.Slice(reader.Position, _totalSize); | ||
| var frame = Http2Frame.Read(package); | ||
| reader.Advance(_totalSize); | ||
| _foundHeader = false; | ||
| _totalSize = 0; | ||
|
|
||
| return frame; | ||
| } | ||
|
|
||
| public override void Reset() | ||
| { | ||
| base.Reset(); | ||
| _foundHeader = false; | ||
| _totalSize = 0; | ||
| } | ||
|
|
||
| private static int GetBodyLengthFromHeader(ReadOnlySequence<byte> headerBuffer) | ||
| { | ||
| Span<byte> header = stackalloc byte[3]; | ||
| headerBuffer.Slice(0, 3).CopyTo(header); | ||
| return (header[0] << 16) | (header[1] << 8) | header[2]; | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
samples/KestrelHttp2DetachDemo/KestrelHttp2DetachDemo.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>$(SamplesTargetFramework)</TargetFramework> | ||
| <IsPackable>false</IsPackable> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Description>Demonstrates a minimal HTTP/2 preface and SETTINGS handshake over SuperSocket.Kestrel, then DetachAsync and exact raw data transfer over the same Kestrel transport.</Description> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
| <ProjectReference Include="../../src/SuperSocket.Connection/SuperSocket.Connection.csproj" /> | ||
| <ProjectReference Include="../../src/SuperSocket.Kestrel/SuperSocket.Kestrel.csproj" /> | ||
| <ProjectReference Include="../../src/SuperSocket.ProtoBase/SuperSocket.ProtoBase.csproj" /> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Console" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be in SuperSocket.Http?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I kept it in the sample because it is currently only a minimal HTTP/2 frame parser for demonstrating the Kestrel detach handoff scenario, not a reusable/general HTTP implementation yet. If you prefer, I can move it under
SuperSocket.Httpand wire the sample to consume it from there.