summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-16 00:48:50 +0800
committerruki <[email protected]>2019-10-15 22:53:03 +0800
commit690dc274a4d5f9f443e6c2004b985b03345874d6 (patch)
tree43b6ba534d331325ca8d4022994bda75e496e5eb /core/src
parente915ad63a7f1a9fa4736472c60b0550cbd174361 (diff)
add socket.send
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/io/socket_send.c68
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
3 files changed, 71 insertions, 0 deletions
diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c
new file mode 100644
index 000000000..f33e77414
--- /dev/null
+++ b/core/src/xmake/io/socket_send.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_send.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "socket_send"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// io.socket_send(file, 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?
+ 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);
+
+ // send data
+ tb_long_t real = tb_socket_send(sock, (tb_byte_t const*)data + 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 0a72b2456..9b9ede041 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -117,6 +117,7 @@ tb_int_t xm_io_socket_bind(lua_State* lua);
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_close(lua_State* lua);
// the path functions
@@ -258,6 +259,7 @@ static luaL_Reg const g_io_functions[] =
, { "socket_listen", xm_io_socket_listen }
, { "socket_accept", xm_io_socket_accept }
, { "socket_connect", xm_io_socket_connect }
+, { "socket_send", xm_io_socket_send }
, { "socket_close", xm_io_socket_close }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 830f83066..c7ebc2644 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -67,6 +67,7 @@ xmake_C_FILES += \
io/socket_listen \
io/socket_accept \
io/socket_connect \
+ io/socket_send \
io/socket_close \
path/relative \
path/absolute \