Skip to content
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

dgram: convert macro to template #47891

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 28 additions & 28 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ using v8::Uint32;
using v8::Undefined;
using v8::Value;

namespace {
template <int (*fn)(uv_udp_t*, int)>
void SetLibuvInt32(const FunctionCallbackInfo<Value>& args) {
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder());
if (wrap == nullptr) {
args.GetReturnValue().Set(UV_EBADF);
return;
}
Environment* env = wrap->env();
CHECK_EQ(args.Length(), 1);
int flag;
if (!args[0]->Int32Value(env->context()).To(&flag)) {
return;
}
int err = fn(wrap->GetLibuvHandle(), flag);
args.GetReturnValue().Set(err);
}
} // namespace

class SendWrap : public ReqWrap<uv_udp_send_t> {
public:
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
Expand Down Expand Up @@ -177,10 +196,15 @@ void UDPWrap::Initialize(Local<Object> target,
SetProtoMethod(
isolate, t, "dropSourceSpecificMembership", DropSourceSpecificMembership);
SetProtoMethod(isolate, t, "setMulticastInterface", SetMulticastInterface);
SetProtoMethod(isolate, t, "setMulticastTTL", SetMulticastTTL);
SetProtoMethod(isolate, t, "setMulticastLoopback", SetMulticastLoopback);
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
SetProtoMethod(isolate, t, "setTTL", SetTTL);
SetProtoMethod(
isolate, t, "setMulticastTTL", SetLibuvInt32<uv_udp_set_multicast_ttl>);
SetProtoMethod(isolate,
t,
"setMulticastLoopback",
SetLibuvInt32<uv_udp_set_multicast_loop>);
SetProtoMethod(
isolate, t, "setBroadcast", SetLibuvInt32<uv_udp_set_broadcast>);
SetProtoMethod(isolate, t, "setTTL", SetLibuvInt32<uv_udp_set_ttl>);
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
SetProtoMethodNoSideEffect(
Expand Down Expand Up @@ -373,30 +397,6 @@ void UDPWrap::Disconnect(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}

#define X(name, fn) \
void UDPWrap::name(const FunctionCallbackInfo<Value>& args) { \
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder()); \
if (wrap == nullptr) { \
args.GetReturnValue().Set(UV_EBADF); \
return; \
} \
Environment* env = wrap->env(); \
CHECK_EQ(args.Length(), 1); \
int flag; \
if (!args[0]->Int32Value(env->context()).To(&flag)) { \
return; \
} \
int err = fn(&wrap->handle_, flag); \
args.GetReturnValue().Set(err); \
}

X(SetTTL, uv_udp_set_ttl)
X(SetBroadcast, uv_udp_set_broadcast)
X(SetMulticastTTL, uv_udp_set_multicast_ttl)
X(SetMulticastLoopback, uv_udp_set_multicast_loop)

#undef X

void UDPWrap::SetMulticastInterface(const FunctionCallbackInfo<Value>& args) {
UDPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap,
Expand Down
7 changes: 2 additions & 5 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ class UDPWrap final : public HandleWrap,
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetMulticastInterface(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetMulticastTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetMulticastLoopback(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSendQueueSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSendQueueCount(
Expand All @@ -175,6 +170,8 @@ class UDPWrap final : public HandleWrap,

AsyncWrap* GetAsyncWrap() override;

inline uv_udp_t* GetLibuvHandle() { return &handle_; }

static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
Expand Down