Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! dgram: make UDPWrap more reusable
  • Loading branch information
jasnell committed Mar 2, 2020
commit fe77e7cf769ffe39b5e2b846cf29e53610ffca4b
9 changes: 5 additions & 4 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ void UDPWrapBase::set_listener(UDPListener* listener) {
}

UDPWrapBase* UDPWrapBase::FromObject(Local<Object> obj) {
CHECK_GT(obj->InternalFieldCount(), UDPWrap::kUDPWrapBaseField);
CHECK_GT(obj->InternalFieldCount(), UDPWrapBase::kUDPWrapBaseField);
return static_cast<UDPWrapBase*>(
obj->GetAlignedPointerFromInternalField(UDPWrap::kUDPWrapBaseField));
obj->GetAlignedPointerFromInternalField(UDPWrapBase::kUDPWrapBaseField));
}

void UDPWrapBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
Expand All @@ -114,7 +114,7 @@ UDPWrap::UDPWrap(Environment* env, Local<Object> object)
reinterpret_cast<uv_handle_t*>(&handle_),
AsyncWrap::PROVIDER_UDPWRAP) {
object->SetAlignedPointerInInternalField(
kUDPWrapBaseField, static_cast<UDPWrapBase*>(this));
UDPWrapBase::kUDPWrapBaseField, static_cast<UDPWrapBase*>(this));

int r = uv_udp_init(env->event_loop(), &handle_);
CHECK_EQ(r, 0); // can't fail anyway
Expand All @@ -130,7 +130,8 @@ void UDPWrap::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);

Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(UDPWrap::kInternalFieldCount);
t->InstanceTemplate()->SetInternalFieldCount(
UDPWrapBase::kInternalFieldCount);
Local<String> udpString =
FIXED_ONE_BYTE_STRING(env->isolate(), "UDP");
t->SetClassName(udpString);
Expand Down
10 changes: 6 additions & 4 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class UDPListener {

class UDPWrapBase {
public:
// While UDPWrapBase itself does not extend from HandleWrap, classes
// derived from it will (like UDPWrap)
enum InternalFields {
kUDPWrapBaseField = HandleWrap::kInternalFieldCount,
kInternalFieldCount
};
virtual ~UDPWrapBase();

// Start emitting OnAlloc() + OnRecv() events on the listener.
Expand Down Expand Up @@ -115,10 +121,6 @@ class UDPWrap final : public HandleWrap,
public UDPWrapBase,
public UDPListener {
public:
enum InternalFields {
kUDPWrapBaseField = HandleWrap::kInternalFieldCount,
kInternalFieldCount
};
enum SocketType {
SOCKET
};
Expand Down