# Server

<a id="server"></a>

# Server

```cpp
#include <icy/http/server.h>

class Server
```

[Defined in src/http/include/icy/http/server.h:333](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L333)

> **Inherits:** [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#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.

## List of all members

| Name | Kind | Owner |
|------|------|-------|
| [`ServerConnection`](#serverconnection) | `friend` | Declared here |
| [`Connection`](#connection-9) | `variable` | Declared here |
| [`Shutdown`](#shutdown-9) | `variable` | Declared here |
| [`Server`](#server-1) | `function` | Declared here |
| [`Server`](#server-2) | `function` | Declared here |
| [`Server`](#server-3) | `function` | Declared here |
| [`Server`](#server-4) | `function` | Declared here |
| [`~Server`](#server-5) | `function` | Declared here |
| [`start`](#start-25) | `function` | Declared here |
| [`stop`](#stop-15) | `function` | Declared here |
| [`setReusePort`](#setreuseport-1) | `function` | Declared here |
| [`setMaxPooledConnections`](#setmaxpooledconnections) | `function` | Declared here |
| [`setKeepAliveTimeout`](#setkeepalivetimeout) | `function` | Declared here |
| [`setWebSocketOriginValidator`](#setwebsocketoriginvalidator) | `function` | Declared here |
| [`connectionCount`](#connectioncount) | `function` | Declared here |
| [`address`](#address-11) | `function` | Declared here |
| [`dateCache`](#datecache-1) | `function` | Declared here |
| [`_loop`](#_loop-1) | `variable` | Declared here |
| [`_address`](#_address) | `variable` | Declared here |
| [`_socket`](#_socket-2) | `variable` | Declared here |
| [`_timer`](#_timer-2) | `variable` | Declared here |
| [`_factory`](#_factory) | `variable` | Declared here |
| [`_webSocketOriginValidator`](#_websocketoriginvalidator) | `variable` | Declared here |
| [`_connections`](#_connections-1) | `variable` | Declared here |
| [`_pool`](#_pool-1) | `variable` | Declared here |
| [`_dateCache`](#_datecache) | `variable` | Declared here |
| [`_keepAliveTimeout`](#_keepalivetimeout) | `variable` | Declared here |
| [`_reusePort`](#_reuseport) | `variable` | Declared here |
| [`createResponder`](#createresponder) | `function` | Declared here |
| [`onClientSocketAccept`](#onclientsocketaccept) | `function` | Declared here |
| [`onConnectionReady`](#onconnectionready) | `function` | Declared here |
| [`onConnectionClose`](#onconnectionclose-1) | `function` | Declared here |
| [`onSocketClose`](#onsocketclose-3) | `function` | Declared here |
| [`onTimer`](#ontimer) | `function` | Declared here |
| [`isWebSocketOriginAllowed`](#iswebsocketoriginallowed) | `function` | Declared here |
| [`loop`](#loop-7) | `function` | Declared here |
| [`WebSocketOriginValidator`](#websocketoriginvalidator) | `typedef` | Declared here |
| [`priority`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#priority-3) | `variable` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter-1) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`~SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter-2) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`send`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#send-1) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`send`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#send-2) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`sendOwned`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendowned-2) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`sendOwned`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendowned-3) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`sendPacket`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendpacket) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`sendPacket`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendpacket-1) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`sendPacket`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendpacket-2) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`setSender`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#setsender) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`sender`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sender) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`addReceiver`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#addreceiver) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`removeReceiver`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#removereceiver) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`hasReceiver`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#hasreceiver) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`receivers`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#receivers) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`onSocketConnect`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketconnect) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`onSocketRecv`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketrecv-1) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`onSocketError`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketerror) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`onSocketClose`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketclose) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`_sender`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_sender) | `variable` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`_receivers`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_receivers) | `variable` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`_dirty`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_dirty) | `variable` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`_dispatchDepth`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_dispatchdepth) | `variable` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |
| [`cleanupReceivers`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#cleanupreceivers) | `function` | Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) |

## Inherited from [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter)

| Kind | Name | Description |
|------|------|-------------|
| `variable` | [`priority`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#priority-3)  | The priority of this adapter for STL sort operations. |
| `function` | [`SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter-1)  | Creates the [SocketAdapter](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter). |
| `function` | [`~SocketAdapter`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter-2) `virtual` `noexcept` | Destroys the [SocketAdapter](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter). |
| `function` | [`send`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#send-1) `virtual` | Sends the given data buffer to the connected peer. Returns the number of bytes sent or -1 on error. No exception will be thrown. For TCP sockets the given peer address must match the connected peer address. |
| `function` | [`send`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#send-2) `virtual` |  |
| `function` | [`sendOwned`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendowned-2) `virtual` | Sends an owned payload buffer to the connected peer. |
| `function` | [`sendOwned`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendowned-3) `virtual` |  |
| `function` | [`sendPacket`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendpacket) `virtual` | Sends the given packet to the connected peer. Returns the number of bytes sent or -1 on error. No exception will be thrown. For TCP sockets the given peer address must match the connected peer address. |
| `function` | [`sendPacket`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendpacket-1) `virtual` |  |
| `function` | [`sendPacket`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sendpacket-2) `virtual` | Sends the given packet to the connected peer. This method provides delegate compatibility, and unlike other send methods throws an exception if the underlying socket is closed. |
| `function` | [`setSender`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#setsender) `virtual` | Sets the pointer to the outgoing data adapter. Send methods proxy data to this adapter by default. |
| `function` | [`sender`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#sender)  | Returns the output [SocketAdapter](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) pointer. |
| `function` | [`addReceiver`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#addreceiver) `virtual` | Sets the pointer to the incoming data adapter. Events proxy data to this adapter by default. |
| `function` | [`removeReceiver`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#removereceiver) `virtual` | Remove the given receiver. |
| `function` | [`hasReceiver`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#hasreceiver) `virtual` | Returns true if the given receiver is connected. |
| `function` | [`receivers`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#receivers)  | Returns all currently registered input [SocketAdapter](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#socketadapter) pointers. Dead (removed) entries are excluded from the returned list. |
| `function` | [`onSocketConnect`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketconnect) `virtual` | Called when the socket establishes a connection. Forwards the event to all registered receivers in priority order. Override to intercept before the application sees the event. |
| `function` | [`onSocketRecv`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketrecv-1) `virtual` | Called when data is received from the socket. Forwards the event to all registered receivers in priority order. |
| `function` | [`onSocketError`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketerror) `virtual` | Called when the socket encounters an error. Forwards the event to all registered receivers in priority order. |
| `function` | [`onSocketClose`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketclose) `virtual` | Called when the socket is closed. Forwards the event to all registered receivers in priority order. |
| `variable` | [`_sender`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_sender)  |  |
| `variable` | [`_receivers`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_receivers)  |  |
| `variable` | [`_dirty`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_dirty)  |  |
| `variable` | [`_dispatchDepth`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#_dispatchdepth)  |  |
| `function` | [`cleanupReceivers`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#cleanupreceivers) `virtual` |  |

## Friends

| Name | Description |
|------|-------------|
| [`ServerConnection`](#serverconnection)  |  |

---

<a id="serverconnection"></a>

### ServerConnection

```cpp
friend class ServerConnection
```

[Defined in src/http/include/icy/http/server.h:444](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L444)

## Public Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`LocalSignal`](https://auscaster.com/icey/docs/api-reference/base.md#localsignal)< void([`ServerConnection::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnection.md#ptr-18))> | [`Connection`](#connection-9)  | Signals when a new connection has been created. A reference to the new connection object is provided. |
| [`LocalSignal`](https://auscaster.com/icey/docs/api-reference/base.md#localsignal)< void()> | [`Shutdown`](#shutdown-9)  | Signals when the server is shutting down. |

---

<a id="connection-9"></a>

### Connection

```cpp
LocalSignal< void(ServerConnection::Ptr)> Connection
```

Type: [`LocalSignal`](https://auscaster.com/icey/docs/api-reference/base.md#localsignal)< void([`ServerConnection::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnection.md#ptr-18))>

[Defined in src/http/include/icy/http/server.h:413](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L413)

Signals when a new connection has been created. A reference to the new connection object is provided.

---

<a id="shutdown-9"></a>

### Shutdown

```cpp
LocalSignal< void()> Shutdown
```

Type: [`LocalSignal`](https://auscaster.com/icey/docs/api-reference/base.md#localsignal)< void()>

[Defined in src/http/include/icy/http/server.h:416](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L416)

Signals when the server is shutting down.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Server`](#server-1)  | Constructs an HTTP server on the given host and port using an internally created TCP socket. |
|  | [`Server`](#server-2)  | Constructs an HTTP server on the given address using an internally created TCP socket. |
|  | [`Server`](#server-3)  | 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`](#server-4)  | Constructs an HTTP server on the given address using a caller-supplied socket. The event loop is derived from the socket. |
|  | [`~Server`](#server-5) `virtual` |  |
| `void` | [`start`](#start-25)  | Start the HTTP server. |
| `void` | [`stop`](#stop-15)  | Stop the HTTP server. |
| `void` | [`setReusePort`](#setreuseport-1) `inline` | Enable SO_REUSEPORT for multicore server instances. Must be called before [start()](#start-25). Allows multiple server instances to bind the same address:port with kernel-level load balancing (Linux 3.9+). |
| `void` | [`setMaxPooledConnections`](#setmaxpooledconnections) `inline` | Set the maximum number of pooled connections (default 128). Set to 0 to disable connection pooling entirely. |
| `void` | [`setKeepAliveTimeout`](#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. |
| `void` | [`setWebSocketOriginValidator`](#setwebsocketoriginvalidator)  | Set an optional WebSocket Origin validator. When set, upgrade requests rejected by the validator receive 403 before the 101 handshake. |
| `size_t` | [`connectionCount`](#connectioncount) `const` `inline` | Return the number of active connections (all states). |
| [`net::Address`](https://auscaster.com/icey/docs/api-reference/icy-net-Address.md#address) & | [`address`](#address-11)  | Return the server bind address. |
| const [`DateCache`](https://auscaster.com/icey/docs/api-reference/icy-http-DateCache.md#datecache) & | [`dateCache`](#datecache-1) `const` `inline` | Return the cached Date header for use in responses. |

---

<a id="server-1"></a>

### Server

```cpp
Server(const std::string & host, short port, uv::Loop * loop = uv::defaultLoop(), std::unique_ptr< ServerConnectionFactory > factory = std::make_unique< ServerConnectionFactory >())
```

[Defined in src/http/include/icy/http/server.h:343](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L343)

Constructs an HTTP server on the given host and port using an internally created TCP socket.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `host` | `const std::string &` | Bind address (e.g. "0.0.0.0" or "127.0.0.1"). |
| `port` | `short` | TCP port to listen on. |
| `loop` | [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) * | Event loop to use. Defaults to the default libuv loop. |
| `factory` | std::unique_ptr< [`ServerConnectionFactory`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnectionFactory.md#serverconnectionfactory) > | [Connection](https://auscaster.com/icey/docs/api-reference/icy-http-Connection.md#connection-1) and responder factory. Defaults to the base factory. |

---

<a id="server-2"></a>

### Server

```cpp
Server(const net::Address & address, uv::Loop * loop = uv::defaultLoop(), std::unique_ptr< ServerConnectionFactory > factory = std::make_unique< ServerConnectionFactory >())
```

[Defined in src/http/include/icy/http/server.h:351](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L351)

Constructs an HTTP server on the given address using an internally created TCP socket.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `address` | const [`net::Address`](https://auscaster.com/icey/docs/api-reference/icy-net-Address.md#address) & | Bind address and port. |
| `loop` | [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) * | Event loop to use. Defaults to the default libuv loop. |
| `factory` | std::unique_ptr< [`ServerConnectionFactory`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnectionFactory.md#serverconnectionfactory) > | [Connection](https://auscaster.com/icey/docs/api-reference/icy-http-Connection.md#connection-1) and responder factory. |

---

<a id="server-3"></a>

### Server

```cpp
Server(const std::string & host, short port, net::TCPSocket::Ptr socket, std::unique_ptr< ServerConnectionFactory > factory = std::make_unique< ServerConnectionFactory >())
```

[Defined in src/http/include/icy/http/server.h:361](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L361)

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

| Parameter | Type | Description |
|-----------|------|-------------|
| `host` | `const std::string &` | Bind address. |
| `port` | `short` | TCP port to listen on. |
| `socket` | [`net::TCPSocket::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-net-TCPSocket.md#ptr-14) | Pre-created socket (e.g. SSLSocket for HTTPS). |
| `factory` | std::unique_ptr< [`ServerConnectionFactory`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnectionFactory.md#serverconnectionfactory) > | [Connection](https://auscaster.com/icey/docs/api-reference/icy-http-Connection.md#connection-1) and responder factory. |

---

<a id="server-4"></a>

### Server

```cpp
Server(const net::Address & address, net::TCPSocket::Ptr socket, std::unique_ptr< ServerConnectionFactory > factory = std::make_unique< ServerConnectionFactory >())
```

[Defined in src/http/include/icy/http/server.h:370](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L370)

Constructs an HTTP server on the given address using a caller-supplied socket. The event loop is derived from the socket.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `address` | const [`net::Address`](https://auscaster.com/icey/docs/api-reference/icy-net-Address.md#address) & | Bind address and port. |
| `socket` | [`net::TCPSocket::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-net-TCPSocket.md#ptr-14) | Pre-created socket (e.g. SSLSocket for HTTPS). |
| `factory` | std::unique_ptr< [`ServerConnectionFactory`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnectionFactory.md#serverconnectionfactory) > | [Connection](https://auscaster.com/icey/docs/api-reference/icy-http-Connection.md#connection-1) and responder factory. |

---

<a id="server-5"></a>

### ~Server

`virtual`

```cpp
virtual ~Server()
```

[Defined in src/http/include/icy/http/server.h:374](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L374)

---

<a id="start-25"></a>

### start

```cpp
void start()
```

[Defined in src/http/include/icy/http/server.h:377](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L377)

Start the HTTP server.

---

<a id="stop-15"></a>

### stop

```cpp
void stop()
```

[Defined in src/http/include/icy/http/server.h:380](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L380)

Stop the HTTP server.

---

<a id="setreuseport-1"></a>

### setReusePort

`inline`

```cpp
inline void setReusePort(bool enable = true)
```

[Defined in src/http/include/icy/http/server.h:387](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L387)

Enable SO_REUSEPORT for multicore server instances. Must be called before [start()](#start-25). Allows multiple server instances to bind the same address:port with kernel-level load balancing (Linux 3.9+).

---

<a id="setmaxpooledconnections"></a>

### setMaxPooledConnections

`inline`

```cpp
inline void setMaxPooledConnections(size_t n)
```

[Defined in src/http/include/icy/http/server.h:391](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L391)

Set the maximum number of pooled connections (default 128). Set to 0 to disable connection pooling entirely.

---

<a id="setkeepalivetimeout"></a>

### setKeepAliveTimeout

`inline`

```cpp
inline void setKeepAliveTimeout(int seconds)
```

[Defined in src/http/include/icy/http/server.h:396](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L396)

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.

---

<a id="setwebsocketoriginvalidator"></a>

### setWebSocketOriginValidator

```cpp
void setWebSocketOriginValidator(WebSocketOriginValidator validator)
```

[Defined in src/http/include/icy/http/server.h:400](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L400)

Set an optional WebSocket Origin validator. When set, upgrade requests rejected by the validator receive 403 before the 101 handshake.

---

<a id="connectioncount"></a>

### connectionCount

`const` `inline`

```cpp
inline size_t connectionCount() const
```

[Defined in src/http/include/icy/http/server.h:403](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L403)

Return the number of active connections (all states).

---

<a id="address-11"></a>

### address

```cpp
net::Address & address()
```

[Defined in src/http/include/icy/http/server.h:406](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L406)

Return the server bind address.

---

<a id="datecache-1"></a>

### dateCache

`const` `inline`

```cpp
inline const DateCache & dateCache() const
```

[Defined in src/http/include/icy/http/server.h:409](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L409)

Return the cached Date header for use in responses.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) * | [`_loop`](#_loop-1)  |  |
| [`net::Address`](https://auscaster.com/icey/docs/api-reference/icy-net-Address.md#address) | [`_address`](#_address)  |  |
| [`net::TCPSocket::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-net-TCPSocket.md#ptr-14) | [`_socket`](#_socket-2)  |  |
| [`Timer`](https://auscaster.com/icey/docs/api-reference/icy-Timer.md#timer) | [`_timer`](#_timer-2)  |  |
| std::unique_ptr< [`ServerConnectionFactory`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnectionFactory.md#serverconnectionfactory) > | [`_factory`](#_factory)  |  |
| [`WebSocketOriginValidator`](#websocketoriginvalidator) | [`_webSocketOriginValidator`](#_websocketoriginvalidator)  |  |
| std::unordered_map< [`ServerConnection`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnection.md#serverconnection-1) *, [`ServerConnection::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnection.md#ptr-18) > | [`_connections`](#_connections-1)  |  |
| [`ConnectionPool`](https://auscaster.com/icey/docs/api-reference/icy-http-ConnectionPool.md#connectionpool) | [`_pool`](#_pool-1)  |  |
| [`DateCache`](https://auscaster.com/icey/docs/api-reference/icy-http-DateCache.md#datecache) | [`_dateCache`](#_datecache)  |  |
| `int` | [`_keepAliveTimeout`](#_keepalivetimeout)  |  |
| `bool` | [`_reusePort`](#_reuseport)  |  |

---

<a id="_loop-1"></a>

### _loop

```cpp
uv::Loop * _loop
```

Type: [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) *

[Defined in src/http/include/icy/http/server.h:432](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L432)

---

<a id="_address"></a>

### _address

```cpp
net::Address _address
```

Type: [`net::Address`](https://auscaster.com/icey/docs/api-reference/icy-net-Address.md#address)

[Defined in src/http/include/icy/http/server.h:433](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L433)

---

<a id="_socket-2"></a>

### _socket

```cpp
net::TCPSocket::Ptr _socket
```

Type: [`net::TCPSocket::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-net-TCPSocket.md#ptr-14)

[Defined in src/http/include/icy/http/server.h:434](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L434)

---

<a id="_timer-2"></a>

### _timer

```cpp
Timer _timer
```

Type: [`Timer`](https://auscaster.com/icey/docs/api-reference/icy-Timer.md#timer)

[Defined in src/http/include/icy/http/server.h:435](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L435)

---

<a id="_factory"></a>

### _factory

```cpp
std::unique_ptr< ServerConnectionFactory > _factory
```

Type: std::unique_ptr< [`ServerConnectionFactory`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnectionFactory.md#serverconnectionfactory) >

[Defined in src/http/include/icy/http/server.h:436](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L436)

---

<a id="_websocketoriginvalidator"></a>

### _webSocketOriginValidator

```cpp
WebSocketOriginValidator _webSocketOriginValidator
```

Type: [`WebSocketOriginValidator`](#websocketoriginvalidator)

[Defined in src/http/include/icy/http/server.h:437](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L437)

---

<a id="_connections-1"></a>

### _connections

```cpp
std::unordered_map< ServerConnection *, ServerConnection::Ptr > _connections
```

Type: std::unordered_map< [`ServerConnection`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnection.md#serverconnection-1) *, [`ServerConnection::Ptr`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerConnection.md#ptr-18) >

[Defined in src/http/include/icy/http/server.h:438](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L438)

---

<a id="_pool-1"></a>

### _pool

```cpp
ConnectionPool _pool
```

Type: [`ConnectionPool`](https://auscaster.com/icey/docs/api-reference/icy-http-ConnectionPool.md#connectionpool)

[Defined in src/http/include/icy/http/server.h:439](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L439)

---

<a id="_datecache"></a>

### _dateCache

```cpp
DateCache _dateCache
```

Type: [`DateCache`](https://auscaster.com/icey/docs/api-reference/icy-http-DateCache.md#datecache)

[Defined in src/http/include/icy/http/server.h:440](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L440)

---

<a id="_keepalivetimeout"></a>

### _keepAliveTimeout

```cpp
int _keepAliveTimeout {30}
```

[Defined in src/http/include/icy/http/server.h:441](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L441)

---

<a id="_reuseport"></a>

### _reusePort

```cpp
bool _reusePort {false}
```

[Defined in src/http/include/icy/http/server.h:442](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L442)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
| std::unique_ptr< [`ServerResponder`](https://auscaster.com/icey/docs/api-reference/icy-http-ServerResponder.md#serverresponder) > | [`createResponder`](#createresponder)  |  |
| `void` | [`onClientSocketAccept`](#onclientsocketaccept)  |  |
| `void` | [`onConnectionReady`](#onconnectionready)  |  |
| `void` | [`onConnectionClose`](#onconnectionclose-1)  |  |
| `bool` | [`onSocketClose`](#onsocketclose-3) `virtual` | Called when the socket is closed. Forwards the event to all registered receivers in priority order. |
| `void` | [`onTimer`](#ontimer)  |  |
| `bool` | [`isWebSocketOriginAllowed`](#iswebsocketoriginallowed) `const` |  |
| [`uv::Loop`](https://auscaster.com/icey/docs/api-reference/uv.md#loop-1) * | [`loop`](#loop-7) `const` `inline` | Return the event loop this server runs on. |

---

<a id="createresponder"></a>

### createResponder

```cpp
std::unique_ptr< ServerResponder > createResponder(ServerConnection & conn)
```

[Defined in src/http/include/icy/http/server.h:419](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L419)

---

<a id="onclientsocketaccept"></a>

### onClientSocketAccept

```cpp
void onClientSocketAccept(const net::TCPSocket::Ptr & socket)
```

[Defined in src/http/include/icy/http/server.h:421](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L421)

---

<a id="onconnectionready"></a>

### onConnectionReady

```cpp
void onConnectionReady(ServerConnection & conn)
```

[Defined in src/http/include/icy/http/server.h:422](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L422)

---

<a id="onconnectionclose-1"></a>

### onConnectionClose

```cpp
void onConnectionClose(ServerConnection & conn)
```

[Defined in src/http/include/icy/http/server.h:423](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L423)

---

<a id="onsocketclose-3"></a>

### onSocketClose

`virtual`

```cpp
virtual bool onSocketClose(net::Socket & socket)
```

[Defined in src/http/include/icy/http/server.h:424](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L424)

Called when the socket is closed. Forwards the event to all registered receivers in priority order. 
#### Returns
true to stop propagation to subsequent receivers.

#### Reimplements

- [`SocketAdapter::onSocketClose`](https://auscaster.com/icey/docs/api-reference/icy-net-SocketAdapter.md#onsocketclose)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `socket` | [`net::Socket`](https://auscaster.com/icey/docs/api-reference/icy-net-Socket.md#socket-1) & | The socket that was closed. |

---

<a id="ontimer"></a>

### onTimer

```cpp
void onTimer()
```

[Defined in src/http/include/icy/http/server.h:425](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L425)

---

<a id="iswebsocketoriginallowed"></a>

### isWebSocketOriginAllowed

`const`

```cpp
bool isWebSocketOriginAllowed(const Request & request) const
```

[Defined in src/http/include/icy/http/server.h:426](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L426)

---

<a id="loop-7"></a>

### loop

`const` `inline`

```cpp
inline uv::Loop * loop() const
```

[Defined in src/http/include/icy/http/server.h:429](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L429)

Return the event loop this server runs on.

## Public Types

| Name | Description |
|------|-------------|
| [`WebSocketOriginValidator`](#websocketoriginvalidator)  |  |

---

<a id="websocketoriginvalidator"></a>

### WebSocketOriginValidator

```cpp
using WebSocketOriginValidator = std::function< bool(const Request &)>
```

Type: std::function< bool(const [`Request`](https://auscaster.com/icey/docs/api-reference/icy-http-Request.md#request-6) &)>

[Defined in src/http/include/icy/http/server.h:336](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/server.h#L336)
