mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-08 14:28:21 +01:00
Add. Allow get error
option to socket.
This commit is contained in:
parent
b84a5f3d08
commit
1de617e355
@ -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 */
|
||||
|
@ -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);
|
||||
|
@ -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}
|
||||
};
|
||||
|
||||
|
@ -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}
|
||||
};
|
||||
|
||||
|
27
test/test_socket_error.lua
Normal file
27
test/test_socket_error.lua
Normal 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)
|
Loading…
Reference in New Issue
Block a user