-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream_base: expose and use External pointer #2351
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,18 +106,31 @@ class WriteWrap: public ReqWrap<uv_write_t>, | |
|
||
class StreamResource { | ||
public: | ||
template <class T> | ||
struct Callback { | ||
Callback() : fn(nullptr), ctx(nullptr) {} | ||
Callback(T fn, void* ctx) : fn(fn), ctx(ctx) {} | ||
Callback(const Callback&) = default; | ||
|
||
inline bool is_empty() { return fn == nullptr; } | ||
inline void clear() { | ||
fn = nullptr; | ||
ctx = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the memory be freed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope! ;) |
||
} | ||
|
||
T fn; | ||
void* ctx; | ||
}; | ||
|
||
typedef void (*AfterWriteCb)(WriteWrap* w, void* ctx); | ||
typedef void (*AllocCb)(size_t size, uv_buf_t* buf, void* ctx); | ||
typedef void (*ReadCb)(ssize_t nread, | ||
const uv_buf_t* buf, | ||
uv_handle_type pending, | ||
void* ctx); | ||
|
||
StreamResource() : after_write_cb_(nullptr), | ||
alloc_cb_(nullptr), | ||
read_cb_(nullptr) { | ||
StreamResource() { | ||
} | ||
|
||
virtual ~StreamResource() = default; | ||
|
||
virtual int DoShutdown(ShutdownWrap* req_wrap) = 0; | ||
|
@@ -131,44 +144,37 @@ class StreamResource { | |
|
||
// Events | ||
inline void OnAfterWrite(WriteWrap* w) { | ||
if (after_write_cb_ != nullptr) | ||
after_write_cb_(w, after_write_ctx_); | ||
if (!after_write_cb_.is_empty()) | ||
after_write_cb_.fn(w, after_write_cb_.ctx); | ||
} | ||
|
||
inline void OnAlloc(size_t size, uv_buf_t* buf) { | ||
if (alloc_cb_ != nullptr) | ||
alloc_cb_(size, buf, alloc_ctx_); | ||
if (!alloc_cb_.is_empty()) | ||
alloc_cb_.fn(size, buf, alloc_cb_.ctx); | ||
} | ||
|
||
inline void OnRead(size_t nread, | ||
const uv_buf_t* buf, | ||
uv_handle_type pending = UV_UNKNOWN_HANDLE) { | ||
if (read_cb_ != nullptr) | ||
read_cb_(nread, buf, pending, read_ctx_); | ||
if (!read_cb_.is_empty()) | ||
read_cb_.fn(nread, buf, pending, read_cb_.ctx); | ||
} | ||
|
||
inline void set_after_write_cb(AfterWriteCb cb, void* ctx) { | ||
after_write_ctx_ = ctx; | ||
after_write_cb_ = cb; | ||
inline void set_after_write_cb(Callback<AfterWriteCb> c) { | ||
after_write_cb_ = c; | ||
} | ||
|
||
inline void set_alloc_cb(AllocCb cb, void* ctx) { | ||
alloc_cb_ = cb; | ||
alloc_ctx_ = ctx; | ||
} | ||
inline void set_alloc_cb(Callback<AllocCb> c) { alloc_cb_ = c; } | ||
inline void set_read_cb(Callback<ReadCb> c) { read_cb_ = c; } | ||
|
||
inline void set_read_cb(ReadCb cb, void* ctx) { | ||
read_cb_ = cb; | ||
read_ctx_ = ctx; | ||
} | ||
inline Callback<AfterWriteCb> after_write_cb() { return after_write_cb_; } | ||
inline Callback<AllocCb> alloc_cb() { return alloc_cb_; } | ||
inline Callback<ReadCb> read_cb() { return read_cb_; } | ||
|
||
private: | ||
AfterWriteCb after_write_cb_; | ||
void* after_write_ctx_; | ||
AllocCb alloc_cb_; | ||
void* alloc_ctx_; | ||
ReadCb read_cb_; | ||
void* read_ctx_; | ||
Callback<AfterWriteCb> after_write_cb_; | ||
Callback<AllocCb> alloc_cb_; | ||
Callback<ReadCb> read_cb_; | ||
}; | ||
|
||
class StreamBase : public StreamResource { | ||
|
@@ -211,7 +217,9 @@ class StreamBase : public StreamResource { | |
|
||
virtual ~StreamBase() = default; | ||
|
||
virtual AsyncWrap* GetAsyncWrap() = 0; | ||
// One of these must be implemented | ||
virtual AsyncWrap* GetAsyncWrap(); | ||
virtual v8::Local<v8::Object> GetObject(); | ||
|
||
// Libuv callbacks | ||
static void AfterShutdown(ShutdownWrap* req, int status); | ||
|
@@ -227,8 +235,12 @@ class StreamBase : public StreamResource { | |
int WriteString(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
|
||
template <class Base> | ||
static void GetFD(v8::Local<v8::String>, | ||
const v8::PropertyCallbackInfo<v8::Value>&); | ||
static void GetFD(v8::Local<v8::String> key, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @trevnorris fixed! |
||
const v8::PropertyCallbackInfo<v8::Value>& args); | ||
|
||
template <class Base> | ||
static void GetExternal(v8::Local<v8::String> key, | ||
const v8::PropertyCallbackInfo<v8::Value>& args); | ||
|
||
template <class Base, | ||
int (StreamBase::*Method)( // NOLINT(whitespace/parens) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
#include "node_crypto_bio.h" // NodeBIO | ||
#include "node_crypto_clienthello.h" // ClientHelloParser | ||
#include "node_crypto_clienthello-inl.h" | ||
#include "node_wrap.h" // WithGenericStream | ||
#include "node_counters.h" | ||
#include "node_internals.h" | ||
#include "stream_base.h" | ||
|
@@ -63,12 +62,12 @@ TLSWrap::TLSWrap(Environment* env, | |
SSL_CTX_sess_set_new_cb(sc_->ctx_, SSLWrap<TLSWrap>::NewSessionCallback); | ||
|
||
stream_->Consume(); | ||
stream_->set_after_write_cb(OnAfterWriteImpl, this); | ||
stream_->set_alloc_cb(OnAllocImpl, this); | ||
stream_->set_read_cb(OnReadImpl, this); | ||
stream_->set_after_write_cb({ OnAfterWriteImpl, this }); | ||
stream_->set_alloc_cb({ OnAllocImpl, this }); | ||
stream_->set_read_cb({ OnReadImpl, this }); | ||
|
||
set_alloc_cb(OnAllocSelf, this); | ||
set_read_cb(OnReadSelf, this); | ||
set_alloc_cb({ OnAllocSelf, this }); | ||
set_read_cb({ OnReadSelf, this }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry. This has nothing to do w/ the PR, but mind explaining this syntax? I haven't seen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is just a way of initializing struct, class in-line. So the |
||
|
||
InitSSL(); | ||
} | ||
|
@@ -177,15 +176,12 @@ void TLSWrap::Wrap(const FunctionCallbackInfo<Value>& args) { | |
if (args.Length() < 3 || !args[2]->IsBoolean()) | ||
return env->ThrowTypeError("Third argument should be boolean"); | ||
|
||
Local<Object> stream_obj = args[0].As<Object>(); | ||
Local<External> stream_obj = args[0].As<External>(); | ||
Local<Object> sc = args[1].As<Object>(); | ||
Kind kind = args[2]->IsTrue() ? SSLWrap<TLSWrap>::kServer : | ||
SSLWrap<TLSWrap>::kClient; | ||
|
||
StreamBase* stream = nullptr; | ||
WITH_GENERIC_STREAM(env, stream_obj, { | ||
stream = wrap; | ||
}); | ||
StreamBase* stream = static_cast<StreamBase*>(stream_obj->Value()); | ||
CHECK_NE(stream, nullptr); | ||
|
||
TLSWrap* res = new TLSWrap(env, kind, stream, Unwrap<SecureContext>(sc)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
key
an unused argument?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is.