2022-02-18 12:23:45 +03:00
|
|
|
import os
|
|
|
|
import net.unix
|
|
|
|
import net
|
|
|
|
|
|
|
|
// ensure that `net` is used, i.e. no warnings
|
|
|
|
const use_net = net.no_timeout
|
|
|
|
|
2022-11-03 10:24:52 +03:00
|
|
|
const tfolder = os.join_path(os.vtmp_dir(), 'v', 'net_and_unix_together')
|
2022-09-16 04:56:19 +03:00
|
|
|
|
|
|
|
const test_port = os.join_path(tfolder, 'unix_domain_socket')
|
|
|
|
|
|
|
|
fn testsuite_begin() {
|
|
|
|
os.mkdir_all(tfolder) or {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
|
|
|
os.rmdir_all(tfolder) or {}
|
|
|
|
}
|
2022-02-18 12:23:45 +03:00
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn test_that_net_and_net_unix_can_be_imported_together_without_conflicts() {
|
2022-02-18 12:23:45 +03:00
|
|
|
mut l := unix.listen_stream(test_port) or { panic(err) }
|
|
|
|
go echo_server(mut l)
|
|
|
|
defer {
|
2022-02-20 13:29:08 +03:00
|
|
|
l.close() or {}
|
2022-02-18 12:23:45 +03:00
|
|
|
}
|
|
|
|
//
|
2022-10-16 09:28:57 +03:00
|
|
|
mut c := unix.connect_stream(test_port)!
|
2022-02-18 12:23:45 +03:00
|
|
|
defer {
|
2022-02-20 13:29:08 +03:00
|
|
|
c.close() or {}
|
2022-02-18 12:23:45 +03:00
|
|
|
}
|
|
|
|
//
|
|
|
|
data := 'Hello from vlib/net!'
|
2022-10-16 09:28:57 +03:00
|
|
|
c.write_string(data)!
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 100}
|
2022-10-16 09:28:57 +03:00
|
|
|
assert c.read(mut buf)! == data.len
|
2022-02-18 12:23:45 +03:00
|
|
|
eprintln('< client read back buf: |${buf[0..data.len].bytestr()}|')
|
|
|
|
assert buf[0..data.len] == data.bytes()
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn perror(s string) ! {
|
2022-02-18 12:23:45 +03:00
|
|
|
println(s)
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn handle_conn(mut c unix.StreamConn) ! {
|
2022-02-18 12:23:45 +03:00
|
|
|
for {
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 100, init: 0}
|
2022-02-18 12:23:45 +03:00
|
|
|
read := c.read(mut buf) or { return perror('Server: connection dropped') }
|
|
|
|
eprintln('> server read ${read:3}, buf: |$buf.bytestr()|')
|
|
|
|
c.write(buf[..read]) or { return perror('Server: connection dropped') }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn echo_server(mut l unix.StreamListener) ! {
|
2022-02-18 12:23:45 +03:00
|
|
|
for {
|
|
|
|
mut new_conn := l.accept() or { continue }
|
|
|
|
handle_conn(mut new_conn) or {}
|
|
|
|
}
|
|
|
|
}
|