Guest User

Untitled

a guest
Nov 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. use futures::future::Future;
  2. use futures::sink::Sink;
  3. use futures::stream::Stream;
  4.  
  5. fn main() {
  6. let addr = std::env::args()
  7. .nth(1)
  8. .unwrap_or("127.0.0.1:8080".to_string());
  9. let addr = addr.parse::<std::net::SocketAddr>().unwrap();
  10.  
  11. let socket = tokio::net::TcpListener::bind(&addr).unwrap();
  12. println!("Listening on: {}", addr);
  13.  
  14. let task = socket
  15. .incoming()
  16. .map_err(|e| println!("failed to accept socket; error = {:?}", e))
  17. .for_each(move |socket| {
  18. let framed = tokio::codec::Framed::new(socket, tokio::codec::BytesCodec::new());
  19. let (sink, stream) = framed.split();
  20.  
  21. let a_stream = stream
  22. .map(|bytes| {
  23. println!("bytes: {:?}", bytes);
  24. println!("start delay!");
  25. tokio::timer::Delay::new(
  26. std::time::Instant::now() + std::time::Duration::from_millis(1000),
  27. )
  28. })
  29. .map(|_| {
  30. "welcome, guy!".into()
  31. })
  32. .map_err(|e| { println!("delay errored; err={:?}", e); e });
  33.  
  34. let a = sink
  35. .send_all(a_stream)
  36. .map(|_| ())
  37. .map_err(|e| println!("==> {:?}", e));
  38.  
  39. tokio::spawn(a)
  40. });
  41.  
  42. tokio::run(task);
  43. }
Add Comment
Please, Sign In to add comment