net: extract a common Socket struct, reuse it by embedding in TcpSocket & UdpSocket (#13559)

pull/13563/head
gcxfd 2022-02-22 16:34:38 +08:00 committed by GitHub
parent 7bd8503170
commit ee1de06678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 14 deletions

11
vlib/net/socket.v 100644
View File

@ -0,0 +1,11 @@
module net
pub struct Socket {
pub:
handle int
}
// address gets the address of a socket
pub fn (s &Socket) address() ?Addr {
return addr_from_socket_handle(s.handle)
}

View File

@ -296,8 +296,7 @@ pub fn (c &TcpListener) addr() ?Addr {
} }
struct TcpSocket { struct TcpSocket {
pub: Socket
handle int
} }
fn new_tcp_socket(family AddrFamily) ?TcpSocket { fn new_tcp_socket(family AddrFamily) ?TcpSocket {
@ -418,8 +417,3 @@ fn (mut s TcpSocket) connect(a Addr) ? {
// otherwise we timed out // otherwise we timed out
return err_connect_timed_out return err_connect_timed_out
} }
// address gets the address of a socket
pub fn (s &TcpSocket) address() ?Addr {
return addr_from_socket_handle(s.handle)
}

View File

@ -8,7 +8,7 @@ const (
) )
struct UdpSocket { struct UdpSocket {
handle int Socket
l Addr l Addr
// TODO(emily): replace with option again // TODO(emily): replace with option again
// when i figure out how to coerce it properly // when i figure out how to coerce it properly
@ -259,11 +259,6 @@ fn new_udp_socket_for_remote(raddr Addr) ?&UdpSocket {
return sock return sock
} }
// address gets the address of a socket
pub fn (s &UdpSocket) address() ?Addr {
return addr_from_socket_handle(s.handle)
}
pub fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ? { pub fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ? {
// TODO reenable when this `in` operation works again // TODO reenable when this `in` operation works again
// if opt !in opts_can_set { // if opt !in opts_can_set {