Skip to main content

naia_shared/connection/
standard_header.rs

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