mirror of
https://github.com/troglobit/libuev.git
synced 2026-10-02 05:42:36 +07:00
Lua test coded, not yet running ..
This commit is contained in:
@@ -13,7 +13,7 @@ CC=$(CROSS_COMPILE)gcc
|
||||
AR=$(CROSS_COMPILE)ar
|
||||
|
||||
INCLUDE += -I /usr/include/lua5.1/
|
||||
LDFLAGS=-s
|
||||
LDFLAGS=-g
|
||||
CFLAGS=-fPIC -g -Os $(INCLUDE)
|
||||
|
||||
MAJOR = 1
|
||||
@@ -23,7 +23,6 @@ lua: luevent.so
|
||||
|
||||
libuevent.a: libuevent.o
|
||||
$(AR) r $@ $^
|
||||
$(CROSS_COMPILE)strip --strip-unneeded $@
|
||||
|
||||
libuevent.so.$(MAJOR): libuevent.o
|
||||
$(CC) $(LDFLAGS) -shared -o $@ $^ -Wl,-soname=libuevent
|
||||
@@ -31,6 +30,10 @@ libuevent.so.$(MAJOR): libuevent.o
|
||||
luevent.so: luevent.o all
|
||||
$(CC) $(LDFLAGS) -shared -o $@ luevent.o -L . -l uevent -Wl,-soname=luevent
|
||||
|
||||
stripped: all lua
|
||||
$(CROSS_COMPILE)strip --strip-unneeded libuevent.a libuevent.so.$(MAJOR) luevent.so
|
||||
|
||||
|
||||
test: all
|
||||
$(CC) -g -o test -D NO_DEBUG test.c libuevent.c && ./test
|
||||
|
||||
@@ -38,7 +41,7 @@ luatest: lua
|
||||
lua test.lua
|
||||
|
||||
clean:
|
||||
rm -f libuevent.o luauevent.o libuevent.so.$(MAJOR) luevent.so test
|
||||
rm -f libuevent.o luevent.o libuevent.so.$(MAJOR) luevent.so test
|
||||
|
||||
|
||||
.PHONY: all test clean luatest lua
|
||||
|
||||
@@ -393,23 +393,25 @@ int luaopen_luevent(lua_State * L)
|
||||
assert(lueContext);
|
||||
|
||||
luaL_register(L, lua_tostring(L, 1), lueventlib); // calls
|
||||
lua_setmetatable(L, -1); // Call GC at module cleanup. Pops it too
|
||||
lua_pushvalue(L, -1); // Dup
|
||||
lua_setmetatable(L, -2); // Call GC at module cleanup.
|
||||
|
||||
l_newType(L, LUA_LTIMER_MT); // create metatable for timers
|
||||
luaL_register(L, NULL, ltimermeta); // methods
|
||||
lua_pop(L, 1);
|
||||
lua_setfield(L, -2, "_meta_timer"); // easy access from test code
|
||||
|
||||
l_newType(L, LUA_LFILEH_MT); // create metatable for file handlers
|
||||
luaL_register(L, NULL, lfilehmeta); // methods
|
||||
lua_pop(L, 1);
|
||||
lua_setfield(L, -2, "_meta_fileh"); // easy access from test code
|
||||
|
||||
lua_newtable(L); // Table[obj] -> Callback-closure
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, LUA_LUEOBJ);
|
||||
lua_pushlightuserdata(L, lueContext);// Table[1] = lueContext
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pop(L, 1);
|
||||
lua_setfield(L, -2, "_lue_objects");// easy access from test code
|
||||
|
||||
lua_pop(L, 1); // pop module
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ static timeOutHdl(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data)
|
||||
timeOut = NULL;
|
||||
printf("Timeout is exceeded %p\n", data);
|
||||
lueRemFd(ctxt, readFd);
|
||||
lueExit(ctxt);
|
||||
lueTerminate(ctxt);
|
||||
}
|
||||
|
||||
static appTimeout(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data)
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env lua
|
||||
--/usr/bin/env luajit
|
||||
---
|
||||
-- @file
|
||||
-- Micro event library libuevent.
|
||||
-- Test & showcase of libuevent
|
||||
--
|
||||
-- (C) Copyright 2012 flemming.madsen at madsensoft.dk. See libuevent.h
|
||||
---
|
||||
require "socket"
|
||||
require "luevent"
|
||||
|
||||
local ADDR = "127.0.0.1"
|
||||
local PORT = 42555
|
||||
local timer
|
||||
local period
|
||||
|
||||
local function timeOut()
|
||||
print("Timeout is exceeded")
|
||||
luevent.terminate()
|
||||
end
|
||||
|
||||
local function writeThread(sck, cnt)
|
||||
sck:send(("TESTING"):sub(cnt))
|
||||
period = cnt + 5
|
||||
luevent.addTimer(100 * period, function() writeThread(sck, cnt + 1) end)
|
||||
end
|
||||
|
||||
local function main()
|
||||
luevent.addTimer(5000, function()
|
||||
print("Lifetime is exceeded")
|
||||
os.exit(1);
|
||||
end)
|
||||
|
||||
local isrv = assert(socket.bind(ADDR, PORT))
|
||||
luevent.addFileIO(isrv:getfd(), function(self)
|
||||
local isock = isrv:accept()
|
||||
luevent.addFileIO(isock:getfd(), function(self)
|
||||
-- Pipe readable
|
||||
if timer then
|
||||
timer:clear()
|
||||
timer = luevent.addTimer(950, timeOut)
|
||||
end
|
||||
local str = isock:receive(50)
|
||||
io.write(str.."."..#str) io.flush()
|
||||
end)
|
||||
|
||||
self:clear() -- This is a oneshot
|
||||
end)
|
||||
timer = luevent.addTimer(950, timeOut)
|
||||
|
||||
local osck = socket.tcp() osck:settimeout(0) osck:connect(ADDR, PORT)
|
||||
luevent.addFileIO(osck:getfd(), function(self)
|
||||
writeThread(osck, 1)
|
||||
self:clear() -- This is a oneshot
|
||||
end, "w")
|
||||
|
||||
luevent.run()
|
||||
print "DONE"
|
||||
for k,v in pairs(luevent._lue_objects) do print(k,v) end
|
||||
|
||||
package.loaded.luevent = nil
|
||||
collectgarbage("collect")
|
||||
print(("Period is %d must be 10: %s"):format(period, 10 == period and "OK" or "ERROR!"))
|
||||
os.exit(period == 10 and 0 or 1)
|
||||
end
|
||||
|
||||
main()
|
||||
-- vim: set sw=3 sts=3 et nu:
|
||||
Reference in New Issue
Block a user