@@ -52,6 +52,25 @@ using v8::Uint32;
5252using v8::Undefined;
5353using v8::Value;
5454
55+ namespace {
56+ template <int (*fn)(uv_udp_t *, int )>
57+ void SetLibuvInt32 (const FunctionCallbackInfo<Value>& args) {
58+ UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder ());
59+ if (wrap == nullptr ) {
60+ args.GetReturnValue ().Set (UV_EBADF);
61+ return ;
62+ }
63+ Environment* env = wrap->env ();
64+ CHECK_EQ (args.Length (), 1 );
65+ int flag;
66+ if (!args[0 ]->Int32Value (env->context ()).To (&flag)) {
67+ return ;
68+ }
69+ int err = fn (wrap->GetLibuvHandle (), flag);
70+ args.GetReturnValue ().Set (err);
71+ }
72+ } // namespace
73+
5574class SendWrap : public ReqWrap <uv_udp_send_t > {
5675 public:
5776 SendWrap (Environment* env, Local<Object> req_wrap_obj, bool have_callback);
@@ -177,10 +196,15 @@ void UDPWrap::Initialize(Local<Object> target,
177196 SetProtoMethod (
178197 isolate, t, " dropSourceSpecificMembership" , DropSourceSpecificMembership);
179198 SetProtoMethod (isolate, t, " setMulticastInterface" , SetMulticastInterface);
180- SetProtoMethod (isolate, t, " setMulticastTTL" , SetMulticastTTL);
181- SetProtoMethod (isolate, t, " setMulticastLoopback" , SetMulticastLoopback);
182- SetProtoMethod (isolate, t, " setBroadcast" , SetBroadcast);
183- SetProtoMethod (isolate, t, " setTTL" , SetTTL);
199+ SetProtoMethod (
200+ isolate, t, " setMulticastTTL" , SetLibuvInt32<uv_udp_set_multicast_ttl>);
201+ SetProtoMethod (isolate,
202+ t,
203+ " setMulticastLoopback" ,
204+ SetLibuvInt32<uv_udp_set_multicast_loop>);
205+ SetProtoMethod (
206+ isolate, t, " setBroadcast" , SetLibuvInt32<uv_udp_set_broadcast>);
207+ SetProtoMethod (isolate, t, " setTTL" , SetLibuvInt32<uv_udp_set_ttl>);
184208 SetProtoMethod (isolate, t, " bufferSize" , BufferSize);
185209 SetProtoMethodNoSideEffect (isolate, t, " getSendQueueSize" , GetSendQueueSize);
186210 SetProtoMethodNoSideEffect (
@@ -373,30 +397,6 @@ void UDPWrap::Disconnect(const FunctionCallbackInfo<Value>& args) {
373397 args.GetReturnValue ().Set (err);
374398}
375399
376- #define X (name, fn ) \
377- void UDPWrap::name (const FunctionCallbackInfo<Value>& args) { \
378- UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder ()); \
379- if (wrap == nullptr ) { \
380- args.GetReturnValue ().Set (UV_EBADF); \
381- return ; \
382- } \
383- Environment* env = wrap->env (); \
384- CHECK_EQ (args.Length (), 1 ); \
385- int flag; \
386- if (!args[0 ]->Int32Value (env->context ()).To (&flag)) { \
387- return ; \
388- } \
389- int err = fn (&wrap->handle_ , flag); \
390- args.GetReturnValue ().Set (err); \
391- }
392-
393- X (SetTTL, uv_udp_set_ttl)
394- X (SetBroadcast, uv_udp_set_broadcast)
395- X (SetMulticastTTL, uv_udp_set_multicast_ttl)
396- X (SetMulticastLoopback, uv_udp_set_multicast_loop)
397-
398- #undef X
399-
400400void UDPWrap::SetMulticastInterface (const FunctionCallbackInfo<Value>& args) {
401401 UDPWrap* wrap;
402402 ASSIGN_OR_RETURN_UNWRAP (&wrap,
0 commit comments