Skip to content

Commit

Permalink
refactor(example): use tokio::io::Stdin in echo_service example
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-faber committed Nov 9, 2020
1 parent 64333e1 commit 6f2a577
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ webpki = "~0.21.3"

[dependencies.tokio]
version = "~0.2.21"
features = [ "rt-core", "sync", "time", "io-driver", "macros" ]
features = [ "rt-core", "sync", "time", "macros", "io-std", "io-util" ]

[dependencies.bytes]
version = "~0.5.5"
Expand All @@ -51,7 +51,6 @@ webpki = "~0.21.3"
features = [ "dangerous_configuration" ]

[dev-dependencies]
async-std = "1.6.5"
rand = "~0.7.3"

[target."cfg(any(all(unix, not(any(target_os = \"android\", target_os = \"androideabi\", target_os = \"ios\"))), windows))".dependencies]
Expand Down
17 changes: 8 additions & 9 deletions examples/echo_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_std::io;
use bytes::Bytes;
use qp2p::{Config, Error, Message, QuicP2p};
use std::env;
use tokio::io::AsyncBufReadExt;

#[tokio::main]
async fn main() -> Result<(), Error> {
Expand Down Expand Up @@ -35,15 +35,12 @@ async fn main() -> Result<(), Error> {
.ok_or_else(|| Error::Unexpected("Error during incoming connection".to_string()))?;
let connecting_peer = messages.remote_addr();
println!("Incoming connection from: {}", &connecting_peer);
let message = messages.next().await;
assert!(message.is_none());
println!("Responded to peer with EchoService response");
println!("Waiting for messages...");
let mut messages = incoming
let message = messages
.next()
.await
.ok_or_else(|| Error::Unexpected("Error during incoming connection".to_string()))?;
let message = messages.next().await.unwrap();
.expect("Error reading message from incoming connection");
println!("Responded to peer with EchoService response");
println!("Waiting for messages...");
let (mut bytes, mut send, mut recv) = if let Message::BiStream {
bytes, send, recv, ..
} = message
Expand Down Expand Up @@ -85,6 +82,8 @@ async fn main() -> Result<(), Error> {

async fn read_from_stdin() -> String {
let mut input = String::new();
io::stdin().read_line(&mut input).await.unwrap_or(0);
let stdin = tokio::io::stdin();
let mut buf_reader = tokio::io::BufReader::new(stdin);
buf_reader.read_line(&mut input).await.unwrap_or(0);
input
}

0 comments on commit 6f2a577

Please sign in to comment.