From: "Xiongfei(Alex) Guo" <[email protected]>

Added fd_add method for uloop lua binding.

  Use uloop.fd_add like this:

    local socket = require "socket"

    udp = socket.udp()

    udp_read_ev = uloop.fd_add(
      udp, -- socket
      function( -- callback function
        ufd,    -- socket object when register the fd
        events  -- uloop events. eg. uloop.ULOOP_READ .
      )
        local words, msg_or_ip, port_or_nil = ufd:receivefrom()
        print(
          'Recv UDP packet from '..
          msg_or_ip ..':'..port_or_nil..
          ' : '..words
        )
      end,
      uloop.ULOOP_READ -- event you want to listen
    )

    udp_read_ev:cancel() -- cancel it

  The `examples/uloop-example.lua` show an example of this work.

Fix stack overflow bug.
  The static variable `state` in `lua/uloop.c` should be clean after every
  callback.

Fix bug of GC
  fd and timeout lua object has a __gc method in its metatable. After the
  object is freed and if another new object use the same reference number in
  __uloop_cb and __uloop_fds, the new object will be freed by the old __gc
  of the old object when garbag collecting.

Signed-off-by: Xiongfei(Alex) Guo <[email protected]>
---
 examples/uloop-example.lua |   34 +++++++++
 lua/uloop.c                |  168 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 201 insertions(+), 1 deletion(-)

diff --git a/examples/uloop-example.lua b/examples/uloop-example.lua
index 2da6ebd..ab85a5d 100755
--- a/examples/uloop-example.lua
+++ b/examples/uloop-example.lua
@@ -1,8 +1,14 @@
 #!/usr/bin/env lua
 
+local socket = require "socket"
+
 local uloop = require("uloop")
 uloop.init()
 
+local udp = socket.udp()
+udp:settimeout(0)
+udp:setsockname('*', 8080)
+
 -- timer example 1
 local timer
 function t()
@@ -40,5 +46,33 @@ uloop.timer(
        end, 2000
 )
 
+udp_ev = uloop.fd_add(udp, function(ufd, events)
+       local words, msg_or_ip, port_or_nil = ufd:receivefrom()
+       print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : 
'..words)
+       if words == "Stop!" then
+               udp_ev:cancel()
+       end
+end, uloop.ULOOP_READ)
+
+udp_count = 0
+udp_send_timer = uloop.timer(
+       function()
+               local s = socket.udp()
+               local words
+               if udp_count > 3 then
+                       words = "Stop!"
+                       udp_send_timer:cancel()
+               else
+                       words = 'Hello!'
+                       udp_send_timer:set(1000)
+               end
+               print('Send UDP packet to 127.0.0.1:8080 :'..words)
+               s:sendto(words, '127.0.0.1', 8080)
+               s:close()
+
+               udp_count = udp_count + 1
+       end, 3000
+)
+
 uloop.run()
 
diff --git a/lua/uloop.c b/lua/uloop.c
index 51f53c2..5bc0e6c 100644
--- a/lua/uloop.c
+++ b/lua/uloop.c
@@ -25,6 +25,12 @@
 #include "../uloop.h"
 #include "../list.h"
 
+struct lua_uloop_fd {
+       struct uloop_fd fd;
+       int r;
+       int fd_r;
+};
+
 struct lua_uloop_timeout {
        struct uloop_timeout t;
        int r;
@@ -43,7 +49,10 @@ static void ul_timer_cb(struct uloop_timeout *t)
 
        lua_getglobal(state, "__uloop_cb");
        lua_rawgeti(state, -1, tout->r);
+       lua_remove(state, -2);
+
        lua_call(state, 0, 0);
+
 }
 
 static int ul_timer_set(lua_State *L)
@@ -70,8 +79,15 @@ static int ul_timer_free(lua_State *L)
        struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
 
        uloop_timeout_cancel(&tout->t);
+
+       /* obj.__index.__gc = nil , make sure executing only once*/
+       lua_getfield(L, -1, "__index");
+       lua_pushstring(L, "__gc");
+       lua_pushnil(L);
+       lua_settable(L, -3);
+
        lua_getglobal(state, "__uloop_cb");
-       luaL_unref(L, -1, tout->r);
+       luaL_unref(state, -1, tout->r);
 
        return 1;
 }
@@ -126,13 +142,136 @@ static int ul_timer(lua_State *L)
        return 1;
 }
 
+static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
+{
+       struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
+
+       lua_getglobal(state, "__uloop_cb");
+       lua_rawgeti(state, -1, ufd->r);
+       lua_remove(state, -2);
+
+       /* push fd object */
+       lua_getglobal(state, "__uloop_fds");
+       lua_rawgeti(state, -1, ufd->fd_r);
+       lua_remove(state, -2);
+
+       /* push events */
+       lua_pushinteger(state, events);
+       lua_call(state, 2, 0);
+}
+
+
+static int get_sock_fd(lua_State* L, int idx) {
+       int fd;
+       if(lua_isnumber(L, idx)) {
+               fd = lua_tonumber(L, idx);
+       } else {
+               luaL_checktype(L, idx, LUA_TUSERDATA);
+               lua_getfield(L, idx, "getfd");
+               if(lua_isnil(L, -1))
+                       return luaL_error(L, "socket type missing 'getfd' 
method");
+               lua_pushvalue(L, idx - 1);
+               lua_call(L, 1, 1);
+               fd = lua_tointeger(L, -1);
+               lua_pop(L, 1);
+       }
+       return fd;
+}
+
+static int ul_ufd_delete(lua_State *L)
+{
+       struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
+
+       uloop_fd_delete(&ufd->fd);
+
+       /* obj.__index.__gc = nil , make sure executing only once*/
+       lua_getfield(L, -1, "__index");
+       lua_pushstring(L, "__gc");
+       lua_pushnil(L);
+       lua_settable(L, -3);
+
+       lua_getglobal(state, "__uloop_cb");
+       luaL_unref(state, -1, ufd->r);
+       lua_remove(state, -1);
+
+       lua_getglobal(state, "__uloop_fds");
+       luaL_unref(state, -1, ufd->fd_r);
+       lua_remove(state, -1);
+
+       return 1;
+}
+
+static const luaL_Reg ufd_m[] = {
+       { "cancel", ul_ufd_delete },
+       { NULL, NULL }
+};
+
+static int ul_ufd_add(lua_State *L)
+{
+       struct lua_uloop_fd *ufd;
+       int fd = 0;
+       unsigned int flags = 0;
+       int ref;
+       int fd_ref;
+
+       if (lua_isnumber(L, -1)) {
+               flags = lua_tointeger(L, -1);
+               lua_pop(L, 1);
+       }
+
+       if (!lua_isfunction(L, -1)) {
+               lua_pushstring(L, "invalid arg list");
+               lua_error(L);
+
+               return 0;
+       }
+
+       fd = get_sock_fd(L, -2);
+
+       lua_getglobal(L, "__uloop_cb");
+       lua_pushvalue(L, -2);
+       ref = luaL_ref(L, -2);
+       lua_pop(L, 1);
+
+       lua_getglobal(L, "__uloop_fds");
+       lua_pushvalue(L, -3);
+       fd_ref = luaL_ref(L, -2);
+       lua_pop(L, 1);
+
+       ufd = lua_newuserdata(L, sizeof(*ufd));
+
+       lua_createtable(L, 0, 2);
+       lua_pushvalue(L, -1);
+       lua_setfield(L, -2, "__index");
+       lua_pushcfunction(L, ul_ufd_delete);
+       lua_setfield(L, -2, "__gc");
+       lua_pushvalue(L, -1);
+       lua_setmetatable(L, -3);
+       lua_pushvalue(L, -2);
+       luaI_openlib(L, NULL, ufd_m, 1);
+       lua_pushvalue(L, -2);
+
+       memset(ufd, 0, sizeof(*ufd));
+
+       ufd->r = ref;
+       ufd->fd.fd = fd;
+       ufd->fd_r = fd_ref;
+       ufd->fd.cb = ul_ufd_cb;
+       if (flags)
+               uloop_fd_add(&ufd->fd, flags);
+
+       return 1;
+}
+
 static void ul_process_cb(struct uloop_process *p, int ret)
 {
        struct lua_uloop_process *proc = container_of(p, struct 
lua_uloop_process, p);
 
        lua_getglobal(state, "__uloop_cb");
        lua_rawgeti(state, -1, proc->r);
+
        luaL_unref(state, -2, proc->r);
+       lua_remove(state, -2);
        lua_pushinteger(state, ret >> 8);
        lua_call(state, 1, 0);
 }
@@ -218,11 +357,19 @@ static int ul_run(lua_State *L)
        return 1;
 }
 
+static int ul_cancel(lua_State *L)
+{
+       uloop_end();
+       return 1;
+}
+
 static luaL_reg uloop_func[] = {
        {"init", ul_init},
        {"run", ul_run},
        {"timer", ul_timer},
        {"process", ul_process},
+       {"fd_add", ul_ufd_add},
+       {"cancel", ul_cancel},
        {NULL, NULL},
 };
 
@@ -237,11 +384,30 @@ int luaopen_uloop(lua_State *L)
        lua_createtable(L, 1, 0);
        lua_setglobal(L, "__uloop_cb");
 
+       lua_createtable(L, 1, 0);
+       lua_setglobal(L, "__uloop_fds");
+
        luaL_openlib(L, "uloop", uloop_func, 0);
        lua_pushstring(L, "_VERSION");
        lua_pushstring(L, "1.0");
        lua_rawset(L, -3);
 
+       lua_pushstring(L, "ULOOP_READ");
+       lua_pushinteger(L, ULOOP_READ);
+       lua_rawset(L, -3);
+
+       lua_pushstring(L, "ULOOP_WRITE");
+       lua_pushinteger(L, ULOOP_WRITE);
+       lua_rawset(L, -3);
+
+       lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
+       lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
+       lua_rawset(L, -3);
+
+       lua_pushstring(L, "ULOOP_BLOCKING");
+       lua_pushinteger(L, ULOOP_BLOCKING);
+       lua_rawset(L, -3);
+
        return 1;
 }
 
-- 
1.7.9.5
_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel

Reply via email to