Skip to content

Commit eb80a71

Browse files
authored
Merge pull request #2327 from microsoft/agents/issue-2326-nvxc-header-layout
nvx: name the NVXC frame header layout
2 parents ebd2ed4 + e1b5668 commit eb80a71

1 file changed

Lines changed: 60 additions & 11 deletions

File tree

  • nvx/src/protocol/app

‎nvx/src/protocol/app/v2.rs‎

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
//! execution that ended get no answer; and standard input beyond the window, or after its end,
5454
//! ends the session.
5555
56+
use std::ops::Range;
57+
5658
use crate::protocol::{APP_HEADER_LEN, OUTER_MAX_PAYLOAD, ProtocolError, array, violation};
5759

5860
/// Version of the frame format and of the protocol, which each header and each `READY` carry.
@@ -98,6 +100,50 @@ const BUSY: u8 = 0x87;
98100
/// Kind of [`Frame::Error`].
99101
const ERROR: u8 = 0xff;
100102

103+
/// Positions of the fields of a frame header, which the module describes, in this order and with
104+
/// no padding, and which [`Frame::encode`] and [`Frame::decode`] share.
105+
mod field {
106+
use super::{APP_HEADER_LEN, Range};
107+
108+
/// Bytes of the magic, [`MAGIC`](super::MAGIC).
109+
pub(super) const MAGIC: Range<usize> = 0..4;
110+
/// Byte of the version, [`VERSION`](super::VERSION).
111+
pub(super) const VERSION: usize = 4;
112+
/// Byte of the frame kind.
113+
pub(super) const KIND: usize = 5;
114+
/// Bytes of the flags, both zero in a valid frame.
115+
pub(super) const FLAGS: Range<usize> = 6..8;
116+
/// Bytes of the request ID, a little-endian `u64`.
117+
pub(super) const REQUEST_ID: Range<usize> = 8..16;
118+
/// Bytes of the status, a little-endian `i32`.
119+
pub(super) const STATUS: Range<usize> = 16..20;
120+
/// Bytes of the payload length, a little-endian `u32`, which end the header.
121+
pub(super) const LENGTH: Range<usize> = 20..APP_HEADER_LEN;
122+
}
123+
124+
/// Returns the number of bytes of `range`.
125+
const fn width(range: Range<usize>) -> usize {
126+
range.end - range.start
127+
}
128+
129+
// The fields tile the header in order, and each range holds exactly the bytes of its field's
130+
// type, so that the header's slices and `array`'s copies always fit.
131+
const _: () = {
132+
assert!(field::MAGIC.start == 0);
133+
assert!(field::MAGIC.end == field::VERSION);
134+
assert!(field::VERSION + 1 == field::KIND);
135+
assert!(field::KIND + 1 == field::FLAGS.start);
136+
assert!(field::FLAGS.end == field::REQUEST_ID.start);
137+
assert!(field::REQUEST_ID.end == field::STATUS.start);
138+
assert!(field::STATUS.end == field::LENGTH.start);
139+
assert!(field::LENGTH.end == APP_HEADER_LEN);
140+
assert!(width(field::MAGIC) == size_of::<[u8; 4]>());
141+
assert!(width(field::FLAGS) == size_of::<[u8; 2]>());
142+
assert!(width(field::REQUEST_ID) == size_of::<u64>());
143+
assert!(width(field::STATUS) == size_of::<i32>());
144+
assert!(width(field::LENGTH) == size_of::<u32>());
145+
};
146+
101147
/// A frame of version 2, without its request ID, which [`Frame::encode`] takes and
102148
/// [`Frame::decode`] returns.
103149
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -237,11 +283,14 @@ impl<'a> Frame<'a> {
237283
return Err(violation("an NVXC frame must fit one outer data record"));
238284
}
239285
let mut frame = Vec::with_capacity(APP_HEADER_LEN + length);
240-
frame.extend_from_slice(&MAGIC);
241-
frame.extend_from_slice(&[VERSION, kind, 0, 0]);
242-
frame.extend_from_slice(&request_id.to_le_bytes());
243-
frame.extend_from_slice(&status.to_le_bytes());
244-
frame.extend_from_slice(&(length as u32).to_le_bytes());
286+
frame.resize(APP_HEADER_LEN, 0);
287+
frame[field::MAGIC].copy_from_slice(&MAGIC);
288+
frame[field::VERSION] = VERSION;
289+
frame[field::KIND] = kind;
290+
// The flags bytes, frame[field::FLAGS], stay zero.
291+
frame[field::REQUEST_ID].copy_from_slice(&request_id.to_le_bytes());
292+
frame[field::STATUS].copy_from_slice(&status.to_le_bytes());
293+
frame[field::LENGTH].copy_from_slice(&(length as u32).to_le_bytes());
245294
frame.extend_from_slice(head);
246295
frame.extend_from_slice(data);
247296
Ok(frame)
@@ -259,19 +308,19 @@ impl<'a> Frame<'a> {
259308
.split_at_checked(APP_HEADER_LEN)
260309
.filter(|_| frame.len() <= OUTER_MAX_PAYLOAD)
261310
.ok_or_else(malformed)?;
262-
if header[..4] != MAGIC || header[4] != VERSION {
311+
if header[field::MAGIC] != MAGIC || header[field::VERSION] != VERSION {
263312
return Err(violation(
264313
"the control peer sent a frame of another protocol than NVXC version 2",
265314
));
266315
}
267-
let request_id = u64::from_le_bytes(array(&header[8..16]));
268-
let status = i32::from_le_bytes(array(&header[16..20]));
269-
let length = u32::from_le_bytes(array(&header[20..24]));
270-
if header[6..8] != [0, 0] || length as usize != payload.len() {
316+
let request_id = u64::from_le_bytes(array(&header[field::REQUEST_ID]));
317+
let status = i32::from_le_bytes(array(&header[field::STATUS]));
318+
let length = u32::from_le_bytes(array(&header[field::LENGTH]));
319+
if header[field::FLAGS] != [0, 0] || length as usize != payload.len() {
271320
return Err(malformed());
272321
}
273322
let empty = |variant| payload.is_empty().then_some(variant);
274-
let decoded = match header[5] {
323+
let decoded = match header[field::KIND] {
275324
PING => empty(Self::Ping),
276325
EXEC => Some(Self::Exec(payload)),
277326
STOP => empty(Self::Stop),

0 commit comments

Comments
 (0)