Skip to content

Commit f271350

Browse files
committed
优化tcp socket写功能
1 parent 5c6d76b commit f271350

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

sample/main.lua

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ print("value", value1, value2)]]
7070
-- # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
7171
-- #endif
7272

73+
7374
if context.winos then
7475
package.cpath = package.cpath ..";".."..\\clib\\?.dll"
7576
package.path = package.path ..";".."..\\lualib\\?.lua"
@@ -78,20 +79,40 @@ else
7879
package.path = package.path ..";".."../lualib/?.lua"
7980
end
8081

81-
for i = 1, 32 do
82-
context.create("test.lua", 1, 2, 3)
83-
end
84-
do return end
82+
-- for i = 1, 32 do
83+
-- context.create("test.lua", 1, 2, 3)
84+
-- end
85+
-- do return end
8586

8687
local ret, sock = tcp.listen("0.0.0.0", 8801)
8788
if not ret then
8889
print("tcp.listens failed: ", context.strerror(sock))
8990
else
9091
print("tcp.listens success: ", sock)
91-
sock:accept(function(result, accept_sock) print("sock:accept", result, accept_sock) sock:close() accept_sock:set_nodelay(false) accept_sock:write("hello world!") end)
92+
sock:accept(function(result, accept_sock)
93+
print("sock:accept", result, accept_sock)
94+
sock:close()
95+
--accept_sock:set_nodelay(false)
96+
accept_sock:set_rwopt({ read_head_endian = "L", read_head_bytes = 2, read_head_max = 65535, write_head_endian = "L", write_head_bytes = 2, write_head_max = 65535,})
97+
accept_sock:set_wshared(true)
98+
print("accept_sock:fd", accept_sock:fd())
99+
tcp.write2(accept_sock:fd(), "hello world!")
100+
accept_sock:write("hello world!")
101+
tcp.write2(accept_sock:fd(), "hello world!")
102+
tcp.write2(accept_sock:fd(), "hello world!")
103+
accept_sock:write("hello world!")
104+
end)
92105
local ret, con_sock = tcp.connect("127.0.0.1", 8801)
93106
if ret then
94107
print("tcp.connect", ret, con_sock)
108+
con_sock:set_rwopt({ read_head_endian = "L", read_head_bytes = 2, read_head_max = 65535, write_head_endian = "L", write_head_bytes = 2, write_head_max = 65535,})
109+
con_sock:set_wshared(true)
110+
local ret, buffer = con_sock:read()
111+
print("tcp read", ret, ret and buffer:tostring())
112+
local ret, buffer = con_sock:read()
113+
print("tcp read", ret, ret and buffer:tostring())
114+
local ret, buffer = con_sock:read()
115+
print("tcp read", ret, ret and buffer:tostring())
95116
local ret, buffer = con_sock:read()
96117
print("tcp read", ret, ret and buffer:tostring())
97118
else

src/lua_tcp_handle.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ lua_tcp_socket_handle_t* lua_tcp_socket_handle_t::create_tcp_socket(uv_tcp_socke
371371
lua_setfield(L, -2, "get_rwopt");
372372
lua_pushcfunction(L, lua_tcp_socket_handle_t::set_nodelay);
373373
lua_setfield(L, -2, "set_nodelay");
374+
lua_pushcfunction(L, lua_tcp_socket_handle_t::set_wshared);
375+
lua_setfield(L, -2, "set_wshared");
374376
lua_pushcfunction(L, lua_tcp_is_closed);
375377
lua_setfield(L, -2, "is_closed");
376378
lua_pushcfunction(L, lua_tcp_get_fd);
@@ -962,6 +964,7 @@ int luaopen_tcp(lua_State *L)
962964
{ "set_rwopt", lua_tcp_socket_handle_t::set_rwopt },
963965
{ "get_rwopt", lua_tcp_socket_handle_t::get_rwopt },
964966
{ "set_nodelay", lua_tcp_socket_handle_t::set_nodelay },
967+
{ "set_wshared", lua_tcp_socket_handle_t::set_wshared },
965968
{ "close", lua_tcp_close },
966969
{ "is_closed", lua_tcp_is_closed },
967970
{ "fd", lua_tcp_get_fd },

src/lua_tcp_handle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "context_lua.h"
77
#include <queue>
88

9-
#define TCP_SOCKET_MAKE_FD(lua_ref, context_id) ((int64_t)(lua_ref)/1000000 + 1000000*(int64_t)(context_id))
9+
#define TCP_SOCKET_MAKE_FD(lua_ref, context_id) ((int64_t)(lua_ref)%1000000 + 1000000*(int64_t)(context_id))
1010

1111
class context_lua_t;
1212
class uv_handle_base_t;

0 commit comments

Comments
 (0)