pub struct RMonitorDecoder { /* private fields */ }
Expand description
A decoder for RMonitor records, which wraps an underlying LinesCodec
to provide framing logic.
Implementations§
Source§impl RMonitorDecoder
impl RMonitorDecoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Returns an RMonitorDecoder
for decoding RMonitor records from a TCP stream.
§Note
The returned RMonitorDecoder
will have an underlying LinesCodec
with no upper
bound on the length of a buffered line. Consider using new_with_max_length
instead.
Sourcepub fn new_with_max_length(max_length: usize) -> Self
pub fn new_with_max_length(max_length: usize) -> Self
Returns an RMonitorDecoder
where the underlying LinesCodec
has a maximum line length
limit.
It is recommended to set such a limit where the input to be supplied to the decoder is untrusted, as an attacker could send an unbounded amount of input with no newline characters.
Examples found in repository?
More examples
examples/sync.rs (line 17)
7fn main() {
8 let mut stream = TcpStream::connect("127.0.0.1:4000").expect("Failed to open connection");
9
10 // .read() won't use the BytesMut directly in the appropriate manner,
11 // so we do one extra bit of buffering here
12 let mut read_buf = [0u8; 256];
13
14 // Create the decode buffer, and a decoder with a maximum line length
15 // of 2048.
16 let mut buffer = BytesMut::with_capacity(4096);
17 let mut decoder = RMonitorDecoder::new_with_max_length(2048);
18
19 loop {
20 let r = stream
21 .read(&mut read_buf)
22 .expect("Failed to read from stream");
23
24 // Put only the read bytes into the decode buffer (not any trailing 0s)
25 if r > 0 {
26 buffer.put(&read_buf[..r]);
27 }
28
29 let maybe_record = decoder.decode(&mut buffer);
30
31 match maybe_record {
32 Ok(None) => {
33 continue;
34 }
35 Ok(Some(r)) => println!("{:?}", r),
36 Err(e) => println!("{:?}", e),
37 }
38 }
39}
Trait Implementations§
Source§impl Debug for RMonitorDecoder
impl Debug for RMonitorDecoder
Source§impl Decoder for RMonitorDecoder
impl Decoder for RMonitorDecoder
Source§type Error = RMonitorCodecError
type Error = RMonitorCodecError
The type of unrecoverable frame decoding errors. Read more
Source§fn decode(
&mut self,
src: &mut BytesMut,
) -> Result<Option<Self::Item>, Self::Error>
fn decode( &mut self, src: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>
Attempts to decode a frame from the provided buffer of bytes. Read more
Source§impl Default for RMonitorDecoder
impl Default for RMonitorDecoder
Source§fn default() -> RMonitorDecoder
fn default() -> RMonitorDecoder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for RMonitorDecoder
impl RefUnwindSafe for RMonitorDecoder
impl Send for RMonitorDecoder
impl Sync for RMonitorDecoder
impl Unpin for RMonitorDecoder
impl UnwindSafe for RMonitorDecoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more