Server
Server
#include <icy/http/server.h>Inherits:
SocketAdapter
HTTP server implementation.
This HTTP server is not strictly standards compliant. It was created to be a fast (nocopy where possible) solution for streaming media to web browsers.
Public Attributes
| Return | Name | Description |
|---|---|---|
LocalSignal< void(ServerConnection::Ptr)> | Connection | Signals when a new connection has been created. A reference to the new connection object is provided. |
LocalSignal< void()> | Shutdown | Signals when the server is shutting down. |
Connection
LocalSignal< void(ServerConnection::Ptr)> ConnectionSignals when a new connection has been created. A reference to the new connection object is provided.
Shutdown
LocalSignal< void()> ShutdownSignals when the server is shutting down.
Public Methods
| Return | Name | Description |
|---|---|---|
Server | Constructs an HTTP server on the given host and port using an internally created TCP socket. | |
Server | Constructs an HTTP server on the given address using an internally created TCP socket. | |
Server | Constructs an HTTP server on the given host and port using a caller-supplied socket. Useful for HTTPS by passing an SSLSocket. The event loop is derived from the socket. | |
Server | Constructs an HTTP server on the given address using a caller-supplied socket. The event loop is derived from the socket. | |
void | start | Start the HTTP server. |
void | stop | Stop the HTTP server. |
void | setReusePort inline | Enable SO_REUSEPORT for multicore server instances. Must be called before start(). Allows multiple server instances to bind the same address:port with kernel-level load balancing (Linux 3.9+). |
void | setMaxPooledConnections inline | Set the maximum number of pooled connections (default 128). Set to 0 to disable connection pooling entirely. |
void | setKeepAliveTimeout inline | Set the keep-alive idle timeout in seconds (default 30). Connections idle longer than this are closed by the timer. Set to 0 to disable idle timeout. |
size_t | connectionCount const inline | Return the number of active connections (all states). |
net::Address & | address | Return the server bind address. |
const DateCache & | dateCache const inline | Return the cached Date header for use in responses. |
Server
Server(const std::string & host, short port, uv::Loop * loop, std::unique_ptr< ServerConnectionFactory > factory)Constructs an HTTP server on the given host and port using an internally created TCP socket.
Parameters
hostBind address (e.g. "0.0.0.0" or "127.0.0.1").portTCP port to listen on.loopEvent loop to use. Defaults to the default libuv loop.factoryConnection and responder factory. Defaults to the base factory.
Server
Server(const net::Address & address, uv::Loop * loop, std::unique_ptr< ServerConnectionFactory > factory)Constructs an HTTP server on the given address using an internally created TCP socket.
Parameters
addressBind address and port.loopEvent loop to use. Defaults to the default libuv loop.factoryConnection and responder factory.
Server
Server(const std::string & host, short port, net::TCPSocket::Ptr socket, std::unique_ptr< ServerConnectionFactory > factory)Constructs an HTTP server on the given host and port using a caller-supplied socket. Useful for HTTPS by passing an SSLSocket. The event loop is derived from the socket.
Parameters
hostBind address.portTCP port to listen on.socketPre-created socket (e.g. SSLSocket for HTTPS).factoryConnection and responder factory.
Server
Server(const net::Address & address, net::TCPSocket::Ptr socket, std::unique_ptr< ServerConnectionFactory > factory)Constructs an HTTP server on the given address using a caller-supplied socket. The event loop is derived from the socket.
Parameters
addressBind address and port.socketPre-created socket (e.g. SSLSocket for HTTPS).factoryConnection and responder factory.
start
void start()Start the HTTP server.
stop
void stop()Stop the HTTP server.
setReusePort
inline
inline void setReusePort(bool enable)Enable SO_REUSEPORT for multicore server instances. Must be called before start(). Allows multiple server instances to bind the same address:port with kernel-level load balancing (Linux 3.9+).
setMaxPooledConnections
inline
inline void setMaxPooledConnections(size_t n)Set the maximum number of pooled connections (default 128). Set to 0 to disable connection pooling entirely.
setKeepAliveTimeout
inline
inline void setKeepAliveTimeout(int seconds)Set the keep-alive idle timeout in seconds (default 30). Connections idle longer than this are closed by the timer. Set to 0 to disable idle timeout.
connectionCount
const inline
inline size_t connectionCount() constReturn the number of active connections (all states).
address
net::Address & address()Return the server bind address.
dateCache
const inline
inline const DateCache & dateCache() constReturn the cached Date header for use in responses.
Protected Attributes
| Return | Name | Description |
|---|---|---|
uv::Loop * | _loop | |
net::Address | _address | |
net::TCPSocket::Ptr | _socket | |
Timer | _timer | |
std::unique_ptr< ServerConnectionFactory > | _factory | |
std::unordered_map< ServerConnection *, ServerConnection::Ptr > | _connections | |
ConnectionPool | _pool | |
DateCache | _dateCache | |
int | _keepAliveTimeout | |
bool | _reusePort |
_loop
uv::Loop * _loop_address
net::Address _address_socket
net::TCPSocket::Ptr _socket_timer
Timer _timer_factory
std::unique_ptr< ServerConnectionFactory > _factory_connections
std::unordered_map< ServerConnection *, ServerConnection::Ptr > _connections_pool
ConnectionPool _pool_dateCache
DateCache _dateCache_keepAliveTimeout
int _keepAliveTimeout {30}_reusePort
bool _reusePort {false}Protected Methods
| Return | Name | Description |
|---|---|---|
std::unique_ptr< ServerResponder > | createResponder | |
void | onClientSocketAccept | |
void | onConnectionReady | |
void | onConnectionClose | |
bool | onSocketClose virtual | Called when the socket is closed. Forwards the event to all registered receivers in priority order. |
void | onTimer | |
uv::Loop * | loop const inline | Return the event loop this server runs on. |
createResponder
std::unique_ptr< ServerResponder > createResponder(ServerConnection & conn)onClientSocketAccept
void onClientSocketAccept(const net::TCPSocket::Ptr & socket)onConnectionReady
void onConnectionReady(ServerConnection & conn)onConnectionClose
void onConnectionClose(ServerConnection & conn)onSocketClose
virtual
virtual bool onSocketClose(net::Socket & socket)Called when the socket is closed. Forwards the event to all registered receivers in priority order.
Parameters
socketThe socket that was closed.
Returns
true to stop propagation to subsequent receivers.
onTimer
void onTimer()loop
const inline
inline uv::Loop * loop() constReturn the event loop this server runs on.
