diff options
| author | ruki <[email protected]> | 2019-10-16 22:15:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-16 07:30:16 +0800 |
| commit | 74fe92ec5fab1725b139567f468f49cab4580888 (patch) | |
| tree | 4cb88fa26fe6580b6441f76b17b41e8cd393a85c /core/src/xmake/io | |
| parent | 690dc274a4d5f9f443e6c2004b985b03345874d6 (diff) | |
add socket.recv
Diffstat (limited to 'core/src/xmake/io')
| -rw-r--r-- | core/src/xmake/io/socket_recv.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c new file mode 100644 index 000000000..849bc832a --- /dev/null +++ b/core/src/xmake/io/socket_recv.c @@ -0,0 +1,68 @@ +/*!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 socket_recv.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_recv" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// io.socket_recv(file, data, start, last) +tb_int_t xm_io_socket_recv(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 data + size_t datasize = 0; + tb_char_t const* data = luaL_checklstring(lua, 2, &datasize); + tb_assert_and_check_return_val(data, 0); + + // get start + tb_size_t start = 1; + if (lua_isnumber(lua, 3)) start = lua_tonumber(lua, 3); + + // get last + tb_size_t last = (tb_size_t)datasize; + if (lua_isnumber(lua, 4)) last = lua_tonumber(lua, 4); + + // recv data + tb_long_t real = tb_socket_recv(sock, (tb_byte_t const*)data + start - 1, last - start + 1); + lua_pushnumber(lua, (tb_int_t)real); + return 1; +} |
