1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 23:42:56 +01:00

fix uds server support

This commit is contained in:
Nikolay Kim 2019-11-21 00:35:44 +06:00
parent d3c5518646
commit ae4394c0f2
6 changed files with 39 additions and 37 deletions

View File

@ -44,7 +44,7 @@ tokio-net = { version = "0.2.0-alpha.6", features = ["signal"] }
tokio-timer = "0.3.0-alpha.6" tokio-timer = "0.3.0-alpha.6"
# unix domain sockets # unix domain sockets
mio-uds = { version = "0.6.7", optional = true } mio-uds = { version = "0.6.7" }
# nativetls # nativetls
native-tls = { version = "0.2", optional = true } native-tls = { version = "0.2", optional = true }

View File

@ -185,11 +185,11 @@ impl ServerBuilder {
Ok(self) Ok(self)
} }
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
/// Add new unix domain service to the server. /// Add new unix domain service to the server.
pub fn bind_uds<F, U, N>(self, name: N, addr: U, factory: F) -> io::Result<Self> pub fn bind_uds<F, U, N>(self, name: N, addr: U, factory: F) -> io::Result<Self>
where where
F: ServiceFactory<tokio_uds::UnixStream>, F: ServiceFactory<tokio_net::uds::UnixStream>,
N: AsRef<str>, N: AsRef<str>,
U: AsRef<std::path::Path>, U: AsRef<std::path::Path>,
{ {
@ -208,7 +208,7 @@ impl ServerBuilder {
self.listen_uds(name, lst, factory) self.listen_uds(name, lst, factory)
} }
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
/// Add new unix domain service to the server. /// Add new unix domain service to the server.
/// Useful when running as a systemd service and /// Useful when running as a systemd service and
/// a socket FD can be acquired using the systemd crate. /// a socket FD can be acquired using the systemd crate.
@ -219,7 +219,7 @@ impl ServerBuilder {
factory: F, factory: F,
) -> io::Result<Self> ) -> io::Result<Self>
where where
F: ServiceFactory<tokio_uds::UnixStream>, F: ServiceFactory<tokio_net::uds::UnixStream>,
{ {
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
let token = self.token.next(); let token = self.token.next();

View File

@ -25,9 +25,9 @@ pub(crate) enum ServerMessage {
} }
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static { pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
type NewService: actix::ServiceFactory<Config = ServerConfig, Request = Io<Stream>>; type Factory: actix::ServiceFactory<Config = ServerConfig, Request = Io<Stream>>;
fn create(&self) -> Self::NewService; fn create(&self) -> Self::Factory;
} }
pub(crate) trait InternalServiceFactory: Send { pub(crate) trait InternalServiceFactory: Send {
@ -183,7 +183,7 @@ where
T: actix::ServiceFactory<Config = ServerConfig, Request = Io<I>>, T: actix::ServiceFactory<Config = ServerConfig, Request = Io<I>>,
I: FromStream, I: FromStream,
{ {
type NewService = T; type Factory = T;
fn create(&self) -> T { fn create(&self) -> T {
(self)() (self)()

View File

@ -6,13 +6,13 @@ use tokio_net::tcp::TcpStream;
pub(crate) enum StdListener { pub(crate) enum StdListener {
Tcp(net::TcpListener), Tcp(net::TcpListener),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
Uds(std::os::unix::net::UnixListener), Uds(std::os::unix::net::UnixListener),
} }
pub(crate) enum SocketAddr { pub(crate) enum SocketAddr {
Tcp(net::SocketAddr), Tcp(net::SocketAddr),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
Uds(std::os::unix::net::SocketAddr), Uds(std::os::unix::net::SocketAddr),
} }
@ -20,7 +20,7 @@ impl fmt::Display for SocketAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self { match *self {
SocketAddr::Tcp(ref addr) => write!(f, "{}", addr), SocketAddr::Tcp(ref addr) => write!(f, "{}", addr),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
SocketAddr::Uds(ref addr) => write!(f, "{:?}", addr), SocketAddr::Uds(ref addr) => write!(f, "{:?}", addr),
} }
} }
@ -30,7 +30,7 @@ impl fmt::Debug for SocketAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self { match *self {
SocketAddr::Tcp(ref addr) => write!(f, "{:?}", addr), SocketAddr::Tcp(ref addr) => write!(f, "{:?}", addr),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
SocketAddr::Uds(ref addr) => write!(f, "{:?}", addr), SocketAddr::Uds(ref addr) => write!(f, "{:?}", addr),
} }
} }
@ -40,7 +40,7 @@ impl fmt::Display for StdListener {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self { match *self {
StdListener::Tcp(ref lst) => write!(f, "{}", lst.local_addr().ok().unwrap()), StdListener::Tcp(ref lst) => write!(f, "{}", lst.local_addr().ok().unwrap()),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
StdListener::Uds(ref lst) => write!(f, "{:?}", lst.local_addr().ok().unwrap()), StdListener::Uds(ref lst) => write!(f, "{:?}", lst.local_addr().ok().unwrap()),
} }
} }
@ -50,7 +50,7 @@ impl StdListener {
pub(crate) fn local_addr(&self) -> SocketAddr { pub(crate) fn local_addr(&self) -> SocketAddr {
match self { match self {
StdListener::Tcp(lst) => SocketAddr::Tcp(lst.local_addr().unwrap()), StdListener::Tcp(lst) => SocketAddr::Tcp(lst.local_addr().unwrap()),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
StdListener::Uds(lst) => SocketAddr::Uds(lst.local_addr().unwrap()), StdListener::Uds(lst) => SocketAddr::Uds(lst.local_addr().unwrap()),
} }
} }
@ -61,7 +61,7 @@ impl StdListener {
mio::net::TcpListener::from_std(lst) mio::net::TcpListener::from_std(lst)
.expect("Can not create mio::net::TcpListener"), .expect("Can not create mio::net::TcpListener"),
), ),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
StdListener::Uds(lst) => SocketListener::Uds( StdListener::Uds(lst) => SocketListener::Uds(
mio_uds::UnixListener::from_listener(lst) mio_uds::UnixListener::from_listener(lst)
.expect("Can not create mio_uds::UnixListener"), .expect("Can not create mio_uds::UnixListener"),
@ -73,13 +73,13 @@ impl StdListener {
#[derive(Debug)] #[derive(Debug)]
pub enum StdStream { pub enum StdStream {
Tcp(std::net::TcpStream), Tcp(std::net::TcpStream),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
Uds(std::os::unix::net::UnixStream), Uds(std::os::unix::net::UnixStream),
} }
pub(crate) enum SocketListener { pub(crate) enum SocketListener {
Tcp(mio::net::TcpListener), Tcp(mio::net::TcpListener),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
Uds(mio_uds::UnixListener), Uds(mio_uds::UnixListener),
} }
@ -89,7 +89,7 @@ impl SocketListener {
SocketListener::Tcp(ref lst) => lst SocketListener::Tcp(ref lst) => lst
.accept_std() .accept_std()
.map(|(stream, addr)| Some((StdStream::Tcp(stream), SocketAddr::Tcp(addr)))), .map(|(stream, addr)| Some((StdStream::Tcp(stream), SocketAddr::Tcp(addr)))),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
SocketListener::Uds(ref lst) => lst.accept_std().map(|res| { SocketListener::Uds(ref lst) => lst.accept_std().map(|res| {
res.map(|(stream, addr)| (StdStream::Uds(stream), SocketAddr::Uds(addr))) res.map(|(stream, addr)| (StdStream::Uds(stream), SocketAddr::Uds(addr)))
}), }),
@ -107,7 +107,7 @@ impl mio::Evented for SocketListener {
) -> io::Result<()> { ) -> io::Result<()> {
match *self { match *self {
SocketListener::Tcp(ref lst) => lst.register(poll, token, interest, opts), SocketListener::Tcp(ref lst) => lst.register(poll, token, interest, opts),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
SocketListener::Uds(ref lst) => lst.register(poll, token, interest, opts), SocketListener::Uds(ref lst) => lst.register(poll, token, interest, opts),
} }
} }
@ -121,14 +121,14 @@ impl mio::Evented for SocketListener {
) -> io::Result<()> { ) -> io::Result<()> {
match *self { match *self {
SocketListener::Tcp(ref lst) => lst.reregister(poll, token, interest, opts), SocketListener::Tcp(ref lst) => lst.reregister(poll, token, interest, opts),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
SocketListener::Uds(ref lst) => lst.reregister(poll, token, interest, opts), SocketListener::Uds(ref lst) => lst.reregister(poll, token, interest, opts),
} }
} }
fn deregister(&self, poll: &mio::Poll) -> io::Result<()> { fn deregister(&self, poll: &mio::Poll) -> io::Result<()> {
match *self { match *self {
SocketListener::Tcp(ref lst) => lst.deregister(poll), SocketListener::Tcp(ref lst) => lst.deregister(poll),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
SocketListener::Uds(ref lst) => { SocketListener::Uds(ref lst) => {
let res = lst.deregister(poll); let res = lst.deregister(poll);
@ -152,7 +152,7 @@ impl FromStream for TcpStream {
fn from_stdstream(sock: StdStream) -> io::Result<Self> { fn from_stdstream(sock: StdStream) -> io::Result<Self> {
match sock { match sock {
StdStream::Tcp(stream) => TcpStream::from_std(stream, &Handle::default()), StdStream::Tcp(stream) => TcpStream::from_std(stream, &Handle::default()),
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
StdStream::Uds(_) => { StdStream::Uds(_) => {
panic!("Should not happen, bug in server impl"); panic!("Should not happen, bug in server impl");
} }
@ -160,13 +160,13 @@ impl FromStream for TcpStream {
} }
} }
#[cfg(all(unix, feature = "uds"))] #[cfg(all(unix))]
impl FromStream for tokio_uds::UnixStream { impl FromStream for tokio_net::uds::UnixStream {
fn from_stdstream(sock: StdStream) -> io::Result<Self> { fn from_stdstream(sock: StdStream) -> io::Result<Self> {
match sock { match sock {
StdStream::Tcp(_) => panic!("Should not happen, bug in server impl"), StdStream::Tcp(_) => panic!("Should not happen, bug in server impl"),
StdStream::Uds(stream) => { StdStream::Uds(stream) => {
tokio_uds::UnixStream::from_std(stream, &Handle::default()) tokio_net::uds::UnixStream::from_std(stream, &Handle::default())
} }
} }
} }

View File

@ -4,6 +4,8 @@ use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use std::task::{self, Context, Poll}; use std::task::{self, Context, Poll};
use futures::future::{ready, Ready};
mod and_then; mod and_then;
mod apply; mod apply;
mod apply_cfg; mod apply_cfg;

View File

@ -6,6 +6,18 @@ use std::task::{Context, Poll};
use crate::{IntoServiceFactory, Service, ServiceFactory}; use crate::{IntoServiceFactory, Service, ServiceFactory};
/// Apply transform to a service. Function returns
/// services factory that in initialization creates
/// service and applies transform to this service.
pub fn apply<T, S, U>(t: T, service: U) -> ApplyTransform<T, S>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
U: IntoServiceFactory<S>,
{
ApplyTransform::new(t, service.into_factory())
}
/// The `Transform` trait defines the interface of a Service factory. `Transform` /// The `Transform` trait defines the interface of a Service factory. `Transform`
/// is often implemented for middleware, defining how to construct a /// is often implemented for middleware, defining how to construct a
/// middleware Service. A Service that is constructed by the factory takes /// middleware Service. A Service that is constructed by the factory takes
@ -70,18 +82,6 @@ where
} }
} }
/// Apply transform to a service. Function returns
/// services factory that in initialization creates
/// service and applies transform to this service.
pub fn apply<T, S, U>(t: T, service: U) -> ApplyTransform<T, S>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
U: IntoServiceFactory<S>,
{
ApplyTransform::new(t, service.into_factory())
}
/// `Apply` transform to new service /// `Apply` transform to new service
pub struct ApplyTransform<T, S> { pub struct ApplyTransform<T, S> {
s: Rc<S>, s: Rc<S>,