1use async_std::{io::ReadExt, os::unix::net::UnixStream};
2use i3ipc_types::{decode_event, event, MAGIC};
3use std::io;
4
5pub struct EventStream {
6 inner: UnixStream,
7}
8
9impl EventStream {
10 pub fn new(inner: UnixStream) -> Self {
11 Self { inner }
12 }
13
14 pub async fn next(&mut self) -> io::Result<event::Event> {
18 let mut init = [0_u8; 14];
19 self.inner.read_exact(&mut init).await?;
20
21 assert!(&init[0..6] == MAGIC.as_bytes(), "Magic str not received");
22 let payload_len = u32::from_ne_bytes([init[6], init[7], init[8], init[9]]) as usize;
23 let msg_type = u32::from_ne_bytes([init[10], init[11], init[12], init[13]]);
24
25 let mut payload = vec![0_u8; payload_len];
26 self.inner.read_exact(&mut payload).await?;
27
28 decode_event(msg_type, payload)
29 }
30}