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:
@ -40,23 +40,23 @@ fn error_code() int {
|
||||
return C.errno
|
||||
}
|
||||
|
||||
fn new_stream_socket() ?StreamSocket {
|
||||
sockfd := net.socket_error(C.socket(net.AddrFamily.unix, net.SocketType.tcp, 0))?
|
||||
fn new_stream_socket() !StreamSocket {
|
||||
sockfd := net.socket_error(C.socket(net.AddrFamily.unix, net.SocketType.tcp, 0))!
|
||||
mut s := StreamSocket{
|
||||
handle: sockfd
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
fn (mut s StreamSocket) close() ? {
|
||||
fn (mut s StreamSocket) close() ! {
|
||||
return shutdown(s.handle)
|
||||
}
|
||||
|
||||
fn (mut s StreamSocket) @select(test Select, timeout time.Duration) ?bool {
|
||||
fn (mut s StreamSocket) @select(test Select, timeout time.Duration) !bool {
|
||||
return @select(s.handle, test, timeout)
|
||||
}
|
||||
|
||||
fn (mut s StreamSocket) connect(a string) ? {
|
||||
fn (mut s StreamSocket) connect(a string) ! {
|
||||
if a.len >= max_sun_path {
|
||||
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
|
||||
}
|
||||
@ -73,12 +73,12 @@ fn (mut s StreamSocket) connect(a string) ? {
|
||||
return
|
||||
}
|
||||
_ := error_code()
|
||||
write_result := s.@select(.write, unix.connect_timeout)?
|
||||
write_result := s.@select(.write, unix.connect_timeout)!
|
||||
if write_result {
|
||||
// succeeded
|
||||
return
|
||||
}
|
||||
except_result := s.@select(.except, unix.connect_timeout)?
|
||||
except_result := s.@select(.except, unix.connect_timeout)!
|
||||
if except_result {
|
||||
return net.err_connect_failed
|
||||
}
|
||||
@ -86,11 +86,11 @@ fn (mut s StreamSocket) connect(a string) ? {
|
||||
return net.err_connect_timed_out
|
||||
}
|
||||
|
||||
pub fn listen_stream(sock string) ?&StreamListener {
|
||||
pub fn listen_stream(sock string) !&StreamListener {
|
||||
if sock.len >= max_sun_path {
|
||||
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
|
||||
}
|
||||
mut s := new_stream_socket()?
|
||||
mut s := new_stream_socket()!
|
||||
s.path = sock
|
||||
mut addr := C.sockaddr_un{}
|
||||
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
|
||||
@ -98,19 +98,19 @@ pub fn listen_stream(sock string) ?&StreamListener {
|
||||
unsafe { C.strncpy(&addr.sun_path[0], &char(sock.str), max_sun_path) }
|
||||
size := C.SUN_LEN(&addr)
|
||||
if os.exists(sock) {
|
||||
os.rm(sock)?
|
||||
os.rm(sock)!
|
||||
}
|
||||
net.socket_error(C.bind(s.handle, voidptr(&addr), size))?
|
||||
os.chmod(sock, 0o777)?
|
||||
net.socket_error(C.listen(s.handle, 128))?
|
||||
net.socket_error(C.bind(s.handle, voidptr(&addr), size))!
|
||||
os.chmod(sock, 0o777)!
|
||||
net.socket_error(C.listen(s.handle, 128))!
|
||||
return &StreamListener{
|
||||
sock: s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn connect_stream(path string) ?&StreamConn {
|
||||
mut s := new_stream_socket()?
|
||||
s.connect(path)?
|
||||
pub fn connect_stream(path string) !&StreamConn {
|
||||
mut s := new_stream_socket()!
|
||||
s.connect(path)!
|
||||
return &StreamConn{
|
||||
sock: s
|
||||
read_timeout: unix.unix_default_read_timeout
|
||||
@ -118,10 +118,10 @@ pub fn connect_stream(path string) ?&StreamConn {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut l StreamListener) accept() ?&StreamConn {
|
||||
pub fn (mut l StreamListener) accept() !&StreamConn {
|
||||
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')
|
||||
@ -137,7 +137,7 @@ pub fn (mut l StreamListener) accept() ?&StreamConn {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (c &StreamListener) accept_deadline() ?time.Time {
|
||||
pub fn (c &StreamListener) accept_deadline() !time.Time {
|
||||
if c.accept_deadline.unix != 0 {
|
||||
return c.accept_deadline
|
||||
}
|
||||
@ -156,21 +156,21 @@ pub fn (mut c StreamListener) set_accept_timeout(t time.Duration) {
|
||||
c.accept_timeout = t
|
||||
}
|
||||
|
||||
pub fn (mut c StreamListener) wait_for_accept() ? {
|
||||
pub fn (mut c StreamListener) wait_for_accept() ! {
|
||||
return wait_for_read(c.sock.handle, c.accept_deadline, c.accept_timeout)
|
||||
}
|
||||
|
||||
pub fn (mut c StreamListener) close() ? {
|
||||
os.rm(c.sock.path)?
|
||||
c.sock.close()?
|
||||
pub fn (mut c StreamListener) close() ! {
|
||||
os.rm(c.sock.path)!
|
||||
c.sock.close()!
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) close() ? {
|
||||
c.sock.close()?
|
||||
pub fn (mut c StreamConn) close() ! {
|
||||
c.sock.close()!
|
||||
}
|
||||
|
||||
// write_ptr blocks and attempts to write all data
|
||||
pub fn (mut c StreamConn) write_ptr(b &u8, len int) ?int {
|
||||
pub fn (mut c StreamConn) write_ptr(b &u8, len int) !int {
|
||||
$if trace_unix ? {
|
||||
eprintln(
|
||||
'>>> StreamConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
|
||||
@ -186,10 +186,10 @@ pub fn (mut c StreamConn) 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 {
|
||||
net.wrap_error(code)?
|
||||
net.wrap_error(code)!
|
||||
}
|
||||
}
|
||||
total_sent += sent
|
||||
@ -199,17 +199,17 @@ pub fn (mut c StreamConn) write_ptr(b &u8, len int) ?int {
|
||||
}
|
||||
|
||||
// write blocks and attempts to write all data
|
||||
pub fn (mut c StreamConn) write(bytes []u8) ?int {
|
||||
pub fn (mut c StreamConn) 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 StreamConn) write_string(s string) ?int {
|
||||
pub fn (mut c StreamConn) write_string(s string) !int {
|
||||
return c.write_ptr(s.str, s.len)
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) 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 (mut c StreamConn) 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_unix ? {
|
||||
eprintln('<<< StreamConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
||||
}
|
||||
@ -218,38 +218,38 @@ pub fn (mut c StreamConn) 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_unix ? {
|
||||
eprintln('<<< StreamConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
||||
}
|
||||
return net.socket_error(res)
|
||||
} else {
|
||||
net.wrap_error(code)?
|
||||
net.wrap_error(code)!
|
||||
}
|
||||
return net.socket_error(code)
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) read(mut buf []u8) ?int {
|
||||
pub fn (mut c StreamConn) read(mut buf []u8) !int {
|
||||
return c.read_ptr(buf.data, buf.len)
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) read_deadline() ?time.Time {
|
||||
pub fn (mut c StreamConn) read_deadline() !time.Time {
|
||||
if c.read_deadline.unix == 0 {
|
||||
return c.read_deadline
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) set_read_deadline(deadline time.Time) {
|
||||
c.read_deadline = deadline
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) write_deadline() ?time.Time {
|
||||
pub fn (mut c StreamConn) write_deadline() !time.Time {
|
||||
if c.write_deadline.unix == 0 {
|
||||
return c.write_deadline
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
pub fn (mut c StreamConn) set_write_deadline(deadline time.Time) {
|
||||
@ -273,12 +273,12 @@ pub fn (mut c StreamConn) set_write_timeout(t time.Duration) {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (mut c StreamConn) wait_for_read() ? {
|
||||
pub fn (mut c StreamConn) wait_for_read() ! {
|
||||
return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (mut c StreamConn) wait_for_write() ? {
|
||||
pub fn (mut c StreamConn) wait_for_write() ! {
|
||||
return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user