@@ -310,10 +310,12 @@ void TLSWrap::EncOut() { |
310 | 310 |
|
311 | 311 |
|
312 | 312 | void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) { |
313 | | - // Report back to the previous listener as well. This is only needed for the |
314 | | - // "empty" writes that are passed through directly to the underlying stream. |
315 | | - if (req_wrap != nullptr) |
316 | | - previous_listener_->OnStreamAfterWrite(req_wrap, status); |
| 313 | + if (current_empty_write_ != nullptr) { |
| 314 | + WriteWrap* finishing = current_empty_write_; |
| 315 | + current_empty_write_ = nullptr; |
| 316 | + finishing->Done(status); |
| 317 | + return; |
| 318 | + } |
317 | 319 |
|
318 | 320 | if (ssl_ == nullptr) |
319 | 321 | status = UV_ECANCELED; |
@@ -579,18 +581,17 @@ int TLSWrap::DoWrite(WriteWrap* w, |
579 | 581 | // However, if there is any data that should be written to the socket, |
580 | 582 | // the callback should not be invoked immediately |
581 | 583 | if (BIO_pending(enc_out_) == 0) { |
582 | | - // We destroy the current WriteWrap* object and create a new one that |
583 | | - // matches the underlying stream, rather than the TLSWrap itself. |
584 | | - |
585 | | - // Note: We cannot simply use w->object() because of the "optimized" |
586 | | - // way in which we read persistent handles; the JS object itself might be |
587 | | - // destroyed by w->Dispose(), and the Local<Object> we have is not a |
588 | | - // "real" handle in the sense the V8 is aware of its existence. |
589 | | - Local<Object> req_wrap_obj = |
590 | | - w->GetAsyncWrap()->persistent().Get(env()->isolate()); |
591 | | - w->Dispose(); |
592 | | - w = underlying_stream()->CreateWriteWrap(req_wrap_obj); |
593 | | - return stream_->DoWrite(w, bufs, count, send_handle); |
| 584 | + CHECK_EQ(current_empty_write_, nullptr); |
| 585 | + current_empty_write_ = w; |
| 586 | + StreamWriteResult res = |
| 587 | + underlying_stream()->Write(bufs, count, send_handle); |
| 588 | + if (!res.async) { |
| 589 | + env()->SetImmediate([](Environment* env, void* data) { |
| 590 | + TLSWrap* self = static_cast<TLSWrap*>(data); |
| 591 | + self->OnStreamAfterWrite(self->current_empty_write_, 0); |
| 592 | + }, this, object()); |
| 593 | + } |
| 594 | + return 0; |
594 | 595 | } |
595 | 596 | } |
596 | 597 |
|
|