From 996d961812f2e6da32be0e57c221eb181cec3e49 Mon Sep 17 00:00:00 2001 From: Pete Chown Date: Tue, 6 Nov 2018 16:37:23 +0000 Subject: [PATCH] Initial check-in. --- .gitignore | 2 + .vscode/launch.json | 28 +++++++++++++ .vscode/tasks.json | 15 +++++++ Program.cs | 97 +++++++++++++++++++++++++++++++++++++++++++++ kestrel.csproj | 16 ++++++++ 5 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Program.cs create mode 100644 kestrel.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ded7c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/obj/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4a83d94 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/kestrel.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..845855c --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/kestrel.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..e8c57aa --- /dev/null +++ b/Program.cs @@ -0,0 +1,97 @@ +using System; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting.Internal; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; +using Microsoft.AspNetCore.WebSockets; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Primitives; + +namespace Kestrel +{ + class Context + { + public IFeatureCollection features; + + public Context(IFeatureCollection features) + { + this.features = features; + } + } + + class Application : IHttpApplication + { + private WebSocketMiddleware wsMiddleware; + + public Application() { + var wsOptions = new WebSocketOptions(); + wsMiddleware = new WebSocketMiddleware(continueRequest, new OptionsWrapper(wsOptions)); + } + + public Context CreateContext(IFeatureCollection contextFeatures) + { + return new Context(contextFeatures); + } + + public void DisposeContext(Context context, Exception exception) + { + } + + public async Task ProcessRequestAsync(Context context) + { + HttpContext httpContext = new DefaultHttpContext(context.features); + await wsMiddleware.Invoke(httpContext); + } + + private async Task continueRequest(HttpContext httpContext) { + if (httpContext.WebSockets.IsWebSocketRequest) + { + var socket = await httpContext.WebSockets.AcceptWebSocketAsync(); + var message = Encoding.ASCII.GetBytes("hello world"); + await socket.SendAsync(new ArraySegment(message), WebSocketMessageType.Text, true, + CancellationToken.None); + + await socket.ReceiveAsync(new byte[4096], CancellationToken.None); + } + else + { + httpContext.Response.Headers.Add("Content-Type", new StringValues("text/plain")); + await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello world")); + } + } + } + + class Program + { + static async Task Main(string[] args) + { + var serverOptions = new KestrelServerOptions(); + serverOptions.ListenAnyIP(8080); + + var uvOptions = new LibuvTransportOptions(); + + var loggerFactory = new NullLoggerFactory(); + var lifetimeLogger = new NullLogger(); + var applicationLifetime = new ApplicationLifetime(lifetimeLogger); + + var transportFactory = new LibuvTransportFactory( + new OptionsWrapper(uvOptions), applicationLifetime, loggerFactory); + + using (var server = new KestrelServer(new OptionsWrapper(serverOptions), + transportFactory, loggerFactory)) + { + await server.StartAsync(new Application(), CancellationToken.None); + Console.ReadLine(); + } + } + } +} diff --git a/kestrel.csproj b/kestrel.csproj new file mode 100644 index 0000000..08387e0 --- /dev/null +++ b/kestrel.csproj @@ -0,0 +1,16 @@ + + + + Exe + netcoreapp2.1 + 7.1 + + + + + + + + + +