Skip to content
Merged
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
59 changes: 59 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build, Test, and Publish

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read # Required for reading repository content
packages: write # Required for publishing packages to NuGet

jobs:
build:
runs-on: ubuntu-latest

steps:
# Checkout the repository
- name: Checkout code
uses: actions/checkout@v3

# Set up .NET SDK
- name: Set up .NET SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: '9.0' # Specify your .NET SDK version

# Restore dependencies
- name: Restore dependencies
run: dotnet restore Unhinged/Unhinged.csproj

# Build the project
- name: Build project
run: dotnet build Unhinged/Unhinged.csproj --configuration Release --no-restore

- name: Pack NuGet package
run: dotnet pack Unhinged/Unhinged.csproj --configuration Release --output ./artifacts

# Run tests only for .NET 9
#- name: Run tests (only for .NET 9)
# run: |
# if [[ "$(dotnet --version)" == 9.* ]]; then
# dotnet test Unhinged.Tests/Unhinged.Tests.csproj --verbosity normal
# else
# echo "Skipping tests: Not running on .NET 9."
# fi

- name: Publish to NuGet
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: dotnet nuget push ./artifacts/*.nupkg --source https://api.nuget.org/v3/index.json --api-key $NUGET_API_KEY --skip-duplicate

# Publish to GitHub Packages
- name: Publish to GitHub Packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: dotnet nuget push ./artifacts/*.nupkg --source https://nuget.pkg.github.com/MDA2AV/index.json --api-key $GITHUB_TOKEN --skip-duplicate
70 changes: 70 additions & 0 deletions Unhinged.Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ReSharper disable always SuggestVarOrType_BuiltInTypes
// (var is avoided intentionally in this project so that concrete types are visible at call sites.)
// ReSharper disable always StackAllocInsideLoop

using System.Runtime.CompilerServices;
using System.Text.Json;
using Unhinged;

#pragma warning disable CA2014

[SkipLocalsInit]
internal static class Program
{
public static void Main(string[] args)
{
var builder = UnhingedEngine
.CreateBuilder()
.SetNWorkersSolver(() => Environment.ProcessorCount / 2)
.SetBacklog(16384)
.SetMaxEventsPerWake(128)
.SetMaxNumberConnectionsPerWorker(32)
.SetPort(8080)
.SetSlabSizes(16 * 1024, 16 * 1024)
.InjectRequestHandler(RequestHandler);

var engine = builder.Build();

engine.Run();
}

private static void RequestHandler(Connection connection)
{
if(connection.HashedRoute == 291830056) // /json
CommitJsonResponse(connection);

else if (connection.HashedRoute == 3454831873) // /plaintext
CommitPlainTextResponse(connection);
}

[ThreadStatic] private static Utf8JsonWriter? t_utf8JsonWriter;
private static readonly JsonContext SerializerContext = JsonContext.Default;
private static void CommitJsonResponse(Connection connection)
{
connection.WriteBuffer.Write("HTTP/1.1 200 OK\r\n"u8 +
"Server: W\r\n"u8 +
"Content-Type: application/json; charset=UTF-8\r\n"u8 +
"Content-Length: 27\r\n"u8);
connection.WriteBuffer.Write(DateHelper.HeaderBytes);

t_utf8JsonWriter ??= new Utf8JsonWriter(connection.WriteBuffer, new JsonWriterOptions { SkipValidation = true });
t_utf8JsonWriter.Reset(connection.WriteBuffer);

// Creating(Allocating) a new JsonMessage every request
var message = new JsonMessage { Message = "Hello, World!" };
// Serializing it every request
JsonSerializer.Serialize(t_utf8JsonWriter, message, SerializerContext.JsonMessage);
}

private static void CommitPlainTextResponse(Connection connection)
{
connection.WriteBuffer.Write("HTTP/1.1 200 OK\r\n"u8 +
"Server: W\r\n"u8 +
"Content-Type: text/plain\r\n"u8 +
//"Content-Length: 13\r\n\r\nHello, World!"u8);
"Content-Length: 13\r\n"u8);
connection.WriteBuffer.WriteUnmanaged(DateHelper.HeaderBytes);
connection.WriteBuffer.Write("Hello, World!"u8);
}
}

26 changes: 26 additions & 0 deletions Unhinged.Playground/Unhinged.Playground.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<!-- Required for self-contained publish -->
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\Unhinged\Unhinged.csproj" />
</ItemGroup>

</Project>
16 changes: 11 additions & 5 deletions Unhinged.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unhinged", "Unhinged\Unhinged.csproj", "{01D62FEF-383D-4446-846B-7503D217B1D7}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unhinged", "Unhinged\Unhinged.csproj", "{4DA83CE5-5152-4142-BD80-EBCCED4694CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unhinged.Playground", "Unhinged.Playground\Unhinged.Playground.csproj", "{5D135CA9-4854-458E-A48C-287CF1A0DEC2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{01D62FEF-383D-4446-846B-7503D217B1D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01D62FEF-383D-4446-846B-7503D217B1D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01D62FEF-383D-4446-846B-7503D217B1D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01D62FEF-383D-4446-846B-7503D217B1D7}.Release|Any CPU.Build.0 = Release|Any CPU
{4DA83CE5-5152-4142-BD80-EBCCED4694CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DA83CE5-5152-4142-BD80-EBCCED4694CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DA83CE5-5152-4142-BD80-EBCCED4694CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DA83CE5-5152-4142-BD80-EBCCED4694CF}.Release|Any CPU.Build.0 = Release|Any CPU
{5D135CA9-4854-458E-A48C-287CF1A0DEC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D135CA9-4854-458E-A48C-287CF1A0DEC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D135CA9-4854-458E-A48C-287CF1A0DEC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D135CA9-4854-458E-A48C-287CF1A0DEC2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion Unhinged/Engine/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public unsafe class Connection : IDisposable
public readonly FixedBufferWriter WriteBuffer;

// <summary>Fnv1a32 hashed route</summary>
internal uint HashedRoute { get; set; }
public uint HashedRoute { get; set; }

/// <param name="maxConnections">Used to size the slabs (typically per-worker slab size).</param>
/// <param name="inSlabSize">Bytes per connection for receive.</param>
Expand Down
Loading
Loading