comparison src/luasocket.c @ 0:4b915342e2a8

LuaSocket 2.0.2 + CMake build description.
author Eric Wing <ewing . public |-at-| gmail . com>
date Tue, 26 Aug 2008 18:40:01 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4b915342e2a8
1 /*=========================================================================*\
2 * LuaSocket toolkit
3 * Networking support for the Lua language
4 * Diego Nehab
5 * 26/11/1999
6 *
7 * This library is part of an effort to progressively increase the network
8 * connectivity of the Lua language. The Lua interface to networking
9 * functions follows the Sockets API closely, trying to simplify all tasks
10 * involved in setting up both client and server connections. The provided
11 * IO routines, however, follow the Lua style, being very similar to the
12 * standard Lua read and write functions.
13 *
14 * RCS ID: $Id: luasocket.c,v 1.53 2005/10/07 04:40:59 diego Exp $
15 \*=========================================================================*/
16
17 /*=========================================================================*\
18 * Standard include files
19 \*=========================================================================*/
20 #include "lua.h"
21 #include "lauxlib.h"
22
23 #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
24 #include "compat-5.1.h"
25 #endif
26
27 /*=========================================================================*\
28 * LuaSocket includes
29 \*=========================================================================*/
30 #include "luasocket.h"
31 #include "auxiliar.h"
32 #include "except.h"
33 #include "timeout.h"
34 #include "buffer.h"
35 #include "inet.h"
36 #include "tcp.h"
37 #include "udp.h"
38 #include "select.h"
39
40 /*-------------------------------------------------------------------------*\
41 * Internal function prototypes
42 \*-------------------------------------------------------------------------*/
43 static int global_skip(lua_State *L);
44 static int global_unload(lua_State *L);
45 static int base_open(lua_State *L);
46
47 /*-------------------------------------------------------------------------*\
48 * Modules and functions
49 \*-------------------------------------------------------------------------*/
50 static const luaL_reg mod[] = {
51 {"auxiliar", auxiliar_open},
52 {"except", except_open},
53 {"timeout", timeout_open},
54 {"buffer", buffer_open},
55 {"inet", inet_open},
56 {"tcp", tcp_open},
57 {"udp", udp_open},
58 {"select", select_open},
59 {NULL, NULL}
60 };
61
62 static luaL_reg func[] = {
63 {"skip", global_skip},
64 {"__unload", global_unload},
65 {NULL, NULL}
66 };
67
68 /*-------------------------------------------------------------------------*\
69 * Skip a few arguments
70 \*-------------------------------------------------------------------------*/
71 static int global_skip(lua_State *L) {
72 int amount = luaL_checkint(L, 1);
73 int ret = lua_gettop(L) - amount - 1;
74 return ret >= 0 ? ret : 0;
75 }
76
77 /*-------------------------------------------------------------------------*\
78 * Unloads the library
79 \*-------------------------------------------------------------------------*/
80 static int global_unload(lua_State *L) {
81 (void) L;
82 socket_close();
83 return 0;
84 }
85
86 /*-------------------------------------------------------------------------*\
87 * Setup basic stuff.
88 \*-------------------------------------------------------------------------*/
89 static int base_open(lua_State *L) {
90 if (socket_open()) {
91 /* export functions (and leave namespace table on top of stack) */
92 luaL_openlib(L, "socket", func, 0);
93 #ifdef LUASOCKET_DEBUG
94 lua_pushstring(L, "_DEBUG");
95 lua_pushboolean(L, 1);
96 lua_rawset(L, -3);
97 #endif
98 /* make version string available to scripts */
99 lua_pushstring(L, "_VERSION");
100 lua_pushstring(L, LUASOCKET_VERSION);
101 lua_rawset(L, -3);
102 return 1;
103 } else {
104 lua_pushstring(L, "unable to initialize library");
105 lua_error(L);
106 return 0;
107 }
108 }
109
110 /*-------------------------------------------------------------------------*\
111 * Initializes all library modules.
112 \*-------------------------------------------------------------------------*/
113 LUASOCKET_API int luaopen_socket_core(lua_State *L) {
114 int i;
115 base_open(L);
116 for (i = 0; mod[i].name; i++) mod[i].func(L);
117 return 1;
118 }