2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource_id;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::error::null_opbuf;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-11-24 18:38:23 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2020-12-03 17:52:55 -05:00
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-11-24 18:38:23 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-31 10:37:38 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-11-24 18:38:23 -05:00
|
|
|
use std::convert::TryFrom;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::env;
|
2020-11-24 18:38:23 -05:00
|
|
|
use std::io::Error;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::net::SocketAddr;
|
2020-09-05 20:34:02 -04:00
|
|
|
use std::rc::Rc;
|
2020-11-24 18:38:23 -05:00
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
use tokio::io::AsyncWriteExt;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
struct Logger;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
impl log::Log for Logger {
|
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
|
|
|
metadata.level() <= log::max_level()
|
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
println!("{} - {}", record.level(), record.args());
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:52:55 -05:00
|
|
|
// Note: a `tokio::net::TcpListener` doesn't need to be wrapped in a cell,
|
|
|
|
// because it only supports one op (`accept`) which does not require a mutable
|
|
|
|
// reference to the listener.
|
|
|
|
struct TcpListener {
|
|
|
|
inner: tokio::net::TcpListener,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
2020-11-24 18:38:23 -05:00
|
|
|
|
|
|
|
impl TcpListener {
|
2020-12-03 17:52:55 -05:00
|
|
|
async fn accept(self: Rc<Self>) -> Result<TcpStream, Error> {
|
|
|
|
let cancel = RcRef::map(&self, |r| &r.cancel);
|
|
|
|
let stream = self.inner.accept().try_or_cancel(cancel).await?.0.into();
|
|
|
|
Ok(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for TcpListener {
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
2020-11-24 18:38:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<std::net::TcpListener> for TcpListener {
|
|
|
|
type Error = Error;
|
2020-12-03 17:52:55 -05:00
|
|
|
fn try_from(
|
|
|
|
std_listener: std::net::TcpListener,
|
|
|
|
) -> Result<Self, Self::Error> {
|
|
|
|
tokio::net::TcpListener::try_from(std_listener).map(|tokio_listener| Self {
|
|
|
|
inner: tokio_listener,
|
|
|
|
cancel: Default::default(),
|
|
|
|
})
|
2020-11-24 18:38:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TcpStream {
|
|
|
|
rd: AsyncRefCell<tokio::net::tcp::OwnedReadHalf>,
|
|
|
|
wr: AsyncRefCell<tokio::net::tcp::OwnedWriteHalf>,
|
2020-12-03 17:52:55 -05:00
|
|
|
// When a `TcpStream` resource is closed, all pending 'read' ops are
|
|
|
|
// canceled, while 'write' ops are allowed to complete. Therefore only
|
|
|
|
// 'read' futures are attached to this cancel handle.
|
|
|
|
cancel: CancelHandle,
|
2020-11-24 18:38:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpStream {
|
2020-12-03 17:52:55 -05:00
|
|
|
async fn read(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, Error> {
|
|
|
|
let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await;
|
|
|
|
let cancel = RcRef::map(self, |r| &r.cancel);
|
|
|
|
rd.read(buf).try_or_cancel(cancel).await
|
2020-11-24 18:38:23 -05:00
|
|
|
}
|
|
|
|
|
2020-12-03 17:52:55 -05:00
|
|
|
async fn write(self: Rc<Self>, buf: &[u8]) -> Result<usize, Error> {
|
|
|
|
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
|
|
|
wr.write(buf).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for TcpStream {
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel()
|
2020-11-24 18:38:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<tokio::net::TcpStream> for TcpStream {
|
|
|
|
fn from(s: tokio::net::TcpStream) -> Self {
|
|
|
|
let (rd, wr) = s.into_split();
|
|
|
|
Self {
|
|
|
|
rd: rd.into(),
|
|
|
|
wr: wr.into(),
|
2020-12-03 17:52:55 -05:00
|
|
|
cancel: Default::default(),
|
2020-11-24 18:38:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:20:20 -04:00
|
|
|
fn create_js_runtime() -> JsRuntime {
|
2020-09-11 09:18:49 -04:00
|
|
|
let mut runtime = JsRuntime::new(Default::default());
|
2021-04-12 15:55:05 -04:00
|
|
|
runtime.register_op("listen", deno_core::op_sync(op_listen));
|
|
|
|
runtime.register_op("close", deno_core::op_sync(op_close));
|
|
|
|
runtime.register_op("accept", deno_core::op_async(op_accept));
|
|
|
|
runtime.register_op("read", deno_core::op_async(op_read));
|
|
|
|
runtime.register_op("write", deno_core::op_async(op_write));
|
2021-04-25 16:00:05 -04:00
|
|
|
runtime.sync_ops_cache();
|
2020-09-10 09:57:45 -04:00
|
|
|
runtime
|
2020-09-05 20:34:02 -04:00
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
fn op_listen(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-02-13 11:56:56 -05:00
|
|
|
_args: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-02-13 11:56:56 -05:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
2021-03-26 12:34:25 -04:00
|
|
|
log::debug!("listen");
|
2020-09-05 20:34:02 -04:00
|
|
|
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
|
|
|
|
let std_listener = std::net::TcpListener::bind(&addr)?;
|
2020-11-24 18:38:23 -05:00
|
|
|
std_listener.set_nonblocking(true)?;
|
|
|
|
let listener = TcpListener::try_from(std_listener)?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(listener);
|
2021-03-31 10:37:38 -04:00
|
|
|
Ok(rid)
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_close(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-31 10:37:38 -04:00
|
|
|
rid: ResourceId,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-02-13 11:56:56 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2021-03-31 10:37:38 -04:00
|
|
|
log::debug!("close rid={}", rid);
|
2020-09-05 20:34:02 -04:00
|
|
|
state
|
2020-12-16 11:14:12 -05:00
|
|
|
.resource_table
|
2021-03-31 10:37:38 -04:00
|
|
|
.close(rid)
|
2021-02-13 11:56:56 -05:00
|
|
|
.map(|_| ())
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(bad_resource_id)
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
async fn op_accept(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-31 10:37:38 -04:00
|
|
|
rid: ResourceId,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-02-13 11:56:56 -05:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
2021-03-31 10:37:38 -04:00
|
|
|
log::debug!("accept rid={}", rid);
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2020-12-03 17:52:55 -05:00
|
|
|
let listener = state
|
2020-11-24 18:38:23 -05:00
|
|
|
.borrow()
|
2020-12-16 11:14:12 -05:00
|
|
|
.resource_table
|
2021-03-31 10:37:38 -04:00
|
|
|
.get::<TcpListener>(rid)
|
2020-11-24 18:38:23 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2020-12-03 17:52:55 -05:00
|
|
|
let stream = listener.accept().await?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.borrow_mut().resource_table.add(stream);
|
2021-03-31 10:37:38 -04:00
|
|
|
Ok(rid)
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
async fn op_read(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-31 10:37:38 -04:00
|
|
|
rid: ResourceId,
|
2021-04-02 09:47:57 -04:00
|
|
|
buf: Option<ZeroCopyBuf>,
|
2021-03-31 10:37:38 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
2021-04-02 09:47:57 -04:00
|
|
|
let mut buf = buf.ok_or_else(null_opbuf)?;
|
2021-03-31 10:37:38 -04:00
|
|
|
log::debug!("read rid={}", rid);
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-12-03 17:52:55 -05:00
|
|
|
let stream = state
|
2020-11-24 18:38:23 -05:00
|
|
|
.borrow()
|
2020-12-16 11:14:12 -05:00
|
|
|
.resource_table
|
2021-03-31 10:37:38 -04:00
|
|
|
.get::<TcpStream>(rid)
|
2020-11-24 18:38:23 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2021-04-02 09:47:57 -04:00
|
|
|
let nread = stream.read(&mut buf).await?;
|
2021-03-31 10:37:38 -04:00
|
|
|
Ok(nread)
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
async fn op_write(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-31 10:37:38 -04:00
|
|
|
rid: ResourceId,
|
2021-04-02 09:47:57 -04:00
|
|
|
buf: Option<ZeroCopyBuf>,
|
2021-03-31 10:37:38 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
2021-04-02 09:47:57 -04:00
|
|
|
let buf = buf.ok_or_else(null_opbuf)?;
|
2021-03-31 10:37:38 -04:00
|
|
|
log::debug!("write rid={}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-12-03 17:52:55 -05:00
|
|
|
let stream = state
|
2020-11-24 18:38:23 -05:00
|
|
|
.borrow()
|
2020-12-16 11:14:12 -05:00
|
|
|
.resource_table
|
2021-03-31 10:37:38 -04:00
|
|
|
.get::<TcpStream>(rid)
|
2020-11-24 18:38:23 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2021-04-02 09:47:57 -04:00
|
|
|
let nwritten = stream.write(&buf).await?;
|
2021-03-31 10:37:38 -04:00
|
|
|
Ok(nwritten)
|
2020-02-06 18:34:40 -05:00
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn main() {
|
|
|
|
log::set_logger(&Logger).unwrap();
|
|
|
|
log::set_max_level(
|
|
|
|
env::args()
|
|
|
|
.find(|a| a == "-D")
|
|
|
|
.map(|_| log::LevelFilter::Debug)
|
|
|
|
.unwrap_or(log::LevelFilter::Warn),
|
|
|
|
);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
// NOTE: `--help` arg will display V8 help and exit
|
|
|
|
deno_core::v8_set_flags(env::args().collect());
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-10-07 11:20:20 -04:00
|
|
|
let mut js_runtime = create_js_runtime();
|
2020-11-24 18:38:23 -05:00
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
2020-02-06 18:34:40 -05:00
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-09-11 09:18:49 -04:00
|
|
|
|
|
|
|
let future = async move {
|
2020-10-07 11:20:20 -04:00
|
|
|
js_runtime
|
2021-06-21 19:45:41 -04:00
|
|
|
.execute_script(
|
2020-09-11 09:18:49 -04:00
|
|
|
"http_bench_json_ops.js",
|
|
|
|
include_str!("http_bench_json_ops.js"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-05-26 15:07:12 -04:00
|
|
|
js_runtime.run_event_loop(false).await
|
2020-09-11 09:18:49 -04:00
|
|
|
};
|
2020-09-22 17:30:03 -04:00
|
|
|
runtime.block_on(future).unwrap();
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|