diff --git a/Esiur.ASPNet/Controllers/MainController.cs b/Esiur.ASPNet/Controllers/MainController.cs index 87b0015..f52caf0 100644 --- a/Esiur.ASPNet/Controllers/MainController.cs +++ b/Esiur.ASPNet/Controllers/MainController.cs @@ -2,7 +2,7 @@ using Esiur.Core; using Esiur.Resource; using Microsoft.AspNetCore.Mvc; -namespace Esiur.ASPNet.Controllers +namespace Esiur.AspNetCore.Example { [ApiController] [Route("[controller]")] diff --git a/Esiur.ASPNet/Esiur.ASPNet.csproj b/Esiur.ASPNet/Esiur.AspNetCore.Example.csproj similarity index 80% rename from Esiur.ASPNet/Esiur.ASPNet.csproj rename to Esiur.ASPNet/Esiur.AspNetCore.Example.csproj index bdd2ea1..bf49d68 100644 --- a/Esiur.ASPNet/Esiur.ASPNet.csproj +++ b/Esiur.ASPNet/Esiur.AspNetCore.Example.csproj @@ -11,7 +11,8 @@ - + + diff --git a/Esiur.ASPNet/Esiur.ASPNet.http b/Esiur.ASPNet/Esiur.AspNetCore.Example.http similarity index 100% rename from Esiur.ASPNet/Esiur.ASPNet.http rename to Esiur.ASPNet/Esiur.AspNetCore.Example.http diff --git a/Esiur.ASPNet/MyResource.cs b/Esiur.ASPNet/MyResource.cs index ad022b4..1fb03eb 100644 --- a/Esiur.ASPNet/MyResource.cs +++ b/Esiur.ASPNet/MyResource.cs @@ -1,6 +1,6 @@ using Esiur.Resource; -namespace Esiur.ASPNet +namespace Esiur.AspNetCore.Example { [Resource] public partial class MyResource diff --git a/Esiur.ASPNet/Program.cs b/Esiur.ASPNet/Program.cs index 5af07c4..2778b3a 100644 --- a/Esiur.ASPNet/Program.cs +++ b/Esiur.ASPNet/Program.cs @@ -1,4 +1,5 @@ -using Esiur.ASPNet; +using Esiur.AspNetCore; +using Esiur.AspNetCore.Example; using Esiur.Core; using Esiur.Net.IIP; using Esiur.Net.Sockets; @@ -33,7 +34,7 @@ app.UseWebSockets(webSocketOptions); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) //{ - // app.UseSwagger(); +// app.UseSwagger(); // app.UseSwaggerUI(); //} @@ -50,26 +51,7 @@ var server = await Warehouse.Put("sys/server", new DistributedServer()); await Warehouse.Open(); -app.Use(async (context, next) => -{ - var buffer = new ArraySegment(new byte[10240]); - - if (context.WebSockets.IsWebSocketRequest) - { - var webSocket = await context.WebSockets.AcceptWebSocketAsync("iip"); - var socket = new FrameworkWebSocket(webSocket); - var iipConnection = new DistributedConnection(); - server.Add(iipConnection); - iipConnection.Assign(socket); - socket.Begin(); - - while (webSocket.State == WebSocketState.Open) ; - } - else - { - await next(context); - } -}); +app.UseEsiur(new EsiurOptions() { Server = server }); await app.RunAsync(); diff --git a/Esiur.AspNetCore/Esiur.AspNetCore.csproj b/Esiur.AspNetCore/Esiur.AspNetCore.csproj new file mode 100644 index 0000000..b17acd2 --- /dev/null +++ b/Esiur.AspNetCore/Esiur.AspNetCore.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/Esiur.AspNetCore/EsiurMiddleware.cs b/Esiur.AspNetCore/EsiurMiddleware.cs new file mode 100644 index 0000000..f2f2c26 --- /dev/null +++ b/Esiur.AspNetCore/EsiurMiddleware.cs @@ -0,0 +1,47 @@ + +using Esiur.Net.IIP; +using Esiur.Net.Sockets; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System.Net.WebSockets; + +namespace Esiur.AspNetCore +{ + public class EsiurMiddleware + { + readonly DistributedServer server; + readonly RequestDelegate next; + readonly ILoggerFactory loggerFactory; + + public async Task InvokeAsync(HttpContext context) + { + var buffer = new ArraySegment(new byte[10240]); + + if (context.WebSockets.IsWebSocketRequest) + { + var webSocket = await context.WebSockets.AcceptWebSocketAsync("iip"); + var socket = new FrameworkWebSocket(webSocket); + var iipConnection = new DistributedConnection(); + server.Add(iipConnection); + iipConnection.Assign(socket); + socket.Begin(); + + while (webSocket.State == WebSocketState.Open) ; + } + else + { + await next(context); + } + + } + + + public EsiurMiddleware(RequestDelegate next, IOptions options, ILoggerFactory loggerFactory) + { + this.server = options.Value.Server; + this.loggerFactory = loggerFactory; + this.next = next; + } + } +} diff --git a/Esiur.AspNetCore/EsiurMiddlewareExtensions.cs b/Esiur.AspNetCore/EsiurMiddlewareExtensions.cs new file mode 100644 index 0000000..3d1ec8e --- /dev/null +++ b/Esiur.AspNetCore/EsiurMiddlewareExtensions.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Options; + +namespace Esiur.AspNetCore +{ + public static class EsiurMiddlewareExtensions + { + public static IApplicationBuilder UseEsiur(this IApplicationBuilder app, EsiurOptions options) + { + ArgumentNullException.ThrowIfNull(app); + ArgumentNullException.ThrowIfNull(options); + + return app.UseMiddleware(Options.Create(options)); + } + + } +} diff --git a/Esiur.AspNetCore/EsiurOptions.cs b/Esiur.AspNetCore/EsiurOptions.cs new file mode 100644 index 0000000..d067dfb --- /dev/null +++ b/Esiur.AspNetCore/EsiurOptions.cs @@ -0,0 +1,9 @@ +using Esiur.Net.IIP; + +namespace Esiur.AspNetCore +{ + public class EsiurOptions + { + public DistributedServer Server { get; set; } + } +} diff --git a/Esiur.sln b/Esiur.sln index 3319d6c..05479d2 100644 --- a/Esiur.sln +++ b/Esiur.sln @@ -16,7 +16,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Esiur.Examples.StandaloneWe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Esiur.CLI", "Esiur.CLI\Esiur.CLI.csproj", "{5C193127-20D1-4709-90C4-DF714D7E6700}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Esiur.ASPNet", "Esiur.ASPNet\Esiur.ASPNet.csproj", "{7C65CC4D-0DE6-4E2A-8DCF-113E1AB85D8A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Esiur.AspNetCore.Example", "Esiur.ASPNet\Esiur.AspNetCore.Example.csproj", "{7C65CC4D-0DE6-4E2A-8DCF-113E1AB85D8A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Esiur.AspNetCore", "Esiur.AspNetCore\Esiur.AspNetCore.csproj", "{7B0C521F-8B13-4F2A-BD78-7C692620C831}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -56,6 +58,10 @@ Global {7C65CC4D-0DE6-4E2A-8DCF-113E1AB85D8A}.Debug|Any CPU.Build.0 = Debug|Any CPU {7C65CC4D-0DE6-4E2A-8DCF-113E1AB85D8A}.Release|Any CPU.ActiveCfg = Release|Any CPU {7C65CC4D-0DE6-4E2A-8DCF-113E1AB85D8A}.Release|Any CPU.Build.0 = Release|Any CPU + {7B0C521F-8B13-4F2A-BD78-7C692620C831}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7B0C521F-8B13-4F2A-BD78-7C692620C831}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7B0C521F-8B13-4F2A-BD78-7C692620C831}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7B0C521F-8B13-4F2A-BD78-7C692620C831}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE