diff options
author | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-03-17 16:34:06 +0100 |
---|---|---|
committer | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-03-19 14:39:58 +0100 |
commit | 6c5df8aa3d4690d63b5edfaba258e8c1d0ccdc43 (patch) | |
tree | ae64812649ae2db3f5fd282e7e39bb98a8e8ab2a /ipc/src | |
parent | 07bd66ad4d5f1e0732c52997e4d6b6d92144d84c (diff) |
ipc: Fully receive cookie in async fn
Diffstat (limited to 'ipc/src')
-rw-r--r-- | ipc/src/lib.rs | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/ipc/src/lib.rs b/ipc/src/lib.rs index 95ef7325..6536f4b5 100644 --- a/ipc/src/lib.rs +++ b/ipc/src/lib.rs @@ -46,7 +46,7 @@ use fs2::FileExt; use futures::{Future, Stream}; use tokio_core::net; -use tokio_io::io::{ReadHalf, ReadExact}; +use tokio_io::io::ReadHalf; use tokio_io::AsyncRead; use capnp_rpc::{RpcSystem, twoparty}; @@ -346,8 +346,8 @@ impl Server { let done = socket.incoming().and_then(|(socket, _addr)| { let _ = socket.set_nodelay(true); Cookie::receive_async(socket) - }).and_then(|(socket, buf)| { - if Cookie::from(&buf).map(|c| c == cookie).unwrap_or(false) { + }).and_then(|(socket, received_cookie)| { + if received_cookie == cookie { Ok(socket) } else { Err(io::Error::new(io::ErrorKind::BrokenPipe, "Bad cookie.")) @@ -414,10 +414,13 @@ impl Cookie { } /// Asynchronously read a cookie from 'socket'. - fn receive_async(socket: net::TcpStream) -> ReadExact<net::TcpStream, - Vec<u8>> { + fn receive_async(socket: net::TcpStream) + -> impl Future<Item = (net::TcpStream, Cookie), Error = io::Error> { let buf = vec![0; Cookie::SIZE]; tokio_io::io::read_exact(socket, buf) + .and_then(|(socket, buf)| { + Ok((socket, Cookie::from(&buf).expect("enough bytes read"))) + }) } |