moonbridge

diff moonbridge.c @ 43:f2efab1ba3d0

Methods :close(), :cancel(), and :readuntil(...) do not throw I/O errors but return error message as second return value after nil
author jbe
date Mon Mar 09 16:42:55 2015 +0100 (2015-03-09)
parents 0bb356c04f6b
children b3d04d528c83
line diff
     1.1 --- a/moonbridge.c	Sun Mar 08 01:09:28 2015 +0100
     1.2 +++ b/moonbridge.c	Mon Mar 09 16:42:55 2015 +0100
     1.3 @@ -808,13 +808,6 @@
     1.4    }
     1.5  }
     1.6  
     1.7 -/* Throws a Lua error message with an error string for errno appended to it */
     1.8 -static void moonbr_child_lua_errno_error(lua_State *L, char *message) {
     1.9 -  char errmsg[MOONBR_MAXSTRERRORLEN];
    1.10 -  strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.11 -  luaL_error(L, "%s: %s", message, errmsg);
    1.12 -}
    1.13 -
    1.14  /* Closes the input stream from peer unless it has already been closed */
    1.15  static int moonbr_child_close_peersocket_inputstream(
    1.16    int cleanshut,  /* nonzero = use shutdown() if applicable */
    1.17 @@ -984,16 +977,24 @@
    1.18    luaL_Stream *stream = lua_touserdata(L, 1);
    1.19    if (stream == moonbr_child_peersocket_inputstream) {
    1.20      if (moonbr_child_close_peersocket_inputstream(1, 0)) {  /* don't mark as closed as it's done by Lua */
    1.21 -      moonbr_child_lua_errno_error(L, "Could not close input stream");
    1.22 +      char errmsg[MOONBR_MAXSTRERRORLEN];
    1.23 +      strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.24 +      lua_pushnil(L);
    1.25 +      lua_pushfstring(L, "Could not close input stream from peer: %s", errmsg);
    1.26 +      return 2;
    1.27      }
    1.28    } else if (stream == moonbr_child_peersocket_outputstream) {
    1.29      if (moonbr_child_close_peersocket_outputstream(1, 0)) {  /* don't mark as closed as it's done by Lua */
    1.30 -      moonbr_child_lua_errno_error(L, "Could not close output stream");
    1.31 +      char errmsg[MOONBR_MAXSTRERRORLEN];
    1.32 +      strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.33 +      lua_pushnil(L);
    1.34 +      lua_pushfstring(L, "Could not close output stream to peer: %s", errmsg);
    1.35 +      return 2;
    1.36      }
    1.37    } else {
    1.38      luaL_argerror(L, 1, "Not a connection socket");
    1.39    }
    1.40 -  lua_pushboolean(L, 1);  // TODO: return nil or false on error instead of throwing error
    1.41 +  lua_pushboolean(L, 1);
    1.42    return 1;
    1.43  }
    1.44  
    1.45 @@ -1009,9 +1010,13 @@
    1.46      luaL_error(L, "Connection with peer has already been explicitly closed");
    1.47    }
    1.48    if (moonbr_child_close_peersocket(timeout)) {
    1.49 -    moonbr_child_lua_errno_error(L, "Could not close socket connection with peer");
    1.50 +    char errmsg[MOONBR_MAXSTRERRORLEN];
    1.51 +    strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.52 +    lua_pushnil(L);
    1.53 +    lua_pushfstring(L, "Could not close socket connection with peer: %s", errmsg);
    1.54 +    return 2;
    1.55    }
    1.56 -  lua_pushboolean(L, 1);  // TODO: return nil or false on error instead of throwing error
    1.57 +  lua_pushboolean(L, 1);
    1.58    return 1;
    1.59  }
    1.60  
    1.61 @@ -1021,9 +1026,13 @@
    1.62      luaL_error(L, "Connection with peer has already been explicitly closed");
    1.63    }
    1.64    if (moonbr_child_cancel_peersocket()) {
    1.65 -    moonbr_child_lua_errno_error(L, "Could not cancel socket connection with peer");
    1.66 +    char errmsg[MOONBR_MAXSTRERRORLEN];
    1.67 +    strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.68 +    lua_pushnil(L);
    1.69 +    lua_pushfstring(L, "Could not cancel socket connection with peer: %s", errmsg);
    1.70 +    return 2;
    1.71    }
    1.72 -  lua_pushboolean(L, 1);  // TODO: return nil or false on error instead of throwing error
    1.73 +  lua_pushboolean(L, 1);
    1.74    return 1;
    1.75  }
    1.76  
    1.77 @@ -2245,10 +2254,12 @@
    1.78    while (maxlen > 0 ? maxlen-- : maxlen) {
    1.79      byte = fgetc(file);
    1.80      if (byte == EOF) {
    1.81 -      if (ferror(file)) {  // TODO: return nil or false on error instead of throwing error
    1.82 +      if (ferror(file)) {
    1.83          char errmsg[MOONBR_MAXSTRERRORLEN];
    1.84          strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.85 -        luaL_error(L, "%s", errmsg);
    1.86 +        lua_pushnil(L);
    1.87 +        lua_pushstring(L, errmsg);
    1.88 +        return 2;
    1.89        } else {
    1.90          break;
    1.91        }

Impressum / About Us