Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions OverclockedApp/OverclockedApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsTestAssetProject>true</IsTestAssetProject>
<ServerGarbageCollection>true</ServerGarbageCollection>
<TieredPGO>true</TieredPGO>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Wired.IO\Wired.IO.csproj" />
</ItemGroup>

<ItemGroup Condition="$(PublishAot) == 'true'">
<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.HillClimbing.Disable" Value="true" />
</ItemGroup>

</Project>
73 changes: 73 additions & 0 deletions OverclockedApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Buffers;
using System.Text.Json;
using System.Text.Json.Serialization;
using Wired.IO.App;
using Wired.IO.Protocol.Response;

namespace OverclockedApp;

// dotnet publish -f net10.0 -c Release /p:PublishAot=true /p:OptimizationPreference=Speed

[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization | JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Program.JsonMessage))]
public partial class JsonContext : JsonSerializerContext { }

public static class Program
{
[ThreadStatic] private static Utf8JsonWriter? _tUtf8JsonWriter;
public static async Task Main(string[] args)
{
await WiredApp
.CreateOverclockedBuilder() // io_uring
.NoScopedEndpoints()
.UseRootEndpoints()
.Port(8080)
.MapGet("/json", context =>
{
context.Respond()
.Status(ResponseStatus.Ok)
.Type("application/json"u8)
.Content(() =>
{
var utf8JsonWriter = _tUtf8JsonWriter ??= new Utf8JsonWriter(context.Connection, new JsonWriterOptions { SkipValidation = true });
utf8JsonWriter.Reset(context.Connection);
JsonSerializer.Serialize(utf8JsonWriter, new JsonMessage { Message = JsonBody }, SerializerContext.JsonMessage);
}, 27);

})
.Build()
.RunAsync();
}

private const string JsonBody = "Hello, World!";
public static readonly JsonContext SerializerContext = JsonContext.Default;
public struct JsonMessage { public string Message { get; set; } }
}


/*
context.Connection.Write("HTTP/1.1 200 OK\r\n"u8 +
"Server: W\r\n"u8 +
"Content-Length: 27\r\n"u8 +
"Content-Type: application/json\r\n\r\n"u8 +
"{\"Message\":\"Hello, World!\"}"u8);
*/

/*
var headers =
"HTTP/1.1 200 OK\r\n"u8 +
"Server: W\r\n"u8 +
"Content-Length: 27\r\n"u8 +
"Content-Type: application/json\r\n"u8;

context.Connection.Write(headers);
context.Connection.Write(DateHelper.HeaderBytes);

_tUtf8JsonWriter ??= new Utf8JsonWriter(context.Connection, new JsonWriterOptions { SkipValidation = true });
_tUtf8JsonWriter.Reset(context.Connection);

// Creating(Allocating) a new JsonMessage every request
var message = new JsonMessage { Message = "Hello, World!" };
// Serializing it every request
JsonSerializer.Serialize(_tUtf8JsonWriter, message, SerializerContext.JsonMessage);
*/
71 changes: 64 additions & 7 deletions Wired.IO.Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
using System.Net;
using System.Text.Json.Serialization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Wired.IO.App;
using Wired.IO.Http11Express.Response.Content;
using Wired.IO.Handlers.Http11Express.Response.Content;
using Wired.IO.Protocol.Response;

// dotnet publish -f net10.0 -c Release /p:PublishAot=true /p:OptimizationPreference=Speed

var services = new ServiceCollection();

services.AddScoped<Service>();

var builder = WiredApp
.CreateExpressBuilder()
//.CreateOverclockedBuilder()
.CreateRocketBuilder()
//.CreateExpressBuilder()
.NoScopedEndpoints()
.Port(8080);

builder.EmbedServices(services);

builder
.MapGroup("/")
.MapGet("/my-endpoint", context =>
.MapGet("/route", context =>
{
JsonContext SerializerContext = JsonContext.Default;

var ip = ((IPEndPoint)context.Inner.RemoteEndPoint!).Address;
Console.WriteLine($"Client: {ip.ToString()}");
context
.Respond()
.Status(ResponseStatus.Ok)
.Type("application/json"u8)
.Content(new ExpressJsonAotContent(new JsonMessage
{
Message = "Hello World!"
}, SerializerContext.JsonMessage));
});

builder
.MapGroup("/api")
.UseMiddleware(async (context, next) =>
{
// logger or any dependencies can be resolved using scope
var logger = context.Services.GetRequiredService<ILogger<Program>>();

try
{
Console.WriteLine("Executing Middleware");
// Execute next in line, could be another middleware or the endpoint
await next(context);
}

catch (Exception e)
{
logger.LogError(e.Message);

context.Respond()
.Status(ResponseStatus.InternalServerError)
.Type("application/json"u8)
.Content(new ExpressJsonContent(new { Error = e.Message }));
}
})
.MapGet("/my-endpoint", async context =>
{
await context.Services.GetRequiredService<Service>().HandleAsync();

JsonContext SerializerContext = JsonContext.Default;

context
.Respond()
Expand All @@ -43,3 +87,16 @@ public struct JsonMessage { public string Message { get; set; } }
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization | JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(JsonMessage))]
public partial class JsonContext : JsonSerializerContext { }

public class Service : IDisposable
{
public Service() => Console.WriteLine("Created Service");

public async Task HandleAsync()
{
await Task.Delay(0);
Console.WriteLine("Handled Service");
}

public void Dispose() => Console.WriteLine("Disposed Service");
}
11 changes: 11 additions & 0 deletions Wired.IO.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wired.IO.Playground", "Wire
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wired.IO.Tests", "Wired.IO.Tests\Wired.IO.Tests.csproj", "{21364EC0-8822-40F5-8EEB-339919ABD5C6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{8BA4F484-5D28-4E2B-BE5A-BA62BD943FB7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OverclockedApp", "OverclockedApp\OverclockedApp.csproj", "{068FC1BE-C9B2-4C82-BACE-98547D68DDB5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,11 +31,18 @@ Global
{21364EC0-8822-40F5-8EEB-339919ABD5C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21364EC0-8822-40F5-8EEB-339919ABD5C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21364EC0-8822-40F5-8EEB-339919ABD5C6}.Release|Any CPU.Build.0 = Release|Any CPU
{068FC1BE-C9B2-4C82-BACE-98547D68DDB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{068FC1BE-C9B2-4C82-BACE-98547D68DDB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{068FC1BE-C9B2-4C82-BACE-98547D68DDB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{068FC1BE-C9B2-4C82-BACE-98547D68DDB5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DC158062-AC46-467E-AFC4-2ED35B471D73}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{068FC1BE-C9B2-4C82-BACE-98547D68DDB5} = {8BA4F484-5D28-4E2B-BE5A-BA62BD943FB7}
EndGlobalSection
EndGlobal
6 changes: 0 additions & 6 deletions Wired.IO/App/App.Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using Wired.IO.Builder;
using Wired.IO.Protocol;
using Wired.IO.Protocol.Handlers;
using Wired.IO.Protocol.Request;
using Wired.IO.Protocol.Response;

Expand Down Expand Up @@ -125,11 +124,6 @@ public sealed partial class WiredApp<TContext>
/// </summary>
internal bool UseRootOnlyEndpoints { get; set; } = false;

/// <summary>
/// Gets or sets the HTTP handler responsible for dispatching requests and handling routing.
/// </summary>
public IHttpHandler<TContext> HttpHandler { get; set; } = null!;

#endregion
}

Expand Down
12 changes: 7 additions & 5 deletions Wired.IO/App/App.ClientHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
using System.Net.Sockets;
using System.Security;
using System.Security.Authentication;
using Wired.IO.MemoryBuffers;
using Wired.IO.Protocol;
using Wired.IO.Protocol.Request;
using Wired.IO.Protocol.Response;
using Wired.IO.Transport.Socket;

namespace Wired.IO.App;

public sealed partial class WiredApp<TContext>
where TContext : IBaseContext<IBaseRequest, IBaseResponse>
{
private Func<TContext, Task> _pipeline = null!;
//private Func<TContext, Task> _pipeline = null!;

internal void SetPipeline(Func<TContext, Task> pipeline) => _pipeline = pipeline;
//internal void SetPipeline(Func<TContext, Task> pipeline) => _pipeline = pipeline;

/*
/// <summary>
/// Handles an incoming plain (non-TLS) TCP client connection.
/// Wraps the client socket in a <see cref="PoolBufferedStream"/> and delegates request handling to <see cref="HttpHandler"/>.
Expand All @@ -26,7 +27,7 @@ public sealed partial class WiredApp<TContext>
private async Task HandlePlainClientAsync(Socket client, CancellationToken stoppingToken)
{
await using var networkStream = new PoolBufferedStream(new NetworkStream(client, ownsSocket: true), 65 * 1024);
await HttpHandler.HandleClientAsync(
await ((ISocketHttpHandler<TContext>)HttpHandler).HandleClientAsync(
client,
networkStream,
_pipeline,
Expand Down Expand Up @@ -72,7 +73,7 @@ private async Task HandleTlsClientAsync(Socket client, CancellationToken stoppin

// Handle the client connection securely
//
await HttpHandler.HandleClientAsync(
await ((ISocketHttpHandler<TContext>)HttpHandler).HandleClientAsync(
client,
sslStream,
_pipeline,
Expand Down Expand Up @@ -126,4 +127,5 @@ private static async Task SendTlsFailureMessageAsync(Socket client)
client.Close();
}
}
*/
}
2 changes: 2 additions & 0 deletions Wired.IO/App/App.Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Wired.IO.App;
public sealed partial class WiredApp<TContext>
where TContext : IBaseContext<IBaseRequest, IBaseResponse>
{
/*
/// <summary>
/// Creates an <see cref="Engine"/> instance configured to accept and process plain (non-TLS) HTTP connections.
/// </summary>
Expand Down Expand Up @@ -126,4 +127,5 @@ private async Task HandleClientAsync(Socket client, Func<Socket, CancellationTok
client.Dispose();
}
}
*/
}
Loading
Loading