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