MCPcopy Create free account
hub / github.com/Tencent/UnLua / meth_setpeername

Function meth_setpeername

Plugins/UnLuaExtensions/LuaSocket/Source/src/udp.cpp:390–419  ·  view source on GitHub ↗

-------------------------------------------------------------------------*\ * Turns a master udp object into a client object. \*-------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

388* Turns a master udp object into a client object.
389\*-------------------------------------------------------------------------*/
390static int meth_setpeername(lua_State *L) {
391 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
392 p_timeout tm = &udp->tm;
393 const char *address = luaL_checkstring(L, 2);
394 int connecting = strcmp(address, "*");
395 const char *port = connecting? luaL_checkstring(L, 3): "0";
396 struct addrinfo connecthints;
397 const char *err;
398 memset(&connecthints, 0, sizeof(connecthints));
399 connecthints.ai_socktype = SOCK_DGRAM;
400 /* make sure we try to connect only to the same family */
401 connecthints.ai_family = udp->family;
402 if (connecting) {
403 err = inet_tryconnect(&udp->sock, &udp->family, address,
404 port, tm, &connecthints);
405 if (err) {
406 lua_pushnil(L);
407 lua_pushstring(L, err);
408 return 2;
409 }
410 auxiliar_setclass(L, "udp{connected}", 1);
411 } else {
412 /* we ignore possible errors because Mac OS X always
413 * returns EAFNOSUPPORT */
414 inet_trydisconnect(&udp->sock, udp->family, tm);
415 auxiliar_setclass(L, "udp{unconnected}", 1);
416 }
417 lua_pushnumber(L, 1);
418 return 1;
419}
420
421/*-------------------------------------------------------------------------*\
422* Closes socket used by object

Callers

nothing calls this directly

Calls 7

auxiliar_checkgroupFunction · 0.85
inet_tryconnectFunction · 0.85
lua_pushnilFunction · 0.85
lua_pushstringFunction · 0.85
auxiliar_setclassFunction · 0.85
inet_trydisconnectFunction · 0.85
lua_pushnumberFunction · 0.85

Tested by

no test coverage detected