A listener accepts TCP connections or UDP flows on one port of a TailcatServer. Listeners are created by TailcatServer$listen(), not directly.
Accepted connections belong to the server rather than the listener: closing the listener stops new connections but leaves accepted ones open, while closing the server closes everything.
Once the listener or its server is closed, every method other than $close(), $stop_serving(), $format(), and $print(), and every field other than $is_closed, $is_serving, and $server, raises a tailcat_closed error. Timeouts (including timeout = 0, a poll) and errors are described in TailcatServer.
Serving connections
$serve(handler) starts an accept loop driven by the later event loop and returns immediately. Each new connection is passed to handler(conn) on the main R thread. The loop only makes progress while the event loop runs: when R is idle at the console, inside Shiny or httpuv, or while a script calls later::run_now() in a loop. Blocking calls inside the handler (such as conn$read()) hold up the loop until they return, so long-running handlers should use the _async() methods and return the resulting promise. A handler may return a promise (or anything promises::is.promising() accepts): if it rejects, the rejection is treated like an error signaled by the handler, so on_error is called and the connection is closed. The loop does not wait for the promise before accepting the next connection. The loop ends quietly when the listener or the server is closed, or when $stop_serving() is called; $stop_serving() leaves the listener open, so it can accept connections again or serve with a new handler. $is_serving tells whether a loop is running.
Active bindings
portThe port number being listened on.
local_addressThe local endpoint as
"host:port"on the server's tailcat network (not the secret tailcat address).network"tcp"or"udp".serverThe owning TailcatServer.
is_servingTRUEwhile an accept loop started by$serve()is running,FALSEotherwise, including once the listener is closed.is_closedTRUEonce the listener or its server has been closed.
Methods
TailcatListener$new()
Internal constructor; use TailcatServer$listen().
Usage
TailcatListener$new(handle, server, network)Arguments
handleNative listener handle.
serverThe owning TailcatServer.
networkInteger network code.
TailcatListener$accept()
Wait for the next connection (TCP) or new flow (UDP). A timeout ends only this call; the listener stays usable.
Arguments
timeoutSeconds to wait;
Infwaits indefinitely and0polls (returns a connection that is already waiting).
TailcatListener$accept_async()
Asynchronous version of $accept().
Returns
A promise resolving with a TailcatConnection.
TailcatListener$serve()
Start an asynchronous accept loop that calls handler(conn) for every connection. See the section on serving connections.
Arguments
handlerA function of one argument, the TailcatConnection. It may return a promise.
on_errorA function
function(cond, conn)called whenhandlersignals an error or the promise it returned rejects (or when accepting fails, withconn = NULL), orNULLto turn such errors into warnings (signaled from the later event loop). After a handler error the connection is closed. An error signaled byon_erroritself is turned into a warning.
TailcatListener$stop_serving()
Stop the accept loop started by $serve() without closing the listener. The pending accept is canceled; connections already accepted are not affected, and a connection whose accept completed at the same moment is still passed to the handler. Calling it when the listener is not serving does nothing.
TailcatListener$close()
Stop accepting connections. Accepted connections stay open. Closing twice is harmless.
Examples
if (FALSE) { # \dontrun{
srv = TailcatServer$new()
lst = srv$listen(7)
lst$serve(function(conn) {
promises::then(conn$read_all_async(timeout = 30), function(data) {
conn$write(data)$close()
})
})
srv$address
} # }