diff options
| author | ruki <[email protected]> | 2019-12-14 00:46:15 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-13 22:37:35 +0800 |
| commit | 26ea5a3e4a0a474366e879b529d6f0a2c918ef2d (patch) | |
| tree | bb1964bc50281bf09f83c1eab44827fe01756a65 /tests/modules/socket/sched_tcp/file_server.lua | |
| parent | 4f171e9e2b1eb119b43f08db23c9e6f271f9021d (diff) | |
add file_server and file_client tests for scheduler/socket
Diffstat (limited to 'tests/modules/socket/sched_tcp/file_server.lua')
| -rw-r--r-- | tests/modules/socket/sched_tcp/file_server.lua | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/modules/socket/sched_tcp/file_server.lua b/tests/modules/socket/sched_tcp/file_server.lua new file mode 100644 index 000000000..904b3db8e --- /dev/null +++ b/tests/modules/socket/sched_tcp/file_server.lua @@ -0,0 +1,33 @@ +import("core.base.socket") +import("core.base.scheduler") + +function _session(sock, filepath) + + local file = io.open(filepath, 'rb') + if file then + local send = sock:sendfile(file, {block = true}) + print("%s: send %s %d bytes!", sock, filepath, send) + file:close() + end + sock:close() +end + +function _listen(addr, port, filepath) + + 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) + scheduler.co_start(_session, sock_client, filepath) + end + end + sock:close() +end + +function main(filepath) + scheduler.co_start(_listen, "127.0.0.1", 9090, filepath) + scheduler.runloop() +end |
