diff options
| author | ruki <[email protected]> | 2019-10-28 22:37:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-28 09:44:32 +0800 |
| commit | da73a6e5c04c70fd4c49d7502fafd225d9a13ab5 (patch) | |
| tree | 58f09d93af509b61a9c3f1d7f952e3d79d23f8d8 | |
| parent | 444f458546dc9b46a6e3d264f75da31b5bc4431d (diff) | |
add socket.sendfile
| -rw-r--r-- | core/src/xmake/io/socket_recv.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_send.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_sendfile.c | 117 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 52 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 9 |
7 files changed, 195 insertions, 6 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c index bfb4fd1d1..162e36c1d 100644 --- a/core/src/xmake/io/socket_recv.c +++ b/core/src/xmake/io/socket_recv.c @@ -34,15 +34,19 @@ * implementation */ -// real, data_or_errors = io.socket_recv(file, size) +// real, data_or_errors = io.socket_recv(sock, size) tb_int_t xm_io_socket_recv(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // is user data? + // check socket if (!lua_isuserdata(lua, 1)) - return 0; + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid socket!"); + return 2; + } // get socket tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c index 0b0e3767c..ed9e864e1 100644 --- a/core/src/xmake/io/socket_send.c +++ b/core/src/xmake/io/socket_send.c @@ -34,15 +34,19 @@ * implementation */ -// io.socket_send(file, data, start, last) +// io.socket_send(sock, data, start, last) tb_int_t xm_io_socket_send(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // is user data? + // check socket if (!lua_isuserdata(lua, 1)) - return 0; + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid socket!"); + return 2; + } // get socket tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/socket_sendfile.c b/core/src/xmake/io/socket_sendfile.c new file mode 100644 index 000000000..df29afd32 --- /dev/null +++ b/core/src/xmake/io/socket_sendfile.c @@ -0,0 +1,117 @@ +/*!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_sendfile.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_sendfile" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// io.socket_sendfile(sock, file, start, last) +tb_int_t xm_io_socket_sendfile(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // check socket + if (!lua_isuserdata(lua, 1)) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid socket!"); + return 2; + } + + // check file + if (!lua_isuserdata(lua, 2)) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid file!"); + return 2; + } + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(sock, 0); + + // get file + xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 2); + tb_check_return_val(file, 0); + + // does not support stdfile + if (!xm_io_file_is_file(file) || !file->stream) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid file type!"); + return 2; + } + + // get file reference + 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_pushliteral(lua, "cannot get file reference!"); + return 2; + } + + // get file size + tb_hize_t filesize = tb_file_size(rawfile); + if (!filesize) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "cannot send empty file!"); + return 2; + } + + // get start + tb_long_t start = 1; + if (lua_isnumber(lua, 3)) start = (tb_long_t)lua_tonumber(lua, 3); + if (start < 1 || start > filesize) + { + lua_pushnumber(lua, -1); + lua_pushfstring(lua, "invalid start position(%ld)!", start); + return 2; + } + + // get last + tb_long_t last = (tb_long_t)filesize; + 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_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); + return 1; +} diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 99a0cf18e..4319023cc 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -118,6 +118,7 @@ tb_int_t xm_io_socket_listen(lua_State* lua); tb_int_t xm_io_socket_accept(lua_State* lua); tb_int_t xm_io_socket_connect(lua_State* lua); tb_int_t xm_io_socket_send(lua_State* lua); +tb_int_t xm_io_socket_sendfile(lua_State* lua); tb_int_t xm_io_socket_recv(lua_State* lua); tb_int_t xm_io_socket_close(lua_State* lua); @@ -261,6 +262,7 @@ static luaL_Reg const g_io_functions[] = , { "socket_accept", xm_io_socket_accept } , { "socket_connect", xm_io_socket_connect } , { "socket_send", xm_io_socket_send } +, { "socket_sendfile", xm_io_socket_sendfile } , { "socket_recv", xm_io_socket_recv } , { "socket_close", xm_io_socket_close } , { tb_null, tb_null } diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 597001707..b3028e984 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -68,6 +68,7 @@ xmake_C_FILES += \ io/socket_accept \ io/socket_connect \ io/socket_send \ + io/socket_sendfile \ io/socket_recv \ io/socket_close \ path/relative \ diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index a90659659..0dfae075b 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -220,6 +220,58 @@ function _instance:send(data, opt) return send, errors end +-- send file to socket +function _instance:sendfile(file, opt) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- init start and last + opt = opt or {} + local start = opt.start or 1 + local last = opt.last or file:size() + + -- check start and last + if start > last or start < 1 then + return -1, string.format("%s: invalid start(%d) and last(%d)!", self, start, last) + end + + -- send it + local send = 0 + local real = 0 + local wait = false + local errors = nil + if opt.block then + while start < last do + real, errors = io.socket_sendfile(self._SOCK, file, start, last) + if real > 0 then + send = send + real + start = start + real + wait = false + elseif real == 0 and not wait then + local events, waiterrs = self:wait(socket.EV_SEND, opt.timeout or -1) + if events == socket.EV_SEND then + wait = true + else + errors = waiterrs + break + end + else + break + end + end + else + send, errors = io.socket_sendfile(self._SOCK, file, start, last) + if send < 0 and errors then + errors = string.format("%s: %s", self, errors) + end + end + return send, errors +end + -- recv data from socket function _instance:recv(size, opt) diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index bfb5769f5..3d679bd52 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -114,6 +114,15 @@ function sandbox_core_base_socket_instance.send(sock, data, opt) return real end +-- send file to socket +function sandbox_core_base_socket_instance.sendfile(sock, file, opt) + local real, errors = sock:_sendfile(file, opt) + if real < 0 and errors then + raise(errors) + end + return real +end + -- recv data from socket function sandbox_core_base_socket_instance.recv(sock, size, opt) local real, data_or_errors = sock:_recv(size, opt) |
