Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const fn #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ homepage = "https://github.com/paritytech/parity-tokio-ipc"
description = """
Interprocess communication library for tokio.
"""
include = ["src/**/*", "LICENSE-*", "README.md"]

[dependencies]
futures = "0.3"
Expand Down
41 changes: 25 additions & 16 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use parity_tokio_ipc::Endpoint;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main(flavor = "current_thread")]
async fn main() {
let path = std::env::args().nth(1).expect("Run it with server path to connect as argument");
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.");
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;
}
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::sleep(std::time::Duration::from_secs(2)).await;
}
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
}
66 changes: 35 additions & 31 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,45 @@ use tokio::io::{split, AsyncReadExt, AsyncWriteExt};
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 endpoint = Endpoint::new(path);
endpoint.set_security_attributes(SecurityAttributes::allow_everyone_create().unwrap());

let incoming = endpoint.incoming().expect("failed to open new socket");
futures::pin_mut!(incoming);
let incoming = endpoint.incoming().expect("failed to open new socket");
futures::pin_mut!(incoming);

while let Some(result) = incoming.next().await
{
match result {
Ok(stream) => {
let (mut reader, mut writer) = split(stream);
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::spawn(async move {
loop {
let mut buf = [0u8; 4];
let pong_buf = b"pong";
if reader.read_exact(&mut buf).await.is_err() {
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(flavor = "current_thread")]
async fn main() {
let path = std::env::args().nth(1).expect("Run it with server path as argument");
run_server(path).await
}
let path = std::env::args()
.nth(1)
.expect("Run it with server path as argument");
run_server(path).await
}
Loading