0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: rewrite ext/net/ ops to op2 (#20471)

This commit is contained in:
Bartek Iwańczuk 2023-09-12 15:39:21 +02:00 committed by GitHub
parent bdf1850679
commit 08d2a32060
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 77 deletions

View file

@ -9,6 +9,7 @@ use deno_core::error::custom_error;
use deno_core::error::generic_error; use deno_core::error::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::CancelFuture; use deno_core::CancelFuture;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
@ -76,10 +77,11 @@ pub(crate) fn accept_err(e: std::io::Error) -> AnyError {
} }
} }
#[op] #[op2(async)]
async fn op_net_accept_tcp( #[serde]
pub async fn op_net_accept_tcp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> { ) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -105,11 +107,12 @@ async fn op_net_accept_tcp(
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr))) Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
} }
#[op] #[op2(async)]
async fn op_net_recv_udp( #[serde]
pub async fn op_net_recv_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
mut buf: JsBuffer, #[buffer] mut buf: JsBuffer,
) -> Result<(usize, IpAddr), AnyError> { ) -> Result<(usize, IpAddr), AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -158,12 +161,12 @@ where
Ok(nwritten) Ok(nwritten)
} }
#[op] #[op2(async)]
async fn op_net_join_multi_v4_udp( pub async fn op_net_join_multi_v4_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
address: String, #[string] address: String,
multi_interface: String, #[string] multi_interface: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -180,12 +183,12 @@ async fn op_net_join_multi_v4_udp(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
async fn op_net_join_multi_v6_udp( pub async fn op_net_join_multi_v6_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
address: String, #[string] address: String,
multi_interface: u32, #[smi] multi_interface: u32,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -201,12 +204,12 @@ async fn op_net_join_multi_v6_udp(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
async fn op_net_leave_multi_v4_udp( pub async fn op_net_leave_multi_v4_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
address: String, #[string] address: String,
multi_interface: String, #[string] multi_interface: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -223,12 +226,12 @@ async fn op_net_leave_multi_v4_udp(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
async fn op_net_leave_multi_v6_udp( pub async fn op_net_leave_multi_v6_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
address: String, #[string] address: String,
multi_interface: u32, #[smi] multi_interface: u32,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -244,10 +247,10 @@ async fn op_net_leave_multi_v6_udp(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
async fn op_net_set_multi_loopback_udp( pub async fn op_net_set_multi_loopback_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
is_v4_membership: bool, is_v4_membership: bool,
loopback: bool, loopback: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
@ -267,11 +270,11 @@ async fn op_net_set_multi_loopback_udp(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
async fn op_net_set_multi_ttl_udp( pub async fn op_net_set_multi_ttl_udp(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
ttl: u32, #[smi] ttl: u32,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -285,10 +288,11 @@ async fn op_net_set_multi_ttl_udp(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_net_connect_tcp<NP>( pub async fn op_net_connect_tcp<NP>(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
addr: IpAddr, #[serde] addr: IpAddr,
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr, IpAddr), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -346,10 +350,11 @@ impl Resource for UdpSocketResource {
} }
} }
#[op] #[op2]
fn op_net_listen_tcp<NP>( #[serde]
pub fn op_net_listen_tcp<NP>(
state: &mut OpState, state: &mut OpState,
addr: IpAddr, #[serde] addr: IpAddr,
reuse_port: bool, reuse_port: bool,
) -> Result<(ResourceId, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr), AnyError>
where where
@ -455,10 +460,11 @@ where
Ok((rid, IpAddr::from(local_addr))) Ok((rid, IpAddr::from(local_addr)))
} }
#[op] #[op2]
fn op_net_listen_udp<NP>( #[serde]
pub fn op_net_listen_udp<NP>(
state: &mut OpState, state: &mut OpState,
addr: IpAddr, #[serde] addr: IpAddr,
reuse_address: bool, reuse_address: bool,
loopback: bool, loopback: bool,
) -> Result<(ResourceId, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr), AnyError>
@ -469,10 +475,11 @@ where
net_listen_udp::<NP>(state, addr, reuse_address, loopback) net_listen_udp::<NP>(state, addr, reuse_address, loopback)
} }
#[op] #[op2]
fn op_node_unstable_net_listen_udp<NP>( #[serde]
pub fn op_node_unstable_net_listen_udp<NP>(
state: &mut OpState, state: &mut OpState,
addr: IpAddr, #[serde] addr: IpAddr,
reuse_address: bool, reuse_address: bool,
loopback: bool, loopback: bool,
) -> Result<(ResourceId, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr), AnyError>
@ -553,10 +560,11 @@ pub struct NameServer {
port: u16, port: u16,
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_dns_resolve<NP>( pub async fn op_dns_resolve<NP>(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
args: ResolveAddrArgs, #[serde] args: ResolveAddrArgs,
) -> Result<Vec<DnsReturnRecord>, AnyError> ) -> Result<Vec<DnsReturnRecord>, AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -640,10 +648,10 @@ where
.collect::<Result<Vec<DnsReturnRecord>, AnyError>>() .collect::<Result<Vec<DnsReturnRecord>, AnyError>>()
} }
#[op] #[op2(fast)]
pub fn op_set_nodelay( pub fn op_set_nodelay(
state: &mut OpState, state: &mut OpState,
rid: ResourceId, #[smi] rid: ResourceId,
nodelay: bool, nodelay: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource: Rc<TcpStreamResource> = let resource: Rc<TcpStreamResource> =
@ -651,10 +659,10 @@ pub fn op_set_nodelay(
resource.set_nodelay(nodelay) resource.set_nodelay(nodelay)
} }
#[op] #[op2(fast)]
pub fn op_set_keepalive( pub fn op_set_keepalive(
state: &mut OpState, state: &mut OpState,
rid: ResourceId, #[smi] rid: ResourceId,
keepalive: bool, keepalive: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource: Rc<TcpStreamResource> = let resource: Rc<TcpStreamResource> =

View file

@ -23,7 +23,7 @@ use deno_core::futures::task::Poll;
use deno_core::futures::task::RawWaker; use deno_core::futures::task::RawWaker;
use deno_core::futures::task::RawWakerVTable; use deno_core::futures::task::RawWakerVTable;
use deno_core::futures::task::Waker; use deno_core::futures::task::Waker;
use deno_core::op; use deno_core::op2;
use deno_core::parking_lot::Mutex; use deno_core::parking_lot::Mutex;
use deno_core::unsync::spawn; use deno_core::unsync::spawn;
@ -779,10 +779,11 @@ pub struct StartTlsArgs {
alpn_protocols: Option<Vec<String>>, alpn_protocols: Option<Vec<String>>,
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_tls_start<NP>( pub async fn op_tls_start<NP>(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
args: StartTlsArgs, #[serde] args: StartTlsArgs,
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr, IpAddr), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -860,11 +861,12 @@ where
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr))) Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_net_connect_tls<NP>( pub async fn op_net_connect_tls<NP>(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
addr: IpAddr, #[serde] addr: IpAddr,
args: ConnectTlsArgs, #[serde] args: ConnectTlsArgs,
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr, IpAddr), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -1000,11 +1002,12 @@ pub struct ListenTlsArgs {
reuse_port: bool, reuse_port: bool,
} }
#[op] #[op2]
#[serde]
pub fn op_net_listen_tls<NP>( pub fn op_net_listen_tls<NP>(
state: &mut OpState, state: &mut OpState,
addr: IpAddr, #[serde] addr: IpAddr,
args: ListenTlsArgs, #[serde] args: ListenTlsArgs,
) -> Result<(ResourceId, IpAddr), AnyError> ) -> Result<(ResourceId, IpAddr), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -1101,10 +1104,11 @@ where
Ok((rid, IpAddr::from(local_addr))) Ok((rid, IpAddr::from(local_addr)))
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_net_accept_tls( pub async fn op_net_accept_tls(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> { ) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -1142,10 +1146,11 @@ pub async fn op_net_accept_tls(
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr))) Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_tls_handshake( pub async fn op_tls_handshake(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<TlsHandshakeInfo, AnyError> { ) -> Result<TlsHandshakeInfo, AnyError> {
let resource = state let resource = state
.borrow() .borrow()

View file

@ -6,6 +6,7 @@ use deno_core::error::bad_resource;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
@ -72,10 +73,11 @@ pub struct UnixListenArgs {
pub path: String, pub path: String,
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_net_accept_unix( pub async fn op_net_accept_unix(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<(ResourceId, Option<String>, Option<String>), AnyError> { ) -> Result<(ResourceId, Option<String>, Option<String>), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -103,10 +105,11 @@ pub async fn op_net_accept_unix(
Ok((rid, local_addr_path, remote_addr_path)) Ok((rid, local_addr_path, remote_addr_path))
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_net_connect_unix<NP>( pub async fn op_net_connect_unix<NP>(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
path: String, #[string] path: String,
) -> Result<(ResourceId, Option<String>, Option<String>), AnyError> ) -> Result<(ResourceId, Option<String>, Option<String>), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -134,11 +137,12 @@ where
Ok((rid, local_addr_path, remote_addr_path)) Ok((rid, local_addr_path, remote_addr_path))
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_net_recv_unixpacket( pub async fn op_net_recv_unixpacket(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
mut buf: JsBuffer, #[buffer] mut buf: JsBuffer,
) -> Result<(usize, Option<String>), AnyError> { ) -> Result<(usize, Option<String>), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -185,10 +189,11 @@ where
Ok(nwritten) Ok(nwritten)
} }
#[op] #[op2]
#[serde]
pub fn op_net_listen_unix<NP>( pub fn op_net_listen_unix<NP>(
state: &mut OpState, state: &mut OpState,
path: String, #[string] path: String,
) -> Result<(ResourceId, Option<String>), AnyError> ) -> Result<(ResourceId, Option<String>), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -231,10 +236,11 @@ where
Ok((rid, pathname)) Ok((rid, pathname))
} }
#[op] #[op2]
#[serde]
pub fn op_net_listen_unixpacket<NP>( pub fn op_net_listen_unixpacket<NP>(
state: &mut OpState, state: &mut OpState,
path: String, #[string] path: String,
) -> Result<(ResourceId, Option<String>), AnyError> ) -> Result<(ResourceId, Option<String>), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,
@ -243,10 +249,11 @@ where
net_listen_unixpacket::<NP>(state, path) net_listen_unixpacket::<NP>(state, path)
} }
#[op] #[op2]
#[serde]
pub fn op_node_unstable_net_listen_unixpacket<NP>( pub fn op_node_unstable_net_listen_unixpacket<NP>(
state: &mut OpState, state: &mut OpState,
path: String, #[string] path: String,
) -> Result<(ResourceId, Option<String>), AnyError> ) -> Result<(ResourceId, Option<String>), AnyError>
where where
NP: NetPermissions + 'static, NP: NetPermissions + 'static,