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-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;
|
|
|
|
use crate::resources::Resource;
|
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use crate::tokio_util;
|
|
|
|
use deno::*;
|
|
|
|
use futures::Future;
|
|
|
|
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;
|
|
|
|
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-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)?;
|
|
|
|
let server_rid = args.rid as u32;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
let server_resource = resources::lookup(server_rid)?;
|
|
|
|
let op = tokio_util::accept(server_resource)
|
|
|
|
.and_then(move |(tcp_stream, _socket_addr)| {
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
|
|
|
let tcp_stream_resource = resources::add_tcp_stream(tcp_stream);
|
|
|
|
Ok((tcp_stream_resource, local_addr, remote_addr))
|
|
|
|
})
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |(tcp_stream_resource, local_addr, remote_addr)| {
|
|
|
|
futures::future::ok(json!({
|
|
|
|
"rid": tcp_stream_resource.rid,
|
|
|
|
"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-08-26 08:50:21 -04:00
|
|
|
let tcp_stream_resource = resources::add_tcp_stream(tcp_stream);
|
2019-08-27 11:35:32 -04:00
|
|
|
Ok((tcp_stream_resource, local_addr, remote_addr))
|
|
|
|
})
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |(tcp_stream_resource, local_addr, remote_addr)| {
|
2019-08-26 08:50:21 -04:00
|
|
|
futures::future::ok(json!({
|
2019-08-27 11:35:32 -04:00
|
|
|
"rid": tcp_stream_resource.rid,
|
|
|
|
"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 mut resource = resources::lookup(rid)?;
|
|
|
|
|
|
|
|
let shutdown_mode = match how {
|
|
|
|
0 => Shutdown::Read,
|
|
|
|
1 => Shutdown::Write,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use UFCS for disambiguation
|
|
|
|
Resource::shutdown(&mut resource, shutdown_mode)?;
|
|
|
|
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-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-08-14 11:03:02 -04:00
|
|
|
let resource = resources::add_tcp_listener(listener);
|
|
|
|
|
2019-08-27 11:35:32 -04:00
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"rid": resource.rid,
|
|
|
|
"localAddr": local_addr.to_string()
|
|
|
|
})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|