Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
HttpAsyncServer.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "HttpConnection.h"
5
6namespace SC
7{
8namespace TypeTraits
9{
11template <typename Base, typename Derived>
13{
14 static constexpr bool value = __is_base_of(Base, Derived);
15};
16
17} // namespace TypeTraits
18
20template <int ReadQueue, int WriteQueue, int HeaderBytes, int StreamBytes>
21struct SC_COMPILER_EXPORT HttpAsyncConnection
22 : public HttpStaticConnection<ReadQueue, WriteQueue, HeaderBytes, StreamBytes, 0, HttpConnection>
23{
24};
25
38struct SC_COMPILER_EXPORT HttpAsyncServer
39{
41 template <typename T,
44 {
45 return initInternal({clients.data(), clients.sizeInElements(), sizeof(T)});
46 }
47
48 template <typename T,
50 Result resize(Span<T> clients)
51 {
52 return resizeInternal({clients.data(), clients.sizeInElements(), sizeof(T)});
53 }
54
58
65
69
71 [[nodiscard]] bool isStarted() const { return state == State::Started; }
72
73 [[nodiscard]] const HttpConnectionsPool& getConnections() const { return connections; }
74
77
81 void setDefaultKeepAlive(bool enabled) { defaultKeepAlive = enabled; }
82
84 [[nodiscard]] bool getDefaultKeepAlive() const { return defaultKeepAlive; }
85
88 void setMaxRequestsPerConnection(uint32_t maxRequests) { maxRequestsPerConnection = maxRequests; }
89
91 [[nodiscard]] uint32_t getMaxRequestsPerConnection() const { return maxRequestsPerConnection; }
92
93 private:
94 HttpConnectionsPool connections;
95
96 uint32_t maxHeaderSize = 8 * 1024;
97
98 enum class State
99 {
100 Stopped, // Server was not started at all, or it was stopped and wait(ed)ForStopToFinish
101 Started, // Server has been started (successfully)
102 Stopping, // Server has stop() called and needs waitForStopToFinish() call
103 };
104
105 State state = State::Stopped;
106
107 bool defaultKeepAlive = true;
108 uint32_t maxRequestsPerConnection = 0;
109
110 void onNewClient(AsyncSocketAccept::Result& result);
111 void closeAsync(HttpConnection& requestClient);
112 void deactivateConnection(HttpConnection& requestClient);
113 void onStreamReceive(HttpConnection& client, AsyncBufferView::ID bufferID);
114
115 Result waitForStopToFinish();
116 Result initInternal(SpanWithStride<HttpConnection> connections);
117 Result resizeInternal(SpanWithStride<HttpConnection> connections);
118
119 AsyncEventLoop* eventLoop = nullptr;
120 SocketDescriptor serverSocket;
121 AsyncSocketAccept asyncServerAccept;
122
123 struct EventDataListener;
124 struct EventEndListener;
125};
126} // namespace SC
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
struct SC_COMPILER_EXPORT Function
Wraps function pointers, member functions and lambdas without ever allocating.
Definition Function.h:19
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1386
Adds compile-time configurable read and write queues to HttpConnection.
Definition HttpAsyncServer.h:23
Async Http Server.
Definition HttpAsyncServer.h:39
Result start(AsyncEventLoop &loop, StringSpan address, uint16_t port)
Starts the http server on the given AsyncEventLoop, address and port.
uint32_t getMaxRequestsPerConnection() const
Get the maximum requests per connection.
Definition HttpAsyncServer.h:91
Result stop()
Stops http server asynchronously pushing cancel and close requests to the event loop.
Result init(Span< T > clients)
Initializes the async server with all needed memory buffers.
Definition HttpAsyncServer.h:43
bool getDefaultKeepAlive() const
Get the default keep-alive setting.
Definition HttpAsyncServer.h:84
void setDefaultKeepAlive(bool enabled)
Set default keep-alive behavior for all connections.
Definition HttpAsyncServer.h:81
Function< void(HttpConnection &)> onRequest
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.
Definition HttpAsyncServer.h:76
void setMaxRequestsPerConnection(uint32_t maxRequests)
Set maximum requests per keep-alive connection.
Definition HttpAsyncServer.h:88
bool isStarted() const
Returns true if the server has been started.
Definition HttpAsyncServer.h:71
Result close()
Closes the server, removing references to the memory buffers passed during init.
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:302
A pool of HttpConnection that can be active or inactive.
Definition HttpConnection.h:385
Adds compile-time configurable read and write queues to any class subclassing HttpConnectionBase.
Definition HttpConnection.h:450
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
EnableIf conditionally defines a type if a boolean template parameter is true.
Definition TypeTraits.h:25
IsBaseOf evaluates to true if the type Base is a base class of Derived, false otherwise.
Definition HttpAsyncServer.h:13