• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "pipe_wrap.h"
23 
24 #include "async_wrap.h"
25 #include "connection_wrap.h"
26 #include "env-inl.h"
27 #include "handle_wrap.h"
28 #include "node.h"
29 #include "node_buffer.h"
30 #include "connect_wrap.h"
31 #include "stream_base-inl.h"
32 #include "stream_wrap.h"
33 #include "util-inl.h"
34 
35 namespace node {
36 
37 using v8::Context;
38 using v8::EscapableHandleScope;
39 using v8::Function;
40 using v8::FunctionCallbackInfo;
41 using v8::FunctionTemplate;
42 using v8::Int32;
43 using v8::Local;
44 using v8::MaybeLocal;
45 using v8::Object;
46 using v8::String;
47 using v8::Value;
48 
Instantiate(Environment * env,AsyncWrap * parent,PipeWrap::SocketType type)49 MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
50                                          AsyncWrap* parent,
51                                          PipeWrap::SocketType type) {
52   EscapableHandleScope handle_scope(env->isolate());
53   AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
54   CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
55   Local<Function> constructor = env->pipe_constructor_template()
56                                     ->GetFunction(env->context())
57                                     .ToLocalChecked();
58   CHECK_EQ(false, constructor.IsEmpty());
59   Local<Value> type_value = Int32::New(env->isolate(), type);
60   return handle_scope.EscapeMaybe(
61       constructor->NewInstance(env->context(), 1, &type_value));
62 }
63 
64 
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)65 void PipeWrap::Initialize(Local<Object> target,
66                           Local<Value> unused,
67                           Local<Context> context,
68                           void* priv) {
69   Environment* env = Environment::GetCurrent(context);
70 
71   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
72   Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
73   t->SetClassName(pipeString);
74   t->InstanceTemplate()
75     ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
76 
77   t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
78 
79   env->SetProtoMethod(t, "bind", Bind);
80   env->SetProtoMethod(t, "listen", Listen);
81   env->SetProtoMethod(t, "connect", Connect);
82   env->SetProtoMethod(t, "open", Open);
83 
84 #ifdef _WIN32
85   env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
86 #endif
87 
88   env->SetProtoMethod(t, "fchmod", Fchmod);
89 
90   target->Set(env->context(),
91               pipeString,
92               t->GetFunction(env->context()).ToLocalChecked()).Check();
93   env->set_pipe_constructor_template(t);
94 
95   // Create FunctionTemplate for PipeConnectWrap.
96   auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
97   cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
98   Local<String> wrapString =
99       FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
100   cwt->SetClassName(wrapString);
101   target->Set(env->context(),
102               wrapString,
103               cwt->GetFunction(env->context()).ToLocalChecked()).Check();
104 
105   // Define constants
106   Local<Object> constants = Object::New(env->isolate());
107   NODE_DEFINE_CONSTANT(constants, SOCKET);
108   NODE_DEFINE_CONSTANT(constants, SERVER);
109   NODE_DEFINE_CONSTANT(constants, IPC);
110   NODE_DEFINE_CONSTANT(constants, UV_READABLE);
111   NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
112   target->Set(context,
113               env->constants_string(),
114               constants).Check();
115 }
116 
117 
New(const FunctionCallbackInfo<Value> & args)118 void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
119   // This constructor should not be exposed to public javascript.
120   // Therefore we assert that we are not trying to call this as a
121   // normal function.
122   CHECK(args.IsConstructCall());
123   CHECK(args[0]->IsInt32());
124   Environment* env = Environment::GetCurrent(args);
125 
126   int type_value = args[0].As<Int32>()->Value();
127   PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
128 
129   bool ipc;
130   ProviderType provider;
131   switch (type) {
132     case SOCKET:
133       provider = PROVIDER_PIPEWRAP;
134       ipc = false;
135       break;
136     case SERVER:
137       provider = PROVIDER_PIPESERVERWRAP;
138       ipc = false;
139       break;
140     case IPC:
141       provider = PROVIDER_PIPEWRAP;
142       ipc = true;
143       break;
144     default:
145       UNREACHABLE();
146   }
147 
148   new PipeWrap(env, args.This(), provider, ipc);
149 }
150 
151 
PipeWrap(Environment * env,Local<Object> object,ProviderType provider,bool ipc)152 PipeWrap::PipeWrap(Environment* env,
153                    Local<Object> object,
154                    ProviderType provider,
155                    bool ipc)
156     : ConnectionWrap(env, object, provider) {
157   int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
158   CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
159                    // Suggestion: uv_pipe_init() returns void.
160 }
161 
162 
Bind(const FunctionCallbackInfo<Value> & args)163 void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
164   PipeWrap* wrap;
165   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
166   node::Utf8Value name(args.GetIsolate(), args[0]);
167   int err = uv_pipe_bind(&wrap->handle_, *name);
168   args.GetReturnValue().Set(err);
169 }
170 
171 
172 #ifdef _WIN32
SetPendingInstances(const FunctionCallbackInfo<Value> & args)173 void PipeWrap::SetPendingInstances(const FunctionCallbackInfo<Value>& args) {
174   PipeWrap* wrap;
175   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
176   CHECK(args[0]->IsInt32());
177   int instances = args[0].As<Int32>()->Value();
178   uv_pipe_pending_instances(&wrap->handle_, instances);
179 }
180 #endif
181 
182 
Fchmod(const v8::FunctionCallbackInfo<v8::Value> & args)183 void PipeWrap::Fchmod(const v8::FunctionCallbackInfo<v8::Value>& args) {
184   PipeWrap* wrap;
185   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
186   CHECK(args[0]->IsInt32());
187   int mode = args[0].As<Int32>()->Value();
188   int err = uv_pipe_chmod(&wrap->handle_, mode);
189   args.GetReturnValue().Set(err);
190 }
191 
192 
Listen(const FunctionCallbackInfo<Value> & args)193 void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
194   PipeWrap* wrap;
195   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
196   Environment* env = wrap->env();
197   int backlog;
198   if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
199   int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
200                       backlog,
201                       OnConnection);
202   args.GetReturnValue().Set(err);
203 }
204 
205 
Open(const FunctionCallbackInfo<Value> & args)206 void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
207   Environment* env = Environment::GetCurrent(args);
208 
209   PipeWrap* wrap;
210   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
211 
212   int fd;
213   if (!args[0]->Int32Value(env->context()).To(&fd)) return;
214 
215   int err = uv_pipe_open(&wrap->handle_, fd);
216   wrap->set_fd(fd);
217 
218   if (err != 0)
219     env->ThrowUVException(err, "uv_pipe_open");
220 }
221 
222 
Connect(const FunctionCallbackInfo<Value> & args)223 void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
224   Environment* env = Environment::GetCurrent(args);
225 
226   PipeWrap* wrap;
227   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
228 
229   CHECK(args[0]->IsObject());
230   CHECK(args[1]->IsString());
231 
232   Local<Object> req_wrap_obj = args[0].As<Object>();
233   node::Utf8Value name(env->isolate(), args[1]);
234 
235   ConnectWrap* req_wrap =
236       new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
237   req_wrap->Dispatch(uv_pipe_connect,
238                      &wrap->handle_,
239                      *name,
240                      AfterConnect);
241 
242   args.GetReturnValue().Set(0);  // uv_pipe_connect() doesn't return errors.
243 }
244 
245 
246 }  // namespace node
247 
248 NODE_MODULE_CONTEXT_AWARE_INTERNAL(pipe_wrap, node::PipeWrap::Initialize)
249