fidl_next/protocol/
buffer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::protocol::{Transport, WireMessageHeader, FLAG_0_WIRE_FORMAT_V2_BIT, MAGIC_NUMBER};
use crate::{u32_le, u64_le, DecodeError, DecoderExt as _, EncodeError, EncoderExt as _};

/// Encodes a message into the given buffer.
pub fn encode_header<T: Transport>(
    buffer: &mut T::SendBuffer,
    txid: u32,
    ordinal: u64,
) -> Result<(), EncodeError> {
    let mut encoder = T::encoder(buffer);
    encoder.encode_next(&mut WireMessageHeader {
        txid: u32_le::from_native(txid),
        flags: [FLAG_0_WIRE_FORMAT_V2_BIT, 0, 0],
        magic_number: MAGIC_NUMBER,
        ordinal: u64_le::from_native(ordinal),
    })
}

/// Parses the transaction ID and ordinal from the given buffer.
pub fn decode_header<T: Transport>(buffer: &mut T::RecvBuffer) -> Result<(u32, u64), DecodeError> {
    let (txid, ordinal) = {
        let mut decoder = T::decoder(buffer);
        let header = decoder.decode_next::<WireMessageHeader>()?;
        (header.txid.to_native(), header.ordinal.to_native())
    };

    Ok((txid, ordinal))
}