Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. use futures;
  2. use std::{
  3. io::{Cursor, Error, ErrorKind},
  4. net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4},
  5. };
  6. use tokio::{
  7. self,
  8. codec::{BytesCodec, Framed},
  9. net::TcpStream,
  10. prelude::future::Either,
  11. prelude::*,
  12. };
  13.  
  14. pub fn stream_map<S, I>(stream: S) -> impl Stream<Item = (I, u32), Error = std::io::Error>
  15. where
  16. S: Stream<Item = I, Error = std::io::Error>,
  17. I: AsyncRead + AsyncWrite + 'static,
  18. {
  19. use std::ops::Add;
  20. stream
  21. .map(|client| (Framed::new(client, BytesCodec::new()).split(), 0))
  22. .and_then(|((w, r), version)| {
  23. let framed = r.reunite(w).unwrap().into_inner();
  24. if version == 255 {
  25. return Box::new(futures::future::err(std::io::Error::from_raw_os_error(0)));
  26. }
  27. match version {
  28. 0 => Box::new(futures::future::ok((framed, version))),
  29. 1 => Box::new(futures::future::ok((framed, 5))),
  30. 2 => Box::new(
  31. tokio::timer::Delay::new(
  32. std::time::Instant::now().add(std::time::Duration::from_secs(1)),
  33. )
  34. .map_err(|_| std::io::Error::from_raw_os_error(0))
  35. .and_then(|_| futures::future::ok((framed, 10))),
  36. ),
  37. _ => unreachable!(),
  38. }
  39. })
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement