A tailcat server registers with a public relay and publishes a secret, shareable address. Clients holding the address can dial the ports the server listens on, over TCP or UDP, without accounts or port forwarding.
Blocking methods have an _async() counterpart that returns a promises::promise() resolving with the same value, or rejecting with the same condition. Asynchronous results are delivered by the later event loop, so they only arrive while R is idle at the console, inside Shiny or httpuv, or while you call later::run_now(). The result goes to the loop that was current when the operation started, so an operation started inside later::with_temp_loop() or later::with_loop() completes while that loop runs. If that loop is destroyed before the operation completes, the result is delivered on the global loop instead (checked about once a second while such operations are pending). A pending asynchronous operation can be canceled with tailcat_cancel().
Blocking methods can be interrupted (Ctrl-C or Esc), which cancels the operation. If the operation completes at the same moment, its result is returned and that interrupt is dropped rather than losing data such as bytes just read.
The address and the private and pre-shared keys are secrets: anyone with the address can connect. Printing a server never shows them. $address and the address element of $info() are tailcat_secret strings, which print as <redacted> but otherwise behave like the full address (for example with writeLines() or as.character()). The server does not keep the address in an R field, so saving the server object (for example with saveRDS() or save.image()) does not write the address to disk. Handles do not survive serialization: a server restored from a saved session is closed.
Once a server is closed, every method other than $close(), $format(), and $print(), and every field other than $is_closed, raises a tailcat_closed error; nothing is kept from before.
Timeouts
Every blocking method and function of this package takes a timeout in seconds. Inf waits indefinitely. timeout = 0 is a non-blocking poll: the call returns what is available right away (for example data already received, or a connection already waiting) and otherwise raises tailcat_timeout at once. This differs from tailcat's C ABI (tailcat.h), where a zero timeout is an already expired deadline that fails without trying; rtailcat gives a poll a deadline of a few milliseconds instead. A poll waits for an operation started before it in the same direction of a connection only for that short time.
A timeout ends only that call: the server, listener, client, or connection stays usable.
Errors
Errors are conditions with class tailcat_error and one subclass that says what went wrong. Each also carries an integer status (the status code of the C ABI) and bytes, the number of bytes transferred before the error (for reads and writes; otherwise 0). The message starts with a one-line summary; a line starting tailcat: gives tailcat's own description when it has one.
status | Class | Meaning |
| 1 | tailcat_invalid_argument | An argument is invalid |
| 2 | tailcat_closed | The object, or the client or server that owns it, is closed |
| 3 | tailcat_timeout | The timeout ran out |
| 4 | tailcat_canceled | The operation was canceled with tailcat_cancel() |
| 5 | tailcat_failure | Anything else, including calls made in the wrong state |
Branch on the class (for example with tryCatch(..., tailcat_timeout = function(e) ...)) or the status, not on the message, whose wording may change.
Operations in flight
Every operation that can block (each method or function with a timeout, and fields such as $address that ask tailcat), synchronous or asynchronous, holds its own operating system thread from the moment it starts until it ends, including while it waits its turn behind other reads or writes on a connection. The option rtailcat.max_operations caps how many such operations may be in flight in the R process at once. It must be a single positive whole number, or Inf for no cap, and defaults to 1024, which stays well below the per-process thread limits of macOS (about 6,000) and typical Linux systems and the 10,000 threads at which the Go runtime ends the process. The option is read each time an operation starts. An operation started when the cap is reached fails at once with a tailcat_failure error that names the option and gives the limit, without affecting the operations already in flight; once some of them finish or are canceled, new operations work again. If the option is set to an invalid value, every such operation fails with a tailcat_failure error saying so.
Relay options
The relay used by servers and clients can be set once for the R session with options, instead of passing the same arguments to every $new() (including calls inside other packages' code). The option rtailcat.region is used by TailcatServer$new() when region is NULL, and rtailcat.relay_map_url by TailcatServer$new(), TailcatClient$new(), and tailcat_address_resolve() when relay_map_url is NULL. They take the same values as the arguments, are read when the object is created, and are ignored when unset (NULL, the default). An argument given explicitly always wins; to go back to the default relays, unset the option.
For example, a deployment that runs its own relay region can call options(rtailcat.region = region) at startup, and one that publishes its own relay map options(rtailcat.relay_map_url = url).
Active bindings
addressThe secret tailcat address to give to clients, as a tailcat_secret. It is fetched from tailcat on each access (a quick local call) rather than stored in R. Errors if the server has not started.
public_keyThe server's public key. Errors if the server has not started, like
$address; test$is_startedfirst when that is in doubt.is_startedTRUEonce the server has started (registered with the relay),FALSEbefore.is_closedTRUEonce the server has been closed.
Methods
TailcatServer$new()
Create a server. Nothing is sent over the network until the server is started (by $start() or $listen()). Only arguments that are not NULL are passed to tailcat.
Usage
TailcatServer$new(
key = NULL,
preshared_key = NULL,
region_id = NULL,
region = NULL,
relay_map_url = NULL,
allowed_clients = NULL,
udp_idle_timeout = NULL
)Arguments
keyPrivate node key: a string in tailcat's text encoding, a
tailcat_keysobject fromtailcat_keygen(), orNULLto generate one. Atailcat_keysobject also supplies the pre-shared key, unlesspreshared_keyis given. Keep the keys to reuse an address across restarts.preshared_keyPre-shared key (text encoding), or
NULLto generate one (or take it fromkey).region_idInteger relay region to register in, or
NULLto choose automatically.regionA relay region definition, as a list or a JSON string, to use instead of the public relay map, or
NULLto use thertailcat.regionoption (see the section on relay options) or, when that is unset, the public relays. Field names may be the snake_case names used bytailcat_address_parse()(region_id,region_code,nodes, and within each nodename,host_name,ipv4,relay_port, and so on) or the PascalCase names of Tailscale'sDERPRegiontype (RegionID,Nodes,HostName,DERPPort, and so on).relay_map_urlURL of a relay map to use instead of the default, or
NULLto use thertailcat.relay_map_urloption (see the section on relay options in TailcatServer) or, when that is unset, the default.allowed_clientsThe clients that may connect: a character vector of client public keys, a
tailcat_keysobject fromtailcat_keygen()(whose public key is used), or a list of public keys andtailcat_keysobjects. If given, only these clients may connect; otherwise anyone with the address may.udp_idle_timeoutSeconds after which an idle UDP flow is dropped, or
NULLfor the default.
TailcatServer$start()
Register with the relay so the server has an address. Calling it again is harmless.
TailcatServer$listen()
Listen on a port, starting the server first if needed.
Usage
TailcatServer$listen(port = 0L, network = c("tcp", "udp"), timeout = 30)Arguments
portPort number;
0picks a free port (see the listener's$port).networkExactly
"tcp"(the default) or"udp".timeoutSeconds to wait;
Infwaits indefinitely and0polls.
TailcatServer$listen_async()
Asynchronous version of $listen().
Usage
TailcatServer$listen_async(port = 0L, network = c("tcp", "udp"), timeout = 30)Arguments
portPort number;
0picks a free port.networkExactly
"tcp"(the default) or"udp".timeoutSeconds to wait;
Infwaits indefinitely and0polls.
Returns
A promise resolving with a TailcatListener.
TailcatServer$allow_client()
Add a client public key to the allowlist. Once the list has an entry, only listed clients can register; existing connections are not affected.
Arguments
public_keyA client public key, for example
TailcatClient$public_key, or the client'stailcat_keysobject fromtailcat_keygen(), whose public key is used.timeoutSeconds to wait;
Infwaits indefinitely and0polls.
TailcatServer$allow_client_async()
Asynchronous version of $allow_client().
TailcatServer$info()
Information about the started server.
Returns
A list of class tailcat_info (so that format() redacts the address in every version of R) with address (a tailcat_secret), ip (the server's own IP address on the tailcat network, without a port), and public_key. Errors if the server has not started.
TailcatServer$drain()
Wait for TCP shutdown traffic to finish. Call it after closing connections and before closing the server or exiting R, so that peers see a clean end of stream.
TailcatServer$close()
Close the server, its listeners, and all of its connections. Pending operations on them end with a tailcat_closed error. Closing twice is harmless. Closing tears the server down immediately, so clients may not see the end of their streams until their own reads time out. To give clients a clean end of stream, close the connections first and pass drain, which waits for their shutdown traffic before closing (see $drain()).
TailcatServer$format()
Format a summary, the lines that $print() shows. The address and keys are never shown. A closed server shows only its state.