This commit is contained in:
ge
2026-05-01 20:58:09 +03:00
parent ee6aaa1e68
commit e3d183d234
2 changed files with 49 additions and 15 deletions
+25 -15
View File
@@ -39,7 +39,8 @@ pub fn Socket.new(domain AddrFamily, st SocketType, proto Protocol) !Socket {
// type reports the actual socket type. // type reports the actual socket type.
pub fn (s Socket) type() !SocketType { pub fn (s Socket) type() !SocketType {
return s.get_option_int(C.SOL_SOCKET, C.SO_TYPE)! // return s.get_option_int(sol_socket, so_type)!
return s.get_option[SocketType](sol_socket, so_type)!
} }
// Socket shutdown modes. See [shutdown(3p)](https://man7.org/linux/man-pages/man3/shutdown.3p.html) for details. // Socket shutdown modes. See [shutdown(3p)](https://man7.org/linux/man-pages/man3/shutdown.3p.html) for details.
@@ -88,28 +89,37 @@ pub fn (s Socket) accept() !(Socket, SocketAddr) {
return sock, sock_addr return sock, sock_addr
} }
fn (s Socket) set_option_raw(level SocketLevel, option SocketOption, value voidptr) ! {
if C.setsockopt(s.fd, i32(level), i32(option), value, sizeof(value)) == -1 {
return os.last_error()
}
}
// set_option sets the socket option. See [socket(7)](https://man7.org/linux/man-pages/man7/socket.7.html) // set_option sets the socket option. See [socket(7)](https://man7.org/linux/man-pages/man7/socket.7.html)
// and [setsockopt(3p)](https://man7.org/linux/man-pages/man3/setsockopt.3p.html) for details. // and [setsockopt(3p)](https://man7.org/linux/man-pages/man3/setsockopt.3p.html) for details.
pub fn (s Socket) set_option(level SocketLevel, option SocketOption, value voidptr) ! { pub fn (s Socket) set_option[T](level SocketLevel, option SocketOption, value T) ! {
if C.setsockopt(s.fd, i32(level), i32(option), value, sizeof(value)) == -1 { s.set_option_raw(level, option, &value)!
}
fn (s Socket) get_option_raw(level SocketLevel, option SocketOption, mut value voidptr, mut size voidptr) ! {
if C.getsockopt(s.fd, i32(level), i32(option), value, size) == -1 {
return os.last_error() return os.last_error()
} }
} }
// get_option gets the socket option. See [socket(7)](https://man7.org/linux/man-pages/man7/socket.7.html) // get_option gets the socket option. See [socket(7)](https://man7.org/linux/man-pages/man7/socket.7.html)
// and [getsockopt(3p)](https://man7.org/linux/man-pages/man3/getsockopt.3p.html) for details. // and [getsockopt(3p)](https://man7.org/linux/man-pages/man3/getsockopt.3p.html) for details.
pub fn (s Socket) get_option(level SocketLevel, option SocketOption, mut value voidptr, mut size voidptr) ! { // Example:
if C.getsockopt(s.fd, i32(level), i32(option), value, size) == -1 { // ```v
return os.last_error() // import netio
} // mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
} // socket.set_option(netio.sol_socket, netio.so_reuseaddr, true)!
// assert socket.get_option[bool](netio.sol_socket, netio.so_reuseaddr)! == true
// get_option gets the socket option as int. The same as `get_option`, but always returns int. // ```
pub fn (s Socket) get_option_int(level SocketLevel, option SocketOption) !int { pub fn (s Socket) get_option[T](level SocketLevel, option SocketOption) !T {
mut result := -1 mut result := T{}
if C.getsockopt(s.fd, i32(level), i32(option), &result, sizeof(result)) == -1 { mut size := sizeof(result)
return os.last_error() s.get_option_raw(level, option, mut &result, mut &size)!
}
return result return result
} }
+24
View File
@@ -0,0 +1,24 @@
import netio
fn test_socket_new() {
socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
socket.close() or { panic(err) }
assert socket.fd != -1
}
fn test_socket_type() {
socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
socket_type := socket.type()!
socket.close() or { panic(err) }
assert socket_type == netio.sock_stream
}
fn test_socket_option() {
mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
socket.set_option(netio.sol_socket, netio.so_reuseaddr, true)!
opt_val_int := socket.get_option[int](netio.sol_socket, netio.so_reuseaddr)!
opt_val_bool := socket.get_option[bool](netio.sol_socket, netio.so_reuseaddr)!
socket.close() or { panic(err) }
assert opt_val_int == 1
assert opt_val_bool == true
}