-
Notifications
You must be signed in to change notification settings - Fork 3
/
IOAlertEmitter.lua
87 lines (64 loc) · 1.91 KB
/
IOAlertEmitter.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
local ffi = require "ffi"
local S = require "syscall"
local UI = require "input"
local IOAlertEmitter = {}
local IOAlertEmitter_mt = {
__index = IOAlertEmitter,
}
function IOAlertEmitter.new(timeout, maxevents)
timeout = timeout or -1
maxevents = maxevents or 16
local handle, err = S.epoll_create();
if not handle then
return false, err
end
local eventBuffer = S.t.epoll_events(maxevents)
local obj = {
AlertHandle = handle,
MaxEvents = maxevents,
EventBuffer = eventBuffer,
Timeout = timeout,
}
setmetatable(obj, IOAlertEmitter_mt);
return obj;
end
--[[
event must have the following:
Descriptor - file descriptor
actions - bitwise OR of actions to observe
--]]
function IOAlertEmitter:AddAlertable(fd, onactivity, whichalerts)
if not fd then
return false, "IOAlertEmitter:AddAlertable(), no descriptor specified"
end
whichalerts = whichalerts or S.c.POLL.RDNORM;
local event = S.t.epoll_event();
event.events = whichalerts;
event.data.fd = fd:getfd();
return S.epoll_ctl(self.AlertHandle, S.c.EPOLL_CTL.ADD, event.data.fd, event);
end
function IOAlertEmitter:AddObserver(observer)
local event = S.t.epoll_event();
event.events = observer.actions;
event.data.fd = observer.Descriptor:getfd();
return S.epoll_ctl(self.AlertHandle, S.c.EPOLL_CTL.ADD, event.data.fd, event);
end
function IOAlertEmitter:RemoveObserver(observer)
return S.epoll_ctl(self.AlertHandle, S.c.EPOLL_CTL.DEL, observer.fd, nil);
end
function IOAlertEmitter:EPollWait()
local ret = S.C.epoll_wait(self.AlertHandle:getfd(), self.EventBuffer, self.MaxEvents, self.Timeout)
if ret == -1 then
return nil, S.t.error()
end
return self.EventBuffer, ret
end
function IOAlertEmitter:Wait(timeout, events, maxevents)
timeout = timeout or 0;
if not maxevents then
events = self.EventBuffer;
maxevents = self.MaxEvents;
end
return S.epoll_wait(self.AlertHandle, events, maxevents, timeout);
end
return IOAlertEmitter;