pub struct Socket { /* private fields */ }Expand description
A connection to a Unix socket, from cx.socket(path). It connects at
once, passes each line it reads to on_line, and
writes what write is given. A clone is cheap and
shares the connection.
It closes when the context that opened it goes. When the other end
closes it, connected turns false and it stays
closed until reconnect: nothing retries on its
own, so a socket that isn’t there never wakes the shell.
Implementations§
Source§impl Socket
impl Socket
Sourcepub fn connected(&self) -> ReadSignal<bool>
pub fn connected(&self) -> ReadSignal<bool>
Whether it is connected: true from a connect that worked until the
other end closes it, it breaks, or close.
Sourcepub fn on_line(self, on_line: impl Fn(&str) + 'static) -> Socket
pub fn on_line(self, on_line: impl Fn(&str) + 'static) -> Socket
Calls on_line with each line read, without its \n, decoded as
UTF-8 with bad bytes replaced. What is left when the other end closes
counts as a last line. A line over 1 MiB is dropped and logged.
It runs on the main thread. If it panics, that is logged with the
socket’s path and where on_line was called, and it isn’t called
again; the socket stays open.
Sourcepub fn write(&self, data: impl AsRef<[u8]>)
pub fn write(&self, data: impl AsRef<[u8]>)
Sends data, all of it, as the other end takes it. While not
connected it is dropped, and that is logged. If more than 4 MiB wait
because the other end reads nothing, the socket is closed.