diff options
author | Andreas Madsen <amwebdk@gmail.com> | 2017-11-20 19:18:40 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-11-28 04:50:54 +0300 |
commit | b44efded8481877c1ec782112b9ae4c4fec37463 (patch) | |
tree | 7c967e36bc1d718c513965b619f65e7f4f9da95a /src/pipe_wrap.cc | |
parent | 91d131210c2d7887d8625d34c1008a3d423cf86b (diff) |
async_wrap: add provider types for net server
Adds `TCPSERVERWRAP` and `PIPESERVERWRAP` as provider types. This
makes it possible to distinguish servers from connections.
PR-URL: https://github.com/nodejs/node/pull/17157
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/pipe_wrap.cc')
-rw-r--r-- | src/pipe_wrap.cc | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 5ed8d304705..76280f0ce77 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -40,6 +40,7 @@ using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; +using v8::Int32; using v8::Local; using v8::Object; using v8::String; @@ -48,14 +49,17 @@ using v8::Value; using AsyncHooks = Environment::AsyncHooks; -Local<Object> PipeWrap::Instantiate(Environment* env, AsyncWrap* parent) { +Local<Object> PipeWrap::Instantiate(Environment* env, + AsyncWrap* parent, + PipeWrap::SocketType type) { EscapableHandleScope handle_scope(env->isolate()); AsyncHooks::InitScope init_scope(env, parent->get_async_id()); CHECK_EQ(false, env->pipe_constructor_template().IsEmpty()); Local<Function> constructor = env->pipe_constructor_template()->GetFunction(); CHECK_EQ(false, constructor.IsEmpty()); + Local<Value> type_value = Int32::New(env->isolate(), type); Local<Object> instance = - constructor->NewInstance(env->context()).ToLocalChecked(); + constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked(); return handle_scope.Escape(instance); } @@ -107,6 +111,15 @@ void PipeWrap::Initialize(Local<Object> target, FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"); cwt->SetClassName(wrapString); target->Set(wrapString, cwt->GetFunction()); + + // Define constants + Local<Object> constants = Object::New(env->isolate()); + NODE_DEFINE_CONSTANT(constants, SOCKET); + NODE_DEFINE_CONSTANT(constants, SERVER); + NODE_DEFINE_CONSTANT(constants, IPC); + target->Set(context, + FIXED_ONE_BYTE_STRING(env->isolate(), "constants"), + constants).FromJust(); } @@ -115,17 +128,40 @@ void PipeWrap::New(const FunctionCallbackInfo<Value>& args) { // Therefore we assert that we are not trying to call this as a // normal function. CHECK(args.IsConstructCall()); + CHECK(args[0]->IsInt32()); Environment* env = Environment::GetCurrent(args); - new PipeWrap(env, args.This(), args[0]->IsTrue()); + + int type_value = args[0].As<Int32>()->Value(); + PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value); + + bool ipc; + ProviderType provider; + switch (type) { + case SOCKET: + provider = PROVIDER_PIPEWRAP; + ipc = false; + break; + case SERVER: + provider = PROVIDER_PIPESERVERWRAP; + ipc = false; + break; + case IPC: + provider = PROVIDER_PIPEWRAP; + ipc = true; + break; + default: + UNREACHABLE(); + } + + new PipeWrap(env, args.This(), provider, ipc); } PipeWrap::PipeWrap(Environment* env, Local<Object> object, + ProviderType provider, bool ipc) - : ConnectionWrap(env, - object, - AsyncWrap::PROVIDER_PIPEWRAP) { + : ConnectionWrap(env, object, provider) { int r = uv_pipe_init(env->event_loop(), &handle_, ipc); CHECK_EQ(r, 0); // How do we proxy this error up to javascript? // Suggestion: uv_pipe_init() returns void. |