mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-03-30 16:41:00 +03:00
source code
This commit is contained in:
24
libs/parity-tokio-ipc/examples/client.rs
Normal file
24
libs/parity-tokio-ipc/examples/client.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use tokio::{self, prelude::*};
|
||||
use parity_tokio_ipc::Endpoint;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let path = std::env::args().nth(1).expect("Run it with server path to connect as argument");
|
||||
|
||||
let mut client = Endpoint::connect(&path).await
|
||||
.expect("Failed to connect client.");
|
||||
|
||||
loop {
|
||||
let mut buf = [0u8; 4];
|
||||
println!("SEND: PING");
|
||||
client.write_all(b"ping").await.expect("Unable to write message to client");
|
||||
client.read_exact(&mut buf[..]).await.expect("Unable to read buffer");
|
||||
if let Ok("pong") = std::str::from_utf8(&buf[..]) {
|
||||
println!("RECEIVED: PONG");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
tokio::time::delay_for(std::time::Duration::from_secs(2)).await;
|
||||
}
|
||||
}
|
||||
47
libs/parity-tokio-ipc/examples/server.rs
Normal file
47
libs/parity-tokio-ipc/examples/server.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use futures::StreamExt as _;
|
||||
use tokio::{
|
||||
prelude::*,
|
||||
self,
|
||||
io::split,
|
||||
};
|
||||
|
||||
use parity_tokio_ipc::{Endpoint, SecurityAttributes};
|
||||
|
||||
async fn run_server(path: String) {
|
||||
let mut endpoint = Endpoint::new(path);
|
||||
endpoint.set_security_attributes(SecurityAttributes::allow_everyone_create().unwrap());
|
||||
|
||||
let mut incoming = endpoint.incoming().expect("failed to open new socket");
|
||||
|
||||
while let Some(result) = incoming.next().await
|
||||
{
|
||||
match result {
|
||||
Ok(stream) => {
|
||||
let (mut reader, mut writer) = split(stream);
|
||||
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let mut buf = [0u8; 4];
|
||||
let pong_buf = b"pong";
|
||||
if let Err(_) = reader.read_exact(&mut buf).await {
|
||||
println!("Closing socket");
|
||||
break;
|
||||
}
|
||||
if let Ok("ping") = std::str::from_utf8(&buf[..]) {
|
||||
println!("RECIEVED: PING");
|
||||
writer.write_all(pong_buf).await.expect("unable to write to socket");
|
||||
println!("SEND: PONG");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => unreachable!("ideally")
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let path = std::env::args().nth(1).expect("Run it with server path as argument");
|
||||
run_server(path).await
|
||||
}
|
||||
7
libs/parity-tokio-ipc/examples/spam-clients.sh
Executable file
7
libs/parity-tokio-ipc/examples/spam-clients.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Spawning 100 processes"
|
||||
for i in {1..100} ;
|
||||
do
|
||||
( cargo run --example client -- /tmp/test.ipc & )
|
||||
done
|
||||
Reference in New Issue
Block a user