moonbridge

changeset 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 e835cda61478
files moonbridge.c reference.txt
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        }
     2.1 --- a/reference.txt	Sun Mar 08 01:09:28 2015 +0100
     2.2 +++ b/reference.txt	Mon Mar 09 16:42:55 2015 +0100
     2.3 @@ -31,7 +31,9 @@
     2.4  ### socket:cancel()
     2.5  
     2.6  Closes the socket connection by sending a TCP RST package if possible to
     2.7 -indicate error condition.
     2.8 +indicate error condition. Returns true on success, or nil plus error message in
     2.9 +case of an I/O error. Using this method on sockets that have already been
    2.10 +closed (or canceled) will throw an error.
    2.11  
    2.12  Warning: Previously sent (and flushed) data may be lost during transmission.
    2.13  
    2.14 @@ -39,8 +41,9 @@
    2.15  ### socket:close(timeout)
    2.16  
    2.17  Closes the socket connection (input and output stream) by flushing all data and
    2.18 -sending a TCP FIN package. Performs no operation if stream has already been
    2.19 -closed.
    2.20 +sending a TCP FIN package. Returns true on success, or nil plus error message
    2.21 +in case of an I/O error. Using this method on sockets that have already been
    2.22 +closed (or canceled) will throw an error.
    2.23  
    2.24  Warning: Pending data on the input stream may cause connection aborts (TCP RST)
    2.25  depending on the particular operating system used. All pending input data
    2.26 @@ -106,10 +109,12 @@
    2.27  
    2.28  Reads as many bytes until a byte equal to the terminator value occurs. An
    2.29  optional maximum length may be specified. The terminating byte is included in
    2.30 -the return value (unless the maximum length would be exceeded).
    2.31 +the return value (unless the maximum length would be exceeded). On EOF, nil is
    2.32 +returned. In case of an I/O error, nil (as first result value) plus an error
    2.33 +message (as second result value) is returned.
    2.34  
    2.35 -Also available as :readuntil(...) method for any other Lua file handle
    2.36 -(including socket.input)
    2.37 +This method is also available as :readuntil(...) for any other Lua file handle
    2.38 +(including socket.input).
    2.39  
    2.40  
    2.41  ### socket.remote_ip4

Impressum / About Us