diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-11-09 19:26:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-09 19:26:17 +0100 |
commit | 375ce63c6390cf7710210ce22f14a2b5a02cbfc3 (patch) | |
tree | 85100876e5e0b50514385ae3c7ce08493c82b38b /ext/net/io.rs | |
parent | 1eae6c139ee1dac28df57d67d993792b773fa1ff (diff) |
feat(core): streams (#12596)
This allows resources to be "streams" by implementing read/write/shutdown. These streams are implicit since their nature (read/write/duplex) isn't known until called, but we could easily add another method to explicitly tag resources as streams.
`op_read/op_write/op_shutdown` are now builtin ops provided by `deno_core`
Note: this current implementation is simple & straightforward but it results in an additional alloc per read/write call
Closes #12556
Diffstat (limited to 'ext/net/io.rs')
-rw-r--r-- | ext/net/io.rs | 121 |
1 files changed, 43 insertions, 78 deletions
diff --git a/ext/net/io.rs b/ext/net/io.rs index 6cefbde2d..2b7aec446 100644 --- a/ext/net/io.rs +++ b/ext/net/io.rs @@ -1,21 +1,15 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::ops_tls::TlsStreamResource; -use deno_core::error::not_supported; use deno_core::error::AnyError; -use deno_core::op_async; use deno_core::AsyncMutFuture; use deno_core::AsyncRefCell; +use deno_core::AsyncResult; use deno_core::CancelHandle; use deno_core::CancelTryFuture; -use deno_core::OpPair; -use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; -use deno_core::ResourceId; use deno_core::ZeroCopyBuf; use std::borrow::Cow; -use std::cell::RefCell; use std::rc::Rc; use tokio::io::AsyncRead; use tokio::io::AsyncReadExt; @@ -26,14 +20,6 @@ use tokio::net::tcp; #[cfg(unix)] use tokio::net::unix; -pub fn init() -> Vec<OpPair> { - vec![ - ("op_net_read_async", op_async(op_read_async)), - ("op_net_write_async", op_async(op_write_async)), - ("op_net_shutdown", op_async(op_shutdown)), - ] -} - /// A full duplex resource has a read and write ends that are completely /// independent, like TCP/Unix sockets and TLS streams. #[derive(Debug)] @@ -80,21 +66,27 @@ where } pub async fn read( - self: &Rc<Self>, - buf: &mut [u8], + self: Rc<Self>, + mut buf: ZeroCopyBuf, ) -> Result<usize, AnyError> { let mut rd = self.rd_borrow_mut().await; - let nread = rd.read(buf).try_or_cancel(self.cancel_handle()).await?; + let nread = rd + .read(&mut buf) + .try_or_cancel(self.cancel_handle()) + .await?; Ok(nread) } - pub async fn write(self: &Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> { + pub async fn write( + self: Rc<Self>, + buf: ZeroCopyBuf, + ) -> Result<usize, AnyError> { let mut wr = self.wr_borrow_mut().await; - let nwritten = wr.write(buf).await?; + let nwritten = wr.write(&buf).await?; Ok(nwritten) } - pub async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> { + pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> { let mut wr = self.wr_borrow_mut().await; wr.shutdown().await?; Ok(()) @@ -109,6 +101,18 @@ impl Resource for TcpStreamResource { "tcpStream".into() } + fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(self.read(buf)) + } + + fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(self.write(buf)) + } + + fn shutdown(self: Rc<Self>) -> AsyncResult<()> { + Box::pin(self.shutdown()) + } + fn close(self: Rc<Self>) { self.cancel_read_ops(); } @@ -124,15 +128,18 @@ pub struct UnixStreamResource; #[cfg(not(unix))] impl UnixStreamResource { pub async fn read( - self: &Rc<Self>, - _buf: &mut [u8], + self: Rc<Self>, + _buf: ZeroCopyBuf, ) -> Result<usize, AnyError> { unreachable!() } - pub async fn write(self: &Rc<Self>, _buf: &[u8]) -> Result<usize, AnyError> { + pub async fn write( + self: Rc<Self>, + _buf: ZeroCopyBuf, + ) -> Result<usize, AnyError> { unreachable!() } - pub async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> { + pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> { unreachable!() } pub fn cancel_read_ops(&self) { @@ -145,61 +152,19 @@ impl Resource for UnixStreamResource { "unixStream".into() } - fn close(self: Rc<Self>) { - self.cancel_read_ops(); + fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(self.read(buf)) } -} -async fn op_read_async( - state: Rc<RefCell<OpState>>, - rid: ResourceId, - mut buf: ZeroCopyBuf, -) -> Result<u32, AnyError> { - let resource = state.borrow().resource_table.get_any(rid)?; - let nread = if let Some(s) = resource.downcast_rc::<TcpStreamResource>() { - s.read(&mut buf).await? - } else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() { - s.read(&mut buf).await? - } else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() { - s.read(&mut buf).await? - } else { - return Err(not_supported()); - }; - Ok(nread as u32) -} + fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(self.write(buf)) + } -async fn op_write_async( - state: Rc<RefCell<OpState>>, - rid: ResourceId, - buf: ZeroCopyBuf, -) -> Result<u32, AnyError> { - let resource = state.borrow().resource_table.get_any(rid)?; - let nwritten = if let Some(s) = resource.downcast_rc::<TcpStreamResource>() { - s.write(&buf).await? - } else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() { - s.write(&buf).await? - } else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() { - s.write(&buf).await? - } else { - return Err(not_supported()); - }; - Ok(nwritten as u32) -} + fn shutdown(self: Rc<Self>) -> AsyncResult<()> { + Box::pin(self.shutdown()) + } -async fn op_shutdown( - state: Rc<RefCell<OpState>>, - rid: ResourceId, - _: (), -) -> Result<(), AnyError> { - let resource = state.borrow().resource_table.get_any(rid)?; - if let Some(s) = resource.downcast_rc::<TcpStreamResource>() { - s.shutdown().await?; - } else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() { - s.shutdown().await?; - } else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() { - s.shutdown().await?; - } else { - return Err(not_supported()); - } - Ok(()) + fn close(self: Rc<Self>) { + self.cancel_read_ops(); + } } |