barectf_parser/types/
packet.rs

1use crate::types::{Event, EventCount, FieldValue, SequenceNumber, StreamId, Timestamp};
2use internment::Intern;
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
7pub struct Packet {
8    pub header: PacketHeader,
9    pub context: PacketContext,
10    pub events: Vec<Event>,
11}
12
13#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
14pub struct PacketHeader {
15    /// Magic number ([`PacketHeader::MAGIC`]) specifies that this is a CTF packet.
16    pub magic_number: Option<u32>,
17    /// Trace UUID, used to ensure the event packet match the metadata used.
18    pub trace_uuid: Option<Uuid>,
19    /// Stream ID, used as reference to stream description in metadata.
20    pub stream_id: StreamId,
21    /// Stream name
22    pub stream_name: Intern<String>,
23    /// Name of this stream's default clock
24    pub clock_name: Option<Intern<String>>,
25}
26
27impl PacketHeader {
28    pub const MAGIC: u32 = 0xC1FC_1FC1;
29}
30
31#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
32pub struct PacketContext {
33    /// Event packet size (in bits, includes padding).
34    pub packet_size_bits: usize,
35    /// Event packet content size (in bits).
36    pub content_size_bits: usize,
37    /// Time-stamp at the beginning of the event packet.
38    pub beginning_timestamp: Option<Timestamp>,
39    /// Time-stamp at the end of the event packet.
40    pub end_timestamp: Option<Timestamp>,
41    /// Snapshot of a per-stream free-running counter,
42    /// counting the number of events discarded that were supposed to be
43    /// written in the stream after the last event in the event packet.
44    pub events_discarded: Option<EventCount>,
45    /// Per-stream event packet sequence count.
46    pub sequence_number: Option<SequenceNumber>,
47    /// Extra, user-defined members to be appended to this data stream type’s packet context structure field type.
48    pub extra_members: Vec<(Intern<String>, FieldValue)>,
49}
50
51impl PacketContext {
52    /// Event packet size (in bytes).
53    pub fn packet_size(&self) -> usize {
54        self.packet_size_bits >> 3
55    }
56
57    /// Event packet content size (in bytes).
58    pub fn content_size(&self) -> usize {
59        self.content_size_bits >> 3
60    }
61}