diff options
| author | ruki <[email protected]> | 2022-04-06 22:39:09 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-06 22:39:09 +0800 |
| commit | cc8c6dee305ab37f2ba18bbd4d04cc62ad0a9f2e (patch) | |
| tree | b3d9c336d7beb6d72285925442aecddc004c2706 | |
| parent | 60c78f84bcef3be6e5a906cd10fe6bc0d7b0c77c (diff) | |
improve socket
| -rw-r--r-- | tests/modules/socket/sched_tcp/echo_client.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/sched_tcp/echo_server.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/sched_tcp/file_client.lua | 10 | ||||
| -rw-r--r-- | tests/modules/socket/sched_udp/echo_client.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/sched_udp/echo_server.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/echo_client.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/echo_server.lua | 8 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/file_client.lua | 10 | ||||
| -rw-r--r-- | tests/modules/socket/udp/echo_client.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/udp/echo_server.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/unix_tcp/echo_client.lua | 4 | ||||
| -rw-r--r-- | tests/modules/socket/unix_tcp/echo_server.lua | 8 | ||||
| -rw-r--r-- | tests/modules/socket/unix_tcp/file_client.lua | 10 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 43 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 8 | ||||
| -rw-r--r-- | xmake/modules/private/service/server/server.lua | 5 |
16 files changed, 66 insertions, 68 deletions
diff --git a/tests/modules/socket/sched_tcp/echo_client.lua b/tests/modules/socket/sched_tcp/echo_client.lua index 95277742e..eb90f2200 100644 --- a/tests/modules/socket/sched_tcp/echo_client.lua +++ b/tests/modules/socket/sched_tcp/echo_client.lua @@ -1,3 +1,4 @@ +import("core.base.bytes") import("core.base.socket") import("core.base.scheduler") @@ -5,8 +6,9 @@ function _session_recv(sock) print("%s: recv ..", sock) local count = 0 local result = nil + local buff = bytes(8192) while count < 100000 do - local recv, data = sock:recv(13, {block = true}) + local recv, data = sock:recv(buff, 13, {block = true}) if recv > 0 then result = data count = count + 1 diff --git a/tests/modules/socket/sched_tcp/echo_server.lua b/tests/modules/socket/sched_tcp/echo_server.lua index 98d1b94d5..bb9cedd08 100644 --- a/tests/modules/socket/sched_tcp/echo_server.lua +++ b/tests/modules/socket/sched_tcp/echo_server.lua @@ -1,3 +1,4 @@ +import("core.base.bytes") import("core.base.socket") import("core.base.scheduler") @@ -5,8 +6,9 @@ function _session_recv(sock) print("%s: recv ..", sock) local count = 0 local result = nil + local buff = bytes(8192) while count < 100000 do - local recv, data = sock:recv(13, {block = true}) + local recv, data = sock:recv(buff, 13, {block = true}) if recv > 0 then result = data count = count + 1 diff --git a/tests/modules/socket/sched_tcp/file_client.lua b/tests/modules/socket/sched_tcp/file_client.lua index 7afe33466..61bfe0762 100644 --- a/tests/modules/socket/sched_tcp/file_client.lua +++ b/tests/modules/socket/sched_tcp/file_client.lua @@ -10,13 +10,12 @@ function _session(addr, port) local recv = 0 local data = nil local wait = false - local results = {} + local buff = bytes(8192) while true do - real, data = sock:recv(8192) + real, data = sock:recv(buff, 8192) if real > 0 then recv = recv + real wait = false - table.insert(results, data) elseif real == 0 and not wait then if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then wait = true @@ -27,10 +26,7 @@ function _session(addr, port) break end end - if #results > 0 then - data = bytes(results) - end - print("%s: recv ok, size: %d, #data: %d!", sock, recv, data and data:size() or 0) + print("%s: recv ok, size: %d!", sock, recv) sock:close() end diff --git a/tests/modules/socket/sched_udp/echo_client.lua b/tests/modules/socket/sched_udp/echo_client.lua index 4a3e9ef2c..a53865e86 100644 --- a/tests/modules/socket/sched_udp/echo_client.lua +++ b/tests/modules/socket/sched_udp/echo_client.lua @@ -1,11 +1,13 @@ +import("core.base.bytes") import("core.base.socket") import("core.base.scheduler") function _session(addr, port, data) + local buff = bytes(8192) local sock = socket.udp() local send = sock:sendto(data or "hello xmake!", addr, port, {block = true}) print("%s: send to %s:%d %d bytes!", sock, addr, port, send) - local recv, data, peer_addr, peer_port = sock:recvfrom(8112, {block = true}) + local recv, data, peer_addr, peer_port = sock:recvfrom(buff, 8112, {block = true}) if recv > 0 then print("%s: recv %d bytes from %s:%d", sock, recv, peer_addr, peer_port) data:dump() diff --git a/tests/modules/socket/sched_udp/echo_server.lua b/tests/modules/socket/sched_udp/echo_server.lua index 905f28dc2..31ca26d64 100644 --- a/tests/modules/socket/sched_udp/echo_server.lua +++ b/tests/modules/socket/sched_udp/echo_server.lua @@ -1,12 +1,14 @@ +import("core.base.bytes") import("core.base.socket") import("core.base.scheduler") function _listen(addr, port) + local buff = bytes(8192) local sock = socket.udp() sock:bind(addr, port) while true do print("%s: recv in %s:%d ..", sock, addr, port) - local recv, data, peer_addr, peer_port = sock:recvfrom(8192, {block = true}) + local recv, data, peer_addr, peer_port = sock:recvfrom(buff, 8192, {block = true}) print("%s: recv %d bytes from: %s:%d", sock, recv, peer_addr, peer_port) if data then data:dump() diff --git a/tests/modules/socket/tcp/echo_client.lua b/tests/modules/socket/tcp/echo_client.lua index 55613cd10..bb2853c95 100644 --- a/tests/modules/socket/tcp/echo_client.lua +++ b/tests/modules/socket/tcp/echo_client.lua @@ -1,3 +1,4 @@ +import("core.base.bytes") import("core.base.socket") function main() @@ -8,10 +9,11 @@ function main() if sock then print("%s: connected!", sock) local count = 0 + local buff = bytes(8192) while count < 10000 do local send = sock:send("hello world..", {block = true}) if send > 0 then - sock:recv(13, {block = true}) + sock:recv(buff, 13, {block = true}) else break end diff --git a/tests/modules/socket/tcp/echo_server.lua b/tests/modules/socket/tcp/echo_server.lua index 162bc2bc0..048db0c37 100644 --- a/tests/modules/socket/tcp/echo_server.lua +++ b/tests/modules/socket/tcp/echo_server.lua @@ -1,3 +1,4 @@ +import("core.base.bytes") import("core.base.socket") function main() @@ -13,8 +14,9 @@ function main() print("%s: accepted", sock_client) local count = 0 local result = nil + local buff = bytes(8192) while true do - local recv, data = sock_client:recv(13, {block = true}) + local recv, data = sock_client:recv(buff, 13, {block = true}) if recv > 0 then result = data sock_client:send(data, {block = true}) @@ -24,7 +26,9 @@ function main() end end print("%s: recv: %d, count: %d", sock_client, result and result:size() or 0, count) - result:dump() + if result then + result:dump() + end sock_client:close() end end diff --git a/tests/modules/socket/tcp/file_client.lua b/tests/modules/socket/tcp/file_client.lua index 92d62976b..27dccfc7d 100644 --- a/tests/modules/socket/tcp/file_client.lua +++ b/tests/modules/socket/tcp/file_client.lua @@ -11,13 +11,12 @@ function main() local recv = 0 local data = nil local wait = false - local results = {} + local buff = bytes(8192) while true do - real, data = sock:recv(8192) + real, data = sock:recv(buff, 8192) if real > 0 then recv = recv + real wait = false - table.insert(results, data) elseif real == 0 and not wait then if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then wait = true @@ -28,9 +27,6 @@ function main() break end end - if #results > 0 then - data = bytes(results) - end - print("%s: recv ok, size: %d, #data: %d!", sock, recv, data and data:size() or 0) + print("%s: recv ok, size: %d!", sock, recv) sock:close() end diff --git a/tests/modules/socket/udp/echo_client.lua b/tests/modules/socket/udp/echo_client.lua index 0d67afeb5..3f20e958d 100644 --- a/tests/modules/socket/udp/echo_client.lua +++ b/tests/modules/socket/udp/echo_client.lua @@ -1,12 +1,14 @@ +import("core.base.bytes") import("core.base.socket") function main(data) local addr = "127.0.0.1" local port = 9091 + local buff = bytes(8192) local sock = socket.udp() local send = sock:sendto(data or "hello xmake!", addr, port, {block = true}) print("%s: send to %s:%d %d bytes!", sock, addr, port, send) - local recv, data, peer_addr, peer_port = sock:recvfrom(8112, {block = true}) + local recv, data, peer_addr, peer_port = sock:recvfrom(buff, 8112, {block = true}) if recv > 0 then print("%s: recv %d bytes from %s:%d", sock, recv, peer_addr, peer_port) data:dump() diff --git a/tests/modules/socket/udp/echo_server.lua b/tests/modules/socket/udp/echo_server.lua index 5b9b8b7d5..4aabb79de 100644 --- a/tests/modules/socket/udp/echo_server.lua +++ b/tests/modules/socket/udp/echo_server.lua @@ -1,13 +1,15 @@ +import("core.base.bytes") import("core.base.socket") function main() local addr = "127.0.0.1" local port = 9091 local sock = socket.udp() + local buff = bytes(8192) sock:bind(addr, port) while true do print("%s: recv in %s:%d ..", sock, addr, port) - local recv, data, peer_addr, peer_port = sock:recvfrom(8192, {block = true}) + local recv, data, peer_addr, peer_port = sock:recvfrom(buff, 8192, {block = true}) print("%s: recv %d bytes from: %s:%d", sock, recv, peer_addr, peer_port) if data then data:dump() diff --git a/tests/modules/socket/unix_tcp/echo_client.lua b/tests/modules/socket/unix_tcp/echo_client.lua index 27c88cb9f..177f2ef48 100644 --- a/tests/modules/socket/unix_tcp/echo_client.lua +++ b/tests/modules/socket/unix_tcp/echo_client.lua @@ -1,8 +1,10 @@ +import("core.base.bytes") import("core.base.socket") function main(addr) addr = addr or path.join(os.tmpdir(), "echo.socket") print("connect %s ..", addr) + local buff = bytes(8192) local sock = socket.connect_unix(addr) if sock then print("%s: connected!", sock) @@ -10,7 +12,7 @@ function main(addr) while count < 10000 do local send = sock:send("hello world..", {block = true}) if send > 0 then - sock:recv(13, {block = true}) + sock:recv(buff, 13, {block = true}) else break end diff --git a/tests/modules/socket/unix_tcp/echo_server.lua b/tests/modules/socket/unix_tcp/echo_server.lua index 8a1ea6c7d..0a9199273 100644 --- a/tests/modules/socket/unix_tcp/echo_server.lua +++ b/tests/modules/socket/unix_tcp/echo_server.lua @@ -1,3 +1,4 @@ +import("core.base.bytes") import("core.base.socket") function main(addr) @@ -13,8 +14,9 @@ function main(addr) print("%s: accepted", sock_client) local count = 0 local result = nil + local buff = bytes(8192) while true do - local recv, data = sock_client:recv(13, {block = true}) + local recv, data = sock_client:recv(buff, 13, {block = true}) if recv > 0 then result = data sock_client:send(data, {block = true}) @@ -24,7 +26,9 @@ function main(addr) end end print("%s: recv: %d, count: %d", sock_client, result and result:size() or 0, count) - result:dump() + if result then + result:dump() + end sock_client:close() end end diff --git a/tests/modules/socket/unix_tcp/file_client.lua b/tests/modules/socket/unix_tcp/file_client.lua index 3eadb211e..3ea2cd135 100644 --- a/tests/modules/socket/unix_tcp/file_client.lua +++ b/tests/modules/socket/unix_tcp/file_client.lua @@ -10,13 +10,12 @@ function main(addr) local recv = 0 local data = nil local wait = false - local results = {} + local buff = bytes(8192) while true do - real, data = sock:recv(8192) + real, data = sock:recv(buff, 8192) if real > 0 then recv = recv + real wait = false - table.insert(results, data) elseif real == 0 and not wait then if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then wait = true @@ -27,9 +26,6 @@ function main(addr) break end end - if #results > 0 then - data = bytes(results) - end - print("%s: recv ok, size: %d, #data: %d!", sock, recv, data and data:size() or 0) + print("%s: recv ok, size: %d!", sock, recv) sock:close() end diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 95a338ce7..4bec31569 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -382,7 +382,7 @@ function _instance:sendfile(file, opt) end -- recv data from socket -function _instance:recv(size, opt) +function _instance:recv(buff, size, opt) -- ensure opened local ok, errors = self:_ensure_opened() @@ -390,6 +390,11 @@ function _instance:recv(size, opt) return -1, errors end + -- check buffer + if not buff and buff:size() < size then + return -1, string.format("%s: too small buffer!", self) + end + -- check size if size == 0 then return 0 @@ -404,15 +409,11 @@ function _instance:recv(size, opt) local wait = false local data_or_errors = nil if opt.block then - local results = {} while recv < size do - local buff = self:_recvbuff() - real, data_or_errors = io.socket_recv(self:cdata(), buff:caddr(), math.min(buff:size(), size - recv)) + real, data_or_errors = io.socket_recv(self:cdata(), buff:caddr() + recv, math.min(buff:size() - recv, size - recv)) if real > 0 then recv = recv + real wait = false - table.insert(results, bytes(buff, 1, real)) - self:_recvbuff_clear() elseif real == 0 and not wait then local events, waiterrs = _instance.wait(self, socket.EV_RECV, opt.timeout or -1) if events == socket.EV_RECV then @@ -426,16 +427,14 @@ function _instance:recv(size, opt) end end if recv == size then - data_or_errors = bytes(results) + data_or_errors = bytes(buff, 1, recv) else recv = -1 end else - local buff = self:_recvbuff() recv, data_or_errors = io.socket_recv(self:cdata(), buff:caddr(), math.min(buff:size(), size)) if recv > 0 then data_or_errors = bytes(buff, 1, recv) - self:_recvbuff_clear() end end if recv < 0 and data_or_errors then @@ -498,7 +497,7 @@ function _instance:sendto(data, addr, port, opt) end -- recv udp data from peer -function _instance:recvfrom(size, opt) +function _instance:recvfrom(buff, size, opt) -- ensure opened local ok, errors = self:_ensure_opened() @@ -511,6 +510,11 @@ function _instance:recvfrom(size, opt) return -1, string.format("%s: sendto() only for udp socket!", self) end + -- check buffer + if not buff and buff:size() < size then + return -1, string.format("%s: too small buffer!", self) + end + -- check size if size == 0 then return 0 @@ -525,11 +529,9 @@ function _instance:recvfrom(size, opt) local data_or_errors = nil if opt.block then while true do - local buff = self:_recvbuff() recv, data_or_errors, addr, port = io.socket_recvfrom(self:cdata(), buff:caddr(), math.min(buff:size(), size)) if recv > 0 then data_or_errors = bytes(buff, 1, recv) - self:_recvbuff_clear() break elseif recv == 0 and not wait then local events, waiterrs = _instance.wait(self, socket.EV_RECV, opt.timeout or -1) @@ -545,11 +547,9 @@ function _instance:recvfrom(size, opt) end end else - local buff = self:_recvbuff() recv, data_or_errors, addr, port = io.socket_recvfrom(self:cdata(), buff:caddr(), math.min(buff:size(), size)) if recv > 0 then data_or_errors = bytes(buff, 1, recv) - self:_recvbuff_clear() end end if recv < 0 and data_or_errors then @@ -606,21 +606,6 @@ function _instance:close() return ok end --- get the recv buffer -function _instance:_recvbuff() - local recvbuff = self._RECVBUFF - if not recvbuff then - recvbuff = bytes(8192) - self._RECVBUFF = recvbuff - end - return recvbuff -end - --- clear the recv buffer -function _instance:_recvbuff_clear() - self._RECVBUFF = nil -end - -- ensure the socket is opened function _instance:_ensure_opened() if not self:cdata() then diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index f15f54c15..b86f3b9c5 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -137,8 +137,8 @@ function sandbox_core_base_socket_instance.sendfile(sock, file, opt) end -- recv data from socket -function sandbox_core_base_socket_instance.recv(sock, size, opt) - local real, data_or_errors = sock:_recv(size, opt) +function sandbox_core_base_socket_instance.recv(sock, buff, size, opt) + local real, data_or_errors = sock:_recv(buff, size, opt) if real < 0 and data_or_errors then raise(data_or_errors) end @@ -155,8 +155,8 @@ function sandbox_core_base_socket_instance.sendto(sock, data, addr, port, opt) end -- recv udp data from peer -function sandbox_core_base_socket_instance.recvfrom(sock, size, opt) - local real, data_or_errors, addr, port = sock:_recvfrom(size, opt) +function sandbox_core_base_socket_instance.recvfrom(sock, buff, size, opt) + local real, data_or_errors, addr, port = sock:_recvfrom(buff, size, opt) if real < 0 and data_or_errors then raise(data_or_errors) end diff --git a/xmake/modules/private/service/server/server.lua b/xmake/modules/private/service/server/server.lua index e5898c416..96adf49dd 100644 --- a/xmake/modules/private/service/server/server.lua +++ b/xmake/modules/private/service/server/server.lua @@ -20,6 +20,7 @@ -- imports import("core.base.object") +import("core.base.bytes") import("core.base.socket") import("core.base.scheduler") import("private.service.stream") @@ -123,9 +124,9 @@ function server:_handle_session(sock) local recv = 0 local data = nil local wait = false - local results = {} + local buff = bytes(8192) while true do - real, data = sock:recv(8192) + real, data = sock:recv(buff, 8192) if real > 0 then if data then end |
