Permalink
Browse files

modified: ../EventLoop.lua

	modified:   ../IOAlertEmitter.lua
  • Loading branch information...
1 parent e9feba8 commit 7806a42a0d9bcb379871edda0912a4380b17a841 @Wiladams committed Nov 6, 2012
Showing with 36 additions and 11 deletions.
  1. +13 −9 EventLoop.lua
  2. +23 −2 IOAlertEmitter.lua
View
@@ -1,14 +1,18 @@
local IOAlertEmitter = require "IOAlertEmitter"
-local EventLoop = {}
+local EventLoop = {
+ MaxEvents = 16,
+}
local EventLoop_mt = {
__index = EventLoop,
}
-EventLoop.new = function()
+EventLoop.new = function(timeout, maxevents)
+ maxevents = maxevents or EventLoop.MaxEvents
+
local obj = {
Observers = {},
- Emitter = IOAlertEmitter.new(),
+ Emitter = IOAlertEmitter.new(timeout, maxevents),
Running = false;
}
@@ -35,16 +39,16 @@ EventLoop.Run = function(self, timeout)
self.Running = true;
while self.Running do
- local alerts, err = self.Emitter:Wait(timeout)
+ local alerts, count = self.Emitter:EPollWait()
- if alerts and #alerts > 0 then
- for i=1,#alerts do
- --print("Event: ", alerts[i].fd, alerts[i].events);
+ if alerts and count > 0 then
+ for i=0,count-1 do
+ --print("Event: ", alerts[i].data.fd, alerts[i].events);
-- get the appropriate observer
- local observer = self.Observers[alerts[i].fd];
+ local observer = self.Observers[alerts[i].data.fd];
if observer and observer.OnAlert then
- observer:OnAlert(self, alerts[i].fd, alerts[i].events)
+ observer:OnAlert(self, alerts[i].data.fd, alerts[i].events)
end
end
end
View
@@ -8,14 +8,22 @@ local IOAlertEmitter_mt = {
__index = IOAlertEmitter,
}
-function IOAlertEmitter.new()
+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);
@@ -57,8 +65,21 @@ 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
+ 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

0 comments on commit 7806a42

Please sign in to comment.