From 9ffd4c3a49d8467a3b53c78f703a35633da25c6d Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 25 Oct 2018 15:09:23 +0200 Subject: net: Improve IPC interface. - Use our Result, communicate errors when making backends. --- net/src/ipc.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'net') diff --git a/net/src/ipc.rs b/net/src/ipc.rs index 7aaa7e2f..5e8041ac 100644 --- a/net/src/ipc.rs +++ b/net/src/ipc.rs @@ -39,7 +39,6 @@ extern crate fs2; use self::fs2::FileExt; -use failure; use std::fs; use std::io::{self, Read, Write}; use std::net::{SocketAddr, AddrParseError, TcpStream, TcpListener}; @@ -67,6 +66,7 @@ use std::os::unix::io::AsRawFd; use std::thread; use sequoia_core as core; +use super::Result; /// Servers need to implement this trait. pub trait Handler { @@ -78,7 +78,8 @@ pub trait Handler { /// A factory for handlers. pub type HandlerFactory = fn(descriptor: Descriptor, - handle: tokio_core::reactor::Handle) -> Option>; + handle: tokio_core::reactor::Handle) + -> Result>; /// A descriptor is used to connect to a service. #[derive(Clone)] @@ -112,7 +113,7 @@ impl Descriptor { /// Connects to a descriptor, starting the server if necessary. pub fn connect(&self, handle: &tokio_core::reactor::Handle) - -> io::Result> { + -> Result> { self.connect_with_policy(handle, *self.ctx.ipc_policy()) } @@ -122,9 +123,9 @@ impl Descriptor { /// the given one. pub fn connect_with_policy(&self, handle: &tokio_core::reactor::Handle, policy: core::IPCPolicy) - -> io::Result> { + -> Result> { let do_connect = - move |cookie: Cookie, mut s: TcpStream| -> io::Result> { + move |cookie: Cookie, mut s: TcpStream| -> Result> { cookie.send(&mut s)?; /* Tokioize. */ @@ -153,7 +154,8 @@ impl Descriptor { file.read_to_end(&mut c)?; if let Some((cookie, a)) = Cookie::extract(c) { - let addr: Result = String::from_utf8_lossy(&a).parse(); + let addr: ::std::result::Result = + String::from_utf8_lossy(&a).parse(); if addr.is_err() { /* Malformed. Invalidate the cookie and try again. */ file.set_len(0)?; @@ -217,14 +219,14 @@ impl Descriptor { /// Try to create a TCP socket, bind it to a random port on /// localhost. - fn listen(&self) -> io::Result { + fn listen(&self) -> Result { let port = OsRng::new()?.next_u32() as u16; - TcpListener::bind((LOCALHOST, port)) + Ok(TcpListener::bind((LOCALHOST, port))?) } /// Start the service, either as an external process or as a /// thread. - fn start(&self, external: bool) -> io::Result { + fn start(&self, external: bool) -> Result { /* Listen on a random port on localhost. */ let mut listener = self.listen(); while listener.is_err() { @@ -243,7 +245,7 @@ impl Descriptor { Ok(addr) } - fn fork(&self, l: TcpListener) -> io::Result<()> { + fn fork(&self, l: TcpListener) -> Result<()> { // Convert to raw fd, then forget l so that it will not be // closed when it is dropped. let fd = l.as_raw_fd(); @@ -262,9 +264,9 @@ impl Descriptor { Ok(()) } - fn spawn(&self, l: TcpListener) -> io::Result<()> { + fn spawn(&self, l: TcpListener) -> Result<()> { let descriptor = self.clone(); - thread::spawn(move || -> io::Result<()> { + thread::spawn(move || -> Result<()> { Ok(Server::new(descriptor) .expect("Failed to spawn server") // XXX .serve_listener(l) @@ -282,7 +284,7 @@ pub struct Server { impl Server { /// Creates a new server for the descriptor. - pub fn new(descriptor: Descriptor) -> io::Result { + pub fn new(descriptor: Descriptor) -> Result { Ok(Server { core: tokio_core::reactor::Core::new()?, descriptor: descriptor, @@ -290,7 +292,7 @@ impl Server { } /// Creates a Context from `env::args()`. - pub fn context() -> Result { + pub fn context() -> Result { use std::env::args; let args: Vec = args().collect(); @@ -341,20 +343,18 @@ impl Server { /// .expect("Failed to start server"); /// } /// ``` - pub fn serve(&mut self) -> io::Result<()> { + pub fn serve(&mut self) -> Result<()> { self.serve_listener(unsafe { TcpListener::from_raw_fd(0) }) } - fn serve_listener(&mut self, l: TcpListener) -> io::Result<()> { + fn serve_listener(&mut self, l: TcpListener) -> Result<()> { /* The first client tells us our cookie. */ let mut i = l.accept()?; let cookie = Cookie::receive(&mut i.0)?; /* XXX: It'd be nice to recycle this connection. */ drop(i); - let handler = (self.descriptor.factory)(self.descriptor.clone(), self.core.handle()) - .ok_or( - io::Error::new(io::ErrorKind::BrokenPipe, "Failed to start server"))?; + let handler = (self.descriptor.factory)(self.descriptor.clone(), self.core.handle())?; /* Tokioize. */ let handle = self.core.handle(); @@ -382,7 +382,7 @@ impl Server { Ok(()) }); - self.core.run(done) + Ok(self.core.run(done)?) } } @@ -397,7 +397,7 @@ const COOKIE_SIZE: usize = 32; impl Cookie { /// Make a new cookie. - fn new() -> io::Result { + fn new() -> Result { let mut c = vec![0; COOKIE_SIZE]; OsRng::new()?.fill_bytes(&mut c); Ok(Cookie(c)) @@ -426,7 +426,7 @@ impl Cookie { } /// Read a cookie from 'from'. - fn receive(from: &mut R) -> io::Result { + fn receive(from: &mut R) -> Result { let mut buf = vec![0; COOKIE_SIZE]; from.read_exact(&mut buf)?; Ok(Cookie(buf)) -- cgit v1.2.3