2000-06-02 10:55:14 -07:00
|
|
|
/*=========================================================================*\
|
2003-06-26 11:47:49 -07:00
|
|
|
* LuaSocket toolkit
|
2002-07-03 12:06:52 -07:00
|
|
|
* Networking support for the Lua language
|
2000-06-02 10:55:14 -07:00
|
|
|
* Diego Nehab
|
|
|
|
* 26/11/1999
|
|
|
|
*
|
2002-07-03 12:06:52 -07:00
|
|
|
* This library is part of an effort to progressively increase the network
|
|
|
|
* connectivity of the Lua language. The Lua interface to networking
|
|
|
|
* functions follows the Sockets API closely, trying to simplify all tasks
|
|
|
|
* involved in setting up both client and server connections. The provided
|
2003-06-26 11:47:49 -07:00
|
|
|
* IO routines, however, follow the Lua style, being very similar to the
|
2002-07-03 12:06:52 -07:00
|
|
|
* standard Lua read and write functions.
|
2000-06-02 10:55:14 -07:00
|
|
|
\*=========================================================================*/
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
2002-07-03 12:06:52 -07:00
|
|
|
* Standard include files
|
2000-06-02 10:55:14 -07:00
|
|
|
\*=========================================================================*/
|
2005-09-28 23:11:42 -07:00
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
2015-08-21 11:39:34 -07:00
|
|
|
#include "compat.h"
|
2005-09-28 23:11:42 -07:00
|
|
|
|
2001-01-14 20:16:35 -08:00
|
|
|
/*=========================================================================*\
|
2002-07-03 12:06:52 -07:00
|
|
|
* LuaSocket includes
|
2001-01-14 20:16:35 -08:00
|
|
|
\*=========================================================================*/
|
2005-09-28 23:11:42 -07:00
|
|
|
#include "luasocket.h"
|
2003-06-26 11:47:49 -07:00
|
|
|
#include "auxiliar.h"
|
2004-06-14 23:24:00 -07:00
|
|
|
#include "except.h"
|
2003-06-09 11:23:40 -07:00
|
|
|
#include "timeout.h"
|
|
|
|
#include "buffer.h"
|
2003-05-24 18:54:13 -07:00
|
|
|
#include "inet.h"
|
|
|
|
#include "tcp.h"
|
|
|
|
#include "udp.h"
|
2003-06-09 11:23:40 -07:00
|
|
|
#include "select.h"
|
2004-02-04 06:29:11 -08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2004-06-14 23:24:00 -07:00
|
|
|
* Internal function prototypes
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int global_skip(lua_State *L);
|
|
|
|
static int global_unload(lua_State *L);
|
|
|
|
static int base_open(lua_State *L);
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Modules and functions
|
2004-02-04 06:29:11 -08:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2011-07-04 15:31:14 -07:00
|
|
|
static const luaL_Reg mod[] = {
|
2005-10-06 21:40:59 -07:00
|
|
|
{"auxiliar", auxiliar_open},
|
2004-06-14 23:24:00 -07:00
|
|
|
{"except", except_open},
|
2005-10-06 21:40:59 -07:00
|
|
|
{"timeout", timeout_open},
|
|
|
|
{"buffer", buffer_open},
|
2004-02-04 06:29:11 -08:00
|
|
|
{"inet", inet_open},
|
|
|
|
{"tcp", tcp_open},
|
|
|
|
{"udp", udp_open},
|
|
|
|
{"select", select_open},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2011-07-04 15:31:14 -07:00
|
|
|
static luaL_Reg func[] = {
|
2004-06-14 23:24:00 -07:00
|
|
|
{"skip", global_skip},
|
|
|
|
{"__unload", global_unload},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Skip a few arguments
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int global_skip(lua_State *L) {
|
2018-08-22 13:37:32 -07:00
|
|
|
int amount = (int) luaL_checkinteger(L, 1);
|
2004-06-14 23:24:00 -07:00
|
|
|
int ret = lua_gettop(L) - amount - 1;
|
|
|
|
return ret >= 0 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Unloads the library
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int global_unload(lua_State *L) {
|
2004-06-17 14:46:22 -07:00
|
|
|
(void) L;
|
2005-10-06 21:40:59 -07:00
|
|
|
socket_close();
|
2004-06-14 23:24:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Setup basic stuff.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int base_open(lua_State *L) {
|
2005-10-06 21:40:59 -07:00
|
|
|
if (socket_open()) {
|
2004-09-26 21:01:18 -07:00
|
|
|
/* export functions (and leave namespace table on top of stack) */
|
2013-05-29 01:56:56 -07:00
|
|
|
lua_newtable(L);
|
|
|
|
luaL_setfuncs(L, func, 0);
|
2004-06-14 23:24:00 -07:00
|
|
|
#ifdef LUASOCKET_DEBUG
|
2005-06-13 21:29:23 -07:00
|
|
|
lua_pushstring(L, "_DEBUG");
|
2004-06-14 23:24:00 -07:00
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
#endif
|
|
|
|
/* make version string available to scripts */
|
2005-06-13 21:29:23 -07:00
|
|
|
lua_pushstring(L, "_VERSION");
|
2004-06-14 23:24:00 -07:00
|
|
|
lua_pushstring(L, LUASOCKET_VERSION);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
lua_pushstring(L, "unable to initialize library");
|
|
|
|
lua_error(L);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-04 06:29:11 -08:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes all library modules.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2005-08-11 22:56:32 -07:00
|
|
|
LUASOCKET_API int luaopen_socket_core(lua_State *L) {
|
2004-05-27 23:16:43 -07:00
|
|
|
int i;
|
2004-06-14 23:24:00 -07:00
|
|
|
base_open(L);
|
2004-06-04 08:15:45 -07:00
|
|
|
for (i = 0; mod[i].name; i++) mod[i].func(L);
|
2003-06-09 11:23:40 -07:00
|
|
|
return 1;
|
2001-01-14 20:16:35 -08:00
|
|
|
}
|