naia_shared/connection/
standard_header.rs

1use naia_serde::SerdeInternal;
2
3use crate::{connection::packet_type::PacketType, types::PacketIndex};
4
5// This header provides reliability information.
6#[derive(Copy, Debug, PartialEq, Clone, SerdeInternal)]
7pub struct StandardHeader {
8    pub packet_type: PacketType,
9    /// Packet index identifying this packet
10    pub sender_packet_index: PacketIndex,
11    /// This is the last acknowledged sequence number.
12    pub sender_ack_index: PacketIndex,
13    /// This is an bitfield of all last 32 acknowledged packages
14    pub sender_ack_bitfield: u32,
15}
16
17impl StandardHeader {
18    /// When we compose packet headers, the local sequence becomes the sequence
19    /// number of the packet, and the remote sequence becomes the ack.
20    /// The ack bitfield is calculated by looking into a queue of up to 33
21    /// packets, containing sequence numbers in the range `[remote sequence - 32,
22    /// remote sequence]`. We set bit _n_ (in `[1,32]`) in ack bits to 1 if the
23    /// sequence number remote sequence - _n_ is in the received queue.
24    pub fn new(
25        packet_type: PacketType,
26        sender_packet_index: PacketIndex,
27        sender_ack_index: PacketIndex,
28        sender_ack_bitfield: u32,
29    ) -> StandardHeader {
30        StandardHeader {
31            packet_type,
32            sender_packet_index,
33            sender_ack_index,
34            sender_ack_bitfield,
35        }
36    }
37}