Skip to content

How to handle updating packet size field automatically #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JohnMoon-Voyager opened this issue Mar 3, 2025 · 3 comments
Open

How to handle updating packet size field automatically #532

JohnMoon-Voyager opened this issue Mar 3, 2025 · 3 comments

Comments

@JohnMoon-Voyager
Copy link

Hi, I'm working on a crate that uses Deku for a format like this:

struct Packet {
    header: PacketHeader,
    #[deku(ctx = "header")]
    payload: Payload,
}

impl Packet {
    fn update_packet_size(&mut self) {
        // walk through the packet and determine how large
        // it is, then set `self.packet.packet_size`.
    }
}

struct PacketHeader {
    packet_type: u16,
    packet_size: u16,
}

#[deku(ctx = "packet_header: &PacketHeader", id = "packet_header.packet_type()")]
enum Payload {
    #[deku(id = "PacketType::Type1")]
    Type1(Type1),
    #[deku(id = "PacketType::Type2")]
    Type2(#[deku(ctx = "packet_header")] Type2),
    ...
}

struct Type2 {
    #[deku(count = "packet_header.packet_size")]
    data: Vec<u8>,
}

impl Type2 {
    fn set_data(&mut self, bytes: &u8]) {
        // set `self.data`
    }
}

In English, there's a packet header that identifies the type of packet and the packet size. For packet types that require knowledge of the size during parsing, I can pass in the header like I do for Type2.

But I'm trying to sort out how to handle setting the packet size when I make changes to the structure. Say I generate a new packet, then go add payload data to the Type2 payload via set_data().

I could have the user run update_packet_size() after making any modifications to the data, but as I'm providing a public API of getters and setters for any fields, it seems like I should be able to abstract that away by running the update in my setters.

Something like:

impl Type2 {
    fn set_data(&mut self, bytes: &u8]) {
        // set `self.data`
        parent.update_packet_size();
    }
}

This ends up being tricky as set_data() requires a &mut self reference, so I can't just pass in another mutable reference to the parent Packet.

Another approach I thought about was trying to inject some logic into the DekuWrite implementation such that it would run update_packet_size() just before serialization (kind of the opposite of how it's using packet_size to determine how much to read during de-serialization).

Is there some obvious way to do what I'm trying to do and abstract the update_packet_size() (or update()) calls from my crate's users?

Thanks so much for all the work on Deku - it's really a pleasure to use!

@wcampbell0x2a
Copy link
Collaborator

Sorry I don't have time to fully look into your issue.

I however ran into maybe this same problem, that I started to fix(?) with #442, but stopped development because of time and testing complexity.

My use case was so small, I just made by own trait in my library: https://github.com/wcampbell0x2a/librarium/blob/master/librarium/src/lib.rs#L88

@JohnMoon-Voyager
Copy link
Author

Thanks for the pointers! The trait you implemented looks like it would more or less solve my issue. I'll have to take some time to look into it.

For posterity, we've since released our crate, so you can look at the actual implementation here: https://github.com/voyager-tech-inc/vita49-rs/blob/main/vita49/src/vrt.rs#L21

@wcampbell0x2a
Copy link
Collaborator

Oh sweet! I always enjoy learning new protocols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants