From 21698c7665ee1cb43e1b83c3ea5cf4dbf827c1df Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 8 Aug 2011 16:11:47 -0700 Subject: [PATCH] Receive of zero for UDP is now possible. Previously, receive of zero was considered to be "closed", but that is only true for stream-based protocols, like TCP. --- src/udp.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/udp.c b/src/udp.c index e604bea..8168bf2 100644 --- a/src/udp.c +++ b/src/udp.c @@ -170,6 +170,9 @@ static int meth_receive(lua_State *L) { count = MIN(count, sizeof(buffer)); timeout_markstart(tm); err = socket_recv(&udp->sock, buffer, count, &got, tm); + /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ + if (err == IO_CLOSED) + err = IO_DONE; if (err != IO_DONE) { lua_pushnil(L); lua_pushstring(L, udp_strerror(err)); @@ -194,6 +197,9 @@ static int meth_receivefrom(lua_State *L) { count = MIN(count, sizeof(buffer)); err = socket_recvfrom(&udp->sock, buffer, count, &got, (SA *) &addr, &addr_len, tm); + /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ + if (err == IO_CLOSED) + err = IO_DONE; if (err == IO_DONE) { lua_pushlstring(L, buffer, got); lua_pushstring(L, inet_ntoa(addr.sin_addr));