upd
This commit is contained in:
+13
-5
@@ -24,7 +24,7 @@ pub:
|
||||
fd int = -1
|
||||
}
|
||||
|
||||
// Socket.new creates new socket.
|
||||
// Socket.new creates the 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, st SocketType, proto Protocol) !Socket {
|
||||
@@ -37,7 +37,7 @@ pub fn Socket.new(domain AddrFamily, st SocketType, proto Protocol) !Socket {
|
||||
}
|
||||
}
|
||||
|
||||
// type reports the actual socket type.
|
||||
// type reports the actual socket type. Look for `sock_*` constants.
|
||||
pub fn (s Socket) type() !SocketType {
|
||||
return s.get_option[SocketType](sol_socket, so_type)!
|
||||
}
|
||||
@@ -49,14 +49,14 @@ pub enum Shutdown {
|
||||
read_and_write
|
||||
}
|
||||
|
||||
// connect connects a socket.
|
||||
// connect connects a socket. See [connect(3p)](https://man7.org/linux/man-pages/man3/connect.3p.html) for details.
|
||||
pub fn (s Socket) connect(addr SocketAddr) ! {
|
||||
if C.connect(s.fd, addr.ptr(), addr.size()) == -1 {
|
||||
return os.last_error()
|
||||
}
|
||||
}
|
||||
|
||||
// bind binds a name to a socket.
|
||||
// bind binds a name to a socket. See [bind(3p)](https://man7.org/linux/man-pages/man3/bind.3p.html) for details.
|
||||
pub fn (s Socket) bind(addr SocketAddr) ! {
|
||||
if C.bind(s.fd, addr.ptr(), addr.size()) == -1 {
|
||||
return os.last_error()
|
||||
@@ -64,6 +64,7 @@ pub fn (s Socket) bind(addr SocketAddr) ! {
|
||||
}
|
||||
|
||||
// listen starts listening for socket connections and limit the queue of incoming connections.
|
||||
// See [listen(3p)](https://man7.org/linux/man-pages/man3/listen.3p.html) for details.
|
||||
pub fn (s Socket) listen(backlog int) ! {
|
||||
if C.listen(s.fd, backlog) == -1 {
|
||||
return os.last_error()
|
||||
@@ -72,6 +73,7 @@ pub fn (s Socket) listen(backlog int) ! {
|
||||
|
||||
// accept accepts a new connection on a socket.
|
||||
// The return values are the new socket connected to remote and the remote socket address.
|
||||
// See [accept(3p)](https://man7.org/linux/man-pages/man3/accept.3p.html) for details.
|
||||
pub fn (s Socket) accept() !(Socket, SocketAddr) {
|
||||
mut sock_addr_storage := &C.sockaddr_storage{}
|
||||
mut sock_addr_len := sizeof(C.sockaddr_storage)
|
||||
@@ -113,7 +115,7 @@ fn (s Socket) get_option_raw(level SocketLevel, option SocketOption, mut value v
|
||||
// import netio
|
||||
// mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
|
||||
// socket.set_option(netio.sol_socket, netio.so_reuseaddr, 1)!
|
||||
// assert socket.get_option[int](netio.sol_socket, netio.so_reuseaddr)! == true
|
||||
// assert socket.get_option[int](netio.sol_socket, netio.so_reuseaddr)! == 1
|
||||
// ```
|
||||
pub fn (s Socket) get_option[T](level SocketLevel, option SocketOption) !T {
|
||||
mut result := T{}
|
||||
@@ -123,6 +125,7 @@ pub fn (s Socket) get_option[T](level SocketLevel, option SocketOption) !T {
|
||||
}
|
||||
|
||||
// receive a message from a connected socket.
|
||||
// See [recv(3p)](https://man7.org/linux/man-pages/man3/recv.3p.html) for details.
|
||||
pub fn (s Socket) recv(mut buf []u8, flags MsgFlag) !int {
|
||||
r := C.recv(s.fd, buf.data, buf.len, flags)
|
||||
if r == -1 {
|
||||
@@ -132,6 +135,7 @@ pub fn (s Socket) recv(mut buf []u8, flags MsgFlag) !int {
|
||||
}
|
||||
|
||||
// receive a message from a connected socket.
|
||||
// See [recvfrom(3p)](https://man7.org/linux/man-pages/man3/recvfrom.3p.html) for details.
|
||||
pub fn (s Socket) recv_from(mut buf []u8, flags MsgFlag) !(int, SocketAddr) {
|
||||
mut sock_addr_storage := &C.sockaddr_storage{}
|
||||
mut sock_addr_len := sizeof(C.sockaddr_storage)
|
||||
@@ -143,6 +147,7 @@ pub fn (s Socket) recv_from(mut buf []u8, flags MsgFlag) !(int, SocketAddr) {
|
||||
}
|
||||
|
||||
// send a message on socket.
|
||||
// See [send(3p)](https://man7.org/linux/man-pages/man3/send.3p.html) for details.
|
||||
pub fn (s Socket) send(buf []u8, flags MsgFlag) !int {
|
||||
r := C.send(s.fd, buf.data, buf.len, flags)
|
||||
if r == -1 {
|
||||
@@ -152,6 +157,7 @@ pub fn (s Socket) send(buf []u8, flags MsgFlag) !int {
|
||||
}
|
||||
|
||||
// send a message on socket using the dst socket address as destination instead of the socket peer address.
|
||||
// See [sendto(3p)](https://man7.org/linux/man-pages/man3/sendto.3p.html) for details.
|
||||
pub fn (s Socket) send_to(buf []u8, dst SocketAddr, flags MsgFlag) !int {
|
||||
r := C.sendto(s.fd, buf.data, buf.len, flags, dst.ptr(), dst.size())
|
||||
if r == -1 {
|
||||
@@ -161,6 +167,7 @@ pub fn (s Socket) send_to(buf []u8, dst SocketAddr, flags MsgFlag) !int {
|
||||
}
|
||||
|
||||
// shutdown shut downs socket send and receive operations.
|
||||
// See [shutodwn(3p)](https://man7.org/linux/man-pages/man3/shutdown.3p.html) for details.
|
||||
pub fn (s Socket) shutdown(how Shutdown) ! {
|
||||
if C.shutdown(s.fd, i32(how)) == -1 {
|
||||
return os.last_error()
|
||||
@@ -168,6 +175,7 @@ pub fn (s Socket) shutdown(how Shutdown) ! {
|
||||
}
|
||||
|
||||
// close closes the socket.
|
||||
// See [close(3p)](https://man7.org/linux/man-pages/man3/close.3p.html) for details.
|
||||
pub fn (s Socket) close() ! {
|
||||
if C.close(s.fd) == -1 {
|
||||
return os.last_error()
|
||||
|
||||
Reference in New Issue
Block a user