Merge pull request #43 from moteus/moteus-getopt-error

Add. Allow get `error` option to socket.
This commit is contained in:
Diego Nehab 2013-05-29 04:20:33 -07:00
commit 5341131cd0
5 changed files with 43 additions and 0 deletions

View File

@ -254,6 +254,19 @@ static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
return 1;
}
int opt_get_error(lua_State *L, p_socket ps)
{
int val = 0;
socklen_t len = sizeof(val);
if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
lua_pushnil(L);
lua_pushstring(L, "getsockopt failed");
return 2;
}
lua_pushstring(L, socket_strerror(val));
return 1;
}
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
{
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */

View File

@ -42,6 +42,7 @@ int opt_get_linger(lua_State *L, p_socket ps);
int opt_get_reuseaddr(lua_State *L, p_socket ps);
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
int opt_get_error(lua_State *L, p_socket ps);
/* invokes the appropriate option handler */
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);

View File

@ -73,6 +73,7 @@ static t_opt optget[] = {
{"reuseaddr", opt_get_reuseaddr},
{"tcp-nodelay", opt_get_tcp_nodelay},
{"linger", opt_get_linger},
{"error", opt_get_error},
{NULL, NULL}
};

View File

@ -85,6 +85,7 @@ static t_opt optset[] = {
static t_opt optget[] = {
{"ip-multicast-if", opt_get_ip_multicast_if},
{"ip-multicast-loop", opt_get_ip_multicast_loop},
{"error", opt_get_error},
{NULL, NULL}
};

View File

@ -0,0 +1,27 @@
local socket = require "socket"
local host, port = "127.0.0.1", "5462"
assert(socket.bind(host, port)):close()
local sock = socket.tcp()
sock:settimeout(0)
local ok, err = sock:connect(host, port)
assert(not ok)
assert('timeout' == err)
for i = 1, 10 do
-- select pass even if socket has error
local _, rec, err = socket.select(nil, {sock}, 1)
assert('timeout' == err)
assert(not next(rec))
err = sock:getoption("error") -- i get 'connection refused' on WinXP
if err then
print("Passed! Error is '" .. err .. "'.")
os.exit(0)
end
end
print("Fail! No error detected!")
os.exit(1)