diff options
Diffstat (limited to 'runtime/ops/io.rs')
-rw-r--r-- | runtime/ops/io.rs | 129 |
1 files changed, 3 insertions, 126 deletions
diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index 18279c0eb..e18846466 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::ops::tls; use deno_core::error::null_opbuf; use deno_core::error::resource_unavailable; use deno_core::error::AnyError; @@ -17,6 +16,9 @@ use deno_core::RcRef; use deno_core::Resource; use deno_core::ResourceId; use deno_core::ZeroCopyBuf; +use deno_net::io::TcpStreamResource; +use deno_net::io::TlsStreamResource; +use deno_net::io::UnixStreamResource; use std::borrow::Cow; use std::cell::RefCell; use std::io::Read; @@ -26,13 +28,10 @@ use tokio::io::AsyncRead; use tokio::io::AsyncReadExt; use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; -use tokio::net::tcp; use tokio::process; #[cfg(unix)] use std::os::unix::io::FromRawFd; -#[cfg(unix)] -use tokio::net::unix; #[cfg(windows)] use std::os::windows::io::FromRawHandle; @@ -238,70 +237,6 @@ where } } -/// A full duplex resource has a read and write ends that are completely -/// independent, like TCP/Unix sockets and TLS streams. -#[derive(Debug)] -pub struct FullDuplexResource<R, W> { - rd: AsyncRefCell<R>, - wr: AsyncRefCell<W>, - // When a full-duplex resource is closed, all pending 'read' ops are - // canceled, while 'write' ops are allowed to complete. Therefore only - // 'read' futures should be attached to this cancel handle. - cancel_handle: CancelHandle, -} - -impl<R, W> FullDuplexResource<R, W> -where - R: AsyncRead + Unpin + 'static, - W: AsyncWrite + Unpin + 'static, -{ - pub fn new((rd, wr): (R, W)) -> Self { - Self { - rd: rd.into(), - wr: wr.into(), - cancel_handle: Default::default(), - } - } - - pub fn into_inner(self) -> (R, W) { - (self.rd.into_inner(), self.wr.into_inner()) - } - - pub fn rd_borrow_mut(self: &Rc<Self>) -> AsyncMutFuture<R> { - RcRef::map(self, |r| &r.rd).borrow_mut() - } - - pub fn wr_borrow_mut(self: &Rc<Self>) -> AsyncMutFuture<W> { - RcRef::map(self, |r| &r.wr).borrow_mut() - } - - pub fn cancel_handle(self: &Rc<Self>) -> RcRef<CancelHandle> { - RcRef::map(self, |r| &r.cancel_handle) - } - - pub fn cancel_read_ops(&self) { - self.cancel_handle.cancel() - } - - async fn read(self: &Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> { - let mut rd = self.rd_borrow_mut().await; - let nread = rd.read(buf).try_or_cancel(self.cancel_handle()).await?; - Ok(nread) - } - - async fn write(self: &Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> { - let mut wr = self.wr_borrow_mut().await; - let nwritten = wr.write(buf).await?; - Ok(nwritten) - } - - async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> { - let mut wr = self.wr_borrow_mut().await; - wr.shutdown().await?; - Ok(()) - } -} - pub type ChildStdinResource = WriteOnlyResource<process::ChildStdin>; impl Resource for ChildStdinResource { @@ -334,64 +269,6 @@ impl Resource for ChildStderrResource { } } -pub type TcpStreamResource = - FullDuplexResource<tcp::OwnedReadHalf, tcp::OwnedWriteHalf>; - -impl Resource for TcpStreamResource { - fn name(&self) -> Cow<str> { - "tcpStream".into() - } - - fn close(self: Rc<Self>) { - self.cancel_read_ops(); - } -} - -pub type TlsStreamResource = FullDuplexResource<tls::ReadHalf, tls::WriteHalf>; - -impl Resource for TlsStreamResource { - fn name(&self) -> Cow<str> { - "tlsStream".into() - } - - fn close(self: Rc<Self>) { - self.cancel_read_ops(); - } -} - -#[cfg(unix)] -pub type UnixStreamResource = - FullDuplexResource<unix::OwnedReadHalf, unix::OwnedWriteHalf>; - -#[cfg(not(unix))] -struct UnixStreamResource; - -#[cfg(not(unix))] -impl UnixStreamResource { - async fn read(self: &Rc<Self>, _buf: &mut [u8]) -> Result<usize, AnyError> { - unreachable!() - } - async fn write(self: &Rc<Self>, _buf: &[u8]) -> Result<usize, AnyError> { - unreachable!() - } - async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> { - unreachable!() - } - fn cancel_read_ops(&self) { - unreachable!() - } -} - -impl Resource for UnixStreamResource { - fn name(&self) -> Cow<str> { - "unixStream".into() - } - - fn close(self: Rc<Self>) { - self.cancel_read_ops(); - } -} - #[derive(Debug, Default)] pub struct StdFileResource { pub fs_file: |