rust-tokio

Is it possible to close a TcpListener in Tokio?

会有一股神秘感。 提交于 2019-12-10 04:54:39
问题 I have a tokio_core::net::TcpListener that I create and then call the incoming method on to get a stream of incoming connections. I then use the for_each method on that stream to turn it into a future and run the future on an event loop. Once I do that is there any way to unbind from the port at some later time? If not, is there any other API in Tokio that can be used to create a TCP server which can be closed? 回答1: In short, you need to drop the TcpListener / the Future returned by for_each

The trait bound `(): futures::Future` is not satisfied when using TcpConnectionNew

亡梦爱人 提交于 2019-12-10 03:31:59
问题 I am trying to write a simple TCP client in Rust using Tokio crate. My code is pretty close to this example minus the TLS: extern crate futures; extern crate tokio_core; extern crate tokio_io; use futures::Future; use tokio_core::net::TcpStream; use tokio_core::reactor::Core; use tokio_io::io; fn main() { let mut core = Core::new().unwrap(); let handle = core.handle(); let connection = TcpStream::connect(&"127.0.0.1:8080".parse().unwrap(), &handle); let server = connection.and_then(|stream| {

Forwarding from a futures::Stream to a futures::Sink

寵の児 提交于 2019-12-08 15:29:35
问题 I am currently trying to wrap my head around the tokio & futures primitives and ecosystem. I started doing some work from the tk-http websockets example, and wanted to do more processing on the received data rather than echoing it back. A first step seemed to be to replace the .forward() call with some kind of loop. It seemed to me that stream.forward(sink) is equivalent to stream.fold(sink, |out_, item| { out.send(item).and_then(Sink::flush) }) , however doing this (commit) the stream is not

Type mismatch resolving the error type when forwarding messages from a futures channel to a WebSocket Sink

我怕爱的太早我们不能终老 提交于 2019-12-08 09:48:43
问题 I'm trying to wrap my head around futures in Rust but I am confused by this code which is supposed to send messages arriving at rx to sink : extern crate futures; extern crate tokio_core; extern crate websocket; use websocket::message::OwnedMessage; use websocket::server::InvalidConnection; use websocket::async::Server; use tokio_core::reactor::Core; use futures::{Future, Sink, Stream}; use futures::sync::mpsc; use std::{thread, time}; use futures::sync::mpsc::Receiver; fn main() { let mut

How do I interpret the signature of read_until and what is AsyncRead + BufRead in Tokio?

旧巷老猫 提交于 2019-12-08 08:12:03
问题 I'm trying to understand asynchronous I/O in Rust. The following code is based on a snippet from Katharina Fey's Jan 2019 talk which works for me: use futures::future::Future; use std::io::BufReader; use tokio::io::*; fn main() { let reader = BufReader::new(tokio::io::stdin()); let buffer = Vec::new(); println!("Type something:"); let fut = tokio::io::read_until(reader, b'\n', buffer) .and_then(move |(stdin, buffer)| { tokio::io::stdout() .write_all(&buffer) .map_err(|e| panic!(e)) }) .map

How can I stop the hyper HTTP web server and return an error?

和自甴很熟 提交于 2019-12-08 03:22:20
问题 The docs for the hyper crate have a simple example to start a web server: extern crate hyper; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; fn main() { // Construct our SocketAddr to listen on... let addr = ([127, 0, 0, 1], 3000).into(); // And a NewService to handle each connection... let new_service = || service_fn_ok(|_req| Response::new(Body::from("Hello World"))); // Then bind and serve... let server = Server::bind(&addr).serve(new_service); // Finally, spawn

How to select between a future and stream in Rust?

空扰寡人 提交于 2019-12-07 07:49:20
问题 I've just started experimenting with futures/tokio in Rust. I can do really basic things with just futures or just with streams. I was wondering how you can select between future and a stream. How can I extend the toy problem from the tokio documentation to use tokio_timer::Timer to do a timed HTTPS request? extern crate futures; extern crate native_tls; extern crate tokio_core; extern crate tokio_io; extern crate tokio_tls; use std::io; use std::net::ToSocketAddrs; use futures::Future; use

Why does calling tokio::spawn result in the panic “SpawnError { is_shutdown: true }”?

时间秒杀一切 提交于 2019-12-07 05:27:02
问题 I want to use Delay to do some work later. If I use tokio::run , it just works fine, but it panics when using tokio::spawn : use std::sync::mpsc; use std::time::*; use tokio::prelude::*; // 0.1.14 fn main() { let (tx, rx) = mpsc::channel(); let task = tokio::timer::Delay::new(Instant::now() + Duration::from_secs(1)) .map(move |_| { tx.send(String::from("hello")).unwrap(); () }) .map_err(|e| { panic!("{:?}", e); }); tokio::spawn(task); let msg = rx.recv().unwrap(); println!("{}", msg); }

How do I interpret the signature of read_until and what is AsyncRead + BufRead in Tokio?

回眸只為那壹抹淺笑 提交于 2019-12-06 22:09:41
I'm trying to understand asynchronous I/O in Rust. The following code is based on a snippet from Katharina Fey's Jan 2019 talk which works for me: use futures::future::Future; use std::io::BufReader; use tokio::io::*; fn main() { let reader = BufReader::new(tokio::io::stdin()); let buffer = Vec::new(); println!("Type something:"); let fut = tokio::io::read_until(reader, b'\n', buffer) .and_then(move |(stdin, buffer)| { tokio::io::stdout() .write_all(&buffer) .map_err(|e| panic!(e)) }) .map_err(|e| panic!(e)); tokio::run(fut); } Before finding that code, I attempted to figure it out from the

How can I stop the hyper HTTP web server and return an error?

£可爱£侵袭症+ 提交于 2019-12-06 22:02:28
The docs for the hyper crate have a simple example to start a web server: extern crate hyper; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; fn main() { // Construct our SocketAddr to listen on... let addr = ([127, 0, 0, 1], 3000).into(); // And a NewService to handle each connection... let new_service = || service_fn_ok(|_req| Response::new(Body::from("Hello World"))); // Then bind and serve... let server = Server::bind(&addr).serve(new_service); // Finally, spawn `server` onto an Executor... hyper::rt::run(server.map_err(|e| { eprintln!("server error: {}", e); })); }