Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add io.socket support #593

Merged
merged 45 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3dcbba4
add opensock
waruqi Oct 12, 2019
938be7f
seperate io submodules
waruqi Oct 12, 2019
02e8be0
add opensock arguments
waruqi Oct 12, 2019
18468a7
improve file module
waruqi Oct 12, 2019
9d3eab7
implement to open socket
waruqi Oct 12, 2019
5773633
add socket.rawfd
waruqi Oct 12, 2019
3d88cf2
add socket.connect
waruqi Oct 13, 2019
2eeba59
add socket module
waruqi Oct 13, 2019
f2a2f51
add socket.connect
waruqi Oct 13, 2019
4041c5a
improve socket type and family
waruqi Oct 14, 2019
95df841
add socket.wait
waruqi Oct 14, 2019
5166d62
add socket.bind
waruqi Oct 14, 2019
91bad8e
improve socket apis
waruqi Oct 14, 2019
7366683
add socket.accept
waruqi Oct 14, 2019
3c4ea75
export socket consts
waruqi Oct 14, 2019
194434e
fix socket.accept
waruqi Oct 15, 2019
e915ad6
move socket test files
waruqi Oct 15, 2019
690dc27
add socket.send
waruqi Oct 15, 2019
74fe92e
add socket.recv
waruqi Oct 16, 2019
6df3d8b
add socket.recv
waruqi Oct 27, 2019
239d4de
improve socket accept and connect
waruqi Oct 27, 2019
ecc9fe9
improve socket recv
waruqi Oct 27, 2019
433872b
improve socket.send
waruqi Oct 27, 2019
fa79256
concat socket.recv data
waruqi Oct 27, 2019
444f458
modify tcp echo demo
waruqi Oct 27, 2019
da73a6e
add socket.sendfile
waruqi Oct 28, 2019
a6e3140
fix some compiler errors
waruqi Oct 28, 2019
8bfbf6b
add sendfile demo
waruqi Oct 28, 2019
b2a507b
improve io.file and io.filelock
waruqi Oct 29, 2019
0ad553e
fix socket_recv
waruqi Oct 29, 2019
5f388f2
fix socket.recv
waruqi Oct 29, 2019
057e930
add bytes module
waruqi Oct 30, 2019
7d47837
impl bytes index and ipairs
waruqi Oct 31, 2019
5617860
export ffi/malloc for windows
waruqi Oct 31, 2019
af6fdba
add bytes test
waruqi Oct 31, 2019
45d6dd4
update bytes test
waruqi Oct 31, 2019
2359364
add bytes:dump()
waruqi Nov 1, 2019
6d4ad7f
improve bytes ctor
waruqi Nov 1, 2019
4f932ba
improve socket.recv using bytes
waruqi Nov 1, 2019
b0a7b7a
fix ffi for linux
waruqi Nov 2, 2019
8eddfb0
add socket.sendto and socket.recvfrom
waruqi Nov 3, 2019
7a63343
improve udp demo
waruqi Nov 3, 2019
8605710
fix socket send
waruqi Nov 4, 2019
821ea82
fix core xmake.lua
waruqi Nov 4, 2019
f00316a
fix core xmake.lua again
waruqi Nov 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/src/demo/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ target("demo")
-- make as a binary
set_kind("binary")

-- set basename of target file
set_basename("xmake")

-- add defines
add_defines("__tb_prefix__=\"xmake\"")

Expand All @@ -30,6 +27,7 @@ target("demo")
-- add links
if is_plat("windows") then
add_links("ws2_32", "advapi32", "shell32")
add_ldflags("/export:malloc", "/export:free")
elseif is_plat("android") then
add_links("m", "c")
elseif is_plat("macosx") then
Expand All @@ -49,6 +47,6 @@ target("demo")

-- copy target to the build directory
after_build(function (target)
os.cp(target:targetfile(), "$(buildir)")
os.cp(target:targetfile(), "$(buildir)/xmake" .. (is_plat("windows") and ".exe" or ""))
end)

4 changes: 2 additions & 2 deletions core/src/xmake/io/file_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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!");
}

4 changes: 2 additions & 2 deletions core/src/xmake/io/file_flush.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}
2 changes: 1 addition & 1 deletion core/src/xmake/io/file_isatty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions core/src/xmake/io/file_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/src/xmake/io/file_rawfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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!");
}
36 changes: 18 additions & 18 deletions core/src/xmake/io/file_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand All @@ -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++;
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/xmake/io/file_seek.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"
Expand All @@ -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");
}
4 changes: 2 additions & 2 deletions core/src/xmake/io/file_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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!");
}
2 changes: 1 addition & 1 deletion core/src/xmake/io/file_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions core/src/xmake/io/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down
57 changes: 57 additions & 0 deletions core/src/xmake/io/socket_accept.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*!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_accept.c
*
*/

/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_accept"
#define TB_TRACE_MODULE_DEBUG (0)

/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/

// local sock = io.socket_accept(sock)
tb_int_t xm_io_socket_accept(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);

// accept socket
tb_socket_ref_t client = tb_socket_accept(sock, tb_null);
if (client) lua_pushlightuserdata(lua, (tb_pointer_t)client);
else lua_pushnil(lua);
return 1;
}

Loading