Revision 5e7e51c2 src/stream_wrap.cc
src/stream_wrap.cc | ||
---|---|---|
27 | 27 |
#include "pipe_wrap.h" |
28 | 28 |
#include "tcp_wrap.h" |
29 | 29 |
#include "req_wrap.h" |
30 |
#include "udp_wrap.h" |
|
30 | 31 |
#include "node_counters.h" |
31 | 32 |
|
32 | 33 |
#include <stdlib.h> // abort() |
... | ... | |
118 | 119 |
|
119 | 120 |
void StreamWrap::SetHandle(uv_handle_t* h) { |
120 | 121 |
HandleWrap::SetHandle(h); |
121 |
stream_ = (uv_stream_t*)h;
|
|
122 |
stream_ = reinterpret_cast<uv_stream_t*>(h);
|
|
122 | 123 |
stream_->data = this; |
123 | 124 |
} |
124 | 125 |
|
... | ... | |
173 | 174 |
} |
174 | 175 |
|
175 | 176 |
|
177 |
template <class WrapType, class UVType> |
|
178 |
static Local<Object> AcceptHandle(uv_stream_t* pipe) { |
|
179 |
HandleScope scope; |
|
180 |
Local<Object> wrap_obj; |
|
181 |
WrapType* wrap; |
|
182 |
UVType* handle; |
|
183 |
|
|
184 |
wrap_obj = WrapType::Instantiate(); |
|
185 |
if (wrap_obj.IsEmpty()) |
|
186 |
return Local<Object>(); |
|
187 |
|
|
188 |
wrap = static_cast<WrapType*>( |
|
189 |
wrap_obj->GetAlignedPointerFromInternalField(0)); |
|
190 |
handle = wrap->UVHandle(); |
|
191 |
|
|
192 |
if (uv_accept(pipe, reinterpret_cast<uv_stream_t*>(handle))) |
|
193 |
abort(); |
|
194 |
|
|
195 |
return scope.Close(wrap_obj); |
|
196 |
} |
|
197 |
|
|
198 |
|
|
176 | 199 |
void StreamWrap::OnReadCommon(uv_stream_t* handle, ssize_t nread, |
177 | 200 |
uv_buf_t buf, uv_handle_type pending) { |
178 | 201 |
HandleScope scope; |
... | ... | |
212 | 235 |
|
213 | 236 |
Local<Object> pending_obj; |
214 | 237 |
if (pending == UV_TCP) { |
215 |
pending_obj = TCPWrap::Instantiate();
|
|
238 |
pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(handle);
|
|
216 | 239 |
} else if (pending == UV_NAMED_PIPE) { |
217 |
pending_obj = PipeWrap::Instantiate(); |
|
240 |
pending_obj = AcceptHandle<PipeWrap, uv_pipe_t>(handle); |
|
241 |
} else if (pending == UV_UDP) { |
|
242 |
pending_obj = AcceptHandle<UDPWrap, uv_udp_t>(handle); |
|
218 | 243 |
} else { |
219 |
// We only support sending UV_TCP and UV_NAMED_PIPE right now. |
|
220 | 244 |
assert(pending == UV_UNKNOWN_HANDLE); |
221 | 245 |
} |
222 | 246 |
|
223 | 247 |
if (!pending_obj.IsEmpty()) { |
224 |
assert(pending_obj->InternalFieldCount() > 0); |
|
225 |
StreamWrap* pending_wrap = static_cast<StreamWrap*>( |
|
226 |
pending_obj->GetAlignedPointerFromInternalField(0)); |
|
227 |
if (uv_accept(handle, pending_wrap->GetStream())) abort(); |
|
228 | 248 |
argv[3] = pending_obj; |
229 | 249 |
argc++; |
230 | 250 |
} |
... | ... | |
246 | 266 |
|
247 | 267 |
void StreamWrap::OnRead2(uv_pipe_t* handle, ssize_t nread, uv_buf_t buf, |
248 | 268 |
uv_handle_type pending) { |
249 |
OnReadCommon((uv_stream_t*)handle, nread, buf, pending);
|
|
269 |
OnReadCommon(reinterpret_cast<uv_stream_t*>(handle), nread, buf, pending);
|
|
250 | 270 |
} |
251 | 271 |
|
252 | 272 |
|
... | ... | |
404 | 424 |
StreamWrap::AfterWrite); |
405 | 425 |
|
406 | 426 |
} else { |
407 |
uv_stream_t* send_stream = NULL;
|
|
427 |
uv_handle_t* send_handle = NULL;
|
|
408 | 428 |
|
409 | 429 |
if (args[1]->IsObject()) { |
410 |
Local<Object> send_stream_obj = args[1]->ToObject();
|
|
411 |
assert(send_stream_obj->InternalFieldCount() > 0);
|
|
412 |
StreamWrap* send_stream_wrap = static_cast<StreamWrap*>(
|
|
413 |
send_stream_obj->GetAlignedPointerFromInternalField(0));
|
|
414 |
send_stream = send_stream_wrap->GetStream();
|
|
430 |
Local<Object> send_handle_obj = args[1]->ToObject();
|
|
431 |
assert(send_handle_obj->InternalFieldCount() > 0);
|
|
432 |
HandleWrap* send_handle_wrap = static_cast<HandleWrap*>(
|
|
433 |
send_handle_obj->GetAlignedPointerFromInternalField(0));
|
|
434 |
send_handle = send_handle_wrap->GetHandle();
|
|
415 | 435 |
|
416 | 436 |
// Reference StreamWrap instance to prevent it from being garbage |
417 | 437 |
// collected before `AfterWrite` is called. |
... | ... | |
419 | 439 |
handle_sym = NODE_PSYMBOL("handle"); |
420 | 440 |
} |
421 | 441 |
assert(!req_wrap->object_.IsEmpty()); |
422 |
req_wrap->object_->Set(handle_sym, send_stream_obj);
|
|
442 |
req_wrap->object_->Set(handle_sym, send_handle_obj);
|
|
423 | 443 |
} |
424 | 444 |
|
425 | 445 |
r = uv_write2(&req_wrap->req_, |
426 | 446 |
wrap->stream_, |
427 | 447 |
&buf, |
428 | 448 |
1, |
429 |
send_stream,
|
|
449 |
reinterpret_cast<uv_stream_t*>(send_handle),
|
|
430 | 450 |
StreamWrap::AfterWrite); |
431 | 451 |
} |
432 | 452 |
|
Also available in: Unified diff