# HG changeset patch # User Thomas Harning Jr # Date 1181709132 0 # Node ID 88ce07d625970bb85cc2b01a21fddad42459944f # Parent 1f3b72ba96c961fba4c905ee700b174cbf643b32 Setup management of socket create/close. Recognized new bug: cannot create threads within threads... C contains reference to closed thread, not global. diff -r 1f3b72ba96c9 -r 88ce07d62597 luaevent/CHANGELOG --- a/luaevent/CHANGELOG Wed Jun 13 03:54:01 2007 +0000 +++ b/luaevent/CHANGELOG Wed Jun 13 04:32:12 2007 +0000 @@ -1,3 +1,11 @@ +====== +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 diff -r 1f3b72ba96c9 -r 88ce07d62597 luaevent/include/luaevent.h --- a/luaevent/include/luaevent.h Wed Jun 13 03:54:01 2007 +0000 +++ b/luaevent/include/luaevent.h Wed Jun 13 04:32:12 2007 +0000 @@ -12,6 +12,7 @@ struct event ev; lua_State* L; int callbackRef; + int objectRef; /* TEMP */ } le_callback; int luaopen_luaevent(lua_State* L); diff -r 1f3b72ba96c9 -r 88ce07d62597 luaevent/luaevent.lua --- a/luaevent/luaevent.lua Wed Jun 13 03:54:01 2007 +0000 +++ b/luaevent/luaevent.lua Wed Jun 13 04:32:12 2007 +0000 @@ -11,9 +11,6 @@ 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 @@ --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 @@ 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 @@ self.timeout=time return end, + close = function(self) + self.socket:close() + end }} function wrap(sock) return setmetatable({socket = sock}, _skt_mt) diff -r 1f3b72ba96c9 -r 88ce07d62597 luaevent/src/luaevent.c --- a/luaevent/src/luaevent.c Wed Jun 13 03:54:01 2007 +0000 +++ b/luaevent/src/luaevent.c Wed Jun 13 04:32:12 2007 +0000 @@ -31,6 +31,7 @@ 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 @@ 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 @@ return; } - printf("RET VAL: %i\n", ret); if(event != ret) setup_event(arg, fd, ret, 1); } @@ -126,7 +125,8 @@ 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 -r 1f3b72ba96c9 -r 88ce07d62597 luaevent/test/test.lua --- a/luaevent/test/test.lua Wed Jun 13 03:54:01 2007 +0000 +++ b/luaevent/test/test.lua Wed Jun 13 04:32:12 2007 +0000 @@ -8,12 +8,13 @@ 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 -r 1f3b72ba96c9 -r 88ce07d62597 luaevent/test/testClient.lua --- a/luaevent/test/testClient.lua Wed Jun 13 03:54:01 2007 +0000 +++ b/luaevent/test/testClient.lua Wed Jun 13 04:32:12 2007 +0000 @@ -4,18 +4,25 @@ 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