2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#ifndef WEB_HTTP_SERVER_SERVERT_H
43#define WEB_HTTP_SERVER_SERVERT_H
45#include "web/http/server/SocketContextFactory.h"
47#ifndef DOXYGEN_SHOULD_SKIP_THIS
54namespace web::http::
server {
56 template <
template <
typename SocketContextFactoryT,
typename... Args>
typename SocketServerT>
59 std::function<
void(
const std::shared_ptr<web::http::
server::
Request>&,
69 std::function<
void(
const std::shared_ptr<Request>&,
const std::shared_ptr<Response>&)>>;
72 using SocketConnection =
typename Super::SocketConnection;
73 using SocketAddress =
typename SocketConnection::SocketAddress;
76 const std::function<
void(SocketConnection*)>& onConnect,
77 const std::function<
void(SocketConnection*)>& onConnected,
78 const std::function<
void(SocketConnection*)>& onDisconnect,
79 std::function<
void(
const std::shared_ptr<Request>&,
const std::shared_ptr<Response>&)>&& onRequestReady)
80 : Super(name, onConnect, onConnected, onDisconnect, std::forward<
decltype(onRequestReady)>(onRequestReady)) {
83 Server(
const std::function<
void(SocketConnection*)>& onConnect,
84 const std::function<
void(SocketConnection*)>& onConnected,
85 const std::function<
void(SocketConnection*)>& onDisconnect,
86 std::function<
void(
const std::shared_ptr<Request>&,
const std::shared_ptr<Response>&)>&& onRequestReady)
87 :
Server(
"", onConnect, onConnected, onDisconnect, std::forward<
decltype(onRequestReady)>(onRequestReady)) {
91 std::function<
void(
const std::shared_ptr<Request>&,
const std::shared_ptr<Response>&)>&& onRequestReady)
92 : Super(name, std::forward<
decltype(onRequestReady)>(onRequestReady)) {
95 explicit Server(
const std::function<
void(
const std::shared_ptr<Request>&, std::shared_ptr<Response>&)>&& onRequestReady)
96 :
Server(
"", std::forward<
decltype(onRequestReady)>(onRequestReady)) {
#define APPLICATION(req, res)
virtual void acceptTimeout()
void signalEvent(int signum) override
AcceptEventReceiver(const std::string &name, const utils::Timeval &timeout)
virtual void acceptEvent()=0
void dispatchEvent() final
void timeoutEvent() final
Config & getConfig() const
State & operator&=(int state)
static constexpr int DISABLED
bool operator==(const int &state) const
State operator&(int state)
static constexpr int ERROR
static constexpr int FATAL
static constexpr int NO_RETRY
SocketAcceptor(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
std::function< void(SocketConnection *)> onConnected
SocketAcceptor(const SocketAcceptor &socketAcceptor)
SocketAddress localAddress
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
std::shared_ptr< core::socket::stream::SocketContextFactory > socketContextFactory
void unobservedEvent() final
std::shared_ptr< Config > config
virtual void useNextSocketAddress()=0
~SocketAcceptor() override
PhysicalServerSocket physicalServerSocket
std::function< void(const SocketAddress &, core::socket::State)> onStatus
SocketServer(Args &&... args)
std::function< void(SocketConnection *)> setOnConnected(const std::function< void(SocketConnection *)> &onConnected)
void realListen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const
SocketServer(const std::string &name, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
SocketServer(const std::string &name, Args &&... args)
std::function< void(SocketConnection *)> onConnect
SocketServer(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
std::function< void(SocketConnection *)> onDisconnect
void listen(const SocketAddress &localAddress, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::shared_ptr< SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> setOnConnect(const std::function< void(SocketConnection *)> &onConnect)
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> onConnected
std::shared_ptr< SocketContextFactory > getSocketContextFactory()
std::function< void(SocketConnection *)> setOnDisconnect(const std::function< void(SocketConnection *)> &onDisconnect)
void useNextSocketAddress() override
SocketAcceptor(const SocketAcceptor &socketAcceptor)
SocketAcceptor(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Controller(const std::shared_ptr< web::http::server::Request > &request, const std::shared_ptr< web::http::server::Response > &response)
void send(const std::string &chunk)
WebAppT(const std::string &name, const Router &router)
WebAppT(const std::string &name)
static void init(int argc, char *argv[])
static core::TickStatus tick(const utils::Timeval &timeOut=0)
static core::State state()
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
WebApp(const Router &router)
const std::string & getInstanceName() const
ConfigPhysicalSocketServer(ConfigInstance *instance)
utils::Timeval getAcceptTimeout() const
CLI::Option * acceptTimeoutOpt
ConfigPhysicalSocketServer & setAcceptTimeout(const utils::Timeval &acceptTimeout)
CLI::Option * acceptsPerTickOpt
ConfigPhysicalSocketServer & setBacklog(int newBacklog)
int getAcceptsPerTick() const
ConfigPhysicalSocketServer & setAcceptsPerTick(int acceptsPerTickSet)
ConfigSocketServer(net::config::ConfigInstance *instance)
ConfigSocketServer(const std::string &name)
std::string toString(bool expanded=true) const override
PhysicalSocketServer(PhysicalSocketServer &&) noexcept=default
~PhysicalSocketServer() override
void listen(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(const std::string &ipOrHostname, uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
~ConfigSocketServer() override
CLI::Option * disableNagleAlgorithmOpt
bool getReusePort() const
ConfigSocketServer & setReusePort(bool reusePort=true)
bool getReuseAddress() const
bool getDisableNagleAlgorithm() const
ConfigSocketServer & setReuseAddress(bool reuseAddress=true)
ConfigSocketServer(net::config::ConfigInstance *instance)
CLI::Option * reusePortOpt
ConfigSocketServer & setDisableNagleAlgorithm(bool disableNagleAlgorithm=true)
CLI::Option * reuseAddressOpt
~ConfigSocketServer() override
ConfigSocketServer(const std::string &name)
void getAttribute(const std::function< void(Attribute &)> &onFound, const std::function< void(const std::string &)> &onNotFound, const std::string &subKey="") const
static double getInRange(double ll, double ul)
Timeval(const std::initializer_list< time_t > &initList) noexcept
void onSourceData(const char *chunk, std::size_t chunkLen) override
Response(SocketContext *socketContext)
Response & operator=(Response &&) noexcept=delete
SocketContext * getSocketContext() const
SocketContext * socketContext
void onSourceConnect(core::pipe::Source *source) override
web::http::CiStringMap< web::http::CookieOptions > cookies
Response & status(int statusCode)
void send(const std::string &chunk)
Response(Response &&) noexcept=delete
void send(const char *chunk, std::size_t chunkLen)
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
web::http::CiStringMap< std::string > trailer
web::http::CiStringMap< std::string > headers
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
TransferEncoding transferEncoding
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string &)> &status)
Response & type(const std::string &type)
void onSourceError(int errnum) override
Response(Response &)=delete
core::socket::stream::SocketContext * socketContextUpgrade
std::size_t contentLength
Response & sendFragment(const std::string &chunk)
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Response & operator=(Response &)=delete
Response & append(const std::string &field, const std::string &value)
void sendStatus(int statusCode)
Response & sendFragment(const char *chunk, std::size_t chunkLen)
void onSourceEof() override
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Response & clearCookie(const std::string &name, const std::map< std::string, std::string > &options={})
ConnectionState connectionState
const std::string & header(const std::string &field)
Server(const std::string &name, std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &&onRequestReady)
Server(const std::string &name, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &&onRequestReady)
Server(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &&onRequestReady)
Server(const std::function< void(const std::shared_ptr< Request > &, std::shared_ptr< Response > &)> &&onRequestReady)
int main(int argc, char *argv[])