Skip to content

Commit a44b42f

Browse files
committed
use stream macro
1 parent c45ca8a commit a44b42f

File tree

4 files changed

+65
-37
lines changed

4 files changed

+65
-37
lines changed

RustApp/Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RustApp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ speexdsp = { git = "https://github.com/wiiznokes/speexdsp-rs-fork", branch = "ve
103103
] }
104104
fslock = "0.2"
105105
interprocess = { version = "2", features = ["tokio"] }
106+
async-stream = "0.3"
106107

107108
[build-dependencies]
108109
prost-build = "0.14"

RustApp/src/single_instance.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use byteorder::WriteBytesExt;
22
use futures::stream::Stream;
3-
use interprocess::local_socket::tokio::Stream as InterprocessTokioStream;
43
use interprocess::local_socket::traits::Stream as InterprocessStreamTrait;
54
use interprocess::local_socket::traits::tokio::Listener as TokioListener;
65
use interprocess::local_socket::{GenericNamespaced, ListenerOptions, ToNsName};
76
use interprocess::local_socket::{Name, Stream as InterprocessStream};
87

98
use tokio::io::AsyncReadExt;
109

10+
use async_stream::stream;
11+
1112
#[derive(Debug, Clone)]
1213
pub enum IpcEvent {
1314
Show,
@@ -38,33 +39,43 @@ fn get_name() -> anyhow::Result<Name<'static>> {
3839
Ok(name)
3940
}
4041

41-
pub async fn create_stream() -> anyhow::Result<InterprocessTokioStream> {
42+
pub fn stream() -> anyhow::Result<impl Stream<Item = IpcEvent>> {
4243
let name = get_name()?;
43-
4444
let opts = ListenerOptions::new().name(name);
45-
4645
let listener = opts.create_tokio()?;
4746

48-
let stream = listener.accept().await?;
49-
Ok(stream)
50-
}
47+
let stream = stream! {
5148

52-
pub fn parse_stream(stream: InterprocessTokioStream) -> impl Stream<Item = IpcEvent> {
53-
futures::stream::unfold(stream, |mut stream| async {
54-
match stream.read_u8().await {
55-
Ok(value) => match value.try_into() {
56-
Ok(event) => Some((event, stream)),
57-
Err(()) => {
58-
error!("can't parse ipc event");
59-
None
49+
loop {
50+
match listener.accept().await {
51+
Ok(mut client) => {
52+
loop {
53+
match client.read_u8().await {
54+
Ok(byte) => match byte.try_into() {
55+
Ok(event) => yield event,
56+
Err(_) => {
57+
error!("can't parse ipc event");
58+
}
59+
},
60+
Err(e) => {
61+
if e.kind() == std::io::ErrorKind::UnexpectedEof {
62+
} else {
63+
error!("error reading client: {e}");
64+
}
65+
break;
66+
}
67+
}
68+
}
69+
}
70+
Err(e) => {
71+
error!("error accepting client: {e}");
72+
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
6073
}
61-
},
62-
Err(e) => {
63-
error!("{e}");
64-
None
6574
}
6675
}
67-
})
76+
};
77+
78+
Ok(stream)
6879
}
6980

7081
pub fn send_event(event: IpcEvent) -> anyhow::Result<()> {

RustApp/src/ui/app.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,23 +407,16 @@ impl Application for AppState {
407407
commands.push(app.open_main_window());
408408
}
409409

410-
commands.push(
411-
cosmic::iced::task::Task::future(async { single_instance::create_stream().await })
412-
.then(|stream| match stream {
413-
Ok(stream) => cosmic::iced::task::Task::run(
414-
single_instance::parse_stream(stream),
415-
|event| match event {
416-
single_instance::IpcEvent::Show => {
417-
cosmic::Action::App(AppMsg::ShowWindow)
418-
}
419-
},
420-
),
421-
Err(e) => {
422-
error!("{e}");
423-
Task::none()
424-
}
425-
}),
426-
);
410+
match single_instance::stream() {
411+
Ok(stream) => {
412+
commands.push(cosmic::iced::task::Task::run(stream, |event| match event {
413+
single_instance::IpcEvent::Show => cosmic::Action::App(AppMsg::ShowWindow),
414+
}));
415+
}
416+
Err(e) => {
417+
error!("can't create ipc stream {e}")
418+
}
419+
}
427420

428421
(app, Task::batch(commands))
429422
}

0 commit comments

Comments
 (0)