Setup management of socket create/close.

Wed, 13 Jun 2007 04:32:12 +0000

author
Thomas Harning Jr <harningt@gmail.com>
date
Wed, 13 Jun 2007 04:32:12 +0000
changeset 10
88ce07d62597
parent 9
1f3b72ba96c9
child 11
8339f6236a3c

Setup management of socket create/close.
Recognized new bug: cannot create threads within threads... C contains reference to closed thread, not
global.

luaevent/CHANGELOG file | annotate | diff | comparison | revisions
luaevent/include/luaevent.h file | annotate | diff | comparison | revisions
luaevent/luaevent.lua file | annotate | diff | comparison | revisions
luaevent/src/luaevent.c file | annotate | diff | comparison | revisions
luaevent/test/test.lua file | annotate | diff | comparison | revisions
luaevent/test/testClient.lua file | annotate | diff | comparison | revisions
--- 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
--- 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);
--- 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)
--- 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.. 
--- 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))
--- 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

mercurial