Skip to content

Commit

Permalink
Overhaul part 2/2 (Refactored Language Client, Pipelines and unit tes…
Browse files Browse the repository at this point in the history
…ts!) (#248)

* Updated language client that mirrors the language server.  Added input handler that uses the new pipelines for lower allocations of incomming data.   Added validation tests for handler methods to ensure that all required methods exist when new handlers are added
* Added global request timeout
* fixed disposable when using named pipes
* fixed the issue with notifications cancelling early when they shouldn't
* Added forceful shutdown to the language server, and the ability to capture unhandled exceptions during input processing
* Refactored options to share common code for on handlers and friends
* fixed issue with parsing headers where sometimes the parsing thread would die
* Moved cancellation from internal to the request to inside the scheduler
* Shifted cancellation into input handler logic to allow for proper propogation of cancelled results back to the caller.  Also fixed an issue where request cancellation was not working.  Also request cancellation would not throw custom exceptions for anyone that was task like.
* Added json specific OnNotification on and OnRequest handlers to ensure that the JToken is not sent through the serailzier (this ensure that the casing stays correct)
* Added support for defining request options (currently just for serial/parallel)
Fixed a bug where things would lock due to response tasks being set in the same thread.
  • Loading branch information
david-driscoll authored Jun 2, 2020
1 parent bdec4c7 commit 21f56ec
Show file tree
Hide file tree
Showing 408 changed files with 53,576 additions and 9,380 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 180

[*.xml]
indent_style = space
Expand Down
5 changes: 4 additions & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
<PackageReference Include="Rocket.Surgery.MSBuild.SourceLink" Version="0.3.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="GitVersion.Tool" Version="5.2.4"/>
Expand Down Expand Up @@ -33,12 +34,14 @@
<PackageReference Update="coverlet.collector" Version="1.2.0"/>
<PackageReference Update="coverlet.msbuild" Version="2.8.0"/>
<PackageReference Update="System.Reactive" Version="4.4.1"/>
<PackageReference Update="System.Collections.Immutable" Version="1.7.1"/>
<PackageReference Update="Microsoft.Reactive.Testing" Version="4.3.2"/>
<PackageReference Update="MediatR" Version="8.0.1"/>
<PackageReference Update="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0"/>
<PackageReference Update="Bogus" Version="29.0.1"/>
<PackageReference Update="Snapper" Version="2.2.4"/>
<PackageReference Update="Xunit.SkippableFact" Version="1.4.8" />
<PackageReference Update="DynamicData" Version="6.14.18" />
<PackageReference Update="System.IO.Pipelines" Version="4.7.1"/>
<PackageReference Update="Nerdbank.Streams" Version="2.4.60" />
</ItemGroup>
</Project>
120 changes: 106 additions & 14 deletions LSP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,21 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = ".build", ".build\.build.csp
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".build", ".build", "{26522B49-0743-4CBE-BA67-6D17FF65CAB9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dap.Tests", "test\Dap.Tests\Dap.Tests.csproj", "{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{954FB493-FA91-470B-8A78-FA32A7B05E97}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pipeline", "benchmarks\Pipeline\Pipeline.csproj", "{B781CDC6-34BB-4131-B18A-3B1294F53062}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "src\Shared\Shared.csproj", "{18FB2302-023B-4F6F-9F6D-099B47B69D9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dap.Tests", "test\Dap.Tests\Dap.Tests.csproj", "{6D9E5BF4-4666-476B-AC88-D108A80567F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dap.Client", "src\Dap.Client\Dap.Client.csproj", "{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dap.Testing", "src\Dap.Testing\Dap.Testing.csproj", "{91919C54-3638-4A3C-963A-327D78368EE3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing", "src\Testing\Testing.csproj", "{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonRpc.Testing", "src\JsonRpc.Testing\JsonRpc.Testing.csproj", "{202BA1AB-25DA-44ED-B962-FD82FCC74543}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -195,18 +209,90 @@ Global
{28B13787-A442-4D28-BF9A-3D65BF13AAEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28B13787-A442-4D28-BF9A-3D65BF13AAEC}.Release|x64.ActiveCfg = Release|Any CPU
{28B13787-A442-4D28-BF9A-3D65BF13AAEC}.Release|x86.ActiveCfg = Release|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Debug|x64.ActiveCfg = Debug|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Debug|x64.Build.0 = Debug|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Debug|x86.ActiveCfg = Debug|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Debug|x86.Build.0 = Debug|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Release|Any CPU.Build.0 = Release|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Release|x64.ActiveCfg = Release|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Release|x64.Build.0 = Release|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Release|x86.ActiveCfg = Release|Any CPU
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8}.Release|x86.Build.0 = Release|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Debug|x64.ActiveCfg = Debug|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Debug|x64.Build.0 = Debug|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Debug|x86.ActiveCfg = Debug|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Debug|x86.Build.0 = Debug|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Release|Any CPU.Build.0 = Release|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Release|x64.ActiveCfg = Release|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Release|x64.Build.0 = Release|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Release|x86.ActiveCfg = Release|Any CPU
{B781CDC6-34BB-4131-B18A-3B1294F53062}.Release|x86.Build.0 = Release|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Debug|x64.ActiveCfg = Debug|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Debug|x64.Build.0 = Debug|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Debug|x86.ActiveCfg = Debug|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Debug|x86.Build.0 = Debug|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Release|Any CPU.Build.0 = Release|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Release|x64.ActiveCfg = Release|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Release|x64.Build.0 = Release|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Release|x86.ActiveCfg = Release|Any CPU
{18FB2302-023B-4F6F-9F6D-099B47B69D9F}.Release|x86.Build.0 = Release|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Debug|x64.ActiveCfg = Debug|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Debug|x64.Build.0 = Debug|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Debug|x86.ActiveCfg = Debug|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Debug|x86.Build.0 = Debug|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Release|Any CPU.Build.0 = Release|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Release|x64.ActiveCfg = Release|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Release|x64.Build.0 = Release|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Release|x86.ActiveCfg = Release|Any CPU
{6D9E5BF4-4666-476B-AC88-D108A80567F6}.Release|x86.Build.0 = Release|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Debug|x64.ActiveCfg = Debug|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Debug|x64.Build.0 = Debug|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Debug|x86.ActiveCfg = Debug|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Debug|x86.Build.0 = Debug|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Release|Any CPU.Build.0 = Release|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Release|x64.ActiveCfg = Release|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Release|x64.Build.0 = Release|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Release|x86.ActiveCfg = Release|Any CPU
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6}.Release|x86.Build.0 = Release|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Debug|x64.ActiveCfg = Debug|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Debug|x64.Build.0 = Debug|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Debug|x86.ActiveCfg = Debug|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Debug|x86.Build.0 = Debug|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Release|Any CPU.Build.0 = Release|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Release|x64.ActiveCfg = Release|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Release|x64.Build.0 = Release|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Release|x86.ActiveCfg = Release|Any CPU
{91919C54-3638-4A3C-963A-327D78368EE3}.Release|x86.Build.0 = Release|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Debug|x64.ActiveCfg = Debug|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Debug|x64.Build.0 = Debug|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Debug|x86.ActiveCfg = Debug|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Debug|x86.Build.0 = Debug|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Release|Any CPU.Build.0 = Release|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Release|x64.ActiveCfg = Release|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Release|x64.Build.0 = Release|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Release|x86.ActiveCfg = Release|Any CPU
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9}.Release|x86.Build.0 = Release|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Debug|Any CPU.Build.0 = Debug|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Debug|x64.ActiveCfg = Debug|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Debug|x64.Build.0 = Debug|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Debug|x86.ActiveCfg = Debug|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Debug|x86.Build.0 = Debug|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Release|Any CPU.ActiveCfg = Release|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Release|Any CPU.Build.0 = Release|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Release|x64.ActiveCfg = Release|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Release|x64.Build.0 = Release|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Release|x86.ActiveCfg = Release|Any CPU
{202BA1AB-25DA-44ED-B962-FD82FCC74543}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -223,7 +309,13 @@ Global
{F2C9D555-118E-442B-A953-9A7B58A53F33} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
{E1A9123B-A236-4240-8C82-A61BD85C3BF4} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
{28B13787-A442-4D28-BF9A-3D65BF13AAEC} = {26522B49-0743-4CBE-BA67-6D17FF65CAB9}
{F3DF7917-6AD3-4EAA-A549-555032DDC4F8} = {2F323ED5-EBF8-45E1-B9D3-C014561B3DDA}
{B781CDC6-34BB-4131-B18A-3B1294F53062} = {954FB493-FA91-470B-8A78-FA32A7B05E97}
{18FB2302-023B-4F6F-9F6D-099B47B69D9F} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
{6D9E5BF4-4666-476B-AC88-D108A80567F6} = {2F323ED5-EBF8-45E1-B9D3-C014561B3DDA}
{678A4DD2-A656-4DCC-AE78-F9940C82A6E6} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
{91919C54-3638-4A3C-963A-327D78368EE3} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
{A1EC39EE-AA1F-4EC9-9939-28C3532585C9} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
{202BA1AB-25DA-44ED-B962-FD82FCC74543} = {D764E024-3D3F-4112-B932-2DB722A1BACC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D38DD0EC-D095-4BCD-B8AF-2D788AF3B9AE}
Expand Down
84 changes: 84 additions & 0 deletions benchmarks/Pipeline/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Buffers;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.IO.Pipelines;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Jobs;

namespace Pipeline
{
[MemoryDiagnoser]
// [MinColumn, MaxColumn, MeanColumn, MedianColumn]
[SimpleJob(RuntimeMoniker.Net472)]
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
[SimpleJob(RuntimeMoniker.NetCoreApp21)]
public class ClassicVsPipelines
{
private const string sampleCommand =
"Content-Length: 88\r\n\r\n{\"seq\":1,\"type\":\"response\",\"request_seq\":1,\"success\":true,\"command\":\"command\",\"body\":{}}";

private const string anotherPayload =
"Content-Length: 894\r\n\r\n{\"edit\":{\"documentChanges\":[{\"textDocument\":{\"version\":1,\"uri\":\"file:///abc/123/d.cs\"},\"edits\":[{\"range\":{\"start\":{\"line\":1,\"character\":1},\"end\":{\"line\":2,\"character\":2}},\"newText\":\"new text\"},{\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":4,\"character\":4}},\"newText\":\"new text2\"}]},{\"textDocument\":{\"version\":1,\"uri\":\"file:///abc/123/b.cs\"},\"edits\":[{\"range\":{\"start\":{\"line\":1,\"character\":1},\"end\":{\"line\":2,\"character\":2}},\"newText\":\"new text2\"},{\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":4,\"character\":4}},\"newText\":\"new text3\"}]},{\"kind\":\"create\",\"uri\":\"file:///abc/123/b.cs\",\"options\":{\"overwrite\":true,\"ignoreIfExists\":true}},{\"kind\":\"rename\",\"oldUri\":\"file:///abc/123/b.cs\",\"newUri\":\"file:///abc/123/c.cs\",\"options\":{\"overwrite\":true,\"ignoreIfExists\":true}},{\"kind\":\"delete\",\"uri\":\"file:///abc/123/c.cs\",\"options\":{\"recursive\":false,\"ignoreIfNotExists\":true}}]}}";


private ClassicHandler _classic;
private PipelinesBased _pipelines;

public ClassicVsPipelines()
{
}

[Params(
sampleCommand,
anotherPayload
)]
public string Payload { get; set; }

[Params(
// 10,
100,
1000
)]
public int Count { get; set; }

public byte[] Bytes { get; set; }

[GlobalSetup]
public void SetupPipelines()
{
var bytes = Encoding.ASCII.GetBytes(Payload);
Bytes = Enumerable.Range(0, Count).SelectMany(z => bytes).ToArray();
}

[Benchmark]
public Task Classic()
{
var pipe = new Pipe();
pipe.Writer.Write(Bytes);
pipe.Writer.Complete();
_classic = new ClassicHandler(pipe.Reader.AsStream());
return _classic.ProcessInputStream();
}

[Benchmark]
public Task Pipelines()
{
var pipe = new Pipe();
pipe.Writer.Write(Bytes);
pipe.Writer.Complete();
_pipelines = new PipelinesBased(pipe.Reader);
return _pipelines.ProcessInputStream(CancellationToken.None);
}
}

public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<ClassicVsPipelines>();
}
}
}
88 changes: 88 additions & 0 deletions benchmarks/Pipeline/ClassicHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Pipeline
{
public class ClassicHandler
{
private readonly Stream _input;
public const char CR = '\r';
public const char LF = '\n';
public static char[] CRLF = {CR, LF};
public static char[] HeaderKeys = {CR, LF, ':'};
public const short MinBuffer = 21; // Minimum size of the buffer "Content-Length: X\r\n\r\n"

public ClassicHandler(Stream input)
{
_input = input;
}

// don't be async: We already allocated a seperate thread for this.
public Task ProcessInputStream()
{
// some time to attach a debugger
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));

// header is encoded in ASCII
// "Content-Length: 0" counts bytes for the following content
// content is encoded in UTF-8
while (_input.CanRead)
{
var buffer = new byte[300];
var current = _input.Read(buffer, 0, MinBuffer);
if (current == 0) return Task.CompletedTask; // no more _input
while (current < MinBuffer ||
buffer[current - 4] != CR || buffer[current - 3] != LF ||
buffer[current - 2] != CR || buffer[current - 1] != LF)
{
var n = _input.Read(buffer, current, 1);
if (n == 0) return Task.CompletedTask; // no more _input, mitigates endless loop here.
current += n;
}

var headersContent = System.Text.Encoding.ASCII.GetString(buffer, 0, current);
var headers = headersContent.Split(HeaderKeys, StringSplitOptions.RemoveEmptyEntries);
long length = 0;
for (var i = 1; i < headers.Length; i += 2)
{
// starting at i = 1 instead of 0 won't throw, if we have uneven headers' length
var header = headers[i - 1];
var value = headers[i].Trim();
if (header.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
{
length = 0;
long.TryParse(value, out length);
}
}

if (length == 0 || length >= int.MaxValue)
{
HandleRequest(string.Empty, CancellationToken.None);
}
else
{
var requestBuffer = new byte[length];
var received = 0;
while (received < length)
{
var n = _input.Read(requestBuffer, received, requestBuffer.Length - received);
if (n == 0) return Task.CompletedTask; // no more _input
received += n;
}

// TODO sometimes: encoding should be based on the respective header (including the wrong "utf8" value)
var payload = System.Text.Encoding.ASCII.GetString(requestBuffer);
HandleRequest(payload, CancellationToken.None);
}
}

return Task.CompletedTask;
}

private void HandleRequest(string request, CancellationToken cancellationToken)
{
}
}
}
16 changes: 16 additions & 0 deletions benchmarks/Pipeline/Pipeline.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.IO.Pipelines" Version="4.7.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="Nerdbank.Streams" Version="2.4.60" />
</ItemGroup>

</Project>
Loading

0 comments on commit 21f56ec

Please sign in to comment.