|
| 1 | +#include <node.h> |
| 2 | +#include <node_buffer.h> |
| 3 | +#include <req_wrap.h> |
| 4 | +#include <handle_wrap.h> |
| 5 | +#include <stream_wrap.h> |
| 6 | + |
| 7 | +#define UNWRAP \ |
| 8 | + assert(!args.Holder().IsEmpty()); \ |
| 9 | + assert(args.Holder()->InternalFieldCount() > 0); \ |
| 10 | + StdIOWrap* wrap = \ |
| 11 | + static_cast<StdIOWrap*>(args.Holder()->GetPointerFromInternalField(0)); \ |
| 12 | + if (!wrap) { \ |
| 13 | + SetErrno(UV_EBADF); \ |
| 14 | + return scope.Close(Integer::New(-1)); \ |
| 15 | + } |
| 16 | + |
| 17 | +namespace node { |
| 18 | + |
| 19 | +using v8::Object; |
| 20 | +using v8::Handle; |
| 21 | +using v8::Local; |
| 22 | +using v8::Persistent; |
| 23 | +using v8::Value; |
| 24 | +using v8::HandleScope; |
| 25 | +using v8::FunctionTemplate; |
| 26 | +using v8::String; |
| 27 | +using v8::Function; |
| 28 | +using v8::TryCatch; |
| 29 | +using v8::Context; |
| 30 | +using v8::Arguments; |
| 31 | +using v8::Integer; |
| 32 | +using v8::Undefined; |
| 33 | + |
| 34 | +extern Persistent<Function> tcpConstructor; |
| 35 | +extern Persistent<Function> pipeConstructor; |
| 36 | +static Persistent<Function> constructor; |
| 37 | + |
| 38 | + |
| 39 | +class StdIOWrap : StreamWrap { |
| 40 | + public: |
| 41 | + static void Initialize(Handle<Object> target) { |
| 42 | + StreamWrap::Initialize(target); |
| 43 | + |
| 44 | + HandleScope scope; |
| 45 | + |
| 46 | + Local<FunctionTemplate> t = FunctionTemplate::New(New); |
| 47 | + t->SetClassName(String::NewSymbol("StdIO")); |
| 48 | + |
| 49 | + t->InstanceTemplate()->SetInternalFieldCount(1); |
| 50 | + |
| 51 | + NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); |
| 52 | + NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); |
| 53 | + NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write); |
| 54 | + NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen); |
| 55 | + |
| 56 | + constructor = Persistent<Function>::New(t->GetFunction()); |
| 57 | + |
| 58 | + target->Set(String::NewSymbol("StdIO"), constructor); |
| 59 | + } |
| 60 | + |
| 61 | + private: |
| 62 | + static Handle<Value> New(const Arguments& args) { |
| 63 | + // This constructor should not be exposed to public javascript. |
| 64 | + // Therefore we assert that we are not trying to call this as a |
| 65 | + // normal function. |
| 66 | + assert(args.IsConstructCall()); |
| 67 | + |
| 68 | + uv_std_type stdHandleType = (uv_std_type)args[0]->Int32Value(); |
| 69 | + |
| 70 | + assert(stdHandleType == UV_STDIN || stdHandleType == UV_STDOUT || stdHandleType == UV_STDERR); |
| 71 | + |
| 72 | + uv_stream_t* stdHandle = uv_std_handle(stdHandleType); |
| 73 | + if (stdHandle) { |
| 74 | + HandleScope scope; |
| 75 | + StdIOWrap* wrap = new StdIOWrap(args.This()); |
| 76 | + assert(wrap); |
| 77 | + |
| 78 | + wrap->handle_ = stdHandle; |
| 79 | + wrap->SetHandle((uv_handle_t*)stdHandle); |
| 80 | + wrap->UpdateWriteQueueSize(); |
| 81 | + |
| 82 | + return scope.Close(args.This()); |
| 83 | + } else { |
| 84 | + return Undefined(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + StdIOWrap(Handle<Object> object) : StreamWrap(object, NULL) { |
| 89 | + } |
| 90 | + |
| 91 | + static Handle<Value> Listen(const Arguments& args) { |
| 92 | + HandleScope scope; |
| 93 | + |
| 94 | + UNWRAP |
| 95 | + |
| 96 | + int backlog = args[0]->Int32Value(); |
| 97 | + |
| 98 | + int r = uv_listen(wrap->handle_, SOMAXCONN, OnConnection); |
| 99 | + |
| 100 | + // Error starting the pipe. |
| 101 | + if (r) SetErrno(uv_last_error().code); |
| 102 | + |
| 103 | + return scope.Close(Integer::New(r)); |
| 104 | + } |
| 105 | + |
| 106 | + // TODO maybe share with TCPWrap? |
| 107 | + static void OnConnection(uv_stream_t* handle, int status) { |
| 108 | + HandleScope scope; |
| 109 | + Local<Object> client_obj; |
| 110 | + |
| 111 | + StdIOWrap* wrap = static_cast<StdIOWrap*>(handle->data); |
| 112 | + assert(wrap->handle_ == handle); |
| 113 | + |
| 114 | + // We should not be getting this callback if someone as already called |
| 115 | + // uv_close() on the handle. |
| 116 | + assert(wrap->object_.IsEmpty() == false); |
| 117 | + |
| 118 | + if (status != 0) { |
| 119 | + // TODO Handle server error (set errno and call onconnection with NULL) |
| 120 | + assert(0); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Instanciate the client javascript object and handle. |
| 125 | + switch (handle->type) { |
| 126 | + case UV_TCP: |
| 127 | + client_obj = tcpConstructor->NewInstance(); |
| 128 | + break; |
| 129 | + case UV_NAMED_PIPE: |
| 130 | + client_obj = pipeConstructor->NewInstance(); |
| 131 | + break; |
| 132 | + default: |
| 133 | + assert(0); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Unwrap the client javascript object. |
| 138 | + assert(client_obj->InternalFieldCount() > 0); |
| 139 | + StreamWrap* client_wrap = |
| 140 | + static_cast<StreamWrap*>(client_obj->GetPointerFromInternalField(0)); |
| 141 | + |
| 142 | + int r = uv_accept(handle, client_wrap->GetStream()); |
| 143 | + |
| 144 | + // uv_accept should always work. |
| 145 | + assert(r == 0); |
| 146 | + |
| 147 | + // Successful accept. Call the onconnection callback in JavaScript land. |
| 148 | + Local<Value> argv[1] = { client_obj }; |
| 149 | + MakeCallback(wrap->object_, "onconnection", 1, argv); |
| 150 | + } |
| 151 | + |
| 152 | + uv_stream_t* handle_; |
| 153 | +}; |
| 154 | + |
| 155 | +} // namespace node |
| 156 | + |
| 157 | +NODE_MODULE(node_stdio_wrap, node::StdIOWrap::Initialize); |
0 commit comments