moonbridge

changeset 261:6c634c96805b

Work on non-blocking close (for TLS)
author jbe
date Tue Sep 20 01:31:28 2016 +0200 (2016-09-20)
parents 1dc729343830
children 446db91762b7
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Mon Sep 19 01:12:38 2016 +0200
     1.2 +++ b/moonbridge_io.c	Tue Sep 20 01:31:28 2016 +0200
     1.3 @@ -102,6 +102,7 @@
     1.4    struct tls *tls;
     1.5    struct tls *servertls;
     1.6    int tlshandshake;
     1.7 +  int tlsclosing;
     1.8  #endif
     1.9  } moonbr_io_handle_t;
    1.10  
    1.11 @@ -754,13 +755,13 @@
    1.12    return 1;
    1.13  }
    1.14  
    1.15 -static int moonbr_io_close_impl(lua_State *L, int reset) {
    1.16 +static int moonbr_io_close_impl(lua_State *L, int nonblocking, int reset) {
    1.17    moonbr_io_handle_t *handle;
    1.18    handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
    1.19    if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
    1.20    if (!reset && handle->fd >= 0) {
    1.21      if (handle->writeleft) {
    1.22 -      lua_pushcfunction(L, moonbr_io_flush);
    1.23 +      lua_pushcfunction(L, nonblocking ? moonbr_io_flush_nb : moonbr_io_flush);
    1.24        lua_pushvalue(L, 1);
    1.25        if (lua_pcall(L, 1, 2, 0)) {
    1.26          handle->closed = 1;
    1.27 @@ -768,21 +769,35 @@
    1.28          handle->fd = -1;
    1.29          lua_error(L);
    1.30        }
    1.31 -      handle->closed = 1;
    1.32 +      if (!nonblocking) handle->closed = 1;  /* TODO: handle nonblocking case */
    1.33        if (!lua_toboolean(L, -2)) {
    1.34          close(handle->fd);
    1.35          handle->fd = -1;
    1.36          return 2;
    1.37        }
    1.38 +#if LUA_VERSION_NUM >= 503
    1.39 +      if (nonblocking && lua_tointeger(L, -2)) {
    1.40 +#else
    1.41 +      if (nonblocking && lua_tonumber(L, -2)) {
    1.42 +#endif
    1.43 +        lua_pushliteral(L, "flush");
    1.44 +        lua_pushvalue(L, -3);
    1.45 +        return 2;
    1.46 +      }
    1.47      } else {
    1.48        handle->closed = 1;
    1.49      }
    1.50  #ifdef MOONBR_IO_USE_TLS
    1.51      if (handle->tls) {
    1.52        int status;
    1.53 -      if (moonbr_io_handle_set_nonblocking(L, handle, 1)) moonbr_io_return_errmsg();
    1.54 +      if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) moonbr_io_return_errmsg();
    1.55        do status = tls_close(handle->tls);
    1.56 -      while (status == TLS_WANT_POLLIN || status == TLS_WANT_POLLOUT);
    1.57 +      while (!nonblocking && (status == TLS_WANT_POLLIN || status == TLS_WANT_POLLOUT));
    1.58 +      if (status == TLS_WANT_POLLIN || status == TLS_WANT_POLLOUT) {
    1.59 +        handle->tlsclosing = status;  /* TODO: handle polling */
    1.60 +        lua_pushliteral(L, "close");
    1.61 +        return 1;
    1.62 +      }
    1.63        if (status) {
    1.64          close(handle->fd);
    1.65          handle->fd = -1;
    1.66 @@ -814,11 +829,11 @@
    1.67  }
    1.68  
    1.69  static int moonbr_io_close(lua_State *L) {
    1.70 -  return moonbr_io_close_impl(L, 0);
    1.71 +  return moonbr_io_close_impl(L, 0, 0);
    1.72  }
    1.73  
    1.74  static int moonbr_io_reset(lua_State *L) {
    1.75 -  return moonbr_io_close_impl(L, 1);
    1.76 +  return moonbr_io_close_impl(L, 0, 1);
    1.77  }
    1.78  
    1.79  static int moonbr_io_handlegc(lua_State *L) {
    1.80 @@ -891,6 +906,7 @@
    1.81    handle->tls = NULL;
    1.82    handle->servertls = NULL;
    1.83    handle->tlshandshake = 0;
    1.84 +  handle->tlsclosing = 0;
    1.85  #endif
    1.86    handle->fd = *fd;  /* required for set_linger call */
    1.87    if (moonbr_io_handle_set_linger(L, handle, 0)) {

Impressum / About Us