pub fn encode_request(
request: RequestToEncode<'_>,
hmac_key: Option<&[u8]>,
) -> Result<Vec<u8>>Expand description
Encodes an outbound request.
This is sender-side semantic construction: the variant determines the flags, so callers never assemble flag bits themselves.
hmac_key is authoritative. Some(key) sets FLAG_HMAC, reserves the
16-byte authentication field after the header, and signs the finished
datagram; None produces an unauthenticated packet.
RequestToEncode::Echo requires the negotiated Params because the
sender must build a datagram of the negotiated length with the payload at
the negotiated offset. The receiving side needs no Params — see
decode_request.
§Errors
Returns ProtoError::PayloadTooLarge when an ECHO payload does not fit
the negotiated length, and ProtoError::PacketLengthUnrepresentable when
the negotiated length is positive and wider than usize. A negative
negotiated length is not an error: see echo_packet_len, which floors it
at the mandatory field block.
§Example
use irtt_proto::{encode_request, Params, RequestToEncode};
// An unauthenticated ECHO probe. `params` supplies the negotiated length,
// which reserves four payload bytes beyond the 16-byte echo header.
let params = Params { length: 20, ..Params::default() };
let packet = encode_request(
RequestToEncode::Echo {
token: 0x1234_5678_9abc_def0,
sequence: 7,
params: ¶ms,
payload: &[1, 2, 3, 4],
},
None,
).unwrap();
assert_eq!(packet.len(), 20);