1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use bytes::BytesMut;
use prost::Message as PbMsg;
use crate::{common::socket::DecodeResult, decode::DecodeBodyError, proto::Error as HrpcError};
pub use crate::common::socket::{ReadSocket, Socket, SocketError, WriteSocket};
pub(super) fn encode_message<Msg: PbMsg>(buf: &mut BytesMut, msg: &Msg) -> Vec<u8> {
crate::encode::encode_protobuf_message_to(buf, msg);
buf.to_vec()
}
pub(super) fn decode_message<Msg: PbMsg + Default>(
raw: Vec<u8>,
) -> Result<DecodeResult<Msg>, DecodeBodyError> {
if raw.is_empty() {
return Err(DecodeBodyError::InvalidProtoMessage(
prost::DecodeError::new("empty protobuf message"),
));
}
let opcode = raw[0];
if opcode == 0 {
Msg::decode(&raw[1..])
.map(DecodeResult::Msg)
.map_err(DecodeBodyError::InvalidProtoMessage)
} else if opcode == 1 {
HrpcError::decode(&raw[1..])
.map(DecodeResult::Error)
.map_err(DecodeBodyError::InvalidProtoMessage)
} else {
Err(DecodeBodyError::InvalidBody(Box::new(
HrpcError::from((
"hrpcrs.http.invalid-socket-message-opcode",
"invalid socket binary message opcode",
))
.with_details(raw),
)))
}
}