luasocket.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /*=========================================================================*\
  15. * Standard include files
  16. \*=========================================================================*/
  17. #include "lua.h"
  18. #include "lauxlib.h"
  19. #include "compat.h"
  20. /*=========================================================================*\
  21. * LuaSocket includes
  22. \*=========================================================================*/
  23. #include "luasocket.h"
  24. #include "auxiliar.h"
  25. #include "except.h"
  26. #include "timeout.h"
  27. #include "buffer.h"
  28. #include "inet.h"
  29. #include "tcp.h"
  30. #include "udp.h"
  31. #include "select.h"
  32. /*-------------------------------------------------------------------------*\
  33. * Internal function prototypes
  34. \*-------------------------------------------------------------------------*/
  35. static int global_skip(lua_State *L);
  36. static int global_unload(lua_State *L);
  37. static int base_open(lua_State *L);
  38. /*-------------------------------------------------------------------------*\
  39. * Modules and functions
  40. \*-------------------------------------------------------------------------*/
  41. static const luaL_Reg mod[] = {
  42. {"auxiliar", auxiliar_open},
  43. {"except", except_open},
  44. {"timeout", timeout_open},
  45. {"buffer", buffer_open},
  46. {"inet", inet_open},
  47. {"tcp", tcp_open},
  48. {"udp", udp_open},
  49. {"select", select_open},
  50. {NULL, NULL}
  51. };
  52. static luaL_Reg func[] = {
  53. {"skip", global_skip},
  54. {"__unload", global_unload},
  55. {NULL, NULL}
  56. };
  57. /*-------------------------------------------------------------------------*\
  58. * Skip a few arguments
  59. \*-------------------------------------------------------------------------*/
  60. static int global_skip(lua_State *L) {
  61. int amount = (int)luaL_checkinteger(L, 1);
  62. int ret = lua_gettop(L) - amount - 1;
  63. return ret >= 0 ? ret : 0;
  64. }
  65. /*-------------------------------------------------------------------------*\
  66. * Unloads the library
  67. \*-------------------------------------------------------------------------*/
  68. static int global_unload(lua_State *L) {
  69. (void) L;
  70. socket_close();
  71. return 0;
  72. }
  73. /*-------------------------------------------------------------------------*\
  74. * Setup basic stuff.
  75. \*-------------------------------------------------------------------------*/
  76. static int base_open(lua_State *L) {
  77. if (socket_open()) {
  78. /* export functions (and leave namespace table on top of stack) */
  79. lua_newtable(L);
  80. luaL_setfuncs(L, func, 0);
  81. #ifdef LUASOCKET_DEBUG
  82. lua_pushstring(L, "_DEBUG");
  83. lua_pushboolean(L, 1);
  84. lua_rawset(L, -3);
  85. #endif
  86. /* make version string available to scripts */
  87. lua_pushstring(L, "_VERSION");
  88. lua_pushstring(L, LUASOCKET_VERSION);
  89. lua_rawset(L, -3);
  90. return 1;
  91. } else {
  92. lua_pushstring(L, "unable to initialize library");
  93. lua_error(L);
  94. return 0;
  95. }
  96. }
  97. /*-------------------------------------------------------------------------*\
  98. * Initializes all library modules.
  99. \*-------------------------------------------------------------------------*/
  100. LUASOCKET_API int luaopen_socket_core(lua_State *L) {
  101. int i;
  102. base_open(L);
  103. for (i = 0; mod[i].name; i++) mod[i].func(L);
  104. return 1;
  105. }