summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2019-11-06 22:40:37 +0800
committerruki <[email protected]>2019-11-06 10:03:44 +0800
commit5a30d496988936bbceee90c3e91fa235e9cf78d9 (patch)
tree508b42cea0899d9bf412cc39f4d13e418779221e /core/src
parent8e6b73d2016fd3a3bf658698e4eb10f82fbcc6f7 (diff)
add poller.wait
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/io/poller_insert.c2
-rw-r--r--core/src/xmake/io/poller_modify.c2
-rw-r--r--core/src/xmake/io/poller_wait.c49
-rw-r--r--core/src/xmake/io/socket_recv.c6
-rw-r--r--core/src/xmake/io/socket_recvfrom.c10
-rw-r--r--core/src/xmake/io/socket_send.c10
-rw-r--r--core/src/xmake/io/socket_sendfile.c16
-rw-r--r--core/src/xmake/io/socket_sendto.c8
8 files changed, 70 insertions, 33 deletions
diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c
index eeb6fd824..3b763007c 100644
--- a/core/src/xmake/io/poller_insert.c
+++ b/core/src/xmake/io/poller_insert.c
@@ -53,7 +53,7 @@ tb_int_t xm_io_poller_insert(lua_State* lua)
tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2);
// insert events to poller
- lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), sock, events, tb_null));
+ lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), sock, events, lua));
return 1;
}
diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c
index 24a27e241..2bba2cabd 100644
--- a/core/src/xmake/io/poller_modify.c
+++ b/core/src/xmake/io/poller_modify.c
@@ -53,7 +53,7 @@ tb_int_t xm_io_poller_modify(lua_State* lua)
tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2);
// modify events in poller
- lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), sock, events, tb_null));
+ lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), sock, events, lua));
return 1;
}
diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c
index e43a70bff..995815319 100644
--- a/core/src/xmake/io/poller_wait.c
+++ b/core/src/xmake/io/poller_wait.c
@@ -32,10 +32,27 @@
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
+ * private implementation
+ */
+static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_socket_ref_t sock, tb_size_t events, tb_cpointer_t priv)
+{
+ // check
+ lua_State* lua = (lua_State*)priv;
+ tb_assert_and_check_return(lua);
+
+ // save socket and events
+ lua_newtable(lua);
+ lua_pushlightuserdata(lua, (tb_pointer_t)sock);
+ lua_rawseti(lua, -2, 1);
+ lua_pushinteger(lua, (tb_int_t)events);
+ lua_rawseti(lua, -2, 2);
+}
+
+/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
-// io.poller_wait(sock, events)
+// local events, count = io.poller_wait(timeout)
tb_int_t xm_io_poller_wait(lua_State* lua)
{
// check
@@ -45,11 +62,31 @@ tb_int_t xm_io_poller_wait(lua_State* lua)
if (!lua_isuserdata(lua, 1))
return 0;
- // get socket
- tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1);
- tb_check_return_val(sock, 0);
+ // get timeout
+ tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 1);
+
+ // wait it
+ lua_newtable(lua);
+ tb_long_t count = tb_poller_wait(xm_io_poller(), xm_io_poller_event, timeout);
+ if (count > 0)
+ {
+ lua_rawseti(lua, -2, (tb_int_t)count);
+ lua_pushinteger(lua, (tb_int_t)count);
+ return 2;
+ }
+ else if (!count)
+ {
+ // timeout
+ lua_pop(lua, 1);
+ lua_pushnil(lua);
+ lua_pushinteger(lua, 0);
+ return 2;
+ }
- // TODO
- return 0;
+ // failed
+ lua_pop(lua, 1);
+ lua_pushnil(lua);
+ lua_pushinteger(lua, -1);
+ return 2;
}
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c
index 4ed41e080..a24eac0cb 100644
--- a/core/src/xmake/io/socket_recv.c
+++ b/core/src/xmake/io/socket_recv.c
@@ -43,7 +43,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua)
// check socket
if (!lua_isuserdata(lua, 1))
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
@@ -58,7 +58,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua)
if (lua_isnumber(lua, 2)) size = (tb_long_t)lua_tonumber(lua, 2);
if (size < 0)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid size(%ld)!", size);
return 2;
}
@@ -67,7 +67,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua)
// recv data
tb_long_t real = tb_socket_recv(sock, data, size);
- lua_pushnumber(lua, (tb_int_t)real);
+ lua_pushinteger(lua, (tb_int_t)real);
if (real > 0)
{
lua_pushlstring(lua, (tb_char_t const*)data, real);
diff --git a/core/src/xmake/io/socket_recvfrom.c b/core/src/xmake/io/socket_recvfrom.c
index 2933a44f0..9bf08c542 100644
--- a/core/src/xmake/io/socket_recvfrom.c
+++ b/core/src/xmake/io/socket_recvfrom.c
@@ -43,7 +43,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua)
// check socket
if (!lua_isuserdata(lua, 1))
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
@@ -59,7 +59,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua)
if (lua_isnumber(lua, 2)) size = (tb_long_t)lua_tonumber(lua, 2);
if (size < 0)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid size(%ld)!", size);
return 2;
}
@@ -69,7 +69,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua)
data = tb_malloc_bytes(size);
if (!data)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "malloc(%ld) failed!", size);
return 2;
}
@@ -80,7 +80,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua)
tb_ipaddr_clear(&ipaddr);
tb_int_t retn = 1;
tb_long_t real = tb_socket_urecv(sock, &ipaddr, data, size);
- lua_pushnumber(lua, (tb_int_t)real);
+ lua_pushinteger(lua, (tb_int_t)real);
if (real > 0)
{
retn = 2;
@@ -91,7 +91,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua)
if (ipstr)
{
lua_pushstring(lua, ipstr);
- lua_pushnumber(lua, (tb_int_t)tb_ipaddr_port(&ipaddr));
+ lua_pushinteger(lua, (tb_int_t)tb_ipaddr_port(&ipaddr));
retn = 4;
}
}
diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c
index 8cce64f29..d5921aa22 100644
--- a/core/src/xmake/io/socket_send.c
+++ b/core/src/xmake/io/socket_send.c
@@ -43,7 +43,7 @@ tb_int_t xm_io_socket_send(lua_State* lua)
// check socket
if (!lua_isuserdata(lua, 1))
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
@@ -77,7 +77,7 @@ tb_int_t xm_io_socket_send(lua_State* lua)
}
if (!data || !size)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p) and size(%zu)!", data, size);
return 2;
}
@@ -87,7 +87,7 @@ tb_int_t xm_io_socket_send(lua_State* lua)
if (lua_isnumber(lua, 3)) start = (tb_long_t)lua_tonumber(lua, 3);
if (start < 1 || start > size)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid start position(%ld)!", start);
return 2;
}
@@ -97,13 +97,13 @@ tb_int_t xm_io_socket_send(lua_State* lua)
if (lua_isnumber(lua, 4)) last = (tb_long_t)lua_tonumber(lua, 4);
if (last < start - 1 || last > size + start - 1)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid last position(%ld)!", last);
return 2;
}
// send data
tb_long_t real = tb_socket_send(sock, data + start - 1, last - start + 1);
- lua_pushnumber(lua, (tb_int_t)real);
+ lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
diff --git a/core/src/xmake/io/socket_sendfile.c b/core/src/xmake/io/socket_sendfile.c
index df29afd32..e92259d66 100644
--- a/core/src/xmake/io/socket_sendfile.c
+++ b/core/src/xmake/io/socket_sendfile.c
@@ -43,7 +43,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
// check socket
if (!lua_isuserdata(lua, 1))
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
@@ -51,7 +51,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
// check file
if (!lua_isuserdata(lua, 2))
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid file!");
return 2;
}
@@ -67,7 +67,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
// does not support stdfile
if (!xm_io_file_is_file(file) || !file->stream)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid file type!");
return 2;
}
@@ -76,7 +76,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
tb_file_ref_t rawfile = tb_null;
if (!tb_stream_ctrl(file->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) || !rawfile)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "cannot get file reference!");
return 2;
}
@@ -85,7 +85,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
tb_hize_t filesize = tb_file_size(rawfile);
if (!filesize)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "cannot send empty file!");
return 2;
}
@@ -95,7 +95,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
if (lua_isnumber(lua, 3)) start = (tb_long_t)lua_tonumber(lua, 3);
if (start < 1 || start > filesize)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid start position(%ld)!", start);
return 2;
}
@@ -105,13 +105,13 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua)
if (lua_isnumber(lua, 4)) last = (tb_long_t)lua_tonumber(lua, 4);
if (last < start - 1 || last > filesize + start - 1)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid last position(%ld)!", last);
return 2;
}
// send file data
tb_long_t real = (tb_long_t)tb_socket_sendf(sock, rawfile, start - 1, last - start + 1);
- lua_pushnumber(lua, (tb_int_t)real);
+ lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
diff --git a/core/src/xmake/io/socket_sendto.c b/core/src/xmake/io/socket_sendto.c
index 6b1f8fa49..230f97aa5 100644
--- a/core/src/xmake/io/socket_sendto.c
+++ b/core/src/xmake/io/socket_sendto.c
@@ -43,7 +43,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
// check socket
if (!lua_isuserdata(lua, 1))
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
@@ -77,7 +77,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
}
if (!data || !size)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p) and size(%zu)!", data, size);
return 2;
}
@@ -87,7 +87,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 4);
if (!addr || !port)
{
- lua_pushnumber(lua, -1);
+ lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid address!");
return 2;
}
@@ -101,6 +101,6 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
// send data
tb_long_t real = tb_socket_usend(sock, &ipaddr, data, size);
- lua_pushnumber(lua, (tb_int_t)real);
+ lua_pushinteger(lua, (tb_int_t)real);
return 1;
}