| Did you know ... | Search Documentation: |
| Pack logtalk -- logtalk-3.101.0/docs/handbook/_sources/libraries/http_socket_transport.rst.txt |
.. _library_http_socket_transport:
http_socket_transport
The http_socket_transport library is the first transport-aware layer
on top of the http_client_core, http_server_core, and
sockets libraries. It uses TCP sockets for connection management
while delegating HTTP message framing and connection semantics to the
existing stream core and server layers.
This library can be used with backend Prolog systems that support
unbound integer arithmetic and the sockets library: ECLiPSe, SICStus
Prolog, SWI-Prolog, Trealla Prolog, and XVM.
Open the `../../apis/library_index.html#http_socket_transport <../../apis/library_index.html#http_socket_transport>`__ link in a web browser.
To load the library, load the loader.lgt file:
::
| ?- logtalk_load(http_socket_transport(loader)).
To test this library, load the tester.lgt file:
::
| ?- logtalk_load(http_socket_transport(tester)).
The current implementation provides nineteen public predicates, grouped by responsibility:
Although it exposes these transport-oriented helpers, the library stays
focused on socket-backed connection and listener management. It
delegates HTTP message framing and connection semantics to
http_client_core and http_server_core, and delegates socket
creation and teardown to the sockets library.
Reusable client connections are represented by
http_connection(Host, Port, ...) handle terms that carry normalized
endpoint metadata and can be passed to exchange/3,
exchange_sequence/3, connection_streams/3, and
close_connection/1.
Accepted WebSocket upgrades are represented by
http_websocket_connection(ClientInfo, Input, Output) handle terms
that can be passed to connection_streams/3 and
close_connection/1.
Managed connection pools are represented by
http_connection_pool(Host, Port, ...) handle terms that carry
normalized endpoint metadata and can be passed to exchange/3,
exchange_sequence/3, close_connection_pool/1, and
connection_pool_stats/2.
Open-ended serving is controlled by a user-supplied control term. A control term is any fresh non-variable term chosen by the caller to identify one active serve_until_shutdown/4 or serve_until_shutdown/5 loop. The same term is later passed to request_shutdown/1 to stop that specific serving loop, distinguishing it from any other listener loops running at the same time.
The open_connection_pool/4 predicate supports the following options:
min_size(N) pre-opens N reusable connections when the pool is
created. The default is 0.max_size(N) limits the pool to at most N managed connections.
The default is 10.connection_options(Options) passes socket options through to
open_connection/4 when creating pooled connections.
Pool exchanges fail immediately with
resource_error(http_socket_transport_connection_pool) when no pooled
connection is available and the pool is already at its maximum size.
The serve_listener/5 predicate supports the following option families:
Shutdown options:
shutdown(keep_open) leaves the listener open after the bounded
serving loop. This is the default.shutdown(close) closes the listener when the bounded serving loop
finishes or aborts.
Worker options:
workers(serial) serves accepted connections sequentially in the
caller thread. This is the default.workers(per_connection) spawns one worker thread per accepted
connection and waits for all workers before returning.workers(pool(N)) serves accepted connections in batches of up to
N worker threads, waiting for each batch to finish before
accepting the next batch.workers(pool(N, rolling)) keeps up to N worker threads active
and accepts the next connection as soon as one worker finishes.
Calling request_listener_shutdown/1 for a listener with an active bounded serve_listener/5 loop wakes any blocked accept call and lets the loop return with the client information terms accepted before the shutdown request.
The serve_until_shutdown/4 and serve_until_shutdown/5 predicates support the workers/1 option family:
workers(serial) serves accepted connections sequentially in the
caller thread. This is the default.workers(per_connection) spawns one worker thread per accepted
connection and waits for active workers when shutdown is requested.workers(pool(N)) serves accepted connections using at most N
concurrent worker threads, waiting for worker completion notifications
before accepting additional connections.workers(pool(N, rolling)) is accepted as an explicit alias for the
rolling fixed-size worker-pool policy used by workers(pool(N)).
Open-ended serving loops should use a fresh control term for each call to serve_until_shutdown/4 or serve_until_shutdown/5.
When the same listener, serving, or pool-management option is given multiple times, the first occurrence is used.
The current WebSocket transport workflow is intentionally limited to the opening handshake handoff:
http_client::open_websocket/4 to obtain a
reusable upgraded connection handle.101 Switching Protocols response, typically
via http_server_core::accept_websocket/3.http_websocket::read_frame/2
and http_websocket::write_frame/2 or, at the next abstraction
level, with http_websocket_messages::read_message/2 and
http_websocket_messages::write_message/2, or with the stateful
http_websocket_session layer when interleaved control frames and
role-aware masking policies matter, including automatic close replies
and optional automatic pong replies, or with the
http_websocket_service layer, which provides the higher-level
run_session/3-4 callback loop that takes ownership of the upgraded
connection and closes it automatically after the close handshake
completes, the client-side http_websocket_client_service::open/4-5
convenience, and the server-side
http_websocket_server_service::serve_once/6-7 convenience.sockets
library.workers(per_connection) and workers(pool(N)) options
depend on backend thread support.