moonbridge
diff moonbridge_io.c @ 104:5e8eeb5b6c84
Some code cleanup in I/O library
author | jbe |
---|---|
date | Wed Apr 08 18:38:14 2015 +0200 (2015-04-08) |
parents | 4f9e4c6109f4 |
children | 11d03470ef19 |
line diff
1.1 --- a/moonbridge_io.c Wed Apr 08 17:43:31 2015 +0200 1.2 +++ b/moonbridge_io.c Wed Apr 08 18:38:14 2015 +0200 1.3 @@ -96,21 +96,19 @@ 1.4 if (!handle->isnetwork || handle->nopush == nopush) return; 1.5 #if defined(TCP_NOPUSH) 1.6 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) { 1.7 +#elif defined(TCP_CORK) 1.8 + if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) { 1.9 +#endif 1.10 moonbr_io_errmsg(); 1.11 close(handle->fd); 1.12 handle->fd = -1; 1.13 handle->closed = 1; 1.14 +#if defined(TCP_NOPUSH) 1.15 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg); 1.16 - } 1.17 #elif defined(TCP_CORK) 1.18 - if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) { 1.19 - moonbr_io_errmsg(); 1.20 - close(handle->fd); 1.21 - handle->fd = -1; 1.22 - handle->closed = 1; 1.23 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg); 1.24 +#endif 1.25 } 1.26 -#endif 1.27 handle->nopush = nopush; 1.28 #else 1.29 #warning Neither TCP_NOPUSH nor TCP_CORK is available 1.30 @@ -137,17 +135,18 @@ 1.31 } 1.32 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */ 1.33 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle"); 1.34 - if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */ 1.35 if (handle->readerr) { 1.36 lua_pushnil(L); 1.37 lua_pushliteral(L, "Previous read error"); 1.38 return 2; 1.39 } 1.40 + if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */ 1.41 + handle->readerr = 1; 1.42 moonbr_io_handle_set_nonblocking(L, handle, nonblocking); 1.43 if (!drain) luaL_buffinit(L, &luabuf); 1.44 while (1) { 1.45 endcnt = -1; 1.46 - if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) { 1.47 + if (maxread > 0 && handle->readbufcnt >= (size_t)maxread - luabufcnt) { 1.48 endcnt = maxread - luabufcnt; 1.49 } else if (terminatorlen) { 1.50 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt); 1.51 @@ -163,6 +162,7 @@ 1.52 } 1.53 handle->readbufcnt -= endcnt; 1.54 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt); 1.55 + handle->readerr = 0; 1.56 return 1; 1.57 } 1.58 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt); 1.59 @@ -174,7 +174,6 @@ 1.60 if (bytesread == 0 || (nonblocking && bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break; 1.61 if (bytesread < 0) { 1.62 moonbr_io_errmsg(); 1.63 - handle->readerr = 1; 1.64 lua_pushnil(L); 1.65 lua_pushstring(L, errmsg); 1.66 return 2; 1.67 @@ -189,6 +188,7 @@ 1.68 handle->readbufcnt = 0; 1.69 if (!drain) { 1.70 if (!luabufcnt && bytesread == 0) { 1.71 + handle->readerr = 0; 1.72 moonbr_io_read_impl_eof: 1.73 lua_pushboolean(L, 0); 1.74 lua_pushliteral(L, "End of file"); 1.75 @@ -198,6 +198,7 @@ 1.76 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1); 1.77 else lua_pushboolean(L, luabufcnt); 1.78 } 1.79 + handle->readerr = 0; 1.80 return 1; 1.81 } 1.82 1.83 @@ -406,8 +407,8 @@ 1.84 lua_pushstring(L, errmsg); 1.85 return 2; 1.86 } 1.87 + handle->fd = -1; 1.88 } 1.89 - handle->fd = -1; 1.90 lua_pushboolean(L, 1); 1.91 return 1; 1.92