-------------------------------------------------------------------------*\ * Receives data from a UDP socket \*-------------------------------------------------------------------------*/
| 236 | * Receives data from a UDP socket |
| 237 | \*-------------------------------------------------------------------------*/ |
| 238 | static 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 |
nothing calls this directly
no test coverage detected