@@ -93,7 +93,6 @@ LibuvStreamWrap::LibuvStreamWrap(Environment* env,
9393 provider),
9494 StreamBase(env),
9595 stream_(stream) {
96- PushStreamListener(this);
9796}
9897
9998
@@ -146,7 +145,13 @@ bool LibuvStreamWrap::IsIPCPipe() {
146145
147146
148147int LibuvStreamWrap::ReadStart() {
149- return uv_read_start(stream(), OnAlloc, OnRead);
148+ return uv_read_start(stream(), [](uv_handle_t* handle,
149+ size_t suggested_size,
150+ uv_buf_t* buf) {
151+ static_cast<LibuvStreamWrap*>(handle->data)->OnUvAlloc(suggested_size, buf);
152+ }, [](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
153+ static_cast<LibuvStreamWrap*>(stream->data)->OnUvRead(nread, buf);
154+ });
150155}
151156
152157
@@ -155,16 +160,11 @@ int LibuvStreamWrap::ReadStop() {
155160}
156161
157162
158- void LibuvStreamWrap::OnAlloc(uv_handle_t* handle,
159- size_t suggested_size,
160- uv_buf_t* buf) {
161- LibuvStreamWrap* wrap = static_cast<LibuvStreamWrap*>(handle->data);
162- HandleScope scope(wrap->env()->isolate());
163- Context::Scope context_scope(wrap->env()->context());
164-
165- CHECK_EQ(wrap->stream(), reinterpret_cast<uv_stream_t*>(handle));
163+ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) {
164+ HandleScope scope(env()->isolate());
165+ Context::Scope context_scope(env()->context());
166166
167- *buf = wrap-> EmitAlloc(suggested_size);
167+ *buf = EmitAlloc(suggested_size);
168168}
169169
170170
@@ -190,64 +190,47 @@ static Local<Object> AcceptHandle(Environment* env, LibuvStreamWrap* parent) {
190190}
191191
192192
193- void LibuvStreamWrap::OnStreamRead(ssize_t nread,
194- const uv_buf_t& buf,
195- uv_handle_type pending) {
196- HandleScope handle_scope(env()->isolate());
193+ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
194+ HandleScope scope(env()->isolate());
197195 Context::Scope context_scope(env()->context());
198-
199- if (nread <= 0) {
200- free(buf.base);
201- if (nread < 0)
202- CallJSOnreadMethod(nread, Local<Object>());
203- return;
204- }
205-
206- CHECK_LE(static_cast<size_t>(nread), buf.len);
207-
208- Local<Object> pending_obj;
209-
210- if (pending == UV_TCP) {
211- pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(env(), this);
212- } else if (pending == UV_NAMED_PIPE) {
213- pending_obj = AcceptHandle<PipeWrap, uv_pipe_t>(env(), this);
214- } else if (pending == UV_UDP) {
215- pending_obj = AcceptHandle<UDPWrap, uv_udp_t>(env(), this);
216- } else {
217- CHECK_EQ(pending, UV_UNKNOWN_HANDLE);
218- }
219-
220- Local<Object> obj = Buffer::New(env(), buf.base, nread).ToLocalChecked();
221- CallJSOnreadMethod(nread, obj, pending_obj);
222- }
223-
224-
225- void LibuvStreamWrap::OnRead(uv_stream_t* handle,
226- ssize_t nread,
227- const uv_buf_t* buf) {
228- LibuvStreamWrap* wrap = static_cast<LibuvStreamWrap*>(handle->data);
229- HandleScope scope(wrap->env()->isolate());
230- Context::Scope context_scope(wrap->env()->context());
231196 uv_handle_type type = UV_UNKNOWN_HANDLE;
232197
233- if (wrap-> is_named_pipe_ipc() &&
234- uv_pipe_pending_count(reinterpret_cast<uv_pipe_t*>(handle )) > 0) {
235- type = uv_pipe_pending_type(reinterpret_cast<uv_pipe_t*>(handle ));
198+ if (is_named_pipe_ipc() &&
199+ uv_pipe_pending_count(reinterpret_cast<uv_pipe_t*>(stream() )) > 0) {
200+ type = uv_pipe_pending_type(reinterpret_cast<uv_pipe_t*>(stream() ));
236201 }
237202
238203 // We should not be getting this callback if someone as already called
239204 // uv_close() on the handle.
240- CHECK_EQ(wrap-> persistent().IsEmpty(), false);
205+ CHECK_EQ(persistent().IsEmpty(), false);
241206
242207 if (nread > 0) {
243- if (wrap-> is_tcp()) {
208+ if (is_tcp()) {
244209 NODE_COUNT_NET_BYTES_RECV(nread);
245- } else if (wrap-> is_named_pipe()) {
210+ } else if (is_named_pipe()) {
246211 NODE_COUNT_PIPE_BYTES_RECV(nread);
247212 }
213+
214+ Local<Object> pending_obj;
215+
216+ if (type == UV_TCP) {
217+ pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(env(), this);
218+ } else if (type == UV_NAMED_PIPE) {
219+ pending_obj = AcceptHandle<PipeWrap, uv_pipe_t>(env(), this);
220+ } else if (type == UV_UDP) {
221+ pending_obj = AcceptHandle<UDPWrap, uv_udp_t>(env(), this);
222+ } else {
223+ CHECK_EQ(type, UV_UNKNOWN_HANDLE);
224+ }
225+
226+ if (!pending_obj.IsEmpty()) {
227+ object()->Set(env()->context(),
228+ env()->pending_handle_string(),
229+ pending_obj).FromJust();
230+ }
248231 }
249232
250- wrap-> EmitRead(nread, *buf, type );
233+ EmitRead(nread, *buf);
251234}
252235
253236
@@ -373,11 +356,6 @@ void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) {
373356 req_wrap->Done(status);
374357}
375358
376-
377- void LibuvStreamWrap::AfterWrite(WriteWrap* w, int status) {
378- StreamBase::AfterWrite(w, status);
379- }
380-
381359} // namespace node
382360
383361NODE_BUILTIN_MODULE_CONTEXT_AWARE(stream_wrap,
0 commit comments