2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
#[macro_use]
|
2020-02-06 18:34:40 -05:00
|
|
|
extern crate log;
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource_id;
|
|
|
|
use deno_core::error::AnyError;
|
2020-11-24 18:38:23 -05:00
|
|
|
use deno_core::AsyncMutFuture;
|
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::AsyncRefFuture;
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::BufVec;
|
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;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_json::Value;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-11-24 18:38:23 -05:00
|
|
|
use std::convert::TryFrom;
|
2020-09-05 20:34:02 -04:00
|
|
|
use std::convert::TryInto;
|
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-11-24 18:38:23 -05:00
|
|
|
// Note: it isn't actually necessary to wrap the `tokio::net::TcpListener` in
|
|
|
|
// a cell, because it only supports one op (`accept`) which does not require
|
|
|
|
// a mutable reference to the listener.
|
|
|
|
struct TcpListener(AsyncRefCell<tokio::net::TcpListener>);
|
|
|
|
|
|
|
|
impl Resource for TcpListener {}
|
|
|
|
|
|
|
|
impl TcpListener {
|
|
|
|
/// Returns a future that yields a shared borrow of the TCP listener.
|
|
|
|
fn borrow(self: Rc<Self>) -> AsyncRefFuture<tokio::net::TcpListener> {
|
|
|
|
RcRef::map(self, |r| &r.0).borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<std::net::TcpListener> for TcpListener {
|
|
|
|
type Error = Error;
|
|
|
|
fn try_from(l: std::net::TcpListener) -> Result<Self, Self::Error> {
|
|
|
|
tokio::net::TcpListener::try_from(l)
|
|
|
|
.map(AsyncRefCell::new)
|
|
|
|
.map(Self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TcpStream {
|
|
|
|
rd: AsyncRefCell<tokio::net::tcp::OwnedReadHalf>,
|
|
|
|
wr: AsyncRefCell<tokio::net::tcp::OwnedWriteHalf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for TcpStream {}
|
|
|
|
|
|
|
|
impl TcpStream {
|
|
|
|
/// Returns a future that yields an exclusive borrow of the read end of the
|
|
|
|
/// tcp stream.
|
|
|
|
fn rd_borrow_mut(
|
|
|
|
self: Rc<Self>,
|
|
|
|
) -> AsyncMutFuture<tokio::net::tcp::OwnedReadHalf> {
|
|
|
|
RcRef::map(self, |r| &r.rd).borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a future that yields an exclusive borrow of the write end of the
|
|
|
|
/// tcp stream.
|
|
|
|
fn wr_borrow_mut(
|
|
|
|
self: Rc<Self>,
|
|
|
|
) -> AsyncMutFuture<tokio::net::tcp::OwnedWriteHalf> {
|
|
|
|
RcRef::map(self, |r| &r.wr).borrow_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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());
|
2020-09-10 09:57:45 -04:00
|
|
|
runtime.register_op("listen", deno_core::json_op_sync(op_listen));
|
|
|
|
runtime.register_op("close", deno_core::json_op_sync(op_close));
|
|
|
|
runtime.register_op("accept", deno_core::json_op_async(op_accept));
|
|
|
|
runtime.register_op("read", deno_core::json_op_async(op_read));
|
|
|
|
runtime.register_op("write", deno_core::json_op_async(op_write));
|
|
|
|
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,
|
2020-09-05 20:34:02 -04:00
|
|
|
_args: Value,
|
|
|
|
_bufs: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-05 20:34:02 -04:00
|
|
|
debug!("listen");
|
|
|
|
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)?;
|
|
|
|
let rid = state.resource_table_2.add(listener);
|
2020-09-05 20:34:02 -04:00
|
|
|
Ok(serde_json::json!({ "rid": 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,
|
2020-09-05 20:34:02 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_buf: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("close rid={}", rid);
|
2020-09-05 20:34:02 -04:00
|
|
|
state
|
2020-11-24 18:38:23 -05:00
|
|
|
.resource_table_2
|
2020-04-18 20:05:13 -04:00
|
|
|
.close(rid)
|
2020-08-21 05:47:57 -04:00
|
|
|
.map(|_| serde_json::json!(()))
|
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>>,
|
2020-09-05 20:34:02 -04:00
|
|
|
args: Value,
|
|
|
|
_bufs: BufVec,
|
2020-11-24 18:38:23 -05:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("accept rid={}", rid);
|
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
let listener_rc = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table_2
|
|
|
|
.get::<TcpListener>(rid)
|
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let listener_ref = listener_rc.borrow().await;
|
|
|
|
|
|
|
|
let stream: TcpStream = listener_ref.accept().await?.0.into();
|
|
|
|
let rid = state.borrow_mut().resource_table_2.add(stream);
|
|
|
|
Ok(serde_json::json!({ "rid": 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>>,
|
2020-09-05 20:34:02 -04:00
|
|
|
args: Value,
|
|
|
|
mut bufs: BufVec,
|
2020-11-24 18:38:23 -05:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2019-02-26 17:36:05 -05:00
|
|
|
debug!("read rid={}", rid);
|
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
let stream_rc = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table_2
|
|
|
|
.get::<TcpStream>(rid)
|
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let mut rd_stream_mut = stream_rc.rd_borrow_mut().await;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
let nread = rd_stream_mut.read(&mut bufs[0]).await?;
|
|
|
|
Ok(serde_json::json!({ "nread": 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>>,
|
2020-09-05 20:34:02 -04:00
|
|
|
args: Value,
|
|
|
|
bufs: BufVec,
|
2020-11-24 18:38:23 -05:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("write rid={}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
let stream_rc = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table_2
|
|
|
|
.get::<TcpStream>(rid)
|
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let mut wr_stream_mut = stream_rc.wr_borrow_mut().await;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-11-24 18:38:23 -05:00
|
|
|
let nwritten = wr_stream_mut.write(&bufs[0]).await?;
|
|
|
|
Ok(serde_json::json!({ "nwritten": 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
|
2020-09-11 09:18:49 -04:00
|
|
|
.execute(
|
|
|
|
"http_bench_json_ops.js",
|
|
|
|
include_str!("http_bench_json_ops.js"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime.run_event_loop().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
|
|
|
}
|