Initial check-in.

This commit is contained in:
Pete Chown
2018-11-06 16:37:23 +00:00
commit 996d961812
5 changed files with 158 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/bin/
/obj/

28
.vscode/launch.json vendored Normal file
View File

@@ -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}"
}
,]
}

15
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/kestrel.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

97
Program.cs Normal file
View File

@@ -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<Context>
{
private WebSocketMiddleware wsMiddleware;
public Application() {
var wsOptions = new WebSocketOptions();
wsMiddleware = new WebSocketMiddleware(continueRequest, new OptionsWrapper<WebSocketOptions>(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<byte>(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<ApplicationLifetime>();
var applicationLifetime = new ApplicationLifetime(lifetimeLogger);
var transportFactory = new LibuvTransportFactory(
new OptionsWrapper<LibuvTransportOptions>(uvOptions), applicationLifetime, loggerFactory);
using (var server = new KestrelServer(new OptionsWrapper<KestrelServerOptions>(serverOptions),
transportFactory, loggerFactory))
{
await server.StartAsync(new Application(), CancellationToken.None);
Console.ReadLine();
}
}
}
}

16
kestrel.csproj Normal file
View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.1.1" />
</ItemGroup>
</Project>