Added blocking and async utilities for sending RaaS messages
parent
fab124d725
commit
3496b9dda1
|
@ -2,6 +2,21 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
|
@ -17,6 +32,21 @@ version = "1.0.86"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.72"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.5.0"
|
||||
|
@ -29,6 +59,12 @@ version = "1.6.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
|
@ -69,6 +105,12 @@ version = "0.4.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.14.5"
|
||||
|
@ -124,12 +166,30 @@ version = "2.7.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multimap"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03"
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.35.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
|
@ -146,6 +206,12 @@ dependencies = [
|
|||
"indexmap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
|
||||
|
||||
[[package]]
|
||||
name = "prettyplease"
|
||||
version = "0.2.20"
|
||||
|
@ -234,6 +300,7 @@ dependencies = [
|
|||
"bytes",
|
||||
"prost",
|
||||
"prost-build",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -265,6 +332,12 @@ version = "0.8.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.34"
|
||||
|
@ -301,6 +374,17 @@ dependencies = [
|
|||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.38.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"bytes",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
|
|
|
@ -6,10 +6,13 @@ description = "Protobuf definitions for RaaS"
|
|||
authors = ["Joey Hines joey@ahines.net"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
async = ["dep:tokio"]
|
||||
|
||||
[dependencies]
|
||||
bytes = "1.6.0"
|
||||
prost = "0.12.6"
|
||||
tokio = {version = "1.38.0", features = ["io-util", "test-util"], optional = true}
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.12.6"
|
95
src/lib.rs
95
src/lib.rs
|
@ -1,4 +1,7 @@
|
|||
pub mod raas {
|
||||
#[cfg(feature = "async")]
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
pub mod register {
|
||||
include!(concat!(env!("OUT_DIR"), "/raas.register.rs"));
|
||||
}
|
||||
|
@ -57,12 +60,70 @@ pub mod raas {
|
|||
bytes
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "async"))]
|
||||
pub fn recv_raas_msg<T: std::io::Read>(stream: &mut T) -> Result<RaasMessage, std::io::Error> {
|
||||
let mut message_len_bytes = [0u8; 4];
|
||||
stream.read_exact(&mut message_len_bytes)?;
|
||||
|
||||
let len = u32::from_be_bytes(message_len_bytes);
|
||||
|
||||
let mut message = vec![0u8; len as usize];
|
||||
|
||||
stream.read_exact(&mut message)?;
|
||||
|
||||
Ok(RaasMessage { len, msg: message })
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "async"))]
|
||||
pub fn send_raas_msg<T: std::io::Write>(
|
||||
stream: &mut T,
|
||||
data: Vec<u8>,
|
||||
) -> Result<(), std::io::Error> {
|
||||
let msg = RaasMessage::new(data);
|
||||
|
||||
stream.write_all(&msg.into_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
pub async fn recv_raas_msg<T: AsyncReadExt + std::marker::Unpin>(
|
||||
stream: &mut T,
|
||||
) -> Result<RaasMessage, std::io::Error> {
|
||||
let mut message_len_bytes = [0u8; 4];
|
||||
stream.read_exact(&mut message_len_bytes).await?;
|
||||
|
||||
let len = u32::from_be_bytes(message_len_bytes);
|
||||
|
||||
let mut message = vec![0u8; len as usize];
|
||||
|
||||
stream.read_exact(&mut message).await?;
|
||||
|
||||
Ok(RaasMessage { len, msg: message })
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
pub async fn send_raas_msg<T: AsyncWriteExt + std::marker::Unpin>(
|
||||
stream: &mut T,
|
||||
data: Vec<u8>,
|
||||
) -> Result<(), std::io::Error> {
|
||||
let msg = RaasMessage::new(data);
|
||||
|
||||
stream.write_all(&msg.into_bytes()).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::raas;
|
||||
use crate::raas::RaasMessage;
|
||||
use crate::raas::{recv_raas_msg, send_raas_msg, RaasMessage};
|
||||
|
||||
#[cfg(not(feature = "async"))]
|
||||
use std::io::BufReader;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use tokio::io::BufReader;
|
||||
|
||||
#[test]
|
||||
fn create_cmd() {
|
||||
|
@ -111,4 +172,36 @@ mod test {
|
|||
assert_eq!(decoded_message.len, decoded_message.len);
|
||||
assert_eq!(decoded_message.msg, original_message.msg);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "async"))]
|
||||
#[test]
|
||||
fn send_recv_test_raas_message() {
|
||||
let mut msg_buffer = Vec::new();
|
||||
let message = Vec::from(b"Hello World");
|
||||
|
||||
send_raas_msg(&mut msg_buffer, message.clone()).unwrap();
|
||||
|
||||
let mut reader = BufReader::new(msg_buffer.as_slice());
|
||||
let out_msg = recv_raas_msg(&mut reader).unwrap();
|
||||
|
||||
assert_eq!(message.len() as u32, out_msg.len);
|
||||
assert_eq!(message, out_msg.msg);
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
#[tokio::test]
|
||||
async fn send_recv_test_raas_message() {
|
||||
let mut msg_buffer = Vec::new();
|
||||
let message = Vec::from(b"Hello World");
|
||||
|
||||
send_raas_msg(&mut msg_buffer, message.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut reader = BufReader::new(msg_buffer.as_slice());
|
||||
let out_msg = recv_raas_msg(&mut reader).await.unwrap();
|
||||
|
||||
assert_eq!(message.len() as u32, out_msg.len);
|
||||
assert_eq!(message, out_msg.msg);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue