Skip to content

Commit

Permalink
Remove createServer op
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Apr 5, 2021
1 parent f45832f commit 50af461
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
3 changes: 2 additions & 1 deletion cli/bench/deno_http.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
async function main(addr) {
const rid = await Deno.http.createServer(addr);
const [hostname, port] = addr.split(":");
const { rid } = await Deno.listen({ hostname, port });
console.log("Server listening on", addr);

while (true) {
Expand Down
8 changes: 0 additions & 8 deletions runtime/js/40_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
return entries;
}

function createServer(address) {
return Deno.core.jsonOpAsync(
"op_http_create_server",
address,
);
}

function accept(serverRid) {
return Deno.core.jsonOpAsync("op_http_accept", serverRid);
}
Expand Down Expand Up @@ -47,7 +40,6 @@
}

window.__bootstrap.http = {
createServer,
accept,
readRequest,
nextRequest,
Expand Down
50 changes: 20 additions & 30 deletions runtime/ops/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use std::rc::Rc;
use std::task::Context;
use std::task::Poll;

use deno_core::error::bad_resource;
use deno_core::error::bad_resource_id;
use deno_core::error::custom_error;
use deno_core::error::null_opbuf;
use deno_core::error::type_error;
use deno_core::error::AnyError;
Expand All @@ -26,6 +28,7 @@ use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::ZeroCopyBuf;

use crate::ops::net::TcpListenerResource;
use hyper::body::HttpBody;
use hyper::http;
use hyper::server::conn::Connection;
Expand All @@ -37,12 +40,10 @@ use hyper::Response;
use serde::Deserialize;
use serde::Serialize;
use tokio::io::AsyncReadExt;
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use tokio_util::io::StreamReader;

pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_async(rt, "op_http_create_server", op_create_server);
super::reg_json_async(rt, "op_http_accept", op_accept_http);
super::reg_json_async(rt, "op_http_next_request", op_next_request);
super::reg_json_async(rt, "op_http_read_request", op_read_request);
Expand Down Expand Up @@ -107,16 +108,6 @@ impl Service<Request<Body>> for DenoService {
}
}

struct HttpServer {
pub listener: TcpListener,
}

impl Resource for HttpServer {
fn name(&self) -> Cow<str> {
"httpServer".into()
}
}

struct ConnResource {
pub hyper_connection:
Rc<RefCell<Connection<tokio::net::TcpStream, DenoService, LocalExecutor>>>,
Expand All @@ -129,33 +120,32 @@ impl Resource for ConnResource {
}
}

pub async fn op_create_server(
state: Rc<RefCell<OpState>>,
address: String,
_data: Option<ZeroCopyBuf>,
) -> Result<ResourceId, AnyError> {
let tcp_listener = TcpListener::bind(address).await?;
let http_server = HttpServer {
listener: tcp_listener,
};

let rid = state.borrow_mut().resource_table.add(http_server);

Ok(rid)
}

pub async fn op_accept_http(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
_data: Option<ZeroCopyBuf>,
) -> Result<ResourceId, AnyError> {
let http_server = state
let resource = state
.borrow()
.resource_table
.get::<HttpServer>(rid)
.get::<TcpListenerResource>(rid)
.ok_or_else(bad_resource_id)?;

let (tcp_stream, _) = http_server.listener.accept().await?;
let listener = RcRef::map(&resource, |r| &r.listener)
.try_borrow_mut()
.ok_or_else(|| custom_error("Busy", "Another accept task is ongoing"))?;
let cancel = RcRef::map(resource, |r| &r.cancel);
let (tcp_stream, _socket_addr) =
listener.accept().try_or_cancel(cancel).await.map_err(|e| {
// FIXME(bartlomieju): compatibility with current JS implementation
if let std::io::ErrorKind::Interrupted = e.kind() {
bad_resource("Listener has been closed")
} else {
e.into()
}
})?;

//let (tcp_stream, _) = http_server.listener.accept().await?;
let deno_service = DenoService::default();

let hyper_connection = Http::new()
Expand Down
6 changes: 3 additions & 3 deletions runtime/ops/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ async fn op_connect(
}
}

struct TcpListenerResource {
listener: AsyncRefCell<TcpListener>,
cancel: CancelHandle,
pub struct TcpListenerResource {
pub listener: AsyncRefCell<TcpListener>,
pub cancel: CancelHandle,
}

impl Resource for TcpListenerResource {
Expand Down

0 comments on commit 50af461

Please sign in to comment.