diff --git a/vlib/net/common.v b/vlib/net/common.v index 4ce01d43d2..5bc9247556 100644 --- a/vlib/net/common.v +++ b/vlib/net/common.v @@ -16,13 +16,36 @@ pub const no_timeout = time.Duration(0) // only ever return with data) pub const infinite_timeout = time.infinite -// Shutdown shutsdown a socket and closes it -fn shutdown(handle int) ! { +// ShutdownDirection is used by `net.shutdown`, for specifying the direction for which the +// communication will be cut. +pub enum ShutdownDirection { + read + write + read_and_write +} + +[params] +pub struct ShutdownConfig { + how ShutdownDirection = .read_and_write +} + +// shutdown shutsdown a socket, given its file descriptor `handle`. +// By default it shuts it down in both directions, both for reading +// and for writing. You can change that using `net.shutdown(handle, how: .read)` +// or `net.shutdown(handle, how: .write)` +pub fn shutdown(handle int, config ShutdownConfig) int { + $if windows { + return C.shutdown(handle, int(config.how)) + } $else { + return C.shutdown(handle, int(config.how)) + } +} + +// close a socket, given its file descriptor `handle`. +pub fn close(handle int) ! { $if windows { - C.shutdown(handle, C.SD_BOTH) socket_error(C.closesocket(handle))! } $else { - C.shutdown(handle, C.SHUT_RDWR) socket_error(C.close(handle))! } } diff --git a/vlib/net/mbedtls/ssl_connection.v b/vlib/net/mbedtls/ssl_connection.v index d7c357b6cc..963422d612 100644 --- a/vlib/net/mbedtls/ssl_connection.v +++ b/vlib/net/mbedtls/ssl_connection.v @@ -89,13 +89,8 @@ pub fn (mut s SSLConn) shutdown() ! { C.mbedtls_ssl_free(&s.ssl) C.mbedtls_ssl_config_free(&s.conf) if s.owns_socket { - $if windows { - C.shutdown(s.handle, C.SD_BOTH) - net.socket_error(C.closesocket(s.handle))! - } $else { - C.shutdown(s.handle, C.SHUT_RDWR) - net.socket_error(C.close(s.handle))! - } + net.shutdown(s.handle) + net.close(s.handle)! } } diff --git a/vlib/net/openssl/ssl_connection.v b/vlib/net/openssl/ssl_connection.v index 5b2ff6891a..7ca3c0f6bf 100644 --- a/vlib/net/openssl/ssl_connection.v +++ b/vlib/net/openssl/ssl_connection.v @@ -104,13 +104,8 @@ pub fn (mut s SSLConn) shutdown() ! { C.SSL_CTX_free(s.sslctx) } if s.owns_socket { - $if windows { - C.shutdown(s.handle, C.SD_BOTH) - net.socket_error(C.closesocket(s.handle))! - } $else { - C.shutdown(s.handle, C.SHUT_RDWR) - net.socket_error(C.close(s.handle))! - } + net.shutdown(s.handle) + net.close(s.handle)! } } diff --git a/vlib/net/tcp.v b/vlib/net/tcp.v index adb377832a..92404877ef 100644 --- a/vlib/net/tcp.v +++ b/vlib/net/tcp.v @@ -454,7 +454,8 @@ pub fn (mut s TcpSocket) bind(addr string) ! { } fn (mut s TcpSocket) close() ! { - return shutdown(s.handle) + shutdown(s.handle) + return close(s.handle) } fn (mut s TcpSocket) @select(test Select, timeout time.Duration) !bool { diff --git a/vlib/net/udp.v b/vlib/net/udp.v index 0d6abe80eb..e5c1d317f1 100644 --- a/vlib/net/udp.v +++ b/vlib/net/udp.v @@ -278,7 +278,8 @@ pub fn (mut s UdpSocket) set_dualstack(on bool) ! { } fn (mut s UdpSocket) close() ! { - return shutdown(s.handle) + shutdown(s.handle) + return close(s.handle) } fn (mut s UdpSocket) @select(test Select, timeout time.Duration) !bool { diff --git a/vlib/net/unix/common.v b/vlib/net/unix/common.v index 348519285b..ec9da8faf1 100644 --- a/vlib/net/unix/common.v +++ b/vlib/net/unix/common.v @@ -3,23 +3,23 @@ module unix import time import net -const ( - error_ewouldblock = C.EWOULDBLOCK -) +const error_ewouldblock = C.EWOULDBLOCK fn C.SUN_LEN(ptr &C.sockaddr_un) int fn C.strncpy(&char, &char, int) -// Shutdown shutsdown a socket and closes it -fn shutdown(handle int) ! { - $if windows { - C.shutdown(handle, C.SD_BOTH) - net.socket_error(C.closesocket(handle))! - } $else { - C.shutdown(handle, C.SHUT_RDWR) - net.socket_error(C.close(handle))! - } +// shutdown shutsdown a socket, given its file descriptor `handle`. +// By default it shuts it down in both directions, both for reading +// and for writing. You can change that using `net.shutdown(handle, how: .read)` +// or `net.shutdown(handle, how: .write)` +pub fn shutdown(handle int, config net.ShutdownConfig) int { + return net.shutdown(handle, config) +} + +// close a socket, given its file descriptor `handle`. +pub fn close(handle int) ! { + net.close(handle)! } // Select waits for an io operation (specified by parameter `test`) to be available diff --git a/vlib/net/unix/stream_nix.v b/vlib/net/unix/stream_nix.v index f924332d5c..caf29821ac 100644 --- a/vlib/net/unix/stream_nix.v +++ b/vlib/net/unix/stream_nix.v @@ -49,7 +49,8 @@ fn new_stream_socket() !StreamSocket { } fn (mut s StreamSocket) close() ! { - return shutdown(s.handle) + shutdown(s.handle) + return close(s.handle) } fn (mut s StreamSocket) @select(test Select, timeout time.Duration) !bool {