From b6c488e48752059959bb04534c4e10d763e40f55 Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Wed, 13 Jun 2007 04:32:12 +0000 Subject: [PATCH] Setup management of socket create/close. Recognized new bug: cannot create threads within threads... C contains reference to closed thread, not global. --- luaevent/CHANGELOG | 8 ++++++++ luaevent/include/luaevent.h | 1 + luaevent/luaevent.lua | 12 ++++++------ luaevent/src/luaevent.c | 6 +++--- luaevent/test/test.lua | 5 +++-- luaevent/test/testClient.lua | 17 ++++++++++++----- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/luaevent/CHANGELOG b/luaevent/CHANGELOG index 02d0d81..c3622c8 100644 --- a/luaevent/CHANGELOG +++ b/luaevent/CHANGELOG @@ -1,4 +1,12 @@ ====== +0.1.1 - Revision 14 - 2007-06-13 ++ Fixed event-handling code to cancel events on nothing being returned ++ Added socket/object cleanup. ++ Filed bug to libevent about the strange valgrind-released errors +- Recognized following issues: + Timeouts needed + Need to handle events setup from inside a coroutine... need to get a global Lua state from a thread... +====== 0.1.0 - Revision 6 - 2007-06-10 22:00 EST Completed mostly working version * Moved to a mode where addevent calls a callback rather than it being instantiated within. diff --git a/luaevent/include/luaevent.h b/luaevent/include/luaevent.h index 64bfb51..8b97be7 100644 --- a/luaevent/include/luaevent.h +++ b/luaevent/include/luaevent.h @@ -12,6 +12,7 @@ typedef struct { struct event ev; lua_State* L; int callbackRef; + int objectRef; /* TEMP */ } le_callback; int luaopen_luaevent(lua_State* L); diff --git a/luaevent/luaevent.lua b/luaevent/luaevent.lua index 6e6cf05..c6bd015 100644 --- a/luaevent/luaevent.lua +++ b/luaevent/luaevent.lua @@ -11,9 +11,6 @@ local fair = false local hookedObjectMt = false --- Weak keys.. the keys are the client sockets -local clientTable = setmetatable({}, {'__mode', 'k'}) - function send(sock, data, start, stop) local s, err local from = start or 1 @@ -79,7 +76,7 @@ local function serverCoroutine(sock, callback) --cl[#cl + 1] = client client:settimeout(0) local coFunc = coroutine.wrap(clientCoroutine) - clientTable[client] = luaevent.core.addevent(client, coFunc, client, callback) + luaevent.core.addevent(client, coFunc, client, callback) end until false end @@ -103,11 +100,11 @@ end function addserver(sock, callback) local coFunc = coroutine.wrap(serverCoroutine) - clientTable[sock] = luaevent.core.addevent(sock, coFunc, sock, callback) + luaevent.core.addevent(sock, coFunc, sock, callback) end function addthread(sock, func, ...) local coFunc = coroutine.wrap(func) - clientTable[sock] = luaevent.core.addevent(sock, coFunc, ...) + luaevent.core.addevent(sock, coFunc, ...) end local _skt_mt = {__index = { connect = function(self, ...) @@ -132,6 +129,9 @@ local _skt_mt = {__index = { self.timeout=time return end, + close = function(self) + self.socket:close() + end }} function wrap(sock) return setmetatable({socket = sock}, _skt_mt) diff --git a/luaevent/src/luaevent.c b/luaevent/src/luaevent.c index 8539a99..e5e33cf 100644 --- a/luaevent/src/luaevent.c +++ b/luaevent/src/luaevent.c @@ -31,6 +31,7 @@ static void freeCallbackArgs(le_callback* arg) { arg->L = NULL; event_del(&arg->ev); luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef); + luaL_unref(L, LUA_REGISTRYINDEX, arg->objectRef); } } @@ -47,7 +48,6 @@ static int call_callback_function(lua_State* L, int argCount) { if(ret < 0) { /* Done, no need to setup event */ return -1; } - printf("WAITING FOR: %i RED: %i WR:%i\n", ret, EV_READ, EV_WRITE); if(ret != EV_READ && ret != EV_WRITE) { printf("BAD RET_VAL IN INIT: %i\n", ret); } @@ -80,7 +80,6 @@ static void luaevent_callback(int fd, short event, void* p) { return; } - printf("RET VAL: %i\n", ret); if(event != ret) setup_event(arg, fd, ret, 1); } @@ -126,7 +125,8 @@ static void push_new_callback(lua_State* L, int callbackRef, int fd, short event arg->L = L; arg->callbackRef = callbackRef; - + lua_pushvalue(L, -1); + arg->objectRef = luaL_ref(L, LUA_REGISTRYINDEX); setup_event(arg, fd, event, 0); } /* Expected to be called at the beginning of the coro that uses it.. diff --git a/luaevent/test/test.lua b/luaevent/test/test.lua index f7a44da..fd9919d 100644 --- a/luaevent/test/test.lua +++ b/luaevent/test/test.lua @@ -8,12 +8,13 @@ require"socket" local function echoHandler(skt) while true do local data,ret = luaevent.receive(skt, 10) - if data == "quit" or ret == 'closed' then + if data == "quit" or ret == 'closed' or not data then break end --collectgarbage() - luaevent.send(skt, data) + if not luaevent.send(skt, data) then return end end + if skt then skt:close() end end local server = assert(socket.bind("localhost", 20000)) diff --git a/luaevent/test/testClient.lua b/luaevent/test/testClient.lua index 0557230..8d14887 100644 --- a/luaevent/test/testClient.lua +++ b/luaevent/test/testClient.lua @@ -4,18 +4,25 @@ local function setupHook(thread) if not thread then debug.sethook(function(event) print("TRACE >: ", debug.getinfo(2, 'n').name) end, 'c') else debug.sethook(thread, function(event) print("TRACE ", thread,">: ", debug.getinfo(2, 'n').name) end, 'c') end end - +local count = 100 local function func(sock) sock = luaevent.wrap(sock) assert(sock:connect("localhost", 20000)) - for i = 1, 10 do - for z = 1, 100 do + for i = 1, 2 do + local maxZ = 10 + for z = 1, maxZ do assert(sock:send("Greet me ")) end - assert(sock:receive(10 * 100)) + assert(sock:receive(10 * maxZ)) + end + if skt then skt:close() end + count = count - 1 + if count > 0 then + --local sock = assert(socket.tcp()) + --luaevent.addthread(sock, func, sock) end end -for i = 1, 1020 do +for i = 1, 500 do local sock = assert(socket.tcp()) luaevent.addthread(sock, func, sock) end -- 2.11.4.GIT