+impl MsgDecodable for Ping {
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 4 {
+ return Err(DecodeError::WrongLength);
+ }
+ let ponglen = byte_utils::slice_to_be16(&v[0..2]);
+ let byteslen = byte_utils::slice_to_be16(&v[2..4]);
+ if v.len() < 4 + byteslen as usize {
+ return Err(DecodeError::WrongLength);
+ }
+ Ok(Self {
+ ponglen,
+ byteslen,
+ })
+ }
+}
+impl MsgEncodable for Ping {
+ fn encode(&self) -> Vec<u8> {
+ let mut res = Vec::with_capacity(self.byteslen as usize + 2);
+ res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
+ res.resize(2 + self.byteslen as usize, 0);
+ res
+ }
+}
+
+impl MsgDecodable for Pong {
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 2 {
+ return Err(DecodeError::WrongLength);
+ }
+ let byteslen = byte_utils::slice_to_be16(&v[0..2]);
+ if v.len() < 2 + byteslen as usize {
+ return Err(DecodeError::WrongLength);
+ }
+ Ok(Self {
+ byteslen
+ })
+ }
+}
+impl MsgEncodable for Pong {
+ fn encode(&self) -> Vec<u8> {
+ let mut res = Vec::with_capacity(self.byteslen as usize + 2);
+ res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
+ res.resize(2 + self.byteslen as usize, 0);
+ res
+ }
+}
+