diff options
| author | ruki <[email protected]> | 2019-11-06 00:36:43 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-11-05 21:40:22 +0800 |
| commit | 4e96ff73864ea29e5ec7730e5e08a20cf42e9b5f (patch) | |
| tree | f2dd6c95ff1200d457018fc809db8ef4e3ad9755 | |
| parent | b1eee1ec65dd0f9d3a26af52596d86c331160587 (diff) | |
add io poller
| -rw-r--r-- | core/src/xmake/io/poller.c | 65 | ||||
| -rw-r--r-- | core/src/xmake/io/poller.h | 41 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_insert.c | 59 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_modify.c | 59 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_remove.c | 56 | ||||
| -rw-r--r-- | core/src/xmake/io/stdfile.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 4 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 40 |
8 files changed, 325 insertions, 1 deletions
diff --git a/core/src/xmake/io/poller.c b/core/src/xmake/io/poller.c new file mode 100644 index 000000000..de514d9ac --- /dev/null +++ b/core/src/xmake/io/poller.c @@ -0,0 +1,65 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file poller.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the singleton type of poller +#define XM_IO_POLLER (TB_SINGLETON_TYPE_USER + 4) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_handle_t xm_io_poller_instance_init(tb_cpointer_t* ppriv) +{ + // init poller + tb_poller_ref_t poller = tb_poller_init(tb_null); + tb_assert_and_check_return_val(poller, tb_null); + + // attach poller to the current thread + tb_poller_attach(poller); + return (tb_handle_t)poller; +} +static tb_void_t xm_io_poller_instance_exit(tb_handle_t poller, tb_cpointer_t priv) +{ + if (poller) tb_poller_exit((tb_poller_ref_t)poller); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_poller_ref_t xm_io_poller() +{ + return (tb_poller_ref_t)tb_singleton_instance(XM_IO_POLLER, xm_io_poller_instance_init, xm_io_poller_instance_exit, tb_null, tb_null); +} + diff --git a/core/src/xmake/io/poller.h b/core/src/xmake/io/poller.h new file mode 100644 index 000000000..6ce82e0dd --- /dev/null +++ b/core/src/xmake/io/poller.h @@ -0,0 +1,41 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file poller.h + * + */ +#ifndef XM_IO_POLLER_H +#define XM_IO_POLLER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* get io poller + * + * @return the io poller + */ +tb_poller_ref_t xm_io_poller(tb_noarg_t); + +#endif + + diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c new file mode 100644 index 000000000..eeb6fd824 --- /dev/null +++ b/core/src/xmake/io/poller_insert.c @@ -0,0 +1,59 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file poller_insert.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_insert" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_insert(sock, events) +tb_int_t xm_io_poller_insert(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + 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 events + 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)); + return 1; +} + diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c new file mode 100644 index 000000000..24a27e241 --- /dev/null +++ b/core/src/xmake/io/poller_modify.c @@ -0,0 +1,59 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file poller_modify.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_modify" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_modify(sock, events) +tb_int_t xm_io_poller_modify(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + 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 events + 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)); + return 1; +} + diff --git a/core/src/xmake/io/poller_remove.c b/core/src/xmake/io/poller_remove.c new file mode 100644 index 000000000..cf6232fce --- /dev/null +++ b/core/src/xmake/io/poller_remove.c @@ -0,0 +1,56 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file poller_remove.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_remove" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_remove(sock) +tb_int_t xm_io_poller_remove(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + 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); + + // remove events from poller + lua_pushboolean(lua, tb_poller_remove(xm_io_poller(), sock)); + return 1; +} + diff --git a/core/src/xmake/io/stdfile.c b/core/src/xmake/io/stdfile.c index 8f466fa7a..f91048a46 100644 --- a/core/src/xmake/io/stdfile.c +++ b/core/src/xmake/io/stdfile.c @@ -36,7 +36,7 @@ #endif /* ////////////////////////////////////////////////////////////////////////////////////// - * macors + * macros */ // the singleton type of stdfile diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index e2660128a..960f5ef8b 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -60,6 +60,10 @@ xmake_C_FILES += \ io/filelock_unlock \ io/filelock_trylock \ io/filelock_close \ + io/poller \ + io/poller_insert \ + io/poller_remove \ + io/poller_modify \ io/socket_open \ io/socket_rawfd \ io/socket_wait \ diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 4a2dfdcdc..6a92d802f 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -20,6 +20,7 @@ -- define module local socket = socket or {} +local _poller = _poller or {} local _instance = _instance or {} -- load modules @@ -519,6 +520,45 @@ function _instance:__gc() end end +-- insert socket events to poller +function _poller:insert(sock, events) + + -- ensure opened + local ok, errors = sock:_ensure_opened() + if not ok then + return false, errors + end + + -- insert it + return io.poller_insert(sock._SOCK, events) +end + +-- modify socket events in poller +function _poller:modify(sock, events) + + -- ensure opened + local ok, errors = sock:_ensure_opened() + if not ok then + return false, errors + end + + -- modify it + return io.poller_modify(sock._SOCK, events) +end + +-- remove socket from poller +function _poller:remove(sock) + + -- ensure opened + local ok, errors = sock:_ensure_opened() + if not ok then + return false, errors + end + + -- remove it + return io.poller_remove(sock._SOCK) +end + -- open a socket -- -- @param socktype the socket type, e.g. tcp, udp, icmp |
