Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::HttpAsyncFileServer Struct Reference

Http file server statically serves files from a directory. More...

#include <HttpAsyncFileServer.h>

Public Member Functions

Result init (StringSpan directoryToServe, Span< HttpAsyncFileServerStream > fileStreams, AsyncBuffersPool &buffersPool, AsyncEventLoop &eventLoop, ThreadPool &threadPool)
 Initialize the web server on the given file system directory to serve.
 
Result serveFile (HttpConnection::ID index, StringSpan url, HttpResponse &response)
 Serve the file requested by this Http Client on its channel Call this method in response to HttpConnectionsPool::onRequest to serve a file.
 
void registerToServeFilesOn (HttpAsyncServer &server)
 Registers to HttpConnectionsPool::onRequest callback to serve files from this file server.
 

Detailed Description

Http file server statically serves files from a directory.

This class registers the onRequest callback provided by HttpAsyncServer to serves files from a given directory.

constexpr int MAX_CONNECTIONS = 16; // Max number of concurrent http connections
constexpr int REQUEST_SLICES = 2; // Number of slices of the request buffer for each connection
constexpr int REQUEST_SIZE = 1024; // How many bytes are allocated to stream data for each connection
constexpr int HEADER_SIZE = 1024 * 8; // How many bytes are dedicated to hold request and response headers
constexpr int EXTRA_BUFFERS = 1; // Extra write slice needed to write headers buffer
constexpr int NUM_FS_THREADS = 4; // Number of threads in the thread pool for async file stream operations
// Note: All fixed arrays could be created dynamically at startup time with new / malloc.
// Alternatively it's also possible to reserve memory for some insane large amount of clients (using VirtualMemory
// class for example) and just dynamically commit as much memory as one needs to handle a given number of clients
// 1. Memory for all http headers of all connections
Buffer headersMemory;
SC_TEST_EXPECT(headersMemory.resize(MAX_CONNECTIONS * HEADER_SIZE));
// 2. Memory to hold all sliced buffers used by the read queues
AsyncReadableStream::Request readQueue[MAX_CONNECTIONS * REQUEST_SLICES];
// 3. Memory to hold all sliced buffers used by the write queues
AsyncWritableStream::Request writeQueue[MAX_CONNECTIONS * (REQUEST_SLICES + EXTRA_BUFFERS)];
// 4. Memory to hold all pre-registered / re-usable buffers used by the read and write queues.
// EXTRA_BUFFERS is used to accommodate some empty slots for external bufs (Strings or other
// pieces of memory allocated, owned and pushed by the user to the write queue)
AsyncBufferView buffers[MAX_CONNECTIONS * (REQUEST_SLICES + EXTRA_BUFFERS)];
// Slice a buffer in equal parts to create re-usable slices of memory when streaming files.
// It's not required to slice the buffer in equal parts, that's just an arbitrary choice.
Buffer requestsMemory;
SC_TEST_EXPECT(requestsMemory.resize(MAX_CONNECTIONS * REQUEST_SIZE));
SC_TEST_EXPECT(HttpAsyncServer::sliceReusableEqualMemoryBuffers(buffers, requestsMemory.toSpan(), MAX_CONNECTIONS,
REQUEST_SLICES, REQUEST_SIZE));
// 5. Memory to hold all http connections
HttpConnection connections[MAX_CONNECTIONS];
// 6. Memory used by the async streams handled by the async file server
HttpAsyncFileServerStream streams[MAX_CONNECTIONS];
// Initialize and start the http and the file server
HttpAsyncServer httpServer;
HttpAsyncFileServer fileServer;
ThreadPool threadPool;
if (eventLoop.needsThreadPoolForFileOperations()) // no thread pool needed for io_uring
{
SC_TEST_EXPECT(threadPool.create(NUM_FS_THREADS));
}
SC_TEST_EXPECT(httpServer.init(connections, headersMemory.toSpan(), readQueue, writeQueue, buffers));
SC_TEST_EXPECT(fileServer.init(webServerFolder, streams, httpServer.getBuffersPool(), eventLoop, threadPool));
fileServer.registerToServeFilesOn(httpServer);
SC_TEST_EXPECT(httpServer.start(eventLoop, "127.0.0.1", 8090));

Member Function Documentation

◆ init()

Result SC::HttpAsyncFileServer::init ( StringSpan directoryToServe,
Span< HttpAsyncFileServerStream > fileStreams,
AsyncBuffersPool & buffersPool,
AsyncEventLoop & eventLoop,
ThreadPool & threadPool )

Initialize the web server on the given file system directory to serve.

◆ registerToServeFilesOn()

void SC::HttpAsyncFileServer::registerToServeFilesOn ( HttpAsyncServer & server)

Registers to HttpConnectionsPool::onRequest callback to serve files from this file server.

◆ serveFile()

Result SC::HttpAsyncFileServer::serveFile ( HttpConnection::ID index,
StringSpan url,
HttpResponse & response )

Serve the file requested by this Http Client on its channel Call this method in response to HttpConnectionsPool::onRequest to serve a file.


The documentation for this struct was generated from the following file: