Skip to content

Commit c7d6a57

Browse files
committed
10.0.0
1 parent b644fbe commit c7d6a57

5 files changed

Lines changed: 58 additions & 29 deletions

File tree

Unhinged.Playground/Program.cs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ public static void Main(string[] args)
3030
engine.Run();
3131
}
3232

33-
private static ValueTask RequestHandler(Connection connection)
33+
private static unsafe ValueTask RequestHandler(Connection connection)
3434
{
35-
/*if(connection.HashedRoute == 291830056) // /json
35+
var route = connection.BinaryH1HeaderData.Route.AsSpan();
36+
if (route[1] == (byte)'j')
37+
{
3638
CommitJsonResponse(connection);
39+
}
40+
else
41+
{
42+
CommitPlainTextResponse(connection);
43+
}
3744

38-
else if (connection.HashedRoute == 3454831873) // /plaintext
39-
CommitPlainTextResponse(connection);*/
40-
45+
/*
4146
if (connection.H1HeaderData.Route.Equals("/json"))
4247
{
4348
CommitJsonResponse(connection);
@@ -47,38 +52,60 @@ private static ValueTask RequestHandler(Connection connection)
4752
{
4853
CommitPlainTextResponse(connection);
4954
}
55+
*/
5056

5157
return ValueTask.CompletedTask;
5258
}
5359

54-
[ThreadStatic] private static Utf8JsonWriter? _tUtf8JsonWriter;
60+
[ThreadStatic] private static Utf8JsonWriter? t_utf8JsonWriter;
5561
private static readonly JsonContext SerializerContext = JsonContext.Default;
56-
private static void CommitJsonResponse(Connection connection)
62+
private static unsafe void CommitJsonResponse(Connection connection)
5763
{
64+
var tail = connection.WriteBuffer.Tail;
5865
connection.WriteBuffer.WriteUnmanaged("HTTP/1.1 200 OK\r\n"u8 +
59-
"Server: W\r\n"u8 +
60-
"Content-Type: application/json; charset=UTF-8\r\n"u8 +
61-
"Content-Length: 27\r\n"u8);
66+
"Content-Length: \r\n"u8 +
67+
"Server: U\r\n"u8 +
68+
"Content-Type: application/json; charset=UTF-8\r\n"u8);
6269
connection.WriteBuffer.WriteUnmanaged(DateHelper.HeaderBytes);
6370

64-
_tUtf8JsonWriter ??= new Utf8JsonWriter(connection.WriteBuffer, new JsonWriterOptions { SkipValidation = true });
65-
_tUtf8JsonWriter.Reset(connection.WriteBuffer);
71+
t_utf8JsonWriter ??= new Utf8JsonWriter(connection.WriteBuffer, new JsonWriterOptions { SkipValidation = true });
72+
t_utf8JsonWriter.Reset(connection.WriteBuffer);
6673

6774
// Creating(Allocating) a new JsonMessage every request
6875
var message = new JsonMessage { Message = "Hello, World!" };
6976
// Serializing it every request
70-
JsonSerializer.Serialize(_tUtf8JsonWriter, message, SerializerContext.JsonMessage);
77+
JsonSerializer.Serialize(t_utf8JsonWriter, message, SerializerContext.JsonMessage);
78+
79+
var contentLength = (int)t_utf8JsonWriter.BytesCommitted;
80+
81+
byte* dst = connection.WriteBuffer.Ptr + tail + 33;
82+
int tens = contentLength / 10;
83+
int ones = contentLength - tens * 10;
84+
85+
dst[0] = (byte)('0' + tens);
86+
dst[1] = (byte)('0' + ones);
87+
7188
}
7289

73-
private static void CommitPlainTextResponse(Connection connection)
90+
private static ReadOnlySpan<byte> s_plainTextBody => "Hello, World!"u8;
91+
92+
private static unsafe void CommitPlainTextResponse(Connection connection)
7493
{
94+
var tail = connection.WriteBuffer.Tail;
95+
var contentLength = s_plainTextBody.Length;
7596
connection.WriteBuffer.WriteUnmanaged("HTTP/1.1 200 OK\r\n"u8 +
76-
"Server: W\r\n"u8 +
77-
"Content-Type: text/plain\r\n"u8 +
78-
//"Content-Length: 13\r\n\r\nHello, World!"u8);
79-
"Content-Length: 13\r\n"u8);
97+
"Content-Length: \r\n"u8 +
98+
"Server: U\r\n"u8 +
99+
"Content-Type: text/plain\r\n"u8);
80100
connection.WriteBuffer.WriteUnmanaged(DateHelper.HeaderBytes);
81-
connection.WriteBuffer.WriteUnmanaged("Hello, World!"u8);
101+
connection.WriteBuffer.WriteUnmanaged(s_plainTextBody);
102+
103+
byte* dst = connection.WriteBuffer.Ptr + tail + 33;
104+
int tens = contentLength / 10;
105+
int ones = contentLength - tens * 10;
106+
107+
dst[0] = (byte)('0' + tens);
108+
dst[1] = (byte)('0' + ones);
82109
}
83110
}
84111

Unhinged.Playground/Unhinged.Playground.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Unhinged/Engine/UnhingedEngine.Worker.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ private static async ValueTask TryParseRequests(
266266
break;
267267

268268
// A full request was received, handle it
269-
connection.H1HeaderData = ExtractH1HeaderData(headerSpan);
269+
connection.BinaryH1HeaderData = ExtractBinaryH1HeaderData(headerSpan);
270+
//connection.H1HeaderData = ExtractH1HeaderData(headerSpan);
270271

271272
// Advance the pointer after the request was dealt with
272273
connection.Head = idx + 4; // advance past CRLFCRLF
@@ -351,7 +352,8 @@ private static unsafe bool TryParseRequests(Connection connection)
351352
// A full request was received, handle it
352353

353354
// Extract the route
354-
connection.H1HeaderData = ExtractH1HeaderData(headerSpan);
355+
connection.BinaryH1HeaderData = ExtractBinaryH1HeaderData(headerSpan);
356+
//connection.H1HeaderData = ExtractH1HeaderData(headerSpan);
355357

356358
_sRequestHandler(connection);
357359

Unhinged/Unhinged.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFrameworks>net9.0;net10.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -10,16 +10,16 @@
1010
<Authors>Diogo Martins</Authors>
1111
<RepositoryUrl>https://github.com/MDA2AV/Unhinged</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
13-
<AssemblyVersion>9.0.7</AssemblyVersion>
14-
<FileVersion>9.0.7</FileVersion>
15-
<Version>9.0.7</Version>
13+
<AssemblyVersion>10.0.0</AssemblyVersion>
14+
<FileVersion>10.0.0</FileVersion>
15+
<Version>10.0.0</Version>
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717
<PackageTags>Unhinged</PackageTags>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1919
<Description>https://github.com/MDA2AV/Unhinged</Description>
2020
<PackageProjectUrl>https://github.com/MDA2AV/Unhinged</PackageProjectUrl>
2121

22-
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
22+
<!--<RuntimeIdentifier>linux-x64</RuntimeIdentifier>-->
2323

2424
</PropertyGroup>
2525

@@ -35,7 +35,7 @@
3535
</ItemGroup>
3636

3737
<ItemGroup>
38-
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="10.0.0-rc.1.25451.107" />
38+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="10.0.3" />
3939
</ItemGroup>
4040

4141
<ItemGroup>

Unhinged/Utilities/PinnedByteSequence.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public PinnedByteSequence(byte* ptr, int length)
2020
Length = length;
2121
}
2222

23-
internal unsafe ReadOnlySpan<byte> AsSpan() => new(_ptr, Length);
23+
public unsafe ReadOnlySpan<byte> AsSpan() => new(_ptr, Length);
2424

2525
public bool Equals(PinnedByteSequence other)
2626
{

0 commit comments

Comments
 (0)