# HG changeset patch # User jbe # Date 1428511094 -7200 # Node ID 5e8eeb5b6c848f0cf3eb927db743bb620dd8048c # Parent 4f9e4c6109f46e904d87d6c30feb0796d65edfb4 Some code cleanup in I/O library diff -r 4f9e4c6109f4 -r 5e8eeb5b6c84 moonbridge_io.c --- a/moonbridge_io.c Wed Apr 08 17:43:31 2015 +0200 +++ b/moonbridge_io.c Wed Apr 08 18:38:14 2015 +0200 @@ -96,21 +96,19 @@ if (!handle->isnetwork || handle->nopush == nopush) return; #if defined(TCP_NOPUSH) if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) { +#elif defined(TCP_CORK) + if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) { +#endif moonbr_io_errmsg(); close(handle->fd); handle->fd = -1; handle->closed = 1; +#if defined(TCP_NOPUSH) luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg); - } #elif defined(TCP_CORK) - if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) { - moonbr_io_errmsg(); - close(handle->fd); - handle->fd = -1; - handle->closed = 1; luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg); +#endif } -#endif handle->nopush = nopush; #else #warning Neither TCP_NOPUSH nor TCP_CORK is available @@ -137,17 +135,18 @@ } lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */ if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle"); - if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */ if (handle->readerr) { lua_pushnil(L); lua_pushliteral(L, "Previous read error"); return 2; } + if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */ + handle->readerr = 1; moonbr_io_handle_set_nonblocking(L, handle, nonblocking); if (!drain) luaL_buffinit(L, &luabuf); while (1) { endcnt = -1; - if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) { + if (maxread > 0 && handle->readbufcnt >= (size_t)maxread - luabufcnt) { endcnt = maxread - luabufcnt; } else if (terminatorlen) { terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt); @@ -163,6 +162,7 @@ } handle->readbufcnt -= endcnt; memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt); + handle->readerr = 0; return 1; } if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt); @@ -174,7 +174,6 @@ if (bytesread == 0 || (nonblocking && bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break; if (bytesread < 0) { moonbr_io_errmsg(); - handle->readerr = 1; lua_pushnil(L); lua_pushstring(L, errmsg); return 2; @@ -189,6 +188,7 @@ handle->readbufcnt = 0; if (!drain) { if (!luabufcnt && bytesread == 0) { + handle->readerr = 0; moonbr_io_read_impl_eof: lua_pushboolean(L, 0); lua_pushliteral(L, "End of file"); @@ -198,6 +198,7 @@ if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1); else lua_pushboolean(L, luabufcnt); } + handle->readerr = 0; return 1; } @@ -406,8 +407,8 @@ lua_pushstring(L, errmsg); return 2; } + handle->fd = -1; } - handle->fd = -1; lua_pushboolean(L, 1); return 1;