net: remove sockets if they already exists (#11264)
parent
2e28c9a4d6
commit
ed06c47a51
|
@ -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() ?
|
||||
}
|
||||
|
||||
|
|
|
@ -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) }
|
||||
|
|
Loading…
Reference in New Issue