mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: change optional to result of io (#16075)
This commit is contained in:
120
vlib/net/tcp.v
120
vlib/net/tcp.v
@@ -21,7 +21,7 @@ mut:
|
||||
is_blocking bool
|
||||
}
|
||||
|
||||
pub fn dial_tcp(address string) ?&TcpConn {
|
||||
pub fn dial_tcp(address string) !&TcpConn {
|
||||
addrs := resolve_addrs_fuzzy(address, .tcp) or {
|
||||
return error('$err.msg(); could not resolve address $address in dial_tcp')
|
||||
}
|
||||
@@ -64,7 +64,7 @@ pub fn dial_tcp(address string) ?&TcpConn {
|
||||
}
|
||||
|
||||
// bind local address and dial.
|
||||
pub fn dial_tcp_with_bind(saddr string, laddr string) ?&TcpConn {
|
||||
pub fn dial_tcp_with_bind(saddr string, laddr string) !&TcpConn {
|
||||
addrs := resolve_addrs_fuzzy(saddr, .tcp) or {
|
||||
return error('$err.msg(); could not resolve address $saddr in dial_tcp_with_bind')
|
||||
}
|
||||
@@ -94,15 +94,15 @@ pub fn dial_tcp_with_bind(saddr string, laddr string) ?&TcpConn {
|
||||
return error('dial_tcp_with_bind failed for address $saddr')
|
||||
}
|
||||
|
||||
pub fn (mut c TcpConn) close() ? {
|
||||
pub fn (mut c TcpConn) close() ! {
|
||||
$if trace_tcp ? {
|
||||
eprintln(' TcpConn.close | c.sock.handle: ${c.sock.handle:6}')
|
||||
}
|
||||
c.sock.close()?
|
||||
c.sock.close()!
|
||||
}
|
||||
|
||||
pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) ?int {
|
||||
mut res := wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0))?
|
||||
pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) !int {
|
||||
mut res := wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0))!
|
||||
$if trace_tcp ? {
|
||||
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
||||
}
|
||||
@@ -115,8 +115,8 @@ pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) ?int {
|
||||
}
|
||||
code := error_code()
|
||||
if code == int(error_ewouldblock) {
|
||||
c.wait_for_read()?
|
||||
res = wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0))?
|
||||
c.wait_for_read()!
|
||||
res = wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0))!
|
||||
$if trace_tcp ? {
|
||||
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
||||
}
|
||||
@@ -128,9 +128,9 @@ pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) ?int {
|
||||
}
|
||||
return socket_error(res)
|
||||
} else {
|
||||
wrap_error(code)?
|
||||
wrap_error(code)!
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
pub fn (c TcpConn) read(mut buf []u8) !int {
|
||||
@@ -142,15 +142,15 @@ pub fn (c TcpConn) read(mut buf []u8) !int {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut c TcpConn) read_deadline() ?time.Time {
|
||||
pub fn (mut c TcpConn) read_deadline() !time.Time {
|
||||
if c.read_deadline.unix == 0 {
|
||||
return c.read_deadline
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
// write_ptr blocks and attempts to write all data
|
||||
pub fn (mut c TcpConn) write_ptr(b &u8, len int) ?int {
|
||||
pub fn (mut c TcpConn) write_ptr(b &u8, len int) !int {
|
||||
$if trace_tcp ? {
|
||||
eprintln(
|
||||
'>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
|
||||
@@ -173,10 +173,10 @@ pub fn (mut c TcpConn) write_ptr(b &u8, len int) ?int {
|
||||
if sent < 0 {
|
||||
code := error_code()
|
||||
if code == int(error_ewouldblock) {
|
||||
c.wait_for_write()?
|
||||
c.wait_for_write()!
|
||||
continue
|
||||
} else {
|
||||
wrap_error(code)?
|
||||
wrap_error(code)!
|
||||
}
|
||||
}
|
||||
total_sent += sent
|
||||
@@ -186,12 +186,12 @@ pub fn (mut c TcpConn) write_ptr(b &u8, len int) ?int {
|
||||
}
|
||||
|
||||
// write blocks and attempts to write all data
|
||||
pub fn (mut c TcpConn) write(bytes []u8) ?int {
|
||||
pub fn (mut c TcpConn) write(bytes []u8) !int {
|
||||
return c.write_ptr(bytes.data, bytes.len)
|
||||
}
|
||||
|
||||
// write_string blocks and attempts to write all data
|
||||
pub fn (mut c TcpConn) write_string(s string) ?int {
|
||||
pub fn (mut c TcpConn) write_string(s string) !int {
|
||||
return c.write_ptr(s.str, s.len)
|
||||
}
|
||||
|
||||
@@ -199,11 +199,11 @@ pub fn (mut c TcpConn) set_read_deadline(deadline time.Time) {
|
||||
c.read_deadline = deadline
|
||||
}
|
||||
|
||||
pub fn (mut c TcpConn) write_deadline() ?time.Time {
|
||||
pub fn (mut c TcpConn) write_deadline() !time.Time {
|
||||
if c.write_deadline.unix == 0 {
|
||||
return c.write_deadline
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
pub fn (mut c TcpConn) set_write_deadline(deadline time.Time) {
|
||||
@@ -227,31 +227,31 @@ pub fn (mut c TcpConn) set_write_timeout(t time.Duration) {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (c TcpConn) wait_for_read() ? {
|
||||
pub fn (c TcpConn) wait_for_read() ! {
|
||||
return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (mut c TcpConn) wait_for_write() ? {
|
||||
pub fn (mut c TcpConn) wait_for_write() ! {
|
||||
return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
|
||||
}
|
||||
|
||||
pub fn (c &TcpConn) peer_addr() ?Addr {
|
||||
pub fn (c &TcpConn) peer_addr() !Addr {
|
||||
mut addr := Addr{
|
||||
addr: AddrData{
|
||||
Ip6: Ip6{}
|
||||
}
|
||||
}
|
||||
mut size := sizeof(Addr)
|
||||
socket_error_message(C.getpeername(c.sock.handle, voidptr(&addr), &size), 'peer_addr failed')?
|
||||
socket_error_message(C.getpeername(c.sock.handle, voidptr(&addr), &size), 'peer_addr failed')!
|
||||
return addr
|
||||
}
|
||||
|
||||
pub fn (c &TcpConn) peer_ip() ?string {
|
||||
return c.peer_addr()?.str()
|
||||
pub fn (c &TcpConn) peer_ip() !string {
|
||||
return c.peer_addr()!.str()
|
||||
}
|
||||
|
||||
pub fn (c &TcpConn) addr() ?Addr {
|
||||
pub fn (c &TcpConn) addr() !Addr {
|
||||
return c.sock.address()
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ mut:
|
||||
accept_deadline time.Time
|
||||
}
|
||||
|
||||
pub fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener {
|
||||
pub fn listen_tcp(family AddrFamily, saddr string) !&TcpListener {
|
||||
s := new_tcp_socket(family) or { return error('$err.msg(); could not create new socket') }
|
||||
|
||||
addrs := resolve_addrs(saddr, family, .tcp) or {
|
||||
@@ -280,8 +280,8 @@ pub fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener {
|
||||
|
||||
// cast to the correct type
|
||||
alen := addr.len()
|
||||
socket_error_message(C.bind(s.handle, voidptr(&addr), alen), 'binding to $saddr failed')?
|
||||
socket_error_message(C.listen(s.handle, 128), 'listening on $saddr failed')?
|
||||
socket_error_message(C.bind(s.handle, voidptr(&addr), alen), 'binding to $saddr failed')!
|
||||
socket_error_message(C.listen(s.handle, 128), 'listening on $saddr failed')!
|
||||
return &TcpListener{
|
||||
sock: s
|
||||
accept_deadline: no_deadline
|
||||
@@ -289,19 +289,19 @@ pub fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut l TcpListener) accept() ?&TcpConn {
|
||||
pub fn (mut l TcpListener) accept() !&TcpConn {
|
||||
$if trace_tcp ? {
|
||||
eprintln(' TcpListener.accept | l.sock.handle: ${l.sock.handle:6}')
|
||||
}
|
||||
mut new_handle := C.accept(l.sock.handle, 0, 0)
|
||||
if new_handle <= 0 {
|
||||
l.wait_for_accept()?
|
||||
l.wait_for_accept()!
|
||||
new_handle = C.accept(l.sock.handle, 0, 0)
|
||||
if new_handle == -1 || new_handle == 0 {
|
||||
return error('accept failed')
|
||||
}
|
||||
}
|
||||
new_sock := tcp_socket_from_handle(new_handle)?
|
||||
new_sock := tcp_socket_from_handle(new_handle)!
|
||||
$if trace_tcp ? {
|
||||
eprintln(' TcpListener.accept | << new_sock.handle: ${new_sock.handle:6}')
|
||||
}
|
||||
@@ -312,7 +312,7 @@ pub fn (mut l TcpListener) accept() ?&TcpConn {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (c &TcpListener) accept_deadline() ?time.Time {
|
||||
pub fn (c &TcpListener) accept_deadline() !time.Time {
|
||||
if c.accept_deadline.unix != 0 {
|
||||
return c.accept_deadline
|
||||
}
|
||||
@@ -331,15 +331,15 @@ pub fn (mut c TcpListener) set_accept_timeout(t time.Duration) {
|
||||
c.accept_timeout = t
|
||||
}
|
||||
|
||||
pub fn (mut c TcpListener) wait_for_accept() ? {
|
||||
pub fn (mut c TcpListener) wait_for_accept() ! {
|
||||
return wait_for_read(c.sock.handle, c.accept_deadline, c.accept_timeout)
|
||||
}
|
||||
|
||||
pub fn (mut c TcpListener) close() ? {
|
||||
c.sock.close()?
|
||||
pub fn (mut c TcpListener) close() ! {
|
||||
c.sock.close()!
|
||||
}
|
||||
|
||||
pub fn (c &TcpListener) addr() ?Addr {
|
||||
pub fn (c &TcpListener) addr() !Addr {
|
||||
return c.sock.address()
|
||||
}
|
||||
|
||||
@@ -347,8 +347,8 @@ struct TcpSocket {
|
||||
Socket
|
||||
}
|
||||
|
||||
fn new_tcp_socket(family AddrFamily) ?TcpSocket {
|
||||
handle := socket_error(C.socket(family, SocketType.tcp, 0))?
|
||||
fn new_tcp_socket(family AddrFamily) !TcpSocket {
|
||||
handle := socket_error(C.socket(family, SocketType.tcp, 0))!
|
||||
mut s := TcpSocket{
|
||||
handle: handle
|
||||
}
|
||||
@@ -362,20 +362,20 @@ fn new_tcp_socket(family AddrFamily) ?TcpSocket {
|
||||
|
||||
// TODO(emily):
|
||||
// Move this to its own function on the socket
|
||||
s.set_option_int(.reuse_addr, 1)?
|
||||
s.set_option_int(.reuse_addr, 1)!
|
||||
|
||||
$if !net_blocking_sockets ? {
|
||||
$if windows {
|
||||
t := u32(1) // true
|
||||
socket_error(C.ioctlsocket(handle, fionbio, &t))?
|
||||
socket_error(C.ioctlsocket(handle, fionbio, &t))!
|
||||
} $else {
|
||||
socket_error(C.fcntl(handle, C.F_SETFL, C.fcntl(handle, C.F_GETFL) | C.O_NONBLOCK))?
|
||||
socket_error(C.fcntl(handle, C.F_SETFL, C.fcntl(handle, C.F_GETFL) | C.O_NONBLOCK))!
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
|
||||
fn tcp_socket_from_handle(sockfd int) !TcpSocket {
|
||||
mut s := TcpSocket{
|
||||
handle: sockfd
|
||||
}
|
||||
@@ -383,22 +383,22 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
|
||||
eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}')
|
||||
}
|
||||
// s.set_option_bool(.reuse_addr, true)?
|
||||
s.set_option_int(.reuse_addr, 1)?
|
||||
s.set_option_int(.reuse_addr, 1)!
|
||||
s.set_dualstack(true) or {
|
||||
// Not ipv6, we dont care
|
||||
}
|
||||
$if !net_blocking_sockets ? {
|
||||
$if windows {
|
||||
t := u32(1) // true
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t))?
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t))!
|
||||
} $else {
|
||||
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK))?
|
||||
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK))!
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
|
||||
pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ! {
|
||||
// TODO reenable when this `in` operation works again
|
||||
// if opt !in opts_can_set {
|
||||
// return err_option_not_settable
|
||||
@@ -407,21 +407,21 @@ pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
|
||||
// return err_option_wrong_type
|
||||
// }
|
||||
x := int(value)
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &x, sizeof(int)))?
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &x, sizeof(int)))!
|
||||
}
|
||||
|
||||
pub fn (mut s TcpSocket) set_dualstack(on bool) ? {
|
||||
pub fn (mut s TcpSocket) set_dualstack(on bool) ! {
|
||||
x := int(!on)
|
||||
socket_error(C.setsockopt(s.handle, C.IPPROTO_IPV6, int(SocketOption.ipv6_only), &x,
|
||||
sizeof(int)))?
|
||||
sizeof(int)))!
|
||||
}
|
||||
|
||||
pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ? {
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int)))?
|
||||
pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ! {
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int)))!
|
||||
}
|
||||
|
||||
// bind a local rddress for TcpSocket
|
||||
pub fn (mut s TcpSocket) bind(addr string) ? {
|
||||
pub fn (mut s TcpSocket) bind(addr string) ! {
|
||||
addrs := resolve_addrs(addr, AddrFamily.ip, .tcp) or {
|
||||
return error('$err.msg(); could not resolve address $addr')
|
||||
}
|
||||
@@ -436,11 +436,11 @@ pub fn (mut s TcpSocket) bind(addr string) ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut s TcpSocket) close() ? {
|
||||
fn (mut s TcpSocket) close() ! {
|
||||
return shutdown(s.handle)
|
||||
}
|
||||
|
||||
fn (mut s TcpSocket) @select(test Select, timeout time.Duration) ?bool {
|
||||
fn (mut s TcpSocket) @select(test Select, timeout time.Duration) !bool {
|
||||
return @select(s.handle, test, timeout)
|
||||
}
|
||||
|
||||
@@ -448,7 +448,7 @@ const (
|
||||
connect_timeout = 5 * time.second
|
||||
)
|
||||
|
||||
fn (mut s TcpSocket) connect(a Addr) ? {
|
||||
fn (mut s TcpSocket) connect(a Addr) ! {
|
||||
$if !net_blocking_sockets ? {
|
||||
res := C.connect(s.handle, voidptr(&a), a.len())
|
||||
if res == 0 {
|
||||
@@ -467,7 +467,7 @@ fn (mut s TcpSocket) connect(a Addr) ? {
|
||||
// determine whether connect() completed successfully (SO_ERROR is zero) or
|
||||
// unsuccessfully (SO_ERROR is one of the usual error codes listed here,
|
||||
// ex‐ plaining the reason for the failure).
|
||||
write_result := s.@select(.write, net.connect_timeout)?
|
||||
write_result := s.@select(.write, net.connect_timeout)!
|
||||
err := 0
|
||||
len := sizeof(err)
|
||||
xyz := C.getsockopt(s.handle, C.SOL_SOCKET, C.SO_ERROR, &err, &len)
|
||||
@@ -476,17 +476,17 @@ fn (mut s TcpSocket) connect(a Addr) ? {
|
||||
}
|
||||
if write_result {
|
||||
if xyz == 0 {
|
||||
wrap_error(err)?
|
||||
wrap_error(err)!
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
return err_timed_out
|
||||
}
|
||||
wrap_error(ecode)?
|
||||
wrap_error(ecode)!
|
||||
return
|
||||
} $else {
|
||||
x := C.connect(s.handle, voidptr(&a), a.len())
|
||||
socket_error(x)?
|
||||
socket_error(x)!
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user