Skip to content

Commit 971d4e6

Browse files
authored
ipc example for test (rustdesk#11127)
Signed-off-by: 21pages <[email protected]>
1 parent 2dbff45 commit 971d4e6

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ os-version = "0.2"
197197

198198
[dev-dependencies]
199199
hound = "3.5"
200+
docopt = "1.1"
200201

201202
[package.metadata.bundle]
202203
name = "RustDesk"

examples/ipc.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use docopt::Docopt;
2+
use hbb_common::{
3+
env_logger::{init_from_env, Env, DEFAULT_FILTER_ENV},
4+
log, tokio,
5+
};
6+
use librustdesk::{ipc::Data, *};
7+
8+
const USAGE: &'static str = "
9+
IPC test program.
10+
11+
Usage:
12+
ipc (-s | --server | -c | --client) [-p <str> | --postfix=<str>]
13+
ipc (-h | --help)
14+
15+
Options:
16+
-h --help Show this screen.
17+
-s --server Run as IPC server.
18+
-c --client Run as IPC client.
19+
-p --postfix=<str> IPC path postfix [default: ].
20+
";
21+
22+
#[derive(Debug, serde::Deserialize)]
23+
struct Args {
24+
flag_server: bool,
25+
flag_client: bool,
26+
flag_postfix: String,
27+
}
28+
29+
#[tokio::main]
30+
async fn main() {
31+
init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
32+
33+
let args: Args = Docopt::new(USAGE)
34+
.and_then(|d| d.deserialize())
35+
.unwrap_or_else(|e| e.exit());
36+
37+
if args.flag_server {
38+
if args.flag_postfix.is_empty() {
39+
log::info!("Starting IPC server...");
40+
} else {
41+
log::info!(
42+
"Starting IPC server with postfix: '{}'...",
43+
args.flag_postfix
44+
);
45+
}
46+
ipc_server(&args.flag_postfix).await;
47+
} else if args.flag_client {
48+
if args.flag_postfix.is_empty() {
49+
log::info!("Starting IPC client...");
50+
} else {
51+
log::info!(
52+
"Starting IPC client with postfix: '{}'...",
53+
args.flag_postfix
54+
);
55+
}
56+
ipc_client(&args.flag_postfix).await;
57+
}
58+
}
59+
60+
async fn ipc_server(postfix: &str) {
61+
let postfix = postfix.to_string();
62+
let postfix2 = postfix.clone();
63+
std::thread::spawn(move || {
64+
if let Err(err) = crate::ipc::start(&postfix) {
65+
log::error!("Failed to start ipc: {}", err);
66+
std::process::exit(-1);
67+
}
68+
});
69+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
70+
ipc_client(&postfix2).await;
71+
}
72+
73+
async fn ipc_client(postfix: &str) {
74+
loop {
75+
match crate::ipc::connect(1000, postfix).await {
76+
Ok(mut conn) => match conn.send(&Data::Empty).await {
77+
Ok(_) => {
78+
log::info!("send message to ipc server success");
79+
}
80+
Err(e) => {
81+
log::error!("Failed to send message to ipc server: {}", e);
82+
}
83+
},
84+
Err(e) => {
85+
log::error!("Failed to connect to ipc server: {}", e);
86+
}
87+
}
88+
tokio::time::sleep(std::time::Duration::from_secs(6)).await;
89+
}
90+
}

0 commit comments

Comments
 (0)