Skip to content
Draft
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
19 changes: 19 additions & 0 deletions Engine/GenHTTP.Engine.Rocket/GenHTTP.Engine.Rocket.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>GenHTTP.Engine.Rocket</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="URocket" Version="10.5.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\API\GenHTTP.Api.csproj" />
<ProjectReference Include="..\Shared\GenHTTP.Engine.Shared.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions Engine/GenHTTP.Engine.Rocket/Host.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GenHTTP.Api.Infrastructure;
using GenHTTP.Engine.Shared.Hosting;
using URocket.Engine.Configs;

namespace GenHTTP.Engine.Rocket;

public static class Host
{
public static IServerHost Create(EngineOptions engineOptions)
{
return new ServerHost(Server.Create(engineOptions));
}
}
66 changes: 66 additions & 0 deletions Engine/GenHTTP.Engine.Rocket/Infrastructure/RocketServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Reflection;
using GenHTTP.Api.Content;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Engine.Shared.Infrastructure;
using URocket.Engine.Configs;

namespace GenHTTP.Engine.Rocket.Infrastructure;

internal sealed class RocketServer : IServer
{
public string Version { get; }

public bool Running { get; }

public bool Development => Configuration.DevelopmentMode;

public IEndPointCollection EndPoints { get; } = null!;

public IServerCompanion? Companion { get; }

public IHandler Handler { get; }

internal URocket.Engine.Engine Engine { get; }

internal ServerConfiguration Configuration { get; }

internal RocketServer(IServerCompanion? companion, IHandler handler, ServerConfiguration config, EngineOptions engineOptions)
{
Version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "(n/a)";

Companion = companion;
Handler = handler;

Configuration = config;

Engine = new URocket.Engine.Engine(engineOptions);

Running = Engine.ServerRunning;
}

private static async ValueTask PrepareHandlerAsync(IHandler handler, IServerCompanion? companion)
{
try
{
await handler.PrepareAsync();
}
catch (Exception e)
{
companion?.OnServerError(ServerErrorScope.General, null, e);
}
}

public async ValueTask StartAsync()
{
await PrepareHandlerAsync(Handler, Companion);

Engine.Listen();
}

public ValueTask DisposeAsync()
{
Engine.Stop();

return ValueTask.CompletedTask;
}
}
21 changes: 21 additions & 0 deletions Engine/GenHTTP.Engine.Rocket/Infrastructure/RocketServerBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GenHTTP.Api.Content;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Engine.Shared.Infrastructure;
using URocket.Engine.Configs;

namespace GenHTTP.Engine.Rocket.Infrastructure;

public class RocketServerBuilder : ServerBuilder
{
private readonly EngineOptions _engineOptions;

public RocketServerBuilder(EngineOptions engineOptions)
{
_engineOptions = engineOptions;
}

protected override IServer Build(IServerCompanion? companion, ServerConfiguration config, IHandler handler)
{
return new RocketServer(companion, handler, config, _engineOptions);
}
}
39 changes: 39 additions & 0 deletions Engine/GenHTTP.Engine.Rocket/Protocol/ClientContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using GenHTTP.Engine.Shared.Types;
using Microsoft.Extensions.ObjectPool;

namespace GenHTTP.Engine.Rocket.Protocol;

internal class ClientContext
{
internal Request Request { get; }

internal ResponseBuilder ResponseBuilder { get; }

internal Response Response { get; }

internal ClientContext()
{
Response = new Response();

ResponseBuilder = new ResponseBuilder(Response);

Request = new Request(ResponseBuilder);
}

internal void Reset()
{
Request.Reset();
Response.Reset();
}
}

internal class ClientContextPolicy : PooledObjectPolicy<ClientContext>
{
public override ClientContext Create() => new();

public override bool Return(ClientContext obj)
{
obj.Reset();
return true;
}
}
44 changes: 44 additions & 0 deletions Engine/GenHTTP.Engine.Rocket/Protocol/ClientHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using GenHTTP.Engine.Rocket.Infrastructure;
using Microsoft.Extensions.ObjectPool;

namespace GenHTTP.Engine.Rocket.Protocol;

internal sealed class ClientHandler
{
private static readonly DefaultObjectPool<ClientContext> ContextPool = new(new ClientContextPolicy(), 1024 * 64);

private readonly RocketServer _server;

public ClientHandler(RocketServer server)
{
_server = server;
}

public async Task RunAsync(CancellationToken cancellationToken = default)
{
try
{
while (_server.Engine.ServerRunning)
{
var conn = await _server.Engine.AcceptAsync(cancellationToken);

if(conn is null)
continue;

Console.WriteLine($"Connection: {conn.ClientFd}");
}
}
catch (OperationCanceledException)
{
Console.WriteLine("Signaled to stop");
}
catch (Exception e)
{
Console.WriteLine($"[Cowabunga!] {e.Message}");
}
finally
{
_server.Engine.Stop();
}
}
}
Loading