diff --git a/Unhinged.Playground/Program.cs b/Unhinged.Playground/Program.cs index 3d80591..8f7d5f0 100644 --- a/Unhinged.Playground/Program.cs +++ b/Unhinged.Playground/Program.cs @@ -17,12 +17,12 @@ public static void Main(string[] args) { var builder = UnhingedEngine .CreateBuilder() - .SetNWorkersSolver(() => (Environment.ProcessorCount / 2) - 2) + .SetNWorkersSolver(() => (Environment.ProcessorCount / 2)) .SetBacklog(16384) .SetMaxEventsPerWake(512) .SetMaxNumberConnectionsPerWorker(512) .SetPort(8080) - .SetSlabSizes(16 * 1024, 16 * 1024) + .SetSlabSizes(512 * 1024, 128 * 1024) .InjectRequestHandler(RequestHandler); var engine = builder.Build(); diff --git a/Unhinged/ABI/Native.cs b/Unhinged/ABI/Native.cs index 421c517..309adb6 100644 --- a/Unhinged/ABI/Native.cs +++ b/Unhinged/ABI/Native.cs @@ -231,6 +231,8 @@ internal struct Linger internal const int EPOLLERR = 0x008; internal const int EPOLLHUP = 0x010; internal const int EPOLLRDHUP = 0x2000; + internal const uint EPOLLET = 0x80000000; + internal const uint EPOLLONESHOT = 0x40000000; // epoll_ctl ops internal const int EPOLL_CTL_ADD = 1; diff --git a/Unhinged/Engine/UnhingedEngine.Worker.cs b/Unhinged/Engine/UnhingedEngine.Worker.cs index 855b7d4..117b9f2 100644 --- a/Unhinged/Engine/UnhingedEngine.Worker.cs +++ b/Unhinged/Engine/UnhingedEngine.Worker.cs @@ -110,7 +110,6 @@ private static unsafe void WorkerLoop(Worker W) // Ensure free space at the tail; compact or grow if necessary. // TODO: This logic needs rework, doesn't sense - //int avail = c.Buf.Length - c.Tail; int avail = _inSlabSize - c.Tail; // If the receiving buffer has no space available, that means that either there are @@ -131,8 +130,7 @@ private static unsafe void WorkerLoop(Worker W) while (true) { long got; - //fixed (byte* p = &c.Buf[c.Tail]) - // got = recv(fd, (IntPtr)p, (ulong)avail, 0); + got = recv(fd, c.ReceiveBuffer + c.Tail, (ulong)avail, 0); if (got > 0) @@ -155,7 +153,7 @@ private static unsafe void WorkerLoop(Worker W) // TODO: That could be an issue because this could give more airtime to a specific fd // TODO: We want to release the loop to move into another fd's? break; - } + } if (err is ECONNRESET or ECONNABORTED or EPIPE) { CloseConn(fd, connections, W); break; } CloseConn(fd, connections, W); break; // default: close on unexpected errors } @@ -257,7 +255,7 @@ private static async ValueTask TryParseRequests( bool hasDataToFlush = false; int idx = 0; - + while (true) { unsafe @@ -268,8 +266,6 @@ private static async ValueTask TryParseRequests( break; // A full request was received, handle it - - // Extract the request Header data connection.H1HeaderData = ExtractH1HeaderData(headerSpan); // Advance the pointer after the request was dealt with @@ -294,17 +290,24 @@ private static async ValueTask TryParseRequests( // Move the incomplete request to the buffer start and reset head and tail to 0 if (connection.Head > 0 && connection.Head < connection.Tail) { + var length = connection.Tail - connection.Head; Buffer.MemoryCopy( connection.ReceiveBuffer + connection.Head, connection.ReceiveBuffer, _inSlabSize, - connection.Tail - connection.Head); + length); + + // Update head to 0 and tail to length + connection.Head = 0; + connection.Tail = length; + } + else + { + //Reset the receiving buffer + connection.Head = connection.Tail = 0; } } - //Reset the receiving buffer - connection.Head = connection.Tail = 0; - if (hasDataToFlush) { var tryEmptyResult = TryEmptyWriteBuffer(connection, ref fd); @@ -441,7 +444,7 @@ private static unsafe EmptyAttemptResult TryEmptyWriteBuffer(Connection connecti private static unsafe void ArmEpollIn(ref int fd, int ep) { byte* ev = stackalloc byte[EvSize]; - WriteEpollEvent(ev, EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP, fd); + WriteEpollEvent(ev, EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET, fd); epoll_ctl(ep, EPOLL_CTL_MOD, fd, (IntPtr)ev); } @@ -452,7 +455,7 @@ private static unsafe void ArmEpollIn(ref int fd, int ep) private static unsafe void ArmEpollOut(ref int fd, int ep) { byte* ev = stackalloc byte[EvSize]; - WriteEpollEvent(ev, EPOLLOUT | EPOLLRDHUP | EPOLLERR | EPOLLHUP, fd); + WriteEpollEvent(ev, EPOLLOUT | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET, fd); epoll_ctl(ep, EPOLL_CTL_MOD, fd, (IntPtr)ev); } diff --git a/Unhinged/Unhinged.csproj b/Unhinged/Unhinged.csproj index 3b14223..790b042 100644 --- a/Unhinged/Unhinged.csproj +++ b/Unhinged/Unhinged.csproj @@ -10,9 +10,9 @@ Diogo Martins https://github.com/MDA2AV/Unhinged git - 9.0.4 - 9.0.4 - 9.0.4 + 9.0.5 + 9.0.5 + 9.0.5 README.md Unhinged LICENSE