Skip to content

Commit c83a0fd

Browse files
committed
Add. skt:poll() poll method.
1 parent 8e85963 commit c83a0fd

File tree

6 files changed

+93
-6
lines changed

6 files changed

+93
-6
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ env:
1010
# - LUAROCKS_GITTAG=v$LUAROCKS_VER
1111
matrix:
1212
# todo: install libffi and test ffi binding on Lua 5.1/5.2
13-
# - PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq3 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1
14-
# - PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq4 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1
15-
# - PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq3 LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2
16-
# - PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq3 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
17-
# - PERF="NO" LZMQ=ffi ZMQ_VER=zeromq3 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
13+
- PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq3 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1
14+
- PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq4 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1
15+
- PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq3 LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2
16+
- PERF="NO" LZMQ=lzmq ZMQ_VER=zeromq3 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
17+
- PERF="NO" LZMQ=ffi ZMQ_VER=zeromq3 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
1818
- PERF="NO" LZMQ=ffi ZMQ_VER=zeromq4 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
1919
- PERF="NO" LZMQ=ffi-lua ZMQ_VER=zeromq3 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
2020

doc/lzmq.ldoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ function socket:unbind () end
355355
-- @usage skt:bind{"tcp://*:5555","ipc://test.pub.ipc"}
356356
function socket:unbind () end
357357

358+
--- Poll specific ZMQ Socket.
359+
--
360+
-- @tparam[opt=-1] number timeout timeout in milliseconds (-1 - infinity)
361+
-- @tparam[opt=zmq.POLLIN] number events poll events.
362+
-- @treturn[1] bool poll result (false if timeout)
363+
-- @treturn[1] number envents
364+
-- @treturn[2] nil
365+
-- @treturn[2] error
366+
--
367+
-- @usage
368+
-- if skt:poll(100) then return skt:recv() end
369+
function socket:poll () end
370+
358371
--- Send message over Socket.
359372
--
360373
-- @tparam string message content of message.

src/lua/lzmq/ffi.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ end
4545
local FLAGS = api.FLAGS
4646
local ERRORS = api.ERRORS
4747
local ZMQ_LINGER = api.SOCKET_OPTIONS.ZMQ_LINGER
48+
local ZMQ_POLLIN = FLAGS.ZMQ_POLLIN
4849

4950

5051
local unpack = unpack or table.unpack
@@ -637,6 +638,27 @@ function Socket:monitor(addr, events)
637638
return addr
638639
end
639640

641+
local poll_item = ffi.new(api.vla_pollitem_t, 1)
642+
643+
function Socket:poll(timeout, events)
644+
timeout = timeout or -1
645+
events = mask or ZMQ_POLLIN
646+
647+
poll_item[0].socket = self._private.skt
648+
poll_item[0].fd = 0
649+
poll_item[0].events = events
650+
poll_item[0].revents = 0
651+
652+
local ret = api.zmq_poll(poll_item, 1, timeout)
653+
654+
poll_item[0].socket = api.NULL
655+
local revents = poll_item[0].revents
656+
657+
if ret == -1 then return nil, zerror() end
658+
659+
return (bit.band(events, revents) ~= 0), revents
660+
end
661+
640662
end
641663

642664
do -- Message

src/zsocket.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,21 @@ static int luazmq_skt_recvx (lua_State *L) {
395395
return i;
396396
}
397397

398+
static int luazmq_skt_poll (lua_State *L) {
399+
zsocket *skt = luazmq_getsocket(L);
400+
int timeout = luaL_optint(L, 2, -1);
401+
int mask = luaL_optint(L, 3, ZMQ_POLLIN);
402+
zmq_pollitem_t items [] = { { skt->skt, 0, mask, 0 } };
403+
404+
if(-1 == zmq_poll (items, 1, timeout)){
405+
return luazmq_fail(L, skt);
406+
}
407+
408+
lua_pushboolean(L, (items[0].revents & mask)?1:0);
409+
lua_pushinteger(L, items[0].revents);
410+
return 2;
411+
}
412+
398413
static int luazmq_skt_monitor (lua_State *L) {
399414
zsocket *skt = luazmq_getsocket(L);
400415
char endpoint[128];
@@ -810,6 +825,7 @@ static const struct luaL_Reg luazmq_skt_methods[] = {
810825
{"unbind", luazmq_skt_unbind },
811826
{"connect", luazmq_skt_connect },
812827
{"disconnect", luazmq_skt_disconnect },
828+
{"poll", luazmq_skt_poll },
813829
{"send", luazmq_skt_send },
814830
{"send_msg", luazmq_skt_send_msg },
815831
{"sendx", luazmq_skt_sendx },

test/lunit/console.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ local table = require "table"
5656

5757
local _M = {}
5858

59+
local function rfill(str, wdt, ch)
60+
if wdt > #str then str = str .. (ch or ' '):rep(wdt - #str) end
61+
return str
62+
end
63+
5964
local function printformat(format, ...)
6065
io.write( string.format(format, ...) )
6166
end
@@ -94,7 +99,7 @@ function _M.begin()
9499
end
95100

96101
function _M.run(testcasename, testname)
97-
io.write(testcasename, '.', testname) io.flush()
102+
io.write(rfill(testcasename .. '.' .. testname, 70)) io.flush()
98103
end
99104

100105
function _M.err(fullname, message, traceback)

test/utest.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,36 @@ end
10901090

10911091
end
10921092

1093+
local _ENV = TEST_CASE'socket poll' if true then
1094+
1095+
local ctx, req, rep, timer
1096+
1097+
function setup()
1098+
ctx = assert(zmq.context())
1099+
rep = assert(ctx:socket{zmq.REP, bind = ECHO_ADDR, rcvtimeo = 100})
1100+
req = assert(ctx:socket{zmq.REQ, connect = ECHO_ADDR})
1101+
timer = ztimer.monotonic()
1102+
end
1103+
1104+
function teardown()
1105+
if ctx then ctx:destroy() end
1106+
timer:close()
1107+
end
1108+
1109+
function test_timeout()
1110+
timer:start()
1111+
assert_false(rep:poll(2000))
1112+
assert_true(ge(1900, timer:stop()))
1113+
end
1114+
1115+
function test_recv()
1116+
req:send("HELLO")
1117+
assert_true(rep:poll(2000))
1118+
assert_equal("HELLO", rep:recv())
1119+
end
1120+
1121+
end
1122+
10931123
local _ENV = TEST_CASE'loop' if true then
10941124

10951125
local loop, timer
@@ -1101,6 +1131,7 @@ end
11011131

11021132
function teardown()
11031133
loop:destroy()
1134+
timer:close()
11041135
wait(500) -- for TCP time to release IP address
11051136
end
11061137

0 commit comments

Comments
 (0)