1use iroh::endpoint::{Connection, SendStream};
7use tokio::io::AsyncWriteExt;
8
9use crate::error::{Error, Result};
10
11#[derive(Debug)]
15pub struct Channel {
16 connection: Connection,
17 send: SendStream,
18}
19
20impl Channel {
21 pub(crate) fn new(connection: Connection, send: SendStream) -> Self {
23 Self { connection, send }
24 }
25
26 pub async fn send(&mut self, payload: &[u8]) -> Result<()> {
28 self.send
29 .write_all(payload)
30 .await
31 .map_err(|e| Error::Transport(format!("channel write failed: {e}")))?;
32 self.send
33 .flush()
34 .await
35 .map_err(|e| Error::Transport(format!("channel flush failed: {e}")))?;
36 Ok(())
37 }
38
39 pub fn close(mut self) {
41 let _ = self.send.finish();
42 self.connection.close(0u32.into(), b"done");
43 }
44
45 pub fn connection(&self) -> &Connection {
47 &self.connection
48 }
49}
50
51impl Drop for Channel {
52 fn drop(&mut self) {
53 let _ = self.send.finish();
54 }
55}