halephant_core/proto/
codec.rs1use bytes::BytesMut;
2use tokio_util::codec::{Decoder, Encoder};
3
4use crate::errors;
5use crate::proto::backend::BackendMessage;
6use crate::proto::frontend::FrontendMessage;
7
8pub struct FrontendCodec {
17 startup: bool,
20}
21
22impl FrontendCodec {
23 pub fn new() -> Self {
24 Self { startup: true }
25 }
26
27 pub fn post_startup() -> Self {
30 Self { startup: false }
31 }
32}
33
34impl Default for FrontendCodec {
35 fn default() -> Self {
36 Self::new()
37 }
38}
39
40impl Decoder for FrontendCodec {
41 type Item = FrontendMessage;
42 type Error = errors::ProtocolError;
43
44 fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
45 if self.startup {
46 decode_initial(src)
47 } else {
48 decode_regular(src, FrontendMessage::parse)
49 }
50 .map(|opt| {
51 if let Some(ref msg) = opt {
52 match msg {
53 FrontendMessage::SslRequest
58 | FrontendMessage::GssEncRequest
59 | FrontendMessage::CancelRequest { .. } => {}
60 _ => self.startup = false,
63 }
64 }
65 opt
66 })
67 }
68}
69
70impl Encoder<BackendMessage> for FrontendCodec {
71 type Error = errors::ProtocolError;
72
73 fn encode(&mut self, item: BackendMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
74 item.encode(dst);
75 Ok(())
76 }
77}
78
79pub struct BackendCodec;
88
89impl BackendCodec {
90 pub fn new() -> Self {
91 Self
92 }
93}
94
95impl Default for BackendCodec {
96 fn default() -> Self {
97 Self::new()
98 }
99}
100
101impl Decoder for BackendCodec {
102 type Item = BackendMessage;
103 type Error = errors::ProtocolError;
104
105 fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
106 decode_regular(src, BackendMessage::parse)
107 }
108}
109
110impl Encoder<FrontendMessage> for BackendCodec {
111 type Error = errors::ProtocolError;
112
113 fn encode(&mut self, item: FrontendMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
114 item.encode(dst);
115 Ok(())
116 }
117}
118
119fn decode_initial(src: &mut BytesMut) -> Result<Option<FrontendMessage>, errors::ProtocolError> {
125 if src.len() < 4 {
126 return Ok(None);
127 }
128 let length = u32::from_be_bytes([src[0], src[1], src[2], src[3]]) as usize;
129 if length < 4 {
130 return Err(errors::ProtocolError::InvalidValue {
131 position: 0,
132 message: format!("initial message length too small: {length}"),
133 });
134 }
135 if src.len() < length {
136 return Ok(None);
138 }
139 let frame = src.split_to(length);
140 FrontendMessage::parse_initial(&frame).map(Some)
141}
142
143fn decode_regular<M>(
147 src: &mut BytesMut,
148 parse: impl FnOnce(u8, &[u8]) -> Result<M, errors::ProtocolError>,
149) -> Result<Option<M>, errors::ProtocolError> {
150 if src.len() < 5 {
151 return Ok(None);
152 }
153 let msg_type = src[0];
154 let length = u32::from_be_bytes([src[1], src[2], src[3], src[4]]) as usize;
155 if length < 4 {
156 return Err(errors::ProtocolError::InvalidValue {
157 position: 1,
158 message: format!("message length too small: {length}"),
159 });
160 }
161 let total = 1 + length; if src.len() < total {
163 return Ok(None);
164 }
165 let frame = src.split_to(total);
166 let payload = &frame[5..]; parse(msg_type, payload).map(Some)
168}