1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
use std::fmt::Debug; use std::fmt::Display; use std::fmt::Formatter; use std::io; use std::net::SocketAddr; use tokio::io::AsyncRead; use tokio::io::AsyncWrite; use crate::socket_unix::SocketAddrUnix; use crate::ServerConf; use futures::stream::Stream; use futures::Future; use std::pin::Pin; use tokio::runtime::Handle; pub trait ToSocketListener { fn to_listener(&self, conf: &ServerConf) -> io::Result<Box<dyn ToTokioListener + Send>>; fn cleanup(&self); } #[derive(Clone, Debug, PartialEq)] pub enum AnySocketAddr { Inet(SocketAddr), Unix(SocketAddrUnix), } impl Display for AnySocketAddr { fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result { match *self { AnySocketAddr::Inet(ref inet_addr) => Display::fmt(inet_addr, f), AnySocketAddr::Unix(ref unix_addr) => Display::fmt(unix_addr, f), } } } impl AnySocketAddr { pub fn port(&self) -> io::Result<u16> { match self { &AnySocketAddr::Inet(ref inet_addr) => Ok(inet_addr.port()), &AnySocketAddr::Unix(_) => Err(io::Error::new( io::ErrorKind::Other, "Cannot get port from unix domain socket", )), } } } impl ToSocketListener for AnySocketAddr { fn to_listener(&self, conf: &ServerConf) -> io::Result<Box<dyn ToTokioListener + Send>> { match self { &AnySocketAddr::Inet(ref inet_addr) => inet_addr.to_listener(conf), &AnySocketAddr::Unix(ref unix_addr) => unix_addr.to_listener(conf), } } fn cleanup(&self) { match self { &AnySocketAddr::Inet(ref inet_addr) => inet_addr.cleanup(), #[cfg(unix)] &AnySocketAddr::Unix(ref unix_addr) => unix_addr.cleanup(), #[cfg(not(unix))] &AnySocketAddr::Unix(..) => {} } } } impl ToClientStream for AnySocketAddr { fn connect( &self, handle: &Handle, ) -> Pin<Box<dyn Future<Output = io::Result<Pin<Box<dyn StreamItem + Send>>>> + Send>> { match self { &AnySocketAddr::Inet(ref inet_addr) => inet_addr.connect(handle), &AnySocketAddr::Unix(ref unix_addr) => unix_addr.connect(handle), } } fn socket_addr(&self) -> AnySocketAddr { self.clone() } } pub trait ToTokioListener { fn to_tokio_listener(self: Box<Self>, handle: &Handle) -> Box<dyn ToServerStream>; fn local_addr(&self) -> io::Result<AnySocketAddr>; } pub trait ToServerStream { fn incoming( self: Box<Self>, ) -> Pin< Box<dyn Stream<Item = io::Result<(Pin<Box<dyn StreamItem + Send>>, AnySocketAddr)>> + Send>, >; } pub trait ToClientStream: Display + Send + Sync { fn connect( &self, handle: &Handle, ) -> Pin<Box<dyn Future<Output = io::Result<Pin<Box<dyn StreamItem + Send>>>> + Send>>; fn socket_addr(&self) -> AnySocketAddr; } pub trait StreamItem: AsyncRead + AsyncWrite + Debug + Send + Sync { fn is_tcp(&self) -> bool; fn set_nodelay(&self, no_delay: bool) -> io::Result<()>; }