2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-11-07 09:59:02 -05:00
|
|
|
use crate::deno_error::bad_resource;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-21 20:42:48 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::resources;
|
2019-11-07 11:11:15 -05:00
|
|
|
use crate::resources::CliResource;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::resources::Resource;
|
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
2019-11-07 09:59:02 -05:00
|
|
|
use futures::Async;
|
2019-08-14 11:03:02 -04:00
|
|
|
use futures::Future;
|
2019-11-07 09:59:02 -05:00
|
|
|
use futures::Poll;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std;
|
2019-08-21 20:42:48 -04:00
|
|
|
use std::convert::From;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::net::Shutdown;
|
2019-11-07 09:59:02 -05:00
|
|
|
use std::net::SocketAddr;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio;
|
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("accept", s.core_op(json_op(s.stateful_op(op_accept))));
|
|
|
|
i.register_op("dial", s.core_op(json_op(s.stateful_op(op_dial))));
|
|
|
|
i.register_op("shutdown", s.core_op(json_op(s.stateful_op(op_shutdown))));
|
|
|
|
i.register_op("listen", s.core_op(json_op(s.stateful_op(op_listen))));
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum AcceptState {
|
|
|
|
Eager,
|
|
|
|
Pending,
|
|
|
|
Done,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simply accepts a connection.
|
|
|
|
pub fn accept(rid: ResourceId) -> Accept {
|
|
|
|
Accept {
|
|
|
|
state: AcceptState::Eager,
|
|
|
|
rid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A future representing state of accepting a TCP connection.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Accept {
|
|
|
|
state: AcceptState,
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Accept {
|
|
|
|
type Item = (TcpStream, SocketAddr);
|
|
|
|
type Error = ErrBox;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if self.state == AcceptState::Done {
|
|
|
|
panic!("poll Accept after it's done");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut table = resources::lock_resource_table();
|
|
|
|
let listener_resource = table
|
|
|
|
.get_mut::<TcpListenerResource>(self.rid)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Listener has been closed",
|
|
|
|
);
|
|
|
|
ErrBox::from(e)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let listener = &mut listener_resource.listener;
|
|
|
|
|
|
|
|
if self.state == AcceptState::Eager {
|
|
|
|
// Similar to try_ready!, but also track/untrack accept task
|
|
|
|
// in TcpListener resource.
|
|
|
|
// In this way, when the listener is closed, the task can be
|
|
|
|
// notified to error out (instead of stuck forever).
|
|
|
|
match listener.poll_accept().map_err(ErrBox::from) {
|
|
|
|
Ok(Async::Ready((stream, addr))) => {
|
|
|
|
self.state = AcceptState::Done;
|
|
|
|
return Ok((stream, addr).into());
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
self.state = AcceptState::Pending;
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.state = AcceptState::Done;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match listener.poll_accept().map_err(ErrBox::from) {
|
|
|
|
Ok(Async::Ready((stream, addr))) => {
|
|
|
|
listener_resource.untrack_task();
|
|
|
|
self.state = AcceptState::Done;
|
|
|
|
Ok((stream, addr).into())
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
listener_resource.track_task()?;
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
listener_resource.untrack_task();
|
|
|
|
self.state = AcceptState::Done;
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct AcceptArgs {
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_accept(
|
2019-08-14 11:03:02 -04:00
|
|
|
_state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: AcceptArgs = serde_json::from_value(args)?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
|
|
|
let table = resources::lock_resource_table();
|
|
|
|
table
|
|
|
|
.get::<TcpListenerResource>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
let op = accept(rid)
|
2019-09-10 00:59:40 -04:00
|
|
|
.and_then(move |(tcp_stream, _socket_addr)| {
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2019-11-07 11:11:15 -05:00
|
|
|
let rid = resources::add_tcp_stream(tcp_stream);
|
|
|
|
Ok((rid, local_addr, remote_addr))
|
2019-09-10 00:59:40 -04:00
|
|
|
})
|
|
|
|
.map_err(ErrBox::from)
|
2019-11-07 11:11:15 -05:00
|
|
|
.and_then(move |(rid, local_addr, remote_addr)| {
|
2019-09-10 00:59:40 -04:00
|
|
|
futures::future::ok(json!({
|
2019-11-07 11:11:15 -05:00
|
|
|
"rid": rid,
|
2019-09-10 00:59:40 -04:00
|
|
|
"localAddr": local_addr.to_string(),
|
|
|
|
"remoteAddr": remote_addr.to_string(),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(JsonOp::Async(Box::new(op)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DialArgs {
|
2019-09-20 18:32:18 -04:00
|
|
|
transport: String,
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_dial(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: DialArgs = serde_json::from_value(args)?;
|
2019-09-20 18:32:18 -04:00
|
|
|
assert_eq!(args.transport, "tcp"); // TODO Support others.
|
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
state.check_net(&args.hostname, args.port)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
let op = resolve_addr(&args.hostname, args.port).and_then(move |addr| {
|
2019-08-27 11:35:32 -04:00
|
|
|
TcpStream::connect(&addr)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |tcp_stream| {
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2019-11-07 11:11:15 -05:00
|
|
|
let rid = resources::add_tcp_stream(tcp_stream);
|
|
|
|
Ok((rid, local_addr, remote_addr))
|
2019-08-27 11:35:32 -04:00
|
|
|
})
|
|
|
|
.map_err(ErrBox::from)
|
2019-11-07 11:11:15 -05:00
|
|
|
.and_then(move |(rid, local_addr, remote_addr)| {
|
2019-08-26 08:50:21 -04:00
|
|
|
futures::future::ok(json!({
|
2019-11-07 11:11:15 -05:00
|
|
|
"rid": rid,
|
2019-08-27 11:35:32 -04:00
|
|
|
"localAddr": local_addr.to_string(),
|
|
|
|
"remoteAddr": remote_addr.to_string(),
|
2019-08-26 08:50:21 -04:00
|
|
|
}))
|
2019-08-27 11:35:32 -04:00
|
|
|
})
|
2019-08-14 11:03:02 -04:00
|
|
|
});
|
2019-08-26 08:50:21 -04:00
|
|
|
|
|
|
|
Ok(JsonOp::Async(Box::new(op)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ShutdownArgs {
|
|
|
|
rid: i32,
|
|
|
|
how: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_shutdown(
|
2019-08-14 11:03:02 -04:00
|
|
|
_state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ShutdownArgs = serde_json::from_value(args)?;
|
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
let rid = args.rid as u32;
|
2019-08-26 08:50:21 -04:00
|
|
|
let how = args.how;
|
2019-09-10 00:59:40 -04:00
|
|
|
|
|
|
|
let shutdown_mode = match how {
|
|
|
|
0 => Shutdown::Read,
|
|
|
|
1 => Shutdown::Write,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
2019-11-07 11:11:15 -05:00
|
|
|
let mut table = resources::lock_resource_table();
|
|
|
|
let resource = table.get_mut::<CliResource>(rid).ok_or_else(bad_resource)?;
|
|
|
|
match resource {
|
|
|
|
CliResource::TcpStream(ref mut stream) => {
|
|
|
|
TcpStream::shutdown(stream, shutdown_mode).map_err(ErrBox::from)?;
|
|
|
|
}
|
|
|
|
_ => return Err(bad_resource()),
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ListenArgs {
|
2019-09-20 18:32:18 -04:00
|
|
|
transport: String,
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
struct TcpListenerResource {
|
|
|
|
listener: tokio::net::TcpListener,
|
|
|
|
task: Option<futures::task::Task>,
|
|
|
|
local_addr: SocketAddr,
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:11:15 -05:00
|
|
|
impl Resource for TcpListenerResource {}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
|
|
|
impl Drop for TcpListenerResource {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.notify_task();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpListenerResource {
|
|
|
|
/// Track the current task so future awaiting for connection
|
|
|
|
/// can be notified when listener is closed.
|
|
|
|
///
|
|
|
|
/// Throws an error if another task is already tracked.
|
|
|
|
pub fn track_task(&mut self) -> Result<(), ErrBox> {
|
|
|
|
// Currently, we only allow tracking a single accept task for a listener.
|
|
|
|
// This might be changed in the future with multiple workers.
|
|
|
|
// Caveat: TcpListener by itself also only tracks an accept task at a time.
|
|
|
|
// See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883
|
|
|
|
if self.task.is_some() {
|
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Another accept task is ongoing",
|
|
|
|
);
|
|
|
|
return Err(ErrBox::from(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.task.replace(futures::task::current());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notifies a task when listener is closed so accept future can resolve.
|
|
|
|
pub fn notify_task(&mut self) {
|
|
|
|
if let Some(task) = self.task.take() {
|
|
|
|
task.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop tracking a task.
|
|
|
|
/// Happens when the task is done and thus no further tracking is needed.
|
|
|
|
pub fn untrack_task(&mut self) {
|
|
|
|
if self.task.is_some() {
|
|
|
|
self.task.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_listen(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ListenArgs = serde_json::from_value(args)?;
|
2019-09-20 18:32:18 -04:00
|
|
|
assert_eq!(args.transport, "tcp");
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
state.check_net(&args.hostname, args.port)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port).wait()?;
|
2019-08-14 11:03:02 -04:00
|
|
|
let listener = TcpListener::bind(&addr)?;
|
2019-08-27 11:35:32 -04:00
|
|
|
let local_addr = listener.local_addr()?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let local_addr_str = local_addr.to_string();
|
|
|
|
let listener_resource = TcpListenerResource {
|
|
|
|
listener,
|
|
|
|
task: None,
|
|
|
|
local_addr,
|
|
|
|
};
|
|
|
|
let mut table = resources::lock_resource_table();
|
|
|
|
let rid = table.add("tcpListener", Box::new(listener_resource));
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-27 11:35:32 -04:00
|
|
|
Ok(JsonOp::Sync(json!({
|
2019-11-07 09:59:02 -05:00
|
|
|
"rid": rid,
|
|
|
|
"localAddr": local_addr_str,
|
2019-08-27 11:35:32 -04:00
|
|
|
})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|