Last active
August 8, 2019 22:46
-
-
Save asonix/515b9ec1ba1bb9f231be335cd232fbf4 to your computer and use it in GitHub Desktop.
Edit: It works now. stdin and stdout need their own processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use failure::Error; | |
use futures::{Future, Stream}; | |
use tokio::{io::AsyncRead, net::UnixListener}; | |
fn main() -> Result<(), Error> { | |
let listener = UnixListener::bind("socket.sock")?; | |
tokio::run( | |
listener | |
.incoming() | |
.for_each(|socket| { | |
let (reader, writer) = socket.split(); | |
tokio::spawn( | |
tokio::io::copy(reader, writer) | |
.map_err(|e| eprintln!("Error copying, {}", e)) | |
.map(|(num, _, _)| println!("Copy complete, {}", num)), | |
); | |
Ok(()) | |
}) | |
.map_err(|e| eprintln!("Error: {}", e)), | |
); | |
Ok(()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bytes::BytesMut; | |
use failure::Error; | |
use futures::{future::lazy, Future, Stream}; | |
use tokio::{ | |
codec::{BytesCodec, Decoder, FramedRead, FramedWrite}, | |
io::{stdin, stdout}, | |
net::UnixStream, | |
sync::mpsc::{channel, Receiver, Sender}, | |
}; | |
fn forward_to_stdout(rx: Receiver<BytesMut>) -> impl Future<Item = (), Error = ()> { | |
rx.map(|b| b.freeze()) | |
.from_err::<Error>() | |
.forward(FramedWrite::new(stdout(), BytesCodec::new())) | |
.map(|_| ()) | |
.map_err(|e| eprintln!("Error forwarding to stdout, {}", e)) | |
} | |
fn forward_from_stdin(tx: Sender<BytesMut>) -> impl Future<Item = (), Error = ()> { | |
FramedRead::new(stdin(), BytesCodec::new()) | |
.from_err::<Error>() | |
.forward(tx) | |
.map(|_| ()) | |
.map_err(|e| eprintln!("Error forwarding from stdin, {}", e)) | |
} | |
fn main() -> Result<(), Error> { | |
tokio::run(lazy(move || { | |
let (to_stdout, rx) = channel(25); | |
tokio::spawn(forward_to_stdout(rx)); | |
let (tx, from_stdin) = channel(25); | |
tokio::spawn(forward_from_stdin(tx.clone())); | |
UnixStream::connect("socket.sock") | |
.map(move |socket| { | |
let (sink, stream) = BytesCodec::new().framed(socket).split(); | |
tokio::spawn( | |
from_stdin | |
.map(|b| { | |
println!("Sending, {:?}", b); | |
b.freeze() | |
}) | |
.from_err::<Error>() | |
.forward(sink) | |
.map(|_| ()) | |
.map_err(|e| eprintln!("Error in echo, {}", e)), | |
); | |
stream | |
}) | |
.flatten_stream() | |
.map(|b| { | |
println!("Received, {:?}", b); | |
b | |
}) | |
.from_err::<Error>() | |
.forward(to_stdout) | |
.map(|_| ()) | |
.map_err(|e| eprintln!("Error: {}", e)) | |
})); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment