mirror of
https://github.com/troglobit/libuev.git
synced 2026-11-27 20:58:43 +00:00
Lua test running
This commit is contained in:
1 parent
22aa570846
commit
285ee70433
2 files changed
+48
-22
No files matched your search
+35
-16
@@ -22,7 +22,7 @@
|
|||||||
#include "libuevent.h"
|
#include "libuevent.h"
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define Debug(f, args...) printf(f "\n", ##args)
|
#define Debug(f, args...) printf(":LUE: " f "\n", ##args)
|
||||||
#else
|
#else
|
||||||
#define Debug(f, args...)
|
#define Debug(f, args...)
|
||||||
#endif
|
#endif
|
||||||
@@ -32,10 +32,18 @@ static clock_t clock_tick = 0;
|
|||||||
/// Enumerate file handle direction
|
/// Enumerate file handle direction
|
||||||
enum LUEFhDirectionT {LUE_FHIN = 0, LUE_FHOUT = 1};
|
enum LUEFhDirectionT {LUE_FHIN = 0, LUE_FHOUT = 1};
|
||||||
|
|
||||||
|
struct LUEFileEventH;
|
||||||
|
struct LUEFileEventRem
|
||||||
|
{
|
||||||
|
LListField(struct LUEFileEventRem);
|
||||||
|
struct LUEFileEventH *ent;
|
||||||
|
};
|
||||||
|
|
||||||
/// File event handle
|
/// File event handle
|
||||||
struct LUEFileEventH
|
struct LUEFileEventH
|
||||||
{
|
{
|
||||||
LListField(struct LUEFileEventH); ///< Elements for linked list
|
LListField(struct LUEFileEventH); ///< Elements for linked list
|
||||||
|
struct LUEFileEventRem rem; ///< For postponed removal
|
||||||
|
|
||||||
int fd; ///< File descriptor
|
int fd; ///< File descriptor
|
||||||
enum LUEFhDirectionT direction; ///< Direction: in or out
|
enum LUEFhDirectionT direction; ///< Direction: in or out
|
||||||
@@ -59,6 +67,7 @@ struct LUETimerH
|
|||||||
*/
|
*/
|
||||||
typedef LList(struct LUETimerH) LUETimerHandlerDL;
|
typedef LList(struct LUETimerH) LUETimerHandlerDL;
|
||||||
typedef LList(struct LUEFileEventH) LUEFileEventDL;
|
typedef LList(struct LUEFileEventH) LUEFileEventDL;
|
||||||
|
typedef LList(struct LUEFileEventRem) LUEFileEventRemL;
|
||||||
/**
|
/**
|
||||||
* @endcond
|
* @endcond
|
||||||
*/
|
*/
|
||||||
@@ -69,7 +78,7 @@ struct LUECtxt
|
|||||||
LUEFileEventDL fileHandlers; ///< File handlers
|
LUEFileEventDL fileHandlers; ///< File handlers
|
||||||
LUETimerHandlerDL timeoutHandlers; ///< Timer handlers
|
LUETimerHandlerDL timeoutHandlers; ///< Timer handlers
|
||||||
|
|
||||||
LUEFileEventDL fileRem; ///< List of file handles to be garbage collected
|
LUEFileEventRemL fileRem; ///< List of file handles to be garbage collected
|
||||||
LUETimerHandlerDL timeRem; ///< List of timer handles to be garbage collected
|
LUETimerHandlerDL timeRem; ///< List of timer handles to be garbage collected
|
||||||
|
|
||||||
clock_t baseTime; ///< Time at last timer tick
|
clock_t baseTime; ///< Time at last timer tick
|
||||||
@@ -86,7 +95,9 @@ struct LUEFileEventH *lueAddIO(struct LUECtxt * ctxt,
|
|||||||
struct LUEFileEventH *ent;
|
struct LUEFileEventH *ent;
|
||||||
|
|
||||||
ent = (struct LUEFileEventH *) malloc(sizeof(*ent));
|
ent = (struct LUEFileEventH *) malloc(sizeof(*ent));
|
||||||
|
Debug("Add IO %d %p", fd, ent);
|
||||||
assert(ent);
|
assert(ent);
|
||||||
|
ent->rem.ent = ent;
|
||||||
ent->fd = fd;
|
ent->fd = fd;
|
||||||
ent->direction = direction;
|
ent->direction = direction;
|
||||||
ent->handler = handler;
|
ent->handler = handler;
|
||||||
@@ -118,14 +129,16 @@ struct LUEFileEventH *lueAddOutput(struct LUECtxt *ctxt,
|
|||||||
static
|
static
|
||||||
int lueRemIO(struct LUECtxt *ctxt, struct LUEFileEventH *hdl)
|
int lueRemIO(struct LUECtxt *ctxt, struct LUEFileEventH *hdl)
|
||||||
{
|
{
|
||||||
struct LUEFileEventH *ent;
|
struct LUEFileEventRem *ent;
|
||||||
|
|
||||||
|
Debug("Rem IO %p", hdl);
|
||||||
lListForeachIn (ent, &ctxt->fileRem)
|
lListForeachIn (ent, &ctxt->fileRem)
|
||||||
if (ent == hdl)
|
if (ent->ent == hdl)
|
||||||
return -1; // Already removed
|
return -1; // Already removed
|
||||||
|
|
||||||
|
Debug("Rem FD %d", hdl->fd);
|
||||||
hdl->handler = NULL;
|
hdl->handler = NULL;
|
||||||
lListAppend(&ctxt->fileRem, hdl);
|
lListAppend(&ctxt->fileRem, &(hdl->rem));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,17 +147,20 @@ int lueRemIO(struct LUECtxt *ctxt, struct LUEFileEventH *hdl)
|
|||||||
*/
|
*/
|
||||||
int lueRemFd(struct LUECtxt *ctxt, int fd)
|
int lueRemFd(struct LUECtxt *ctxt, int fd)
|
||||||
{
|
{
|
||||||
struct LUEFileEventH *ent, *rem;
|
struct LUEFileEventH *ent;
|
||||||
|
struct LUEFileEventRem *rem;
|
||||||
|
|
||||||
|
Debug("Remfd FD %d", ent->fd);
|
||||||
lListForeachIn(ent, &ctxt->fileHandlers)
|
lListForeachIn(ent, &ctxt->fileHandlers)
|
||||||
{
|
{
|
||||||
if (ent->fd != fd)
|
if (ent->fd != fd)
|
||||||
continue;
|
continue;
|
||||||
lListForeachIn(rem, &ctxt->fileRem) if (rem == ent) break; // Already removed
|
lListForeachIn(rem, &ctxt->fileRem)
|
||||||
if (rem) {
|
if (rem->ent == ent)
|
||||||
ent->handler = NULL;
|
return -1; // Already removed
|
||||||
lListAppend(&ctxt->fileRem, ent);
|
Debug("Remfd ent %p", ent);
|
||||||
}
|
ent->handler = NULL;
|
||||||
|
lListAppend(&ctxt->fileRem, &(ent->rem));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -179,6 +195,7 @@ int lueRemTimer(struct LUECtxt *ctxt, struct LUETimerH *hdl)
|
|||||||
if (ent == hdl)
|
if (ent == hdl)
|
||||||
return -1; // Already removed
|
return -1; // Already removed
|
||||||
|
|
||||||
|
Debug("Remtimer %p", hdl);
|
||||||
lListRemove(&ctxt->timeoutHandlers, hdl);
|
lListRemove(&ctxt->timeoutHandlers, hdl);
|
||||||
lListAppend(&ctxt->timeRem, hdl); // Remember to free him (at a safe time)
|
lListAppend(&ctxt->timeRem, hdl); // Remember to free him (at a safe time)
|
||||||
}
|
}
|
||||||
@@ -226,14 +243,14 @@ struct LUETimerH *lueAddTimer(struct LUECtxt * ctxt, int periodMs,
|
|||||||
ent->dueTime = 0;
|
ent->dueTime = 0;
|
||||||
if (nent && ent->dueTime > periodMs)
|
if (nent && ent->dueTime > periodMs)
|
||||||
{
|
{
|
||||||
Debug("Insert %d before %d", periodMs, ent->dueTime);
|
Debug("Timer Insert %d before %d %p", periodMs, ent->dueTime, nent);
|
||||||
lListInsert(&ctxt->timeoutHandlers, nent, ent);
|
lListInsert(&ctxt->timeoutHandlers, nent, ent);
|
||||||
nent = NULL;
|
nent = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nent)
|
if (nent)
|
||||||
{
|
{
|
||||||
Debug("Append %d at end", periodMs);
|
Debug("Timer Append %d at end %p", periodMs, nent);
|
||||||
lListAppend(&ctxt->timeoutHandlers, nent);
|
lListAppend(&ctxt->timeoutHandlers, nent);
|
||||||
}
|
}
|
||||||
ctxt->baseTime = curTime;
|
ctxt->baseTime = curTime;
|
||||||
@@ -329,6 +346,7 @@ static int _lueProcessPending(struct LUECtxt *ctxt, int immediate)
|
|||||||
while((timer = lListHead(&ctxt->timeoutHandlers)) &&
|
while((timer = lListHead(&ctxt->timeoutHandlers)) &&
|
||||||
timer->dueTime <= curPeriod)
|
timer->dueTime <= curPeriod)
|
||||||
{
|
{
|
||||||
|
Debug("Timer execution %d", timer->dueTime);
|
||||||
timer->handler(ctxt, timer, timer->data);
|
timer->handler(ctxt, timer, timer->data);
|
||||||
lueRemTimer(ctxt, timer);
|
lueRemTimer(ctxt, timer);
|
||||||
|
|
||||||
@@ -404,12 +422,13 @@ static int _lueProcessPending(struct LUECtxt *ctxt, int immediate)
|
|||||||
}
|
}
|
||||||
if (lListHead(&ctxt->fileRem))
|
if (lListHead(&ctxt->fileRem))
|
||||||
{
|
{
|
||||||
struct LUEFileEventH *ent;
|
struct LUEFileEventRem *ent;
|
||||||
|
|
||||||
while (NULL != (ent = lListHead(&ctxt->fileRem)))
|
while (NULL != (ent = lListHead(&ctxt->fileRem)))
|
||||||
{
|
{
|
||||||
lListRemove(&ctxt->fileHandlers, ent);
|
lListRemove(&ctxt->fileRem, ent);
|
||||||
free(ent);
|
lListRemove(&ctxt->fileHandlers, ent->ent);
|
||||||
|
free(ent->ent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,20 +14,24 @@ local ADDR = "127.0.0.1"
|
|||||||
local PORT = 42555
|
local PORT = 42555
|
||||||
local timer
|
local timer
|
||||||
local period
|
local period
|
||||||
|
local appTimer, readHdl, writeTimer
|
||||||
|
|
||||||
local function timeOut()
|
local function timeOut()
|
||||||
print("Timeout is exceeded")
|
print("Timeout is exceeded")
|
||||||
|
appTimer:clear()
|
||||||
|
readHdl:clear()
|
||||||
|
writeTimer:clear()
|
||||||
luevent.terminate()
|
luevent.terminate()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function writeThread(sck, cnt)
|
local function writeThread(sck, cnt)
|
||||||
sck:send(("TESTING"):sub(cnt))
|
sck:send(("TESTING"):sub(1, cnt))
|
||||||
period = cnt + 5
|
period = cnt + 5
|
||||||
luevent.addTimer(100 * period, function() writeThread(sck, cnt + 1) end)
|
writeTimer = luevent.addTimer(100 * period, function() writeThread(sck, cnt + 1) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function main()
|
local function main()
|
||||||
luevent.addTimer(5000, function()
|
appTimer = luevent.addTimer(5000, function()
|
||||||
print("Lifetime is exceeded")
|
print("Lifetime is exceeded")
|
||||||
os.exit(1);
|
os.exit(1);
|
||||||
end)
|
end)
|
||||||
@@ -35,14 +39,16 @@ local function main()
|
|||||||
local isrv = assert(socket.bind(ADDR, PORT))
|
local isrv = assert(socket.bind(ADDR, PORT))
|
||||||
luevent.addFileIO(isrv:getfd(), function(self)
|
luevent.addFileIO(isrv:getfd(), function(self)
|
||||||
local isock = isrv:accept()
|
local isock = isrv:accept()
|
||||||
luevent.addFileIO(isock:getfd(), function(self)
|
isock:settimeout(0)
|
||||||
|
readHdl = luevent.addFileIO(isock:getfd(), function(self)
|
||||||
-- Pipe readable
|
-- Pipe readable
|
||||||
if timer then
|
if timer then
|
||||||
timer:clear()
|
timer:clear()
|
||||||
timer = luevent.addTimer(950, timeOut)
|
timer = luevent.addTimer(950, timeOut)
|
||||||
end
|
end
|
||||||
local str = isock:receive(50)
|
local str, err, prt = isock:receive(50)
|
||||||
io.write(str.."."..#str) io.flush()
|
if not str and err == "timeout" then str = prt end
|
||||||
|
io.write(str.."."..#str.." ") io.flush()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
self:clear() -- This is a oneshot
|
self:clear() -- This is a oneshot
|
||||||
@@ -57,6 +63,7 @@ local function main()
|
|||||||
|
|
||||||
luevent.run()
|
luevent.run()
|
||||||
print "DONE"
|
print "DONE"
|
||||||
|
-- Only index '1' should be there now
|
||||||
for k,v in pairs(luevent._lue_objects) do print(k,v) end
|
for k,v in pairs(luevent._lue_objects) do print(k,v) end
|
||||||
|
|
||||||
package.loaded.luevent = nil
|
package.loaded.luevent = nil
|
||||||
|
|||||||
Reference in new issue
Block a user