-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.rs
39 lines (32 loc) · 1.14 KB
/
sync.rs
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
use tokio_util::bytes::{BufMut, BytesMut};
use rmonitor::codec::RMonitorDecoder;
use std::io::Read;
use std::net::TcpStream;
use tokio_util::codec::Decoder;
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:4000").expect("Failed to open connection");
// .read() won't use the BytesMut directly in the appropriate manner,
// so we do one extra bit of buffering here
let mut read_buf = [0u8; 256];
// Create the decode buffer, and a decoder with a maximum line length
// of 2048.
let mut buffer = BytesMut::with_capacity(4096);
let mut decoder = RMonitorDecoder::new_with_max_length(2048);
loop {
let r = stream
.read(&mut read_buf)
.expect("Failed to read from stream");
// Put only the read bytes into the decode buffer (not any trailing 0s)
if r > 0 {
buffer.put(&read_buf[..r]);
}
let maybe_record = decoder.decode(&mut buffer);
match maybe_record {
Ok(None) => {
continue;
}
Ok(Some(r)) => println!("{:?}", r),
Err(e) => println!("{:?}", e),
}
}
}