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

Function meth_receive

Plugins/UnLuaExtensions/LuaSocket/Source/src/unixdgram.cpp:173–197  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

171}
172
173static int meth_receive(lua_State *L) {
174 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
175 char buf[UNIXDGRAM_DATAGRAMSIZE];
176 size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
177 char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
178 int err;
179 p_timeout tm = &un->tm;
180 timeout_markstart(tm);
181 if (!dgram) {
182 lua_pushnil(L);
183 lua_pushliteral(L, "out of memory");
184 return 2;
185 }
186 err = socket_recv(&un->sock, dgram, wanted, &got, tm);
187 /* Unlike STREAM, recv() of zero is not closed, but a zero-length packet. */
188 if (err != IO_DONE && err != IO_CLOSED) {
189 lua_pushnil(L);
190 lua_pushstring(L, unixdgram_strerror(err));
191 if (wanted > sizeof(buf)) free(dgram);
192 return 2;
193 }
194 lua_pushlstring(L, dgram, got);
195 if (wanted > sizeof(buf)) free(dgram);
196 return 1;
197}
198
199/*-------------------------------------------------------------------------*\
200* Receives data and sender from a DGRAM 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
unixdgram_strerrorFunction · 0.85
lua_pushlstringFunction · 0.85
socket_recvFunction · 0.70

Tested by

no test coverage detected