1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net: remove sockets if they already exists (#11264)

This commit is contained in:
Simon 2021-08-22 12:38:02 +02:00 committed by GitHub
parent 2e28c9a4d6
commit ed06c47a51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 9 deletions

View File

@ -49,7 +49,6 @@ fn new_stream_socket() ?StreamSocket {
}
fn (mut s StreamSocket) close() ? {
os.rm(s.path) ?
return shutdown(s.handle)
}
@ -98,7 +97,11 @@ pub fn listen_stream(sock string) ?&StreamListener {
addr.sun_family = C.AF_UNIX
unsafe { C.strncpy(&addr.sun_path[0], &char(sock.str), max_sun_path) }
size := C.SUN_LEN(&addr)
if os.exists(sock) {
os.rm(sock) ?
}
net.socket_error(C.bind(s.handle, voidptr(&addr), size)) ?
os.chmod(sock, 0o777)
net.socket_error(C.listen(s.handle, 128)) ?
return &StreamListener{
sock: s
@ -158,6 +161,7 @@ pub fn (mut c StreamListener) wait_for_accept() ? {
}
pub fn (mut c StreamListener) close() ? {
os.rm(c.sock.path) ?
c.sock.close() ?
}

View File

@ -3,14 +3,6 @@ import net.unix
const test_port = os.join_path(os.temp_dir(), 'unix_domain_socket')
fn testsuite_begin() {
os.rm(test_port) or {}
}
fn testsuite_end() {
os.rm(test_port) or {}
}
fn handle_conn(mut c unix.StreamConn) {
for {
mut buf := []byte{len: 100, init: 0}
@ -50,6 +42,7 @@ fn echo() ? {
}
fn test_tcp() {
assert os.exists(test_port) == false
mut l := unix.listen_stream(test_port) or { panic(err) }
go echo_server(mut l)
echo() or { panic(err) }