ruck/src/client.rs

29 lines
957 B
Rust
Raw Normal View History

2022-02-07 00:54:36 +00:00
use crate::message::Message;
2022-02-06 19:04:36 +00:00
2022-02-07 00:54:36 +00:00
use bytes::{BufMut, BytesMut};
2022-02-06 19:04:36 +00:00
use futures::prelude::*;
2022-02-07 00:54:36 +00:00
use std::path::PathBuf;
2022-02-06 19:04:36 +00:00
use tokio::net::TcpStream;
use tokio_util::codec::{FramedWrite, LengthDelimitedCodec};
2022-02-07 00:54:36 +00:00
pub async fn send(paths: &Vec<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
2022-02-06 19:04:36 +00:00
// Delimit frames using a length header
2022-02-07 00:54:36 +00:00
let socket = TcpStream::connect("127.0.0.1:8080").await.unwrap();
2022-02-06 19:04:36 +00:00
let length_delimited = FramedWrite::new(socket, LengthDelimitedCodec::new());
let mut stream = tokio_serde::SymmetricallyFramed::new(
length_delimited,
tokio_serde::formats::SymmetricalBincode::<Message>::default(),
);
// Send the value
2022-02-07 00:54:36 +00:00
for path in paths.iter() {
let b = path.to_str().unwrap().as_bytes();
let mut buf = BytesMut::with_capacity(1024);
buf.put(&b[..]);
let body = buf.freeze();
let m = Message { body: body };
stream.send(m).await.unwrap();
}
Ok(())
2022-02-06 19:04:36 +00:00
}