| 171 | } |
| 172 | |
| 173 | static 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 |
nothing calls this directly
no test coverage detected