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

Function meth_receive

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

-------------------------------------------------------------------------*\ * Receives data from a UDP socket \*-------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

236* Receives data from a UDP socket
237\*-------------------------------------------------------------------------*/
238static int meth_receive(lua_State *L) {
239 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
240 char buf[UDP_DATAGRAMSIZE];
241 size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
242 char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
243 int err;
244 p_timeout tm = &udp->tm;
245 timeout_markstart(tm);
246 if (!dgram) {
247 lua_pushnil(L);
248 lua_pushliteral(L, "out of memory");
249 return 2;
250 }
251 err = socket_recv(&udp->sock, dgram, wanted, &got, tm);
252 /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
253 if (err != IO_DONE && err != IO_CLOSED) {
254 lua_pushnil(L);
255 lua_pushstring(L, udp_strerror(err));
256 if (wanted > sizeof(buf)) free(dgram);
257 return 2;
258 }
259 lua_pushlstring(L, dgram, got);
260 if (wanted > sizeof(buf)) free(dgram);
261 return 1;
262}
263
264/*-------------------------------------------------------------------------*\
265* Receives data and sender from a UDP socket

Callers

nothing calls this directly

Calls 8

auxiliar_checkgroupFunction · 0.85
luaL_optnumberFunction · 0.85
timeout_markstartFunction · 0.85
lua_pushnilFunction · 0.85
lua_pushstringFunction · 0.85
udp_strerrorFunction · 0.85
lua_pushlstringFunction · 0.85
socket_recvFunction · 0.70

Tested by

no test coverage detected