mirror of https://github.com/lukechilds/node.git
Ryan Dahl
13 years ago
40 changed files with 1629 additions and 647 deletions
@ -0,0 +1,144 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#include "uv.h" |
||||
|
#include "task.h" |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <stddef.h> |
||||
|
#include <stdlib.h> |
||||
|
|
||||
|
#define WRITE_REQ_DATA "Hello, world." |
||||
|
#define NUM_WRITE_REQS (1000 * 1000) |
||||
|
|
||||
|
#define container_of(ptr, type, member) \ |
||||
|
((type *) ((char *) (ptr) - offsetof(type, member))) |
||||
|
|
||||
|
typedef struct { |
||||
|
uv_write_t req; |
||||
|
uv_buf_t buf; |
||||
|
} write_req; |
||||
|
|
||||
|
|
||||
|
static write_req* write_reqs; |
||||
|
static uv_tcp_t tcp_client; |
||||
|
static uv_connect_t connect_req; |
||||
|
static uv_shutdown_t shutdown_req; |
||||
|
|
||||
|
static int shutdown_cb_called = 0; |
||||
|
static int connect_cb_called = 0; |
||||
|
static int write_cb_called = 0; |
||||
|
static int close_cb_called = 0; |
||||
|
|
||||
|
static void connect_cb(uv_connect_t* req, int status); |
||||
|
static void write_cb(uv_write_t* req, int status); |
||||
|
static void shutdown_cb(uv_shutdown_t* req, int status); |
||||
|
static void close_cb(uv_handle_t* handle); |
||||
|
|
||||
|
|
||||
|
static void connect_cb(uv_connect_t* req, int status) { |
||||
|
write_req* w; |
||||
|
int i; |
||||
|
int r; |
||||
|
|
||||
|
ASSERT(req->handle == (uv_stream_t*)&tcp_client); |
||||
|
|
||||
|
for (i = 0; i < NUM_WRITE_REQS; i++) { |
||||
|
w = &write_reqs[i]; |
||||
|
r = uv_write(&w->req, req->handle, &w->buf, 1, write_cb); |
||||
|
ASSERT(r == 0); |
||||
|
} |
||||
|
|
||||
|
r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
connect_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void write_cb(uv_write_t* req, int status) { |
||||
|
ASSERT(req != NULL); |
||||
|
ASSERT(status == 0); |
||||
|
write_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void shutdown_cb(uv_shutdown_t* req, int status) { |
||||
|
ASSERT(req->handle == (uv_stream_t*)&tcp_client); |
||||
|
ASSERT(req->handle->write_queue_size == 0); |
||||
|
|
||||
|
uv_close((uv_handle_t*)req->handle, close_cb); |
||||
|
free(write_reqs); |
||||
|
|
||||
|
shutdown_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void close_cb(uv_handle_t* handle) { |
||||
|
ASSERT(handle == (uv_handle_t*)&tcp_client); |
||||
|
close_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
BENCHMARK_IMPL(tcp_write_batch) { |
||||
|
struct sockaddr_in addr; |
||||
|
uv_loop_t* loop; |
||||
|
uint64_t start; |
||||
|
uint64_t stop; |
||||
|
int i; |
||||
|
int r; |
||||
|
|
||||
|
write_reqs = malloc(sizeof(*write_reqs) * NUM_WRITE_REQS); |
||||
|
ASSERT(write_reqs != NULL); |
||||
|
|
||||
|
/* Prepare the data to write out. */ |
||||
|
for (i = 0; i < NUM_WRITE_REQS; i++) { |
||||
|
write_reqs[i].buf = uv_buf_init(WRITE_REQ_DATA, |
||||
|
sizeof(WRITE_REQ_DATA) - 1); |
||||
|
} |
||||
|
|
||||
|
loop = uv_default_loop(); |
||||
|
addr = uv_ip4_addr("127.0.0.1", TEST_PORT); |
||||
|
|
||||
|
r = uv_tcp_init(loop, &tcp_client); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_tcp_connect(&connect_req, &tcp_client, addr, connect_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
start = uv_hrtime(); |
||||
|
|
||||
|
r = uv_run(loop); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
stop = uv_hrtime(); |
||||
|
|
||||
|
ASSERT(connect_cb_called == 1); |
||||
|
ASSERT(write_cb_called == NUM_WRITE_REQS); |
||||
|
ASSERT(shutdown_cb_called == 1); |
||||
|
ASSERT(close_cb_called == 1); |
||||
|
|
||||
|
printf("%ld write requests in %.2fs.\n", |
||||
|
(long)NUM_WRITE_REQS, |
||||
|
(stop - start) / 10e8); |
||||
|
|
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,122 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#include "uv.h" |
||||
|
#include "task.h" |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <stddef.h> |
||||
|
#include <stdlib.h> |
||||
|
|
||||
|
#define container_of(ptr, type, member) \ |
||||
|
((type *) ((char *) (ptr) - offsetof(type, member))) |
||||
|
|
||||
|
typedef struct { |
||||
|
uv_tcp_t handle; |
||||
|
uv_shutdown_t shutdown_req; |
||||
|
} conn_rec; |
||||
|
|
||||
|
static uv_tcp_t tcp_server; |
||||
|
|
||||
|
static void connection_cb(uv_stream_t* stream, int status); |
||||
|
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size); |
||||
|
static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf); |
||||
|
static void shutdown_cb(uv_shutdown_t* req, int status); |
||||
|
static void close_cb(uv_handle_t* handle); |
||||
|
|
||||
|
|
||||
|
static void connection_cb(uv_stream_t* stream, int status) { |
||||
|
conn_rec* conn; |
||||
|
int r; |
||||
|
|
||||
|
ASSERT(status == 0); |
||||
|
ASSERT(stream == (uv_stream_t*)&tcp_server); |
||||
|
|
||||
|
conn = malloc(sizeof *conn); |
||||
|
ASSERT(conn != NULL); |
||||
|
|
||||
|
r = uv_tcp_init(stream->loop, &conn->handle); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_accept(stream, (uv_stream_t*)&conn->handle); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_read_start((uv_stream_t*)&conn->handle, alloc_cb, read_cb); |
||||
|
ASSERT(r == 0); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) { |
||||
|
static char buf[65536]; |
||||
|
return uv_buf_init(buf, sizeof buf); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) { |
||||
|
conn_rec* conn; |
||||
|
int r; |
||||
|
|
||||
|
if (nread >= 0) |
||||
|
return; |
||||
|
|
||||
|
ASSERT(uv_last_error(stream->loop).code == UV_EOF); |
||||
|
|
||||
|
conn = container_of(stream, conn_rec, handle); |
||||
|
|
||||
|
r = uv_shutdown(&conn->shutdown_req, stream, shutdown_cb); |
||||
|
ASSERT(r == 0); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void shutdown_cb(uv_shutdown_t* req, int status) { |
||||
|
conn_rec* conn = container_of(req, conn_rec, shutdown_req); |
||||
|
uv_close((uv_handle_t*)&conn->handle, close_cb); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void close_cb(uv_handle_t* handle) { |
||||
|
conn_rec* conn = container_of(handle, conn_rec, handle); |
||||
|
free(conn); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
HELPER_IMPL(tcp4_blackhole_server) { |
||||
|
struct sockaddr_in addr; |
||||
|
uv_loop_t* loop; |
||||
|
int r; |
||||
|
|
||||
|
loop = uv_default_loop(); |
||||
|
addr = uv_ip4_addr("127.0.0.1", TEST_PORT); |
||||
|
|
||||
|
r = uv_tcp_init(loop, &tcp_server); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_tcp_bind(&tcp_server, addr); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_listen((uv_stream_t*)&tcp_server, 128, connection_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_run(loop); |
||||
|
ASSERT(0 && "Blackhole server dropped out of event loop."); |
||||
|
|
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,221 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#include "uv.h" |
||||
|
#include "task.h" |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <string.h> |
||||
|
|
||||
|
static char exepath[1024]; |
||||
|
static size_t exepath_size = 1024; |
||||
|
static char* args[3]; |
||||
|
static uv_pipe_t channel; |
||||
|
static uv_tcp_t tcp_server; |
||||
|
|
||||
|
static int exit_cb_called; |
||||
|
static int read2_cb_called; |
||||
|
static int local_conn_accepted; |
||||
|
static int remote_conn_accepted; |
||||
|
static int tcp_server_listening; |
||||
|
|
||||
|
static uv_write_t write_req; |
||||
|
|
||||
|
typedef struct { |
||||
|
uv_connect_t conn_req; |
||||
|
uv_tcp_t conn; |
||||
|
} tcp_conn; |
||||
|
|
||||
|
#define CONN_COUNT 100 |
||||
|
|
||||
|
|
||||
|
static void close_server_conn_cb(uv_handle_t* handle) { |
||||
|
free(handle); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void ipc_on_connection(uv_stream_t* server, int status) { |
||||
|
uv_tcp_t* conn; |
||||
|
int r; |
||||
|
|
||||
|
if (!local_conn_accepted) { |
||||
|
/* Accept the connection and close it. Also and close the server. */ |
||||
|
ASSERT(status == 0); |
||||
|
ASSERT((uv_stream_t*)&tcp_server == server); |
||||
|
|
||||
|
conn = malloc(sizeof(*conn)); |
||||
|
ASSERT(conn); |
||||
|
r = uv_tcp_init(server->loop, conn); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_accept(server, (uv_stream_t*)conn); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
uv_close((uv_handle_t*)conn, close_server_conn_cb); |
||||
|
uv_close((uv_handle_t*)server, NULL); |
||||
|
local_conn_accepted = 1; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void exit_cb(uv_process_t* process, int exit_status, int term_signal) { |
||||
|
printf("exit_cb\n"); |
||||
|
exit_cb_called++; |
||||
|
ASSERT(exit_status == 0); |
||||
|
uv_close((uv_handle_t*)process, NULL); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) { |
||||
|
return uv_buf_init(malloc(suggested_size), suggested_size); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void close_client_conn_cb(uv_handle_t* handle) { |
||||
|
tcp_conn* p = (tcp_conn*)handle->data; |
||||
|
free(p); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void connect_cb(uv_connect_t* req, int status) { |
||||
|
uv_close((uv_handle_t*)req->handle, close_client_conn_cb); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void make_many_connections() { |
||||
|
tcp_conn* conn; |
||||
|
struct sockaddr_in addr; |
||||
|
int r, i; |
||||
|
|
||||
|
for (i = 0; i < CONN_COUNT; i++) { |
||||
|
conn = malloc(sizeof(*conn)); |
||||
|
ASSERT(conn); |
||||
|
|
||||
|
r = uv_tcp_init(uv_default_loop(), &conn->conn); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
addr = uv_ip4_addr("127.0.0.1", TEST_PORT); |
||||
|
|
||||
|
r = uv_tcp_connect(&conn->conn_req, (uv_tcp_t*)&conn->conn, addr, connect_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
conn->conn.data = conn; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf, |
||||
|
uv_handle_type pending) { |
||||
|
int r; |
||||
|
uv_buf_t outbuf; |
||||
|
uv_err_t err; |
||||
|
|
||||
|
if (nread == 0) { |
||||
|
/* Everything OK, but nothing read. */ |
||||
|
free(buf.base); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (nread < 0) { |
||||
|
err = uv_last_error(pipe->loop); |
||||
|
if (err.code == UV_EOF) { |
||||
|
free(buf.base); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
printf("error recving on channel: %s\n", uv_strerror(err)); |
||||
|
abort(); |
||||
|
} |
||||
|
|
||||
|
fprintf(stderr, "got %d bytes\n", (int)nread); |
||||
|
|
||||
|
if (!tcp_server_listening) { |
||||
|
ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE); |
||||
|
read2_cb_called++; |
||||
|
|
||||
|
/* Accept the pending TCP server, and start listening on it. */ |
||||
|
ASSERT(pending == UV_TCP); |
||||
|
r = uv_tcp_init(uv_default_loop(), &tcp_server); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
tcp_server_listening = 1; |
||||
|
|
||||
|
/* Make sure that the expected data is correctly multiplexed. */ |
||||
|
ASSERT(memcmp("hello\n", buf.base, nread) == 0); |
||||
|
|
||||
|
outbuf = uv_buf_init("world\n", 6); |
||||
|
r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
/* Create a bunch of connections to get both servers to accept. */ |
||||
|
make_many_connections(); |
||||
|
} else if (memcmp("accepted_connection\n", buf.base, nread) == 0) { |
||||
|
/* Remote server has accepted a connection. Close the channel. */ |
||||
|
ASSERT(pending == UV_UNKNOWN_HANDLE); |
||||
|
remote_conn_accepted = 1; |
||||
|
uv_close((uv_handle_t*)&channel, NULL); |
||||
|
} |
||||
|
|
||||
|
free(buf.base); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
TEST_IMPL(ipc) { |
||||
|
int r; |
||||
|
uv_process_options_t options; |
||||
|
uv_process_t process; |
||||
|
|
||||
|
r = uv_pipe_init(uv_default_loop(), &channel, 1); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
memset(&options, 0, sizeof(uv_process_options_t)); |
||||
|
|
||||
|
r = uv_exepath(exepath, &exepath_size); |
||||
|
ASSERT(r == 0); |
||||
|
exepath[exepath_size] = '\0'; |
||||
|
args[0] = exepath; |
||||
|
args[1] = "ipc_helper"; |
||||
|
args[2] = NULL; |
||||
|
options.file = exepath; |
||||
|
options.args = args; |
||||
|
options.exit_cb = exit_cb; |
||||
|
options.stdin_stream = &channel; |
||||
|
|
||||
|
r = uv_spawn(uv_default_loop(), &process, options); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
uv_read2_start((uv_stream_t*)&channel, on_alloc, on_read); |
||||
|
|
||||
|
r = uv_run(uv_default_loop()); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
ASSERT(local_conn_accepted == 1); |
||||
|
ASSERT(remote_conn_accepted == 1); |
||||
|
ASSERT(read2_cb_called == 1); |
||||
|
ASSERT(exit_cb_called == 1); |
||||
|
return 0; |
||||
|
} |
@ -1,157 +0,0 @@ |
|||||
#include <node.h> |
|
||||
#include <node_buffer.h> |
|
||||
#include <req_wrap.h> |
|
||||
#include <handle_wrap.h> |
|
||||
#include <stream_wrap.h> |
|
||||
|
|
||||
#define UNWRAP \ |
|
||||
assert(!args.Holder().IsEmpty()); \ |
|
||||
assert(args.Holder()->InternalFieldCount() > 0); \ |
|
||||
StdIOWrap* wrap = \ |
|
||||
static_cast<StdIOWrap*>(args.Holder()->GetPointerFromInternalField(0)); \ |
|
||||
if (!wrap) { \ |
|
||||
SetErrno(UV_EBADF); \ |
|
||||
return scope.Close(Integer::New(-1)); \ |
|
||||
} |
|
||||
|
|
||||
namespace node { |
|
||||
|
|
||||
using v8::Object; |
|
||||
using v8::Handle; |
|
||||
using v8::Local; |
|
||||
using v8::Persistent; |
|
||||
using v8::Value; |
|
||||
using v8::HandleScope; |
|
||||
using v8::FunctionTemplate; |
|
||||
using v8::String; |
|
||||
using v8::Function; |
|
||||
using v8::TryCatch; |
|
||||
using v8::Context; |
|
||||
using v8::Arguments; |
|
||||
using v8::Integer; |
|
||||
using v8::Undefined; |
|
||||
|
|
||||
extern Persistent<Function> tcpConstructor; |
|
||||
extern Persistent<Function> pipeConstructor; |
|
||||
static Persistent<Function> constructor; |
|
||||
|
|
||||
|
|
||||
class StdIOWrap : StreamWrap { |
|
||||
public: |
|
||||
static void Initialize(Handle<Object> target) { |
|
||||
StreamWrap::Initialize(target); |
|
||||
|
|
||||
HandleScope scope; |
|
||||
|
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(New); |
|
||||
t->SetClassName(String::NewSymbol("StdIO")); |
|
||||
|
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1); |
|
||||
|
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); |
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); |
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write); |
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen); |
|
||||
|
|
||||
constructor = Persistent<Function>::New(t->GetFunction()); |
|
||||
|
|
||||
target->Set(String::NewSymbol("StdIO"), constructor); |
|
||||
} |
|
||||
|
|
||||
private: |
|
||||
static Handle<Value> New(const Arguments& args) { |
|
||||
// This constructor should not be exposed to public javascript.
|
|
||||
// Therefore we assert that we are not trying to call this as a
|
|
||||
// normal function.
|
|
||||
assert(args.IsConstructCall()); |
|
||||
|
|
||||
uv_std_type stdHandleType = (uv_std_type)args[0]->Int32Value(); |
|
||||
|
|
||||
assert(stdHandleType == UV_STDIN || stdHandleType == UV_STDOUT || stdHandleType == UV_STDERR); |
|
||||
|
|
||||
uv_stream_t* stdHandle = uv_std_handle(uv_default_loop(), stdHandleType); |
|
||||
if (stdHandle) { |
|
||||
HandleScope scope; |
|
||||
StdIOWrap* wrap = new StdIOWrap(args.This()); |
|
||||
assert(wrap); |
|
||||
|
|
||||
wrap->handle_ = stdHandle; |
|
||||
wrap->SetHandle((uv_handle_t*)stdHandle); |
|
||||
wrap->UpdateWriteQueueSize(); |
|
||||
|
|
||||
return scope.Close(args.This()); |
|
||||
} else { |
|
||||
return Undefined(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
StdIOWrap(Handle<Object> object) : StreamWrap(object, NULL) { |
|
||||
} |
|
||||
|
|
||||
static Handle<Value> Listen(const Arguments& args) { |
|
||||
HandleScope scope; |
|
||||
|
|
||||
UNWRAP |
|
||||
|
|
||||
int backlog = args[0]->Int32Value(); |
|
||||
|
|
||||
int r = uv_listen(wrap->handle_, SOMAXCONN, OnConnection); |
|
||||
|
|
||||
// Error starting the pipe.
|
|
||||
if (r) SetErrno(uv_last_error(uv_default_loop()).code); |
|
||||
|
|
||||
return scope.Close(Integer::New(r)); |
|
||||
} |
|
||||
|
|
||||
// TODO maybe share with TCPWrap?
|
|
||||
static void OnConnection(uv_stream_t* handle, int status) { |
|
||||
HandleScope scope; |
|
||||
Local<Object> client_obj; |
|
||||
|
|
||||
StdIOWrap* wrap = static_cast<StdIOWrap*>(handle->data); |
|
||||
assert(wrap->handle_ == handle); |
|
||||
|
|
||||
// We should not be getting this callback if someone as already called
|
|
||||
// uv_close() on the handle.
|
|
||||
assert(wrap->object_.IsEmpty() == false); |
|
||||
|
|
||||
if (status != 0) { |
|
||||
// TODO Handle server error (set errno and call onconnection with NULL)
|
|
||||
assert(0); |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
// Instanciate the client javascript object and handle.
|
|
||||
switch (handle->type) { |
|
||||
case UV_TCP: |
|
||||
client_obj = tcpConstructor->NewInstance(); |
|
||||
break; |
|
||||
case UV_NAMED_PIPE: |
|
||||
client_obj = pipeConstructor->NewInstance(); |
|
||||
break; |
|
||||
default: |
|
||||
assert(0); |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
// Unwrap the client javascript object.
|
|
||||
assert(client_obj->InternalFieldCount() > 0); |
|
||||
StreamWrap* client_wrap = |
|
||||
static_cast<StreamWrap*>(client_obj->GetPointerFromInternalField(0)); |
|
||||
|
|
||||
int r = uv_accept(handle, client_wrap->GetStream()); |
|
||||
|
|
||||
// uv_accept should always work.
|
|
||||
assert(r == 0); |
|
||||
|
|
||||
// Successful accept. Call the onconnection callback in JavaScript land.
|
|
||||
Local<Value> argv[1] = { client_obj }; |
|
||||
MakeCallback(wrap->object_, "onconnection", 1, argv); |
|
||||
} |
|
||||
|
|
||||
uv_stream_t* handle_; |
|
||||
}; |
|
||||
|
|
||||
} // namespace node
|
|
||||
|
|
||||
NODE_MODULE(node_stdio_wrap, node::StdIOWrap::Initialize); |
|
Loading…
Reference in new issue