quiche_tokio/
qlog.rs

1use tokio::io::AsyncWriteExt;
2
3pub struct QLog {
4    bytes_tx: tokio::sync::mpsc::UnboundedSender<Vec<u8>>,
5}
6
7impl QLog {
8    pub async fn new(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
9        let mut file = tokio::fs::File::create(path).await?;
10        let (bytes_tx, mut bytes_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
11
12        tokio::task::spawn(async move {
13            loop {
14                match bytes_rx.recv().await {
15                    None => break,
16                    Some(b) => {
17                        if let Err(e) = file.write_all(&b).await {
18                            warn!("Error writing qlog: {}", e);
19                            break;
20                        }
21                    }
22                }
23            }
24        });
25
26        Ok(QLog { bytes_tx })
27    }
28}
29
30impl std::io::Write for QLog {
31    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
32        if self.bytes_tx.send(buf.to_vec()).is_err() {
33            return Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, ""));
34        }
35        Ok(buf.len())
36    }
37
38    fn flush(&mut self) -> std::io::Result<()> {
39        Ok(())
40    }
41}