/src/h2/src/frame/go_away.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::fmt; |
2 | | |
3 | | use bytes::{BufMut, Bytes}; |
4 | | |
5 | | use crate::frame::{self, Error, Head, Kind, Reason, StreamId}; |
6 | | |
7 | | #[derive(Clone, Eq, PartialEq)] |
8 | | pub struct GoAway { |
9 | | last_stream_id: StreamId, |
10 | | error_code: Reason, |
11 | | debug_data: Bytes, |
12 | | } |
13 | | |
14 | | impl GoAway { |
15 | 32 | pub fn new(last_stream_id: StreamId, reason: Reason) -> Self { |
16 | 32 | GoAway { |
17 | 32 | last_stream_id, |
18 | 32 | error_code: reason, |
19 | 32 | debug_data: Bytes::new(), |
20 | 32 | } |
21 | 32 | } |
22 | | |
23 | 6.10k | pub fn with_debug_data(last_stream_id: StreamId, reason: Reason, debug_data: Bytes) -> Self { |
24 | 6.10k | Self { |
25 | 6.10k | last_stream_id, |
26 | 6.10k | error_code: reason, |
27 | 6.10k | debug_data, |
28 | 6.10k | } |
29 | 6.10k | } |
30 | | |
31 | 14.6k | pub fn last_stream_id(&self) -> StreamId { |
32 | 14.6k | self.last_stream_id |
33 | 14.6k | } |
34 | | |
35 | 20.7k | pub fn reason(&self) -> Reason { |
36 | 20.7k | self.error_code |
37 | 20.7k | } |
38 | | |
39 | 8.63k | pub fn debug_data(&self) -> &Bytes { |
40 | 8.63k | &self.debug_data |
41 | 8.63k | } |
42 | | |
43 | 8.53k | pub fn load(payload: &[u8]) -> Result<GoAway, Error> { |
44 | 8.53k | if payload.len() < 8 { |
45 | 4 | return Err(Error::BadFrameSize); |
46 | 8.53k | } |
47 | 8.53k | |
48 | 8.53k | let (last_stream_id, _) = StreamId::parse(&payload[..4]); |
49 | 8.53k | let error_code = unpack_octets_4!(payload, 4, u32); |
50 | 8.53k | let debug_data = Bytes::copy_from_slice(&payload[8..]); |
51 | 8.53k | |
52 | 8.53k | Ok(GoAway { |
53 | 8.53k | last_stream_id, |
54 | 8.53k | error_code: error_code.into(), |
55 | 8.53k | debug_data, |
56 | 8.53k | }) |
57 | 8.53k | } |
58 | | |
59 | 5.92k | pub fn encode<B: BufMut>(&self, dst: &mut B) { |
60 | 5.92k | tracing::trace!("encoding GO_AWAY; code={:?}", self.error_code); |
61 | 5.92k | let head = Head::new(Kind::GoAway, 0, StreamId::zero()); |
62 | 5.92k | head.encode(8 + self.debug_data.len(), dst); |
63 | 5.92k | dst.put_u32(self.last_stream_id.into()); |
64 | 5.92k | dst.put_u32(self.error_code.into()); |
65 | 5.92k | dst.put(self.debug_data.slice(..)); |
66 | 5.92k | } Unexecuted instantiation: <h2::frame::go_away::GoAway>::encode::<_> Unexecuted instantiation: <h2::frame::go_away::GoAway>::encode::<bytes::bytes_mut::BytesMut> <h2::frame::go_away::GoAway>::encode::<bytes::bytes_mut::BytesMut> Line | Count | Source | 59 | 5.92k | pub fn encode<B: BufMut>(&self, dst: &mut B) { | 60 | 5.92k | tracing::trace!("encoding GO_AWAY; code={:?}", self.error_code); | 61 | 5.92k | let head = Head::new(Kind::GoAway, 0, StreamId::zero()); | 62 | 5.92k | head.encode(8 + self.debug_data.len(), dst); | 63 | 5.92k | dst.put_u32(self.last_stream_id.into()); | 64 | 5.92k | dst.put_u32(self.error_code.into()); | 65 | 5.92k | dst.put(self.debug_data.slice(..)); | 66 | 5.92k | } |
|
67 | | } |
68 | | |
69 | | impl<B> From<GoAway> for frame::Frame<B> { |
70 | 14.4k | fn from(src: GoAway) -> Self { |
71 | 14.4k | frame::Frame::GoAway(src) |
72 | 14.4k | } <h2::frame::Frame as core::convert::From<h2::frame::go_away::GoAway>>::from Line | Count | Source | 70 | 8.53k | fn from(src: GoAway) -> Self { | 71 | 8.53k | frame::Frame::GoAway(src) | 72 | 8.53k | } |
<h2::frame::Frame<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>> as core::convert::From<h2::frame::go_away::GoAway>>::from Line | Count | Source | 70 | 5.92k | fn from(src: GoAway) -> Self { | 71 | 5.92k | frame::Frame::GoAway(src) | 72 | 5.92k | } |
|
73 | | } |
74 | | |
75 | | impl fmt::Debug for GoAway { |
76 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
77 | 0 | let mut builder = f.debug_struct("GoAway"); |
78 | 0 | builder.field("error_code", &self.error_code); |
79 | 0 | builder.field("last_stream_id", &self.last_stream_id); |
80 | 0 |
|
81 | 0 | if !self.debug_data.is_empty() { |
82 | 0 | builder.field("debug_data", &self.debug_data); |
83 | 0 | } |
84 | | |
85 | 0 | builder.finish() |
86 | 0 | } |
87 | | } |