use serde::{Deserialize, Serialize};
#[repr(C, align(128))]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct PulseFrameHeader {
pub magic: u32,
pub version_128: u128,
pub flags_128: u128,
pub rpki_fingerprint: [u8; 32],
pub zcmk_bid_128: u128,
pub semantic_hash_128: u128,
pub timestamp_ns_128: u128,
pub priority: u8,
pub ttl: u8,
pub reserved: [u8; 10],
pub checksum: u32,
}
impl PulseFrameHeader {
#[inline(always)]
pub fn as_bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self as *const _ as *const u8, 128) }
}
#[inline(always)]
pub fn new(fingerprint: [u8; 32], bid: u128, sem_hash: u128) -> Self {
let now = std::time::Instant::now().elapsed().as_nanos() as u128;
Self {
magic: 0x5254_5450,
version_128: 125,
flags_128: 0b0000_1111, rpki_fingerprint: fingerprint,
zcmk_bid_128: bid,
semantic_hash_128: sem_hash,
timestamp_ns_128: now,
priority: 128,
ttl: 64,
reserved: [0u8; 10],
checksum: 0,
}
}
}
pub fn on_pulse_received(frame: &[u8]) {
if frame.len() < 128 {
handle_malformed_pulse();
return;
}
let header = unsafe { &*(frame.as_ptr() as *const PulseFrameHeader) };
if header.magic != 0x5254_5450 {
return;
}
#[cfg(debug_assertions)]
println!(
"\x1b[1;36m[RTTP-PULSE]\x1b[0m 128-byte v1.2.5 Header verified. Reflex Arc: {}ns",
header.timestamp_ns_128
);
}
#[cold]
#[inline(never)]
fn handle_malformed_pulse() {
eprintln!("\x1b[1;31m[RTTP-ERROR]\x1b[0m Inbound frame size underflow. 128-byte suture broken.");
}