pub struct SocketServer { /* private fields */ }Expand description
A server on a Unix socket, from cx.socket_server(path), as
Quickshell’s SocketServer. It listens from the start and hands each
client that connects to on_connection as a
Socket. A clone is cheap and shares the server.
It stops when the context that made it goes, or at
close: every connection closes, and the socket file
goes if it is still the one the server made. A socket file already at
the path is replaced if nothing listens on it. One that something
listens on, or anything that isn’t a socket, is left alone and logged,
and the server never listens. The file gets the permissions the
shell’s umask leaves, so keep it in a directory only the user can
reach, such as $XDG_RUNTIME_DIR.
Implementations§
Source§impl SocketServer
impl SocketServer
Sourcepub fn on_connection(
self,
on_connection: impl Fn(&Cx, Socket) + 'static,
) -> SocketServer
pub fn on_connection( self, on_connection: impl Fn(&Cx, Socket) + 'static, ) -> SocketServer
Calls on_connection with each client that connects: a context of
the connection’s own, which goes on the loop’s turn after the
connection ends, and the connection as a Socket, connected, for
on_line or on_split, write and close. Lines are capped as a
Socket’s are. The socket’s connected signal goes with that
context too, so code outside it reads it with try_get. A client
that connects while nothing handles it is closed at once.
A client that only stops sending, as printf cmd | socat - UNIX-CONNECT:path does, has its last line passed on, and the
connection stays open for the reply until the config closes it, a
write fails or 5 s pass, as the shell’s own IPC socket waits. One
that hangs up while still sending ends the connection at once; one
that hangs up after it stopped sending, as socat does once its wait
for a reply ends, keeps its place until a write fails or the 5 s
pass, so a config that answers should close the connection.
It runs on the main thread. If it panics, that is logged with the
server’s path and where on_connection was called, that connection
is closed, and it isn’t called again; the server and its other
connections go on.