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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
//! Optional endpoint encoding that retains unchanged terminal cells across projections.
use super::{CellData, PaneSurfaceFrame, ServerMessage};
use serde::{Deserialize, Serialize};
pub(crate) const CAPABILITY: &str = "surface_reuse";
pub(crate) const MESSAGE_KIND: &str = "endpoint.surface-reuse.v1";
#[derive(Serialize, Deserialize)]
struct SurfaceReuse<S> {
base_surface_revision: u64,
surface: S,
}
pub(crate) fn message(
base_surface_revision: u64,
surface: &mut PaneSurfaceFrame,
) -> serde_json::Result<Option<ServerMessage>> {
let cells = std::mem::take(&mut surface.frame.cells);
let data = serde_json::to_string(&SurfaceReuse {
base_surface_revision,
surface: &*surface,
});
surface.frame.cells = cells;
let message = ServerMessage::EndpointControl {
kind: MESSAGE_KIND.into(),
data: data?,
};
// JSON can expand non-cell data (for example escaped hyperlink URLs). A
// failed compact encoding must fall back, not strand a newer snapshot.
let size = bincode::serde::encode_into_std_write(
&message,
&mut std::io::sink(),
bincode::config::standard(),
);
match size {
Ok(size) => Ok((size <= super::MAX_FRAME_SIZE).then_some(message)),
Err(error) => {
tracing::warn!(%error, "failed to size surface reuse");
Ok(None)
}
}
}
#[derive(Default)]
struct CellBaseline {
boot_id: String,
projection_revision: u64,
surface_revision: u64,
width: u16,
height: u16,
cells: Vec<CellData>,
popup: Option<PopupBaseline>,
}
impl CellBaseline {
/// Whether a patch applies to exactly this baseline as its next revision.
fn matches_patch(&self, patch: &super::PaneSurfacePatch) -> bool {
patch.boot_id == self.boot_id
&& patch.projection_revision == self.projection_revision
&& patch.base_surface_revision == self.surface_revision
&& patch.surface_revision == self.surface_revision.saturating_add(1)
}
}
struct PopupBaseline {
terminal_id: String,
width: u16,
height: u16,
cells: Vec<CellData>,
}
fn popup_baseline(surface: &PaneSurfaceFrame) -> Option<PopupBaseline> {
surface.popup.as_ref().map(|popup| PopupBaseline {
terminal_id: popup.terminal_id.clone(),
width: popup.frame.width,
height: popup.frame.height,
cells: popup.frame.cells.clone(),
})
}
/// Connection-local decoding happens before activation and presentation filtering, so
/// switching endpoints cannot discard a baseline needed by the next wire message.
#[derive(Default)]
pub(crate) struct Decoder {
baseline: Option<CellBaseline>,
surface_delta: bool,
surface_scroll: bool,
}
impl Decoder {
pub(crate) fn new(surface_delta: bool, surface_scroll: bool) -> Self {
Self {
baseline: None,
surface_delta,
surface_scroll,
}
}
pub(crate) fn decode(&mut self, message: ServerMessage) -> Result<ServerMessage, String> {
let message = match message {
ServerMessage::EndpointControl { kind, data }
if kind == super::surface_delta::MESSAGE_KIND =>
{
if !self.surface_delta {
return Err("surface delta was not negotiated".into());
}
return self.decode_delta(&data).map(ServerMessage::PaneSurface);
}
ServerMessage::EndpointControl { kind, data }
if kind == super::surface_scroll::MESSAGE_KIND =>
{
if !self.surface_scroll {
return Err("surface scroll was not negotiated".into());
}
return self
.decode_scroll(&data)
.map(ServerMessage::PaneSurfacePatch);
}
ServerMessage::EndpointControl { kind, data } if kind == MESSAGE_KIND => {
let reuse: SurfaceReuse<PaneSurfaceFrame> = serde_json::from_str(&data)
.map_err(|error| format!("invalid surface reuse: {error}"))?;
let Some(base) = &mut self.baseline else {
return Err("surface reuse without a baseline".into());
};
let mut surface = reuse.surface;
if base.boot_id != surface.boot_id
|| base.surface_revision != reuse.base_surface_revision
|| surface.surface_revision != base.surface_revision.saturating_add(1)
|| base.width != surface.frame.width
|| base.height != surface.frame.height
|| !surface.frame.cells.is_empty()
{
return Err("surface reuse does not match its baseline".into());
}
surface.frame.cells.clone_from(&base.cells);
base.projection_revision = surface.projection_revision;
base.surface_revision = surface.surface_revision;
if self.surface_delta {
base.popup = popup_baseline(&surface);
}
return Ok(ServerMessage::PaneSurface(surface));
}
message => message,
};
match &message {
ServerMessage::PaneSurface(surface) => {
let base = self.baseline.get_or_insert_with(CellBaseline::default);
base.boot_id.clone_from(&surface.boot_id);
base.projection_revision = surface.projection_revision;
base.surface_revision = surface.surface_revision;
base.width = surface.frame.width;
base.height = surface.frame.height;
base.cells.clone_from(&surface.frame.cells);
if self.surface_delta {
base.popup = popup_baseline(surface);
}
}
ServerMessage::PaneSurfacePatch(patch) => {
if let Some(base) = &mut self.baseline {
if !base.matches_patch(patch) {
self.baseline = None;
} else {
for row in &patch.rows {
let start =
usize::from(row.y) * usize::from(base.width) + usize::from(row.x);
let end = start.saturating_add(row.cells.len());
if row.y >= base.height
|| usize::from(row.x) + row.cells.len() > usize::from(base.width)
|| end > base.cells.len()
{
return Err("surface patch exceeds the cell baseline".into());
}
base.cells[start..end].clone_from_slice(&row.cells);
}
base.surface_revision = patch.surface_revision;
}
}
}
_ => {}
}
Ok(message)
}
fn decode_scroll(&mut self, data: &str) -> Result<super::PaneSurfacePatch, String> {
let Some(base) = &mut self.baseline else {
return Err("surface scroll without a baseline".into());
};
let scroll = super::surface_scroll::decode(data)?;
if !base.matches_patch(&scroll.patch) {
return Err("surface scroll does not match its baseline".into());
}
let patch = super::surface_scroll::apply(&mut base.cells, base.width, base.height, scroll)?;
base.surface_revision = patch.surface_revision;
Ok(patch)
}
fn decode_delta(&mut self, data: &str) -> Result<PaneSurfaceFrame, String> {
use super::surface_delta::{self, GridUpdate};
let Some(base) = &mut self.baseline else {
return Err("surface delta without a baseline".into());
};
let delta = surface_delta::decode_for(data, (base.width, base.height))?;
let mut surface = delta.surface;
if surface.boot_id != base.boot_id
|| delta.base_projection_revision != base.projection_revision
|| delta.base_surface_revision != base.surface_revision
|| surface.surface_revision != base.surface_revision.saturating_add(1)
|| surface.projection_revision < base.projection_revision
|| base.cells.len() != usize::from(base.width) * usize::from(base.height)
{
return Err("surface delta does not match its baseline".into());
}
surface.frame.cells.clone_from(&base.cells);
surface_delta::apply_rows(&mut surface.frame.cells, base.width, &delta.rows);
match (&mut surface.popup, delta.popup_cells) {
(None, None) => {}
(Some(popup), Some(update)) => {
let count = usize::from(popup.frame.width) * usize::from(popup.frame.height);
match update {
GridUpdate::Replace(cells) => popup.frame.cells = cells,
GridUpdate::Patch(rows) => {
let previous = base
.popup
.as_ref()
.filter(|previous| {
previous.terminal_id == popup.terminal_id
&& previous.width == popup.frame.width
&& previous.height == popup.frame.height
&& previous.cells.len() == count
})
.ok_or("popup delta does not match its baseline")?;
popup.frame.cells.clone_from(&previous.cells);
surface_delta::apply_rows(&mut popup.frame.cells, previous.width, &rows);
}
}
}
_ => return Err("popup delta is missing or unexpected".into()),
}
for frame in
std::iter::once(&surface.frame).chain(surface.popup.as_ref().map(|popup| &popup.frame))
{
if frame.cells.iter().any(|cell| {
cell.hyperlink
.is_some_and(|index| index as usize >= frame.hyperlinks.len())
}) {
return Err("surface delta has an invalid hyperlink index".into());
}
}
// Validate the entire update before advancing either grid or revision.
surface_delta::apply_rows(&mut base.cells, base.width, &delta.rows);
base.popup = popup_baseline(&surface);
base.projection_revision = surface.projection_revision;
base.surface_revision = surface.surface_revision;
Ok(surface)
}
}
|