1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! The inner croft's privileged channel to its host: who is typing, granting and revoking
//! write control, and disconnecting clients.
use super::*;
impl InnerChannel {
/// Connect using the CROFT_SESSION_SOCKET / CROFT_SESSION_TOKEN pair the
/// host put in the inner croft's environment; None when not running
/// under a session host.
pub fn from_env() -> Option<Self> {
// Hermetic under test: the host's exported socket/token pair
// reaches a `cargo test` run from a croft-hosted shell, and
// connecting here made every test-built App authenticate to the
// developer's live session host as a construction side effect
// (#60). Same treatment as the collab env read (#55); tests that
// want a channel dial a socket they own via [`Self::connect`].
if cfg!(test) {
return None;
}
let socket = PathBuf::from(std::env::var_os("CROFT_SESSION_SOCKET")?);
let token = std::env::var("CROFT_SESSION_TOKEN").ok()?;
Self::connect(&socket, &token)
}
pub fn connect(socket: &Path, token: &str) -> Option<Self> {
let mut stream = UnixStream::connect(socket).ok()?;
stream
.write_all(&encode_control_frame(&Control::Inner {
token: token.to_string(),
}))
.ok()?;
// Non-blocking so the app's per-tick drain_typing never stalls the
// render loop. Outbound frames are a few dozen bytes into an
// otherwise idle socket, so writes don't meaningfully block either.
stream.set_nonblocking(true).ok()?;
Some(Self {
stream,
reader: FrameReader::new(),
dead: false,
presence: presence_path(socket),
stale_marker: stale_marker_path(socket),
host_version: host_version_path(socket),
})
}
/// Drain any host frames waiting on the channel and return the "who is
/// typing" attributions in arrival order, collapsing only consecutive
/// repeats. Returning every distinct typist (not just the last) keeps a
/// rapid A->B->C writer burst within one tick from dropping B's caret
/// hand-over. Non-blocking.
pub fn drain_typing(&mut self) -> Vec<u64> {
let mut typists: Vec<u64> = Vec::new();
let mut buf = [0u8; 4096];
loop {
match self.stream.read(&mut buf) {
// EOF on a non-blocking socket means the peer closed: the
// host died or swapped to a successor image (#238).
Ok(0) => {
self.dead = true;
break;
}
Ok(n) => {
for frame in self.reader.push(&buf[..n]) {
if let Frame::Control(Control::Typing { id }) = frame
&& typists.last() != Some(&id)
{
typists.push(id);
}
}
}
// WouldBlock is the normal "nothing waiting" case; anything
// else is the connection failing under us.
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
Err(_) => {
self.dead = true;
break;
}
}
}
typists
}
/// True once the host side of this channel is gone; the connection never
/// recovers by itself, so the owner should connect a fresh channel.
pub fn is_dead(&self) -> bool {
self.dead
}
pub fn set_control(&mut self, id: u64, grant: bool) -> bool {
let control = if grant {
Control::Grant { id }
} else {
Control::Revoke { id }
};
let ok = write_frame_blocking(&mut self.stream, &encode_control_frame(&control));
self.dead |= !ok;
ok
}
pub fn kick(&mut self, id: u64) -> bool {
let ok = write_frame_blocking(
&mut self.stream,
&encode_control_frame(&Control::Kick { id }),
);
self.dead |= !ok;
ok
}
}