MCPcopy Create free account
hub / github.com/BohemiaInteractive/CWR / frame_request

Function frame_request

mserver/Client/src/framing.rs:36–48  ·  view source on GitHub ↗

Wrap a magic-packet body in a broadcast `MsgHeader` (flags `MAGIC | TO_BCAST`), filling the length and CRC. `serial` is echoed back by the server; any value works for a probe.

(body: &[u8], serial: u32)

Source from the content-addressed store, hash-verified

34/// Wrap a magic-packet body in a broadcast `MsgHeader` (flags `MAGIC | TO_BCAST`), filling
35/// the length and CRC. `serial` is echoed back by the server; any value works for a probe.
36pub fn frame_request(body: &[u8], serial: u32) -> Vec<u8> {
37 let total = MSG_HEADER_LEN + body.len();
38 let mut msg = vec![0u8; total];
39 msg[0..2].copy_from_slice(&u16::try_from(total).unwrap_or(u16::MAX).to_le_bytes());
40 msg[2..4].copy_from_slice(&(MSG_MAGIC_FLAG | MSG_TO_BCAST_FLAG).to_le_bytes());
41 // crc (4..8) stays zero for the computation
42 msg[8..12].copy_from_slice(&serial.to_le_bytes());
43 // ackOrigin (12..16) and ack-union (16..24) stay zero for a broadcast packet
44 msg[MSG_HEADER_LEN..].copy_from_slice(body);
45 let crc = crc32(&msg);
46 msg[OFF_CRC..OFF_CRC + 4].copy_from_slice(&crc.to_le_bytes());
47 msg
48}
49
50/// Validate an incoming magic datagram and return its body. Checks the magic flag, the
51/// self-described length, and the CRC; returns `None` on any mismatch.

Calls 1

crc32Function · 0.70