GeezerVison is showing github.com — src: DRY getsockname/getpeername code · nodejs/node@31823e3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_internals.h +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,6 +79,19 @@ v8::Local<v8::Object> AddressToJS( | 79 | 79 | const sockaddr* addr, | 80 | 80 | v8::Local<v8::Object> info = v8::Handle<v8::Object>()); | 81 | 81 |
| | 82 | +template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)> | | 83 | +void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) { | | 84 | + T* const wrap = Unwrap<T>(args.Holder()); | | 85 | + CHECK(args[0]->IsObject()); | | 86 | + sockaddr_storage storage; | | 87 | + int addrlen = sizeof(storage); | | 88 | + sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage); | | 89 | + const int err = F(&wrap->handle_, addr, &addrlen); | | 90 | + if (err == 0) | | 91 | + AddressToJS(wrap->env(), addr, args[0].As<v8::Object>()); | | 92 | + args.GetReturnValue().Set(err); | | 93 | +} | | 94 | + | 82 | 95 | #ifdef _WIN32 | 83 | 96 | // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer | 84 | 97 | // on overflow... | |
Collapse file Copy file name to clipboardExpand all lines: src/tcp_wrap.cc +4-46Lines changed: 4 additions & 46 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -98,8 +98,10 @@ void TCPWrap::Initialize(Handle<Object> target, | 98 | 98 | env->SetProtoMethod(t, "connect", Connect); | 99 | 99 | env->SetProtoMethod(t, "bind6", Bind6); | 100 | 100 | env->SetProtoMethod(t, "connect6", Connect6); | 101 | | - env->SetProtoMethod(t, "getsockname", GetSockName); | 102 | | - env->SetProtoMethod(t, "getpeername", GetPeerName); | | 101 | + env->SetProtoMethod(t, "getsockname", | | 102 | + GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>); | | 103 | + env->SetProtoMethod(t, "getpeername", | | 104 | + GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>); | 103 | 105 | env->SetProtoMethod(t, "setNoDelay", SetNoDelay); | 104 | 106 | env->SetProtoMethod(t, "setKeepAlive", SetKeepAlive); | 105 | 107 |
| @@ -162,50 +164,6 @@ TCPWrap::~TCPWrap() { | 162 | 164 | } | 163 | 165 |
| 164 | 166 |
| 165 | | -void TCPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) { | 166 | | - Environment* env = Environment::GetCurrent(args); | 167 | | - struct sockaddr_storage address; | 168 | | - | 169 | | - TCPWrap* wrap = Unwrap<TCPWrap>(args.Holder()); | 170 | | - | 171 | | - CHECK(args[0]->IsObject()); | 172 | | - Local<Object> out = args[0].As<Object>(); | 173 | | - | 174 | | - int addrlen = sizeof(address); | 175 | | - int err = uv_tcp_getsockname(&wrap->handle_, | 176 | | - reinterpret_cast<sockaddr*>(&address), | 177 | | - &addrlen); | 178 | | - if (err == 0) { | 179 | | - const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address); | 180 | | - AddressToJS(env, addr, out); | 181 | | - } | 182 | | - | 183 | | - args.GetReturnValue().Set(err); | 184 | | -} | 185 | | - | 186 | | - | 187 | | -void TCPWrap::GetPeerName(const FunctionCallbackInfo<Value>& args) { | 188 | | - Environment* env = Environment::GetCurrent(args); | 189 | | - struct sockaddr_storage address; | 190 | | - | 191 | | - TCPWrap* wrap = Unwrap<TCPWrap>(args.Holder()); | 192 | | - | 193 | | - CHECK(args[0]->IsObject()); | 194 | | - Local<Object> out = args[0].As<Object>(); | 195 | | - | 196 | | - int addrlen = sizeof(address); | 197 | | - int err = uv_tcp_getpeername(&wrap->handle_, | 198 | | - reinterpret_cast<sockaddr*>(&address), | 199 | | - &addrlen); | 200 | | - if (err == 0) { | 201 | | - const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address); | 202 | | - AddressToJS(env, addr, out); | 203 | | - } | 204 | | - | 205 | | - args.GetReturnValue().Set(err); | 206 | | -} | 207 | | - | 208 | | - | 209 | 167 | void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) { | 210 | 168 | TCPWrap* wrap = Unwrap<TCPWrap>(args.Holder()); | 211 | 169 | int enable = static_cast<int>(args[0]->BooleanValue()); | |
Collapse file Copy file name to clipboardExpand all lines: src/tcp_wrap.h +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,12 +19,16 @@ class TCPWrap : public StreamWrap { | 19 | 19 | size_t self_size() const override { return sizeof(*this); } | 20 | 20 |
| 21 | 21 | private: | | 22 | + typedef uv_tcp_t HandleType; | | 23 | + | | 24 | + template <typename T, | | 25 | + int (*F)(const typename T::HandleType*, sockaddr*, int*)> | | 26 | + friend void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>&); | | 27 | + | 22 | 28 | TCPWrap(Environment* env, v8::Handle<v8::Object> object, AsyncWrap* parent); | 23 | 29 | ~TCPWrap(); | 24 | 30 |
| 25 | 31 | static void New(const v8::FunctionCallbackInfo<v8::Value>& args); | 26 | | - static void GetSockName(const v8::FunctionCallbackInfo<v8::Value>& args); | 27 | | - static void GetPeerName(const v8::FunctionCallbackInfo<v8::Value>& args); | 28 | 32 | static void SetNoDelay(const v8::FunctionCallbackInfo<v8::Value>& args); | 29 | 33 | static void SetKeepAlive(const v8::FunctionCallbackInfo<v8::Value>& args); | 30 | 34 | static void Bind(const v8::FunctionCallbackInfo<v8::Value>& args); | |
Collapse file Copy file name to clipboardExpand all lines: src/udp_wrap.cc +2-24Lines changed: 2 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -96,7 +96,8 @@ void UDPWrap::Initialize(Handle<Object> target, | 96 | 96 | env->SetProtoMethod(t, "close", Close); | 97 | 97 | env->SetProtoMethod(t, "recvStart", RecvStart); | 98 | 98 | env->SetProtoMethod(t, "recvStop", RecvStop); | 99 | | - env->SetProtoMethod(t, "getsockname", GetSockName); | | 99 | + env->SetProtoMethod(t, "getsockname", | | 100 | + GetSockOrPeerName<UDPWrap, uv_udp_getsockname>); | 100 | 101 | env->SetProtoMethod(t, "addMembership", AddMembership); | 101 | 102 | env->SetProtoMethod(t, "dropMembership", DropMembership); | 102 | 103 | env->SetProtoMethod(t, "setMulticastTTL", SetMulticastTTL); | @@ -325,29 +326,6 @@ void UDPWrap::RecvStop(const FunctionCallbackInfo<Value>& args) { | 325 | 326 | } | 326 | 327 |
| 327 | 328 |
| 328 | | -void UDPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) { | 329 | | - Environment* env = Environment::GetCurrent(args); | 330 | | - | 331 | | - struct sockaddr_storage address; | 332 | | - UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder()); | 333 | | - | 334 | | - CHECK(args[0]->IsObject()); | 335 | | - Local<Object> obj = args[0].As<Object>(); | 336 | | - | 337 | | - int addrlen = sizeof(address); | 338 | | - int err = uv_udp_getsockname(&wrap->handle_, | 339 | | - reinterpret_cast<sockaddr*>(&address), | 340 | | - &addrlen); | 341 | | - | 342 | | - if (err == 0) { | 343 | | - const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address); | 344 | | - AddressToJS(env, addr, obj); | 345 | | - } | 346 | | - | 347 | | - args.GetReturnValue().Set(err); | 348 | | -} | 349 | | - | 350 | | - | 351 | 329 | // TODO(bnoordhuis) share with StreamWrap::AfterWrite() in stream_wrap.cc | 352 | 330 | void UDPWrap::OnSend(uv_udp_send_t* req, int status) { | 353 | 331 | SendWrap* req_wrap = static_cast<SendWrap*>(req->data); | |
Collapse file Copy file name to clipboardExpand all lines: src/udp_wrap.h +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,12 @@ class UDPWrap: public HandleWrap { | 40 | 40 | size_t self_size() const override { return sizeof(*this); } | 41 | 41 |
| 42 | 42 | private: | | 43 | + typedef uv_udp_t HandleType; | | 44 | + | | 45 | + template <typename T, | | 46 | + int (*F)(const typename T::HandleType*, sockaddr*, int*)> | | 47 | + friend void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>&); | | 48 | + | 43 | 49 | UDPWrap(Environment* env, v8::Handle<v8::Object> object, AsyncWrap* parent); | 44 | 50 |
| 45 | 51 | static void DoBind(const v8::FunctionCallbackInfo<v8::Value>& args, | |
|