blob_stream/
protocol.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use flood_rs::{ReadOctetStream, WriteOctetStream};
6use std::io;
7
8#[derive(Debug, Clone, Eq, PartialEq)]
9pub struct SetChunkData {
10    pub chunk_index: u32,
11    pub payload: Vec<u8>,
12}
13
14impl SetChunkData {
15    /// # Errors
16    ///
17    /// This function will return an `io::Error` if there is an issue with writing to the stream.
18    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
19    #[allow(clippy::cast_possible_truncation)]
20    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
21        stream.write_u32(self.chunk_index)?;
22        stream.write_u16(self.payload.len() as u16)?;
23        stream.write(&self.payload[..])?;
24        Ok(())
25    }
26
27    /// # Errors
28    ///
29    /// This function will return an `io::Error` if there is an issue with writing to the stream.
30    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
31    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
32        let chunk_index = stream.read_u32()?;
33        let octet_length = stream.read_u16()?;
34        let mut payload = vec![0u8; octet_length as usize];
35        stream.read(&mut payload)?;
36
37        Ok(Self {
38            chunk_index,
39            payload,
40        })
41    }
42}
43
44#[derive(Debug, Clone, Copy, Eq, PartialEq)]
45pub struct TransferId(pub u16);
46
47impl TransferId {
48    /// # Errors
49    ///
50    /// This function will return an `io::Error` if there is an issue with writing to the stream.
51    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
52
53    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
54        stream.write_u16(self.0)
55    }
56
57    /// # Errors
58    ///
59    /// This function will return an `io::Error` if there is an issue with writing to the stream.
60    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
61    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
62        Ok(Self(stream.read_u16()?))
63    }
64}
65
66// ---------- Receiver
67
68#[derive(Debug, Copy, Clone, Eq, PartialEq)]
69pub struct AckChunkData {
70    pub waiting_for_chunk_index: u32, // first chunk index that remote has not received fully in sequence. (first gap in chunks from the start).
71    pub receive_mask_after_last: u64, // receive bit mask for chunks after the `waiting_for_chunk_index`
72}
73
74impl AckChunkData {
75    /// # Errors
76    ///
77    /// This function will return an `io::Error` if there is an issue with writing to the stream.
78    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
79    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
80        stream.write_u32(self.waiting_for_chunk_index)?;
81        stream.write_u64(self.receive_mask_after_last)?;
82        Ok(())
83    }
84
85    /// # Errors
86    ///
87    /// This function will return an `io::Error` if there is an issue with writing to the stream.
88    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
89    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
90        Ok(Self {
91            waiting_for_chunk_index: stream.read_u32()?,
92            receive_mask_after_last: stream.read_u64()?,
93        })
94    }
95}
96
97#[derive(Debug, Clone, Eq, PartialEq)]
98pub struct StartTransferData {
99    pub transfer_id: u16, // Unique transfer_id for this session
100    pub total_octet_size: u32,
101    pub chunk_size: u16,
102}
103
104impl StartTransferData {
105    /// # Errors
106    ///
107    /// This function will return an `io::Error` if there is an issue with writing to the stream.
108    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
109    #[allow(clippy::cast_possible_truncation)]
110    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
111        stream.write_u16(self.transfer_id)?;
112        stream.write_u32(self.total_octet_size)?;
113        stream.write_u16(self.chunk_size)
114    }
115
116    /// # Errors
117    ///
118    /// This function will return an `io::Error` if there is an issue with writing to the stream.
119    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
120    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
121        let transfer_id = stream.read_u16()?;
122        let total_octet_size = stream.read_u32()?;
123        let chunk_size = stream.read_u16()?;
124
125        Ok(Self {
126            transfer_id,
127            total_octet_size,
128            chunk_size,
129        })
130    }
131}