From e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Fri, 21 Aug 2015 15:39:34 -0300 Subject: New compat.h module implements luaL_setfuncs. Makes initialization code simpler everywhere. --- src/unix.c | 56 ++++++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 36 deletions(-) (limited to 'src/unix.c') diff --git a/src/unix.c b/src/unix.c index 91aaaf8..5bc3148 100644 --- a/src/unix.c +++ b/src/unix.c @@ -1,8 +1,8 @@ /*=========================================================================*\ -* Unix domain socket +* Unix domain socket * LuaSocket toolkit \*=========================================================================*/ -#include +#include #include "lua.h" #include "lauxlib.h" @@ -11,7 +11,7 @@ #include "socket.h" #include "options.h" #include "unix.h" -#include +#include /*=========================================================================*\ * Internal function prototypes @@ -68,15 +68,6 @@ static t_opt optset[] = { {NULL, NULL} }; -/* our socket creation function */ -/* this is an ad-hoc module that returns a single function - * as such, do not include other functions in this array. */ -static luaL_Reg func[] = { - {"unix", global_create}, - {NULL, NULL} -}; - - /*-------------------------------------------------------------------------*\ * Initializes module \*-------------------------------------------------------------------------*/ @@ -89,15 +80,8 @@ int luaopen_socket_unix(lua_State *L) { auxiliar_add2group(L, "unix{master}", "unix{any}"); auxiliar_add2group(L, "unix{client}", "unix{any}"); auxiliar_add2group(L, "unix{server}", "unix{any}"); -#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) - lua_pushcfunction(L, global_create); - (void) func; -#else - /* set function into socket namespace */ - luaL_openlib(L, "socket", func, 0); - lua_pushcfunction(L, global_create); -#endif /* return the function instead of the 'socket' table */ + lua_pushcfunction(L, global_create); return 1; } @@ -147,7 +131,7 @@ static int meth_getfd(lua_State *L) { /* this is very dangerous, but can be handy for those that are brave enough */ static int meth_setfd(lua_State *L) { p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); - un->sock = (t_socket) luaL_checknumber(L, 2); + un->sock = (t_socket) luaL_checknumber(L, 2); return 0; } @@ -158,8 +142,8 @@ static int meth_dirty(lua_State *L) { } /*-------------------------------------------------------------------------*\ -* Waits for and returns a client object attempting connection to the -* server object +* Waits for and returns a client object attempting connection to the +* server object \*-------------------------------------------------------------------------*/ static int meth_accept(lua_State *L) { p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); @@ -173,20 +157,20 @@ static int meth_accept(lua_State *L) { /* initialize structure fields */ socket_setnonblocking(&sock); clnt->sock = sock; - io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, + io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, (p_error) socket_ioerror, &clnt->sock); timeout_init(&clnt->tm, -1, -1); buffer_init(&clnt->buf, &clnt->io, &clnt->tm); return 1; } else { - lua_pushnil(L); + lua_pushnil(L); lua_pushstring(L, socket_strerror(err)); return 2; } } /*-------------------------------------------------------------------------*\ -* Binds an object to an address +* Binds an object to an address \*-------------------------------------------------------------------------*/ static const char *unix_trybind(p_unix un, const char *path) { struct sockaddr_un local; @@ -197,16 +181,16 @@ static const char *unix_trybind(p_unix un, const char *path) { strcpy(local.sun_path, path); local.sun_family = AF_UNIX; #ifdef UNIX_HAS_SUN_LEN - local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) + local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) + len + 1; err = socket_bind(&un->sock, (SA *) &local, local.sun_len); -#else - err = socket_bind(&un->sock, (SA *) &local, +#else + err = socket_bind(&un->sock, (SA *) &local, sizeof(local.sun_family) + len); #endif if (err != IO_DONE) socket_destroy(&un->sock); - return socket_strerror(err); + return socket_strerror(err); } static int meth_bind(lua_State *L) { @@ -236,11 +220,11 @@ static const char *unix_tryconnect(p_unix un, const char *path) remote.sun_family = AF_UNIX; timeout_markstart(&un->tm); #ifdef UNIX_HAS_SUN_LEN - remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) + remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) + len + 1; err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); #else - err = socket_connect(&un->sock, (SA *) &remote, + err = socket_connect(&un->sock, (SA *) &remote, sizeof(remote.sun_family) + len, &un->tm); #endif if (err != IO_DONE) socket_destroy(&un->sock); @@ -264,7 +248,7 @@ static int meth_connect(lua_State *L) } /*-------------------------------------------------------------------------*\ -* Closes socket used by object +* Closes socket used by object \*-------------------------------------------------------------------------*/ static int meth_close(lua_State *L) { @@ -319,13 +303,13 @@ static int meth_settimeout(lua_State *L) { * Library functions \*=========================================================================*/ /*-------------------------------------------------------------------------*\ -* Creates a master unix object +* Creates a master unix object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { t_socket sock; int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); /* try to allocate a system socket */ - if (err == IO_DONE) { + if (err == IO_DONE) { /* allocate unix object */ p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); /* set its type as master object */ @@ -333,7 +317,7 @@ static int global_create(lua_State *L) { /* initialize remaining structure fields */ socket_setnonblocking(&sock); un->sock = sock; - io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, + io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &un->sock); timeout_init(&un->tm, -1, -1); buffer_init(&un->buf, &un->io, &un->tm); -- cgit v1.2.3-55-g6feb