diff options
| author | ruki <[email protected]> | 2020-01-31 23:28:19 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-01-31 14:50:00 +0800 |
| commit | 20c8823da8a5bde7de52479d7c504a6bde8a23fb (patch) | |
| tree | f7e32dc83ad8bee1d46a8b5dcc86cdc2a75188ba /core/src | |
| parent | 14bb17934b8acf2a0633f8af185c6a024fe37e54 (diff) | |
add core/pipe open and close
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/tbox/makefile | 1 | ||||
| -rw-r--r-- | core/src/xmake/io/pipe_close.c | 55 | ||||
| -rw-r--r-- | core/src/xmake/io/pipe_open.c | 62 | ||||
| -rw-r--r-- | core/src/xmake/io/pipe_openpair.c | 61 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 8 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 3 |
6 files changed, 190 insertions, 0 deletions
diff --git a/core/src/tbox/makefile b/core/src/tbox/makefile index 1b2ad1299..ac392519d 100644 --- a/core/src/tbox/makefile +++ b/core/src/tbox/makefile @@ -232,6 +232,7 @@ tbox_C_FILES += \ tbox/src/tbox/platform/cpu \ tbox/src/tbox/platform/sched \ tbox/src/tbox/platform/semaphore \ + tbox/src/tbox/platform/pipe \ tbox/src/tbox/platform/socket \ tbox/src/tbox/platform/syserror \ tbox/src/tbox/platform/thread \ diff --git a/core/src/xmake/io/pipe_close.c b/core/src/xmake/io/pipe_close.c new file mode 100644 index 000000000..f0bb4113e --- /dev/null +++ b/core/src/xmake/io/pipe_close.c @@ -0,0 +1,55 @@ +/*!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-2020, TBOOX Open Source Group. + * + * @author ruki + * @file pipe_close.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "pipe_close" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.pipe_close(pipe) +tb_int_t xm_io_pipe_close(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get the pipe file + tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(pipefile, 0); + + // exit pipe file + lua_pushboolean(lua, tb_pipe_file_exit(pipefile)); + return 1; +} + diff --git a/core/src/xmake/io/pipe_open.c b/core/src/xmake/io/pipe_open.c new file mode 100644 index 000000000..8a5c55c85 --- /dev/null +++ b/core/src/xmake/io/pipe_open.c @@ -0,0 +1,62 @@ +/*!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-2020, TBOOX Open Source Group. + * + * @author ruki + * @file pipe_open.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "pipe_open" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* + * io.pipe_open(name, mode, buffsize) + */ +tb_int_t xm_io_pipe_open(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get pipe name and mode + tb_char_t const* name = luaL_checkstring(lua, 1); + tb_char_t const* modestr = luaL_optstring(lua, 2, "r"); + tb_assert_and_check_return_val(name && modestr, 0); + + // get file mode value + tb_size_t mode = TB_FILE_MODE_RO; + if (modestr[0] == 'w') mode = TB_FILE_MODE_WO; + + // get buffer size + tb_size_t buffsize = (tb_size_t)luaL_checknumber(lua, 3); + + // open pipe file + tb_pipe_file_ref_t pipefile = tb_pipe_file_init(name, mode, buffsize); + if (pipefile) lua_pushlightuserdata(lua, (tb_pointer_t)pipefile); + else lua_pushnil(lua); + return 1; +} diff --git a/core/src/xmake/io/pipe_openpair.c b/core/src/xmake/io/pipe_openpair.c new file mode 100644 index 000000000..a1f42cc97 --- /dev/null +++ b/core/src/xmake/io/pipe_openpair.c @@ -0,0 +1,61 @@ +/*!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-2020, TBOOX Open Source Group. + * + * @author ruki + * @file pipe_openpair.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "pipe_openpair" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* + * io.pipe_openpair(buffsize) + */ +tb_int_t xm_io_pipe_openpair(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get buffer size + tb_size_t buffsize = (tb_size_t)luaL_checknumber(lua, 1); + + // init pipe + tb_pipe_file_ref_t pipefile[2]; + if (tb_pipe_file_init_pair(pipefile, buffsize)) + { + lua_pushlightuserdata(lua, (tb_pointer_t)pipefile[0]); + lua_pushlightuserdata(lua, (tb_pointer_t)pipefile[1]); + } + else + { + lua_pushnil(lua); + lua_pushnil(lua); + } + return 2; +} diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 5e8a45d5a..13be65354 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -124,6 +124,11 @@ tb_int_t xm_io_socket_recv(lua_State* lua); tb_int_t xm_io_socket_recvfrom(lua_State* lua); tb_int_t xm_io_socket_close(lua_State* lua); +// the io/pipe functions +tb_int_t xm_io_pipe_open(lua_State* lua); +tb_int_t xm_io_pipe_openpair(lua_State* lua); +tb_int_t xm_io_pipe_close(lua_State* lua); + // the io/poller functions tb_int_t xm_io_poller_insert(lua_State* lua); tb_int_t xm_io_poller_modify(lua_State* lua); @@ -277,6 +282,9 @@ static luaL_Reg const g_io_functions[] = , { "socket_recv", xm_io_socket_recv } , { "socket_recvfrom", xm_io_socket_recvfrom } , { "socket_close", xm_io_socket_close } +, { "pipe_open", xm_io_pipe_open } +, { "pipe_openpair", xm_io_pipe_openpair } +, { "pipe_close", xm_io_pipe_close } , { "poller_insert", xm_io_poller_insert } , { "poller_modify", xm_io_poller_modify } , { "poller_remove", xm_io_poller_remove } diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 935e210a6..d0dce1f67 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -80,6 +80,9 @@ xmake_C_FILES += \ io/socket_recv \ io/socket_recvfrom \ io/socket_close \ + io/pipe_open \ + io/pipe_openpair \ + io/pipe_close \ path/relative \ path/absolute \ path/translate \ |
