summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-13 07:35:51 +0800
committerruki <[email protected]>2019-10-13 07:35:51 +0800
commit577363396ad259128813f52148547fd97f89f95b (patch)
treec6795ed6c2a1d7ca99e2723e76fb13cb47ed659c /core/src
parent9d3eab796916f907a5fc95b4333791ee2ac6df49 (diff)
add socket.rawfd
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/io/file_close.c4
-rw-r--r--core/src/xmake/io/file_flush.c4
-rw-r--r--core/src/xmake/io/file_isatty.c2
-rw-r--r--core/src/xmake/io/file_open.c8
-rw-r--r--core/src/xmake/io/file_rawfd.c4
-rw-r--r--core/src/xmake/io/file_read.c36
-rw-r--r--core/src/xmake/io/file_seek.c8
-rw-r--r--core/src/xmake/io/file_size.c4
-rw-r--r--core/src/xmake/io/file_write.c2
-rw-r--r--core/src/xmake/io/prefix.h4
-rw-r--r--core/src/xmake/io/socket_open.c12
-rw-r--r--core/src/xmake/io/socket_rawfd.c62
-rw-r--r--core/src/xmake/io/stdfile.c2
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
15 files changed, 105 insertions, 50 deletions
diff --git a/core/src/xmake/io/file_close.c b/core/src/xmake/io/file_close.c
index 6b3efdd98..1e3ebd131 100644
--- a/core/src/xmake/io/file_close.c
+++ b/core/src/xmake/io/file_close.c
@@ -42,7 +42,7 @@ tb_int_t xm_io_file_close(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "close(invalid file)!");
+ xm_io_return_error(lua, "close(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
@@ -88,6 +88,6 @@ tb_int_t xm_io_file_close(lua_State* lua)
lua_pushboolean(lua, tb_true);
return 1;
}
- else xm_io_file_return_error(lua, "cannot close this file!");
+ else xm_io_return_error(lua, "cannot close this file!");
}
diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c
index 72969b09d..a4bf0f672 100644
--- a/core/src/xmake/io/file_flush.c
+++ b/core/src/xmake/io/file_flush.c
@@ -69,7 +69,7 @@ tb_int_t xm_io_file_flush(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "flush(invalid file)!");
+ xm_io_return_error(lua, "flush(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
@@ -82,5 +82,5 @@ tb_int_t xm_io_file_flush(lua_State* lua)
lua_pushboolean(lua, tb_true);
return 1;
}
- else xm_io_file_return_error(lua, "failed to flush file");
+ else xm_io_return_error(lua, "failed to flush file");
}
diff --git a/core/src/xmake/io/file_isatty.c b/core/src/xmake/io/file_isatty.c
index d8c5183d8..06eea4b29 100644
--- a/core/src/xmake/io/file_isatty.c
+++ b/core/src/xmake/io/file_isatty.c
@@ -42,7 +42,7 @@ tb_int_t xm_io_file_isatty(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "isatty(invalid file)!");
+ xm_io_return_error(lua, "isatty(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
diff --git a/core/src/xmake/io/file_open.c b/core/src/xmake/io/file_open.c
index 46e63cd62..9db81a82c 100644
--- a/core/src/xmake/io/file_open.c
+++ b/core/src/xmake/io/file_open.c
@@ -217,10 +217,10 @@ tb_int_t xm_io_file_open(lua_State* lua)
else
{
if (stream) tb_stream_exit(stream);
- xm_io_file_return_error(lua, "file not found!");
+ xm_io_return_error(lua, "file not found!");
}
}
- else xm_io_file_return_error(lua, "invalid open mode!");
+ else xm_io_return_error(lua, "invalid open mode!");
tb_assert_and_check_return_val(encoding != XM_IO_FILE_ENCODING_UNKNOWN, 0);
// open file
@@ -271,9 +271,7 @@ tb_int_t xm_io_file_open(lua_State* lua)
fstream = tb_null;
// return errors
- lua_pushnil(lua);
- lua_pushliteral(lua, "failed to open file.");
- return 2;
+ xm_io_return_error(lua, "failed to open file!");
}
// make file
diff --git a/core/src/xmake/io/file_rawfd.c b/core/src/xmake/io/file_rawfd.c
index dcc69eceb..a298c29c2 100644
--- a/core/src/xmake/io/file_rawfd.c
+++ b/core/src/xmake/io/file_rawfd.c
@@ -58,7 +58,7 @@ tb_int_t xm_io_file_rawfd(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "get rawfd for invalid file!");
+ xm_io_return_error(lua, "get rawfd for invalid file!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
@@ -76,5 +76,5 @@ tb_int_t xm_io_file_rawfd(lua_State* lua)
}
// get rawfd failed
- xm_io_file_return_error(lua, "get rawfd for invalid file!");
+ xm_io_return_error(lua, "get rawfd for invalid file!");
}
diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c
index 438dc022d..d5748ffe3 100644
--- a/core/src/xmake/io/file_read.c
+++ b/core/src/xmake/io/file_read.c
@@ -182,7 +182,7 @@ static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file_t* file)
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
- xm_io_file_return_error(lua, "init buffer failed!");
+ xm_io_return_error(lua, "init buffer failed!");
// read all
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
@@ -219,7 +219,7 @@ static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file_t* file, tb_char_
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
- xm_io_file_return_error(lua, "init buffer failed!");
+ xm_io_return_error(lua, "init buffer failed!");
// read all
tb_bool_t has_content = tb_false;
@@ -239,7 +239,7 @@ static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file_t* file, tb_char_
case PL_FAIL:
default:
tb_buffer_exit(&buf);
- xm_io_file_return_error(lua, "failed to read all");
+ xm_io_return_error(lua, "failed to read all");
break;
}
}
@@ -253,7 +253,7 @@ static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file_t* file, tb_char
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
- xm_io_file_return_error(lua, "init buffer failed!");
+ xm_io_return_error(lua, "init buffer failed!");
// read line
tb_bool_t has_content = tb_false;
@@ -276,7 +276,7 @@ static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file_t* file, tb_char
case PL_FAIL:
default:
tb_buffer_exit(&buf);
- xm_io_file_return_error(lua, "failed to readline");
+ xm_io_return_error(lua, "failed to readline");
break;
}
}
@@ -289,11 +289,11 @@ static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file_t* file, tb_char_t
// check continuation
if (*continuation != '\0')
- xm_io_file_return_error(lua, "continuation is not supported for read number of bytes");
+ xm_io_return_error(lua, "continuation is not supported for read number of bytes");
// check encoding
if (file->encoding != XM_IO_FILE_ENCODING_BINARY)
- xm_io_file_return_error(lua, "read number of bytes only allows binary file, reopen with 'rb' and try again");
+ xm_io_return_error(lua, "read number of bytes only allows binary file, reopen with 'rb' and try again");
tb_bool_t ok = tb_false;
if (n == 0)
@@ -381,7 +381,7 @@ static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file_t* file, tb_
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
- xm_io_file_return_error(lua, "init buffer failed!");
+ xm_io_return_error(lua, "init buffer failed!");
// read line
tb_bool_t has_content = tb_false;
@@ -404,7 +404,7 @@ static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file_t* file, tb_
case PL_FAIL:
default:
tb_buffer_exit(&buf);
- xm_io_file_return_error(lua, "failed to readline");
+ xm_io_return_error(lua, "failed to readline");
break;
}
}
@@ -418,7 +418,7 @@ static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_c
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
- xm_io_file_return_error(lua, "init buffer failed!");
+ xm_io_return_error(lua, "init buffer failed!");
// read all
tb_bool_t has_content = tb_false;
@@ -438,7 +438,7 @@ static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_c
case PL_FAIL:
default:
tb_buffer_exit(&buf);
- xm_io_file_return_error(lua, "failed to readline");
+ xm_io_return_error(lua, "failed to readline");
break;
}
}
@@ -451,7 +451,7 @@ static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file_t* file, tb_cha
// check continuation
if (*continuation != '\0')
- xm_io_file_return_error(lua, "continuation is not supported for std streams");
+ xm_io_return_error(lua, "continuation is not supported for std streams");
// io.read(0)
if (n == 0)
@@ -482,7 +482,7 @@ static tb_int_t xm_io_file_std_read_num(lua_State* lua, xm_io_file_t* file, tb_c
// check continuation
if (*continuation != '\0')
- xm_io_file_return_error(lua, "continuation is not supported for std streams");
+ xm_io_return_error(lua, "continuation is not supported for std streams");
// read number
tb_char_t strbuf[512];
@@ -506,7 +506,7 @@ tb_int_t xm_io_file_read(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "read(invalid file)!");
+ xm_io_return_error(lua, "read(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
@@ -521,7 +521,7 @@ tb_int_t xm_io_file_read(lua_State* lua)
if (lua_isnumber(lua, 2))
{
count = (tb_long_t)lua_tointeger(lua, 2);
- if (count < 0) xm_io_file_return_error(lua, "invalid read size, must be positive nubmber or 0");
+ if (count < 0) xm_io_return_error(lua, "invalid read size, must be positive nubmber or 0");
}
else if (*mode == '*')
mode++;
@@ -533,10 +533,10 @@ tb_int_t xm_io_file_read(lua_State* lua)
{
case 'a': return xm_io_file_read_all(lua, file, continuation);
case 'L': return xm_io_file_read_line(lua, file, continuation, tb_true);
- case 'n': xm_io_file_return_error(lua, "read number is not implemented");
+ case 'n': xm_io_return_error(lua, "read number is not implemented");
case 'l': return xm_io_file_read_line(lua, file, continuation, tb_false);
default:
- xm_io_file_return_error(lua, "unknonwn read mode");
+ xm_io_return_error(lua, "unknonwn read mode");
return 0;
}
}
@@ -550,7 +550,7 @@ tb_int_t xm_io_file_read(lua_State* lua)
case 'n': return xm_io_file_std_read_num(lua, file, continuation);
case 'l': return xm_io_file_std_read_line(lua, file, continuation, tb_false);
default:
- xm_io_file_return_error(lua, "unknonwn read mode");
+ xm_io_return_error(lua, "unknonwn read mode");
return 0;
}
}
diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c
index a4e14bffa..67cfe061c 100644
--- a/core/src/xmake/io/file_seek.c
+++ b/core/src/xmake/io/file_seek.c
@@ -42,7 +42,7 @@ tb_int_t xm_io_file_seek(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "seek(invalid file)!");
+ xm_io_return_error(lua, "seek(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
@@ -66,7 +66,7 @@ tb_int_t xm_io_file_seek(lua_State* lua)
tb_hong_t size = tb_stream_size(file->file_ref);
if (size > 0 && size + offset <= size)
offset = size + offset;
- else xm_io_file_return_error(lua, "seek failed, invalid offset!");
+ else xm_io_return_error(lua, "seek failed, invalid offset!");
}
break;
default: // "cur"
@@ -79,7 +79,7 @@ tb_int_t xm_io_file_seek(lua_State* lua)
lua_pushnumber(lua, (lua_Number)offset);
return 1;
}
- else xm_io_file_return_error(lua, "seek failed!");
+ else xm_io_return_error(lua, "seek failed!");
}
- else xm_io_file_return_error(lua, "seek is not supported on this file");
+ else xm_io_return_error(lua, "seek is not supported on this file");
}
diff --git a/core/src/xmake/io/file_size.c b/core/src/xmake/io/file_size.c
index 4e502bbfb..637268ced 100644
--- a/core/src/xmake/io/file_size.c
+++ b/core/src/xmake/io/file_size.c
@@ -42,7 +42,7 @@ tb_int_t xm_io_file_size(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "get size for invalid file!");
+ xm_io_return_error(lua, "get size for invalid file!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
@@ -56,5 +56,5 @@ tb_int_t xm_io_file_size(lua_State* lua)
lua_pushnumber(lua, (lua_Number)tb_stream_size(file->stream));
return 1;
}
- else xm_io_file_return_error(lua, "get size for invalid file!");
+ else xm_io_return_error(lua, "get size for invalid file!");
}
diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c
index 43aaf606e..b276a0e10 100644
--- a/core/src/xmake/io/file_write.c
+++ b/core/src/xmake/io/file_write.c
@@ -116,7 +116,7 @@ tb_int_t xm_io_file_write(lua_State* lua)
// is user data?
if (!lua_isuserdata(lua, 1))
- xm_io_file_return_error(lua, "write(invalid file)!");
+ xm_io_return_error(lua, "write(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
diff --git a/core/src/xmake/io/prefix.h b/core/src/xmake/io/prefix.h
index ba567f6fb..c96570396 100644
--- a/core/src/xmake/io/prefix.h
+++ b/core/src/xmake/io/prefix.h
@@ -33,8 +33,8 @@
#define xm_io_file_is_std(file) ((file)->type != XM_IO_FILE_TYPE_FILE)
#define xm_io_file_is_tty(file) (!!((file)->type & XM_IO_FILE_FLAG_TTY))
-// return file error
-#define xm_io_file_return_error(lua, error) \
+// return io error
+#define xm_io_return_error(lua, error) \
do \
{ \
lua_pushnil(lua); \
diff --git a/core/src/xmake/io/socket_open.c b/core/src/xmake/io/socket_open.c
index 647cd247e..64cbea80e 100644
--- a/core/src/xmake/io/socket_open.c
+++ b/core/src/xmake/io/socket_open.c
@@ -59,11 +59,7 @@ tb_int_t xm_io_socket_open(lua_State* lua)
else if (!tb_strcmp(socktype, "icmp"))
t = TB_SOCKET_TYPE_ICMP;
else
- {
- lua_pushnil(lua);
- lua_pushliteral(lua, "invalid socket type!");
- return 2;
- }
+ xm_io_return_error(lua, "invalid socket type!");
// map address family
tb_size_t f = TB_IPADDR_FAMILY_NONE;
@@ -72,11 +68,7 @@ tb_int_t xm_io_socket_open(lua_State* lua)
else if (!tb_strcmp(family, "ipv6"))
f = TB_IPADDR_FAMILY_IPV6;
else
- {
- lua_pushnil(lua);
- lua_pushliteral(lua, "invalid address family!");
- return 2;
- }
+ xm_io_return_error(lua, "invalid address family!");
// init socket
tb_socket_ref_t sock = tb_socket_init(t, f);
diff --git a/core/src/xmake/io/socket_rawfd.c b/core/src/xmake/io/socket_rawfd.c
new file mode 100644
index 000000000..8682e99fc
--- /dev/null
+++ b/core/src/xmake/io/socket_rawfd.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 sock 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
+ * @sock socket_rawfd.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "socket_rawfd"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * macros
+ */
+
+// socket to fd
+#define xm_io_sock2fd(sock) (lua_Number)tb_sock2fd(sock)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* io.socket_rawfd(sock)
+ */
+tb_int_t xm_io_socket_rawfd(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // is user data?
+ if (!lua_isuserdata(lua, 1))
+ xm_io_return_error(lua, "get rawfd for invalid sock!");
+
+ // get socket
+ tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1);
+ tb_check_return_val(sock, 0);
+
+ // return result
+ lua_pushnumber(lua, xm_io_sock2fd(sock));
+ return 1;
+}
diff --git a/core/src/xmake/io/stdfile.c b/core/src/xmake/io/stdfile.c
index c5adae143..8f466fa7a 100644
--- a/core/src/xmake/io/stdfile.c
+++ b/core/src/xmake/io/stdfile.c
@@ -167,6 +167,6 @@ tb_int_t xm_io_stdfile(lua_State* lua)
lua_pushlightuserdata(lua, (tb_pointer_t)file);
return 1;
}
- else xm_io_file_return_error(lua, "invalid stdfile type!");
+ else xm_io_return_error(lua, "invalid stdfile type!");
}
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 412106985..1cb2b94b6 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -111,6 +111,7 @@ tb_int_t xm_io_filelock_close(lua_State* lua);
// the io/socket functions
tb_int_t xm_io_socket_open(lua_State* lua);
+tb_int_t xm_io_socket_rawfd(lua_State* lua);
tb_int_t xm_io_socket_close(lua_State* lua);
// the path functions
@@ -246,6 +247,7 @@ static luaL_Reg const g_io_functions[] =
, { "filelock_unlock", xm_io_filelock_unlock }
, { "filelock_close", xm_io_filelock_close }
, { "socket_open", xm_io_socket_open }
+, { "socket_rawfd", xm_io_socket_rawfd }
, { "socket_close", xm_io_socket_close }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 1572cecbf..bc9b27ba2 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -61,6 +61,7 @@ xmake_C_FILES += \
io/filelock_trylock \
io/filelock_close \
io/socket_open \
+ io/socket_rawfd \
io/socket_close \
path/relative \
path/absolute \