1 | use crate::frame::{self, Error, Head, Kind, Reason, StreamId}; |
2 | |
3 | use bytes::BufMut; |
4 | |
5 | #[derive (Copy, Clone, Debug, Eq, PartialEq)] |
6 | pub struct Reset { |
7 | stream_id: StreamId, |
8 | error_code: Reason, |
9 | } |
10 | |
11 | impl Reset { |
12 | pub fn new(stream_id: StreamId, error: Reason) -> Reset { |
13 | Reset { |
14 | stream_id, |
15 | error_code: error, |
16 | } |
17 | } |
18 | |
19 | pub fn stream_id(&self) -> StreamId { |
20 | self.stream_id |
21 | } |
22 | |
23 | pub fn reason(&self) -> Reason { |
24 | self.error_code |
25 | } |
26 | |
27 | pub fn load(head: Head, payload: &[u8]) -> Result<Reset, Error> { |
28 | if payload.len() != 4 { |
29 | return Err(Error::InvalidPayloadLength); |
30 | } |
31 | |
32 | let error_code = unpack_octets_4!(payload, 0, u32); |
33 | |
34 | Ok(Reset { |
35 | stream_id: head.stream_id(), |
36 | error_code: error_code.into(), |
37 | }) |
38 | } |
39 | |
40 | pub fn encode<B: BufMut>(&self, dst: &mut B) { |
41 | tracing::trace!( |
42 | "encoding RESET; id= {:?} code= {:?}" , |
43 | self.stream_id, |
44 | self.error_code |
45 | ); |
46 | let head = Head::new(Kind::Reset, 0, self.stream_id); |
47 | head.encode(4, dst); |
48 | dst.put_u32(self.error_code.into()); |
49 | } |
50 | } |
51 | |
52 | impl<B> From<Reset> for frame::Frame<B> { |
53 | fn from(src: Reset) -> Self { |
54 | frame::Frame::Reset(src) |
55 | } |
56 | } |
57 | |