functions for manipulating packets

This commit is contained in:
Vinzenz Schroeter 2025-04-12 12:05:19 +02:00
commit d215f7199e
9 changed files with 80 additions and 36 deletions

View file

@ -5,8 +5,9 @@
//!
//! The raw packet
use std::ptr::NonNull;
use crate::SPByteSlice;
use servicepoint::{Header, Packet, TypedCommand};
use std::ptr::NonNull;
/// Turns a [SPCommand] into a [SPPacket].
/// The [SPCommand] gets consumed.
@ -107,19 +108,40 @@ pub unsafe extern "C" fn sp_packet_from_parts(
Vec::from(payload)
};
let packet = Box::new(Packet {
header,
payload,
});
let packet = Box::new(Packet { header, payload });
NonNull::from(Box::leak(packet))
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_get_header(
packet: *const Packet,
) -> Header {
pub unsafe extern "C" fn sp_packet_get_header(packet: *mut Packet) -> *mut Header {
assert!(!packet.is_null());
unsafe { (*packet).header }
&mut unsafe { (*packet).header }
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_get_payload(packet: *mut Packet) -> SPByteSlice {
assert!(!packet.is_null());
unsafe { SPByteSlice::from_slice(&mut *(*packet).payload) }
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_set_payload(packet: *mut Packet, data: SPByteSlice) {
assert!(!packet.is_null());
unsafe {
(*packet).payload = data.as_slice().to_vec()
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_write_to(
packet: *const Packet,
mut buffer: SPByteSlice,
) {
assert!(!packet.is_null());
unsafe {
(*packet).serialize_to(buffer.as_slice_mut());
}
}
/// Clones a [SPPacket].