Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: add more SocketOptions enum values, make public fn new_tcp_socket #22378

Merged
merged 8 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions vlib/net/socket_options.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub enum SocketOption {
send_timeout = C.SO_SNDTIMEO
socket_type = C.SO_TYPE
ipv6_only = C.IPV6_V6ONLY
ip_proto_ipv6 = C.IPPROTO_IPV6
reuse_port = C.SO_REUSEPORT
// tcp_fastopen = C.TCP_FASTOPEN // TODO make it work in windows
// tcp_quickack = C.TCP_QUICKACK // TODO make it work in os != linux
// tcp_defer_accept = C.TCP_DEFER_ACCEPT // TODO make it work in windows
}

pub const opts_bool = [SocketOption.broadcast, .debug, .dont_route, .error, .keep_alive, .oob_inline]
Expand Down
7 changes: 4 additions & 3 deletions vlib/net/tcp.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import strings

pub const tcp_default_read_timeout = 30 * time.second
pub const tcp_default_write_timeout = 30 * time.second
pub const int_size = sizeof(int)

// TCPDialer is a concrete instance of the Dialer interface,
// for creating tcp connections.
Expand Down Expand Up @@ -355,7 +356,7 @@ pub:
}

pub fn listen_tcp(family AddrFamily, saddr string, options ListenOptions) !&TcpListener {
if family != .ip && family != .ip6 {
if family !in [.ip, .ip6] {
return error('listen_tcp only supports ip and ip6')
}
mut s := new_tcp_socket(family) or { return error('${err.msg()}; could not create new socket') }
Expand Down Expand Up @@ -515,7 +516,7 @@ struct TcpSocket {
// This is a workaround for issue https://github.com/vlang/v/issues/20858
// `noline` ensure that in `-prod` mode(CFLAG = `-O3 -flto`), gcc does not generate wrong instruction sequence
@[noinline]
fn new_tcp_socket(family AddrFamily) !TcpSocket {
pub fn new_tcp_socket(family AddrFamily) !TcpSocket {
handle := $if is_coroutine ? {
socket_error(C.photon_socket(family, SocketType.tcp, 0))!
} $else {
Expand Down Expand Up @@ -571,7 +572,7 @@ pub fn tcp_socket_from_handle_raw(sockfd int) TcpSocket {
}

fn (mut s TcpSocket) set_option(level int, opt int, value int) ! {
socket_error(C.setsockopt(s.handle, level, opt, &value, sizeof(int)))!
socket_error(C.setsockopt(s.handle, level, opt, &value, int_size))!
}

pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ! {
Expand Down
Loading