49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
|
|
//! Shared Iroh stream helpers.
|
||
|
|
|
||
|
|
use crate::NodeError;
|
||
|
|
|
||
|
|
pub(crate) async fn read_iroh_line(
|
||
|
|
recv: &mut iroh::endpoint::RecvStream,
|
||
|
|
max_len: usize,
|
||
|
|
) -> Result<String, NodeError> {
|
||
|
|
let mut bytes = Vec::new();
|
||
|
|
let mut byte = [0_u8; 1];
|
||
|
|
loop {
|
||
|
|
if bytes.len() >= max_len {
|
||
|
|
return Err(NodeError::IrohPeer(format!(
|
||
|
|
"iroh response line exceeded {max_len} bytes"
|
||
|
|
)));
|
||
|
|
}
|
||
|
|
let Some(n) = recv
|
||
|
|
.read(&mut byte)
|
||
|
|
.await
|
||
|
|
.map_err(|error| NodeError::IrohPeer(error.to_string()))?
|
||
|
|
else {
|
||
|
|
if bytes.is_empty() {
|
||
|
|
return Err(NodeError::IrohPeer(
|
||
|
|
"iroh stream closed before response line".to_owned(),
|
||
|
|
));
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
};
|
||
|
|
if n == 0 {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
bytes.push(byte[0]);
|
||
|
|
if byte[0] == b'\n' {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
String::from_utf8(bytes).map_err(|error| NodeError::IrohPeer(error.to_string()))
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(crate) async fn finish_iroh_send(
|
||
|
|
send: &mut iroh::endpoint::SendStream,
|
||
|
|
) -> Result<(), NodeError> {
|
||
|
|
send.finish()
|
||
|
|
.map_err(|error| NodeError::IrohPeer(error.to_string()))?;
|
||
|
|
send.stopped()
|
||
|
|
.await
|
||
|
|
.map_err(|error| NodeError::IrohPeer(error.to_string()))?;
|
||
|
|
Ok(())
|
||
|
|
}
|