moonbridge

changeset 144:e7fac0918f9c

Added new methods drain_call, drain_yield
author jbe
date Sat May 02 14:31:55 2015 +0200 (2015-05-02)
parents 41da87a681d6
children bd88dfa4f294
files moonbridge_io.c reference.txt
line diff
     1.1 --- a/moonbridge_io.c	Sat May 02 01:48:29 2015 +0200
     1.2 +++ b/moonbridge_io.c	Sat May 02 14:31:55 2015 +0200
     1.3 @@ -312,13 +312,13 @@
     1.4      } else {
     1.5        lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
     1.6      }
     1.7 -    if (remaining >= 0) {
     1.8 +    if (strcmp(lua_tostring(L, -1), "block") != 0) break;
     1.9 +    lua_pop(L, 1);
    1.10 +    if (remaining >= 0 && len) {
    1.11        remaining -= len;
    1.12        lua_pushinteger(L, remaining);
    1.13        lua_replace(L, 3);
    1.14      }
    1.15 -    if (strcmp(lua_tostring(L, -1), "block") != 0) break;
    1.16 -    lua_pop(L, 1);
    1.17      lua_pushvalue(L, 2);
    1.18      lua_pushvalue(L, 1);
    1.19      lua_pushliteral(L, "r");
    1.20 @@ -362,6 +362,65 @@
    1.21    return lua_gettop(L);
    1.22  }
    1.23  
    1.24 +#if LUA_VERSION_NUM >= 503
    1.25 +static int moonbr_io_drain_cont(lua_State *L, int status, lua_KContext ctx) {
    1.26 +#else
    1.27 +static int moonbr_io_drain_cont(lua_State *L) {
    1.28 +#endif
    1.29 +  lua_Integer remaining, len;
    1.30 +  size_t totallen = 0;
    1.31 +#if !(LUA_VERSION_NUM >= 503)
    1.32 +  int ctx = 0;
    1.33 +  lua_getctx(L, &ctx);
    1.34 +#endif
    1.35 +  remaining = lua_tointeger(L, 3);
    1.36 +  while (1) {
    1.37 +    lua_pushcfunction(L, moonbr_io_drain_nb);
    1.38 +    lua_pushvalue(L, 1);
    1.39 +    lua_pushvalue(L, 3);
    1.40 +    lua_pushvalue(L, 4);
    1.41 +    lua_call(L, 3, 2);
    1.42 +    if (lua_isnil(L, -2)) return 2;
    1.43 +    lua_insert(L, -2);
    1.44 +    len = lua_tointeger(L, -1);
    1.45 +    lua_pop(L, 1);
    1.46 +    totallen += len;
    1.47 +    if (strcmp(lua_tostring(L, -1), "block") != 0) break;
    1.48 +    lua_pop(L, 1);
    1.49 +    if (remaining >= 0 && len) {
    1.50 +      remaining -= len;
    1.51 +      lua_pushinteger(L, remaining);
    1.52 +      lua_replace(L, 3);
    1.53 +    }
    1.54 +    lua_pushvalue(L, 2);
    1.55 +    lua_pushvalue(L, 1);
    1.56 +    lua_pushliteral(L, "r");
    1.57 +    lua_callk(L, 2, 0, ctx, moonbr_io_drain_cont);
    1.58 +  }
    1.59 +  lua_pushinteger(L, totallen);
    1.60 +  lua_pushvalue(L, -2);
    1.61 +  return 2;
    1.62 +}
    1.63 +
    1.64 +static int moonbr_io_drain_call(lua_State *L) {
    1.65 +#if LUA_VERSION_NUM >= 503
    1.66 +  return moonbr_io_drain_cont(L, 0, 0);
    1.67 +#else
    1.68 +  return moonbr_io_drain_cont(L);
    1.69 +#endif
    1.70 +}
    1.71 +
    1.72 +static int moonbr_io_drain_yield(lua_State *L) {
    1.73 +  int args;
    1.74 +  lua_pushcfunction(L, moonbr_io_drain_call);
    1.75 +  lua_insert(L, 1);
    1.76 +  args = lua_gettop(L);
    1.77 +  lua_pushcfunction(L, moonbr_io_yield);
    1.78 +  lua_insert(L, 3);
    1.79 +  lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall);
    1.80 +  return lua_gettop(L);
    1.81 +}
    1.82 +
    1.83  static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
    1.84    moonbr_io_handle_t *handle;
    1.85    int i, top;
    1.86 @@ -1217,6 +1276,8 @@
    1.87    {"read_yield", moonbr_io_read_yield},
    1.88    {"drain", moonbr_io_drain},
    1.89    {"drain_nb", moonbr_io_drain_nb},
    1.90 +  {"drain_call", moonbr_io_drain_call},
    1.91 +  {"drain_yield", moonbr_io_drain_yield},
    1.92    {"write", moonbr_io_write},
    1.93    {"write_nb", moonbr_io_write_nb},
    1.94    {"flush", moonbr_io_flush},
     2.1 --- a/reference.txt	Sat May 02 01:48:29 2015 +0200
     2.2 +++ b/reference.txt	Sat May 02 14:31:55 2015 +0200
     2.3 @@ -77,6 +77,12 @@
     2.4  second return value) are returned.
     2.5  
     2.6  
     2.7 +### socket:drain_call(waitfunc, maxlen, terminator)
     2.8 +
     2.9 +Same as socket:drain(maxlen, terminator), but calls waitfunc(socket, "r") (in
    2.10 +an infinite loop) as long as the reading is blocked.
    2.11 +
    2.12 +
    2.13  ### socket:drain_nb(maxlen, terminator)
    2.14  
    2.15  Same as socket:drain(maxlen, terminator), but non-blocking. The status code
    2.16 @@ -87,6 +93,11 @@
    2.17  second return value) are returned.
    2.18  
    2.19  
    2.20 +### socket:drain_yield(maxlen, terminator)
    2.21 +
    2.22 +Alias for socket:drain_call(coroutine.yield, maxlen, terminator)
    2.23 +
    2.24 +
    2.25  ### socket:finish()
    2.26  
    2.27  Sends a TCP FIN packet to indicate EOF on write stream. Subsequent reads are

Impressum / About Us