summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-28 22:40:27 +0800
committerruki <[email protected]>2019-10-28 10:02:46 +0800
commit8bfbf6b7fb255cda1bfd3d970c7442ed0941104c (patch)
tree356e02c9fece1f27909a64f6d270e0201ab270b5
parenta6e3140520f31c756cc8982d3ab20eff95b75160 (diff)
add sendfile demo
-rw-r--r--tests/modules/socket/tcp/file_client.lua30
-rw-r--r--tests/modules/socket/tcp/file_server.lua24
-rw-r--r--xmake/core/base/socket.lua1
3 files changed, 55 insertions, 0 deletions
diff --git a/tests/modules/socket/tcp/file_client.lua b/tests/modules/socket/tcp/file_client.lua
new file mode 100644
index 000000000..3d5a14651
--- /dev/null
+++ b/tests/modules/socket/tcp/file_client.lua
@@ -0,0 +1,30 @@
+import("core.base.socket")
+
+function main()
+ local addr = "127.0.0.1"
+ local port = 9090
+ print("connect %s:%d ..", addr, port)
+ local sock = socket.connect(addr, port)
+ print("%s: connected!", sock)
+ local real = 0
+ local recv = 0
+ local data = nil
+ local wait = false
+ while true do
+ real, data = sock:recv(8192, {prevdata = data})
+ if real > 0 then
+ recv = recv + real
+ wait = false
+ elseif real == 0 and not wait then
+ if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then
+ wait = true
+ else
+ break
+ end
+ else
+ break
+ end
+ end
+ print("%s: recv ok, size: %d!", sock, recv)
+ sock:close()
+end
diff --git a/tests/modules/socket/tcp/file_server.lua b/tests/modules/socket/tcp/file_server.lua
new file mode 100644
index 000000000..d619a496f
--- /dev/null
+++ b/tests/modules/socket/tcp/file_server.lua
@@ -0,0 +1,24 @@
+import("core.base.socket")
+
+function main(filepath)
+
+ local addr = "127.0.0.1"
+ local port = 9090
+ local sock = socket.bind(addr, port)
+ sock:listen(20)
+ print("%s: listening %s:%d ..", sock, addr, port)
+ while true do
+ local sock_client = sock:accept()
+ if sock_client then
+ print("%s: accepted", sock_client)
+ local file = io.open(filepath, 'rb')
+ if file then
+ local send = sock_client:sendfile(file, {block = true})
+ print("%s: send %s %d bytes!", sock_client, filepath, send)
+ file:close()
+ end
+ sock_client:close()
+ end
+ end
+ sock:close()
+end
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index 0dfae075b..47c358599 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -246,6 +246,7 @@ function _instance:sendfile(file, opt)
local errors = nil
if opt.block then
while start < last do
+ -- TODO pass file/handle, improve block send
real, errors = io.socket_sendfile(self._SOCK, file, start, last)
if real > 0 then
send = send + real