httpd/server
src/httpd/server.tur
POSIX socket listener + worker dispatch.
Since: H6 (pool dispatch added in H7)
defn
server-start
(server-start [port : int handler ptr<void>] :)
bind a TCP listener on `port` and dispatch requests to
Parameters
| port | TCP port to bind on (IPv4, INADDR_ANY) | |
| handler | :ptr<void> function pointer with C signature | |
| int64_t (*)(int64_t). In Turmeric, define it as a top-level | ||
| defn of type [req :int] :int returning a Response handle, and | ||
| pass the defn name. The handler is invoked from a worker | ||
| thread; it must be Send-safe. The server takes ownership of | ||
| the Response it returns (it is freed after the response is | ||
| written, regardless of return value of write). |
Returns
:ptr<void> -- opaque server handle. Pass to server-stop to shut down, or NULL if the listening socket / pool could not be created.
Example
(defn my-handler [req :int] :int
(resp-ok "text/plain" "Hello"))
(let [srv (server-start 8080 my-handler)]
;; ... handle SIGINT, etc. ...
(server-stop srv))
Since: H6 (pool default added in H7)
defn
server-start-pool
(server-start-pool [port : int handler ptr<void> size : int] :)
like server-start but with an explicit pool size.
Parameters
| port | TCP port to bind on | |
| handler | :ptr<void> request handler (see server-start) | |
| size | number of worker threads in the pool (clamped to >= 1) |
Returns
:ptr<void> -- opaque server handle, or NULL on failure.
Example
(def srv (server-start-pool 8080 my-handler 32))
Since: H7
defn
server-start-spawn
(server-start-spawn [port : int handler ptr<void>] :)
thread-per-connection fallback (H6 dispatch mode).
Parameters
| port | TCP port to bind on | |
| handler | :ptr<void> request handler (see server-start) |
Returns
:ptr<void> -- opaque server handle, or NULL on failure.
Example
(def srv (server-start-spawn 8080 my-handler))
Since: H7
defn
server-stop
(server-stop [s ptr<void>] :)
signal the listener to stop, close the socket, join the
Parameters
| s | :ptr<void> server handle returned by server-start (or NULL: no-op). |
Returns
:void
Example
(server-stop srv)
Since: H6 (pool join added in H7)
Internal definitions
srv-make-listenersrv-accept-onesrv-close-fdsrv-shutdown-fdsrv-recv-requestsrv-raw-bytessrv-raw-lensrv-raw-freesrv-write-allsrv-call-handlersrv-worker-loopsrv-warg->intsrv-warg-fdsrv-warg-handlersrv-warg-freesrv-null-ptrsrv-spawn-workersrv-state-newsrv-state->intsrv-state-listen-fdsrv-state-handler-ptrsrv-state-stopping?srv-state-mark-stoppingsrv-state-close-listensrv-state-set-threadsrv-state-get-threadsrv-state-set-poolsrv-state-poolsrv-state-pool-intsrv-state-freesrv-listener-loopsrv-spawn-listenersrv-join-listenersrv-default-pool-size