remux/
frame.rs

1// Copyright (c) 2018-2019 Parity Technologies (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 or MIT license, at your option.
4//
5// A copy of the Apache License, Version 2.0 is included in the software as
6// LICENSE-APACHE and a copy of the MIT license is included in the software
7// as LICENSE-MIT. You may also obtain a copy of the Apache License, Version 2.0
8// at https://www.apache.org/licenses/LICENSE-2.0 and a copy of the MIT license
9// at https://opensource.org/licenses/MIT.
10
11pub mod header;
12mod io;
13
14use futures::future::Either;
15use header::{Header, StreamId, Data, WindowUpdate, GoAway, Ping};
16use std::{convert::TryInto, num::TryFromIntError};
17
18pub(crate) use io::Io;
19pub use io::FrameDecodeError;
20
21/// A Remux message frame consisting of header and body.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct Frame<T> {
24    header: Header<T>,
25    body: Vec<u8>
26}
27
28impl<T> Frame<T> {
29    pub fn new(header: Header<T>) -> Self {
30        Frame { header, body: Vec::new() }
31    }
32
33    pub fn header(&self) -> &Header<T> {
34        &self.header
35    }
36
37    pub fn header_mut(&mut self) -> &mut Header<T> {
38        &mut self.header
39    }
40
41    /// Introduce this frame to the right of a binary frame type.
42    pub(crate) fn right<U>(self) -> Frame<Either<U, T>> {
43        Frame { header: self.header.right(), body: self.body }
44    }
45
46    /// Introduce this frame to the left of a binary frame type.
47    pub(crate) fn left<U>(self) -> Frame<Either<T, U>> {
48        Frame { header: self.header.left(), body: self.body }
49    }
50}
51
52impl Frame<()> {
53    pub(crate) fn into_data(self) -> Frame<Data> {
54        Frame { header: self.header.into_data(), body: self.body }
55    }
56
57    pub(crate) fn into_window_update(self) -> Frame<WindowUpdate> {
58        Frame { header: self.header.into_window_update(), body: self.body }
59    }
60
61    pub(crate) fn into_ping(self) -> Frame<Ping> {
62        Frame { header: self.header.into_ping(), body: self.body }
63    }
64}
65
66impl Frame<Data> {
67    pub fn data(id: StreamId, b: Vec<u8>) -> Result<Self, TryFromIntError> {
68        Ok(Frame {
69            header: Header::data(id, b.len().try_into()?),
70            body: b
71        })
72    }
73
74    pub fn body(&self) -> &[u8] {
75        &self.body
76    }
77
78    pub fn body_len(&self) -> u32 {
79        // Safe cast since we construct `Frame::<Data>`s only with
80        // `Vec<u8>` of length [0, u32::MAX] in `Frame::data` above.
81        self.body().len() as u32
82    }
83
84    pub fn into_body(self) -> Vec<u8> {
85        self.body
86    }
87}
88
89impl Frame<WindowUpdate> {
90    pub fn window_update(id: StreamId, credit: u32) -> Self {
91        Frame {
92            header: Header::window_update(id, credit),
93            body: Vec::new()
94        }
95    }
96}
97
98impl Frame<GoAway> {
99    pub fn term() -> Self {
100        Frame {
101            header: Header::term(),
102            body: Vec::new()
103        }
104    }
105
106    pub fn protocol_error() -> Self {
107        Frame {
108            header: Header::protocol_error(),
109            body: Vec::new()
110        }
111    }
112
113    pub fn internal_error() -> Self {
114        Frame {
115            header: Header::internal_error(),
116            body: Vec::new()
117        }
118    }
119}
120