This commit is contained in:
ge
2026-04-12 16:30:28 +03:00
parent 5b56659d21
commit 2d2281b8cc
26 changed files with 1002 additions and 640 deletions
+20 -11
View File
@@ -13,7 +13,7 @@ fn C.accept(i32, voidptr, voidptr) i32
fn C.shutdown(i32, i32) i32
fn C.close(i32) i32
fn C.setsockopt(i32, i32, i32, voidptr, i32) i32
fn C.getsockopt(i32, i32, i32, voidptr, i32) i32
fn C.getsockopt(i32, i32, i32, voidptr, voidptr) i32
struct C.sockaddr_storage {}
@@ -25,8 +25,8 @@ pub:
// Socket.new creates new socket.
// See [socket(7)](https://www.man7.org/linux/man-pages/man7/socket.7.html) and
// [socket(3)](https://man7.org/linux/man-pages/man3/socket.3p.html) for details.
pub fn Socket.new(domain AddrFamily, typ SocketType, protocol Protocol) !Socket {
fd := C.socket(i32(domain), i32(typ), i32(protocol))
pub fn Socket.new(domain AddrFamily, st SocketType, proto Protocol) !Socket {
fd := C.socket(i32(domain), i32(st), i32(proto))
if fd == -1 {
return os.last_error()
}
@@ -42,9 +42,9 @@ pub fn (s Socket) type_of() !SocketType {
// Socket shutdown modes. See [shutdown(3p)](https://man7.org/linux/man-pages/man3/shutdown.3p.html) for details.
pub enum Shutdown {
read = C.SHUT_RD
write = C.SHUT_WR
read_and_write = C.SHUT_RDWR
read
write
read_and_write
}
// connect connects a socket.
@@ -86,16 +86,25 @@ pub fn (s Socket) accept() !(Socket, SocketAddr) {
return sock, sock_addr
}
// set_option sets the socket option.
pub fn (s Socket) set_option(level SocketLevel, option SocketOption, value int) ! {
if C.setsockopt(s.fd, i32(level), i32(option), i32(value), sizeof(value)) == -1 {
// 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.
pub fn (s Socket) set_option(level SocketLevel, option SocketOption, value voidptr) ! {
if C.setsockopt(s.fd, i32(level), i32(option), value, sizeof(value)) == -1 {
return os.last_error()
}
}
// get_option gets the socket option as int.
// 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.
pub fn (s Socket) get_option(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()
}
}
// 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 {
mut result := 0
mut result := -1
if C.getsockopt(s.fd, i32(level), i32(option), &result, sizeof(result)) == -1 {
return os.last_error()
}