-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
127 lines (108 loc) · 4.07 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Diagnostics.Metrics;
using System.Net.WebSockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace Kestrel;
class Context(IFeatureCollection features)
{
public IFeatureCollection features = features;
}
class Application : IHttpApplication<Context>
{
private readonly WebSocketMiddleware wsMiddleware;
public Application(ILoggerFactory loggerFactory)
{
var wsOptions = new WebSocketOptions();
wsMiddleware = new WebSocketMiddleware(ContinueRequest, new OptionsWrapper<WebSocketOptions>(wsOptions),
loggerFactory);
}
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);
message = new byte[4096];
var received = await socket.ReceiveAsync(message, CancellationToken.None);
if (received.MessageType == WebSocketMessageType.Text)
Console.WriteLine($"Received: {Encoding.ASCII.GetString(message, 0, received.Count)}");
}
else
{
httpContext.Response.Headers.ContentType = "text/plain";
await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello world\n"));
}
}
}
class MeterFactory : IMeterFactory
{
public Meter Create(MeterOptions options) => new(options);
public void Dispose() { }
}
class AppServices : IServiceProvider
{
public object? GetService(Type serviceType)
{
if (serviceType == typeof(ILoggerFactory))
return new NullLoggerFactory();
if (serviceType.FullName == "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.KestrelMetrics")
#pragma warning disable IL2067
return Activator.CreateInstance(serviceType, new MeterFactory());
#pragma warning restore IL2067
return null;
}
}
class Program
{
static async Task Main()
{
KestrelServerOptions serverOptions = new();
if (File.Exists("localhost.pfx"))
{
HttpsConnectionAdapterOptions httpsOptions = new()
{
ServerCertificate = new X509Certificate2("localhost.pfx")
};
serverOptions.ListenAnyIP(8080, options =>
{
options.KestrelServerOptions.ApplicationServices = new AppServices();
options.UseHttps(httpsOptions);
});
}
else
{
serverOptions.ListenAnyIP(8080);
}
var transportOptions = new SocketTransportOptions();
var loggerFactory = new NullLoggerFactory();
var transportFactory = new SocketTransportFactory(
new OptionsWrapper<SocketTransportOptions>(transportOptions), loggerFactory);
using var server = new KestrelServer(
new OptionsWrapper<KestrelServerOptions>(serverOptions), transportFactory, loggerFactory);
await server.StartAsync(new Application(loggerFactory), CancellationToken.None);
Console.WriteLine("Server started");
await Task.Delay(Timeout.Infinite);
}
}