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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
use super::*;
pub(super) fn start_endpoint_transport(
stream: LocalStream,
lifetime: impl Send + 'static,
event_tx: &tokio::sync::mpsc::Sender<ClientLoopEvent>,
endpoint_id: endpoint::ClientEndpointId,
generation: u64,
max_frame_size: usize,
) -> Result<endpoint::NativeEndpointTransport, ClientError> {
let reader = stream.try_clone().map_err(ClientError::ConnectionFailed)?;
let transport = endpoint::NativeEndpointTransport::with_lifetime(stream, lifetime)
.map_err(ClientError::ConnectionFailed)?;
let stopped = transport.stop_handle();
let event_tx = event_tx.clone();
std::thread::Builder::new()
.name("endpoint-reader".into())
.spawn(move || {
server_reader_thread(
reader,
event_tx,
&stopped,
max_frame_size,
endpoint_id,
generation,
);
})
.map_err(ClientError::ConnectionFailed)?;
Ok(transport)
}
/// Reads complete frames while retaining partial-read progress across nonblocking polls.
pub(super) fn server_reader_thread(
mut stream: LocalStream,
event_tx: tokio::sync::mpsc::Sender<ClientLoopEvent>,
should_quit: &Arc<AtomicBool>,
max_frame_size: usize,
endpoint_id: endpoint::ClientEndpointId,
generation: u64,
) {
if stream.set_nonblocking(true).is_err() {
let _ = event_tx.blocking_send(ClientLoopEvent::ServerDisconnected {
endpoint_id,
generation,
});
return;
}
let mut stream = EndpointReader {
stream: &mut stream,
stopped: should_quit,
};
loop {
if should_quit.load(Ordering::Acquire) {
break;
}
match protocol::read_message(&mut stream, max_frame_size) {
Ok(msg) => {
if event_tx
.blocking_send(ClientLoopEvent::ServerMessage {
endpoint_id: endpoint_id.clone(),
generation,
message: Box::new(msg),
})
.is_err()
{
break;
}
}
Err(protocol::FramingError::UnexpectedEof) => {
let _ = event_tx.blocking_send(ClientLoopEvent::ServerDisconnected {
endpoint_id: endpoint_id.clone(),
generation,
});
break;
}
Err(protocol::FramingError::Io(err)) if err.kind() == io::ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(1));
}
Err(err) => {
warn!(err = %err, "server read error");
let _ = event_tx.blocking_send(ClientLoopEvent::ServerDisconnected {
endpoint_id: endpoint_id.clone(),
generation,
});
break;
}
}
}
}
struct EndpointReader<'a> {
stream: &'a mut LocalStream,
stopped: &'a AtomicBool,
}
impl io::Read for EndpointReader<'_> {
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
loop {
if self.stopped.load(Ordering::Acquire) {
return Ok(0);
}
match crate::ipc::poll_local_stream_read_count(self.stream, buffer)? {
crate::ipc::LocalStreamReadCount::Data(count) => return Ok(count),
crate::ipc::LocalStreamReadCount::Closed => return Ok(0),
crate::ipc::LocalStreamReadCount::Pending => {
crate::platform::wait_client_stream_readable(self.stream)?;
}
}
}
}
}
pub(in crate::client) fn write_to_local_server(
stream: &mut LocalStream,
msg: &ClientMessage,
) -> io::Result<()> {
protocol::write_message(stream, msg).map_err(|error| io::Error::other(error.to_string()))
}
pub(super) trait ClientMessageSink {
fn send_client_message(&mut self, message: &ClientMessage) -> io::Result<()>;
}
impl ClientMessageSink for LocalStream {
fn send_client_message(&mut self, message: &ClientMessage) -> io::Result<()> {
write_to_local_server(self, message)
}
}
impl ClientMessageSink for endpoint::EndpointRegistry {
fn send_client_message(&mut self, message: &ClientMessage) -> io::Result<()> {
// The lifecycle loop consumes failures for every endpoint, including Local. A send
// failure must not bypass that transition or tear down unrelated connections.
self.send(message);
Ok(())
}
}
pub(super) fn write_to_server(
stream: &mut impl ClientMessageSink,
msg: &ClientMessage,
) -> io::Result<()> {
stream.send_client_message(msg)
}
|