aboutsummaryrefslogtreecommitdiffstats
path: root/luaevent/luaevent.lua
diff options
context:
space:
mode:
authorThomas Harning Jr <harningt@gmail.com>2007-06-11 01:08:59 +0000
committerThomas Harning Jr <harningt@gmail.com>2007-06-11 01:08:59 +0000
commit35ac0581fa8e21502283480b99fc67ed7d46dc3b (patch)
treeedb989b681d95ca16caaebb55712cec6d23a1999 /luaevent/luaevent.lua
parent9839406cc22bd47bbf4bd82d73860f9f7e270155 (diff)
downloadluaevent-prosody-35ac0581fa8e21502283480b99fc67ed7d46dc3b.tar.gz
luaevent-prosody-35ac0581fa8e21502283480b99fc67ed7d46dc3b.zip
* Completed mostly working version
* Moved to a mode where addevent calls a callback rather than it being instantiated within. * If the callback returns -1, then no event is ever setup * Otherwise the integer value is used to setup the event * This allows for using coroutine.wrap rather than a cooked-up wrapper * Tests work, although there are a few remaining issues: * Need to figure a good way of preserving the event object, not sure if current method is good enough, since the socket is the only anchor, and it is only held inside the coro.. circular reference, something that Lua 'handles' well. * Doing more than the maximum sockets the process is allows causes strangeness to occur in libevent.. somehow it is getting around to epoll_add which is causing valgrind to barf.
Diffstat (limited to 'luaevent/luaevent.lua')
-rw-r--r--luaevent/luaevent.lua54
1 files changed, 29 insertions, 25 deletions
diff --git a/luaevent/luaevent.lua b/luaevent/luaevent.lua
index 3b8ed18..be7f296 100644
--- a/luaevent/luaevent.lua
+++ b/luaevent/luaevent.lua
@@ -9,18 +9,10 @@ local EV_READ = luaevent.core.EV_READ
local EV_WRITE = luaevent.core.EV_WRITE
local fair = false
--- Weak keys.. the keys are the client sockets
-local clientTable = {} or setmetatable({}, {'__mode', 'k'})
+local hookedObjectMt = false
-local function getWrapper()
- local running = coroutine.running()
- return function(...)
- print(coroutine.running(), running)
- print(debug.traceback())
- if coroutine.running() == running then return end
- return select(2, coroutine.resume(running, ...))
- end
-end
+-- Weak keys.. the keys are the client sockets
+local clientTable = setmetatable({}, {'__mode', 'k'})
function send(sock, data, start, stop)
local s, err
@@ -35,7 +27,6 @@ function send(sock, data, start, stop)
coroutine.yield(EV_WRITE)
end
if s or err ~= "timeout" then return s, err, sent end
- if not clientTable[sock] then clientTable[sock] = luaevent.core.addevent(sock, EV_WRITE, getWrapper()) end
coroutine.yield(EV_WRITE)
until false
end
@@ -45,7 +36,6 @@ function receive(sock, pattern, part)
repeat
s, err, part = sock:receive(pattern, part)
if s or err ~= "timeout" then return s, err, part end
- if not clientTable[sock] then clientTable[sock] = luaevent.core.addevent(sock, EV_READ, getWrapper()) end
coroutine.yield(EV_READ)
until false
end
@@ -58,7 +48,6 @@ function receivePartial(client, pattern)
s, err, part = client:receive(pattern)
if s or ( (type(pattern)=="number") and part~="" and part ~=nil ) or
err ~= "timeout" then return s, err, part end
- if not clientTable[sock] then clientTable[sock] = luaevent.core.addevent(sock, EV_READ, getWrapper()) end
coroutine.yield(EV_READ)
until false
end
@@ -66,7 +55,6 @@ function connect(sock, ...)
sock:settimeout(0)
local ret, err = sock:connect(...)
if ret or err ~= "timeout" then return ret, err end
- if not clientTable[sock] then clientTable[sock] = luaevent.core.addevent(sock, EV_WRITE, getWrapper()) end
coroutine.yield(EV_WRITE)
ret, err = sock:connect(...)
if err == "already connected" then
@@ -81,28 +69,44 @@ local function clientCoroutine(sock, handler)
-- Figure out what to do ......
return handler(sock)
end
-local function handleClient(co, client, handler)
- local ok, res, event = coroutine.resume(co, client, handler)
-end
+
local function serverCoroutine(sock, callback)
- local listenItem = luaevent.core.addevent(sock, EV_READ, getWrapper())
repeat
local event = coroutine.yield(EV_READ)
-- Get new socket
local client = sock:accept()
if client then
+ --cl[#cl + 1] = client
client:settimeout(0)
- local co = coroutine.create(clientCoroutine)
- handleClient(co, client, callback)
+ local coFunc = coroutine.wrap(clientCoroutine)
+ clientTable[client] = luaevent.core.addevent(client, coFunc, client, callback)
end
until false
end
+
+local oldAddEvent = luaevent.core.addevent
+luaevent.core.addevent = function(...)
+ local item = oldAddEvent(...)
+ print("SETUP ITEM FOR: ", debug.getmetatable(item).getfd(item))
+ if not hookedObjectMt then
+ hookedObjectMt = true
+ local mt = debug.getmetatable(item)
+ local oldGC = mt.__gc
+ mt.__gc = function(...)
+ print("RELEASING ITEM FOR: ", mt.getfd(...))
+ return oldGC(...)
+ end
+ end
+ return item
+end
+
function addserver(sock, callback)
- local coro = coroutine.create(serverCoroutine)
- assert(coroutine.resume(coro, sock, callback))
+ local coFunc = coroutine.wrap(serverCoroutine)
+ clientTable[sock] = luaevent.core.addevent(sock, coFunc, sock, callback)
end
-function addthread(func, ...)
- return coroutine.resume(coroutine.create(func), ...)
+function addthread(sock, func, ...)
+ local coFunc = coroutine.wrap(func)
+ clientTable[sock] = luaevent.core.addevent(sock, coFunc, ...)
end
local _skt_mt = {__index = {
connect = function(self, ...)