rrthomas/luaposix — pull request #16 by spbnick (2011-10-22 16:15:41+00:00)
Title: Add full poll(2) interfaces

Rename Ppoll to Prpoll to match external name of "rpoll".
Add full poll(2) interface under name "poll".

The arguments to the "poll" function are fd->pollfd map and an optional
timeout in milliseconds (default being -1, i.e. infinity).

"Pollfd" is a table analogue of struct pollfd, with "event" and "revent"
fields being tables with boolean fields corresponding to bits, named without
"POLL" suffix.

Example usage:

local pollfd_map = {
[fd1] = {events = {IN = true}},
[fd2] = {events = {IN = true}},
[fd3] = {events = {IN = true}}
}

while next(pollfd_map) ~= nil do
local ready, errmsg = posix.poll(pollfd_map)

if ready == nil then
error(errmsg)
end

for fd, pollfd in pairs(polfd_map) do
if pollfd.revents.IN or pollfd.revents.HUP then
local input, errmsg, errno = posix.read(fd, 8)

if input == nil then
error(errmsg)
elseif #input == 0 then
pollfd_map[fd] = nil
else
print(tostring(fd) .. ":", input:byte(1, -1))
end
end
end
end