diff options
| author | ruki <[email protected]> | 2019-12-20 00:53:14 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-19 23:19:27 +0800 |
| commit | 4ce400a33c26c4d59e76ccb5dc321fe6dabc60f7 (patch) | |
| tree | 7fba42df02bab3ab2702ad57c896002b22cb6518 /tests/modules | |
| parent | db52fd10877391ebc89be2c6551b94901a31e7b6 (diff) | |
add unix socket support
Diffstat (limited to 'tests/modules')
| -rw-r--r-- | tests/modules/socket/unix_tcp/echo_client.lua | 20 | ||||
| -rw-r--r-- | tests/modules/socket/unix_tcp/echo_server.lua | 31 | ||||
| -rw-r--r-- | tests/modules/socket/unix_tcp/file_client.lua | 34 | ||||
| -rw-r--r-- | tests/modules/socket/unix_tcp/file_server.lua | 22 |
4 files changed, 107 insertions, 0 deletions
diff --git a/tests/modules/socket/unix_tcp/echo_client.lua b/tests/modules/socket/unix_tcp/echo_client.lua new file mode 100644 index 000000000..53788d428 --- /dev/null +++ b/tests/modules/socket/unix_tcp/echo_client.lua @@ -0,0 +1,20 @@ +import("core.base.socket") + +function main(addr) + addr = addr or path.join(os.tmpdir(), "echo.socket") + print("connect %s ..", addr) + local sock = socket.connect_unix(addr) + print("%s: connected!", sock) + local count = 0 + while count < 10000 do + local send = sock:send("hello world..", {block = true}) + if send > 0 then + sock:recv(13, {block = true}) + else + break + end + count = count + 1 + end + print("%s: send ok, count: %d!", sock, count) + sock:close() +end diff --git a/tests/modules/socket/unix_tcp/echo_server.lua b/tests/modules/socket/unix_tcp/echo_server.lua new file mode 100644 index 000000000..7a2334095 --- /dev/null +++ b/tests/modules/socket/unix_tcp/echo_server.lua @@ -0,0 +1,31 @@ +import("core.base.socket") + +function main(addr) + + addr = addr or path.join(os.tmpdir(), "echo.socket") + local sock = socket.bind_unix(addr) + sock:listen(20) + print("%s: listening %s ..", sock, addr) + while true do + local sock_client = sock:accept() + if sock_client then + print("%s: accepted", sock_client) + local count = 0 + local result = nil + while true do + local recv, data = sock_client:recv(13, {block = true}) + if recv > 0 then + result = data + sock_client:send(data, {block = true}) + count = count + 1 + else + break + end + end + print("%s: recv: %d, count: %d", sock_client, result and result:size() or 0, count) + result:dump() + sock_client:close() + end + end + sock:close() +end diff --git a/tests/modules/socket/unix_tcp/file_client.lua b/tests/modules/socket/unix_tcp/file_client.lua new file mode 100644 index 000000000..3f870ae3f --- /dev/null +++ b/tests/modules/socket/unix_tcp/file_client.lua @@ -0,0 +1,34 @@ +import("core.base.socket") + +function main(addr) + addr = addr or path.join(os.tmpdir(), "file.socket") + print("connect %s ..", addr) + local sock = socket.connect_unix(addr) + print("%s: connected!", sock) + local real = 0 + local recv = 0 + local data = nil + local wait = false + local results = {} + while true do + real, data = sock:recv(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 + else + break + end + else + 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) + sock:close() +end diff --git a/tests/modules/socket/unix_tcp/file_server.lua b/tests/modules/socket/unix_tcp/file_server.lua new file mode 100644 index 000000000..b17d71104 --- /dev/null +++ b/tests/modules/socket/unix_tcp/file_server.lua @@ -0,0 +1,22 @@ +import("core.base.socket") + +function main(filepath, addr) + addr = addr or path.join(os.tmpdir(), "file.socket") + local sock = socket.bind_unix(addr) + sock:listen(20) + print("%s: listening %s ..", sock, addr) + 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 |
