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:
@ -3,41 +3,41 @@ module websocket
|
||||
import net
|
||||
|
||||
// socket_read reads from socket into the provided buffer
|
||||
fn (mut ws Client) socket_read(mut buffer []u8) ?int {
|
||||
fn (mut ws Client) socket_read(mut buffer []u8) !int {
|
||||
lock {
|
||||
if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
|
||||
return error('socket_read: trying to read a closed socket')
|
||||
}
|
||||
if ws.is_ssl {
|
||||
r := ws.ssl_conn.read(mut buffer) or { return none }
|
||||
r := ws.ssl_conn.read(mut buffer) or { return error('none') }
|
||||
return r
|
||||
} else {
|
||||
r := ws.conn.read(mut buffer) or { return none }
|
||||
r := ws.conn.read(mut buffer) or { return error('none') }
|
||||
return r
|
||||
}
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
// socket_read reads from socket into the provided byte pointer and length
|
||||
fn (mut ws Client) socket_read_ptr(buf_ptr &u8, len int) ?int {
|
||||
fn (mut ws Client) socket_read_ptr(buf_ptr &u8, len int) !int {
|
||||
lock {
|
||||
if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
|
||||
return error('socket_read_ptr: trying to read a closed socket')
|
||||
}
|
||||
if ws.is_ssl {
|
||||
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)?
|
||||
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)!
|
||||
return r
|
||||
} else {
|
||||
r := ws.conn.read_ptr(buf_ptr, len)?
|
||||
r := ws.conn.read_ptr(buf_ptr, len)!
|
||||
return r
|
||||
}
|
||||
}
|
||||
return none
|
||||
return error('none')
|
||||
}
|
||||
|
||||
// socket_write writes the provided byte array to the socket
|
||||
fn (mut ws Client) socket_write(bytes []u8) ?int {
|
||||
fn (mut ws Client) socket_write(bytes []u8) !int {
|
||||
lock {
|
||||
if ws.state == .closed || ws.conn.sock.handle <= 1 {
|
||||
ws.debug_log('socket_write: Socket allready closed')
|
||||
@ -61,25 +61,25 @@ fn (mut ws Client) socket_write(bytes []u8) ?int {
|
||||
}
|
||||
|
||||
// shutdown_socket shuts down the socket properly when connection is closed
|
||||
fn (mut ws Client) shutdown_socket() ? {
|
||||
fn (mut ws Client) shutdown_socket() ! {
|
||||
ws.debug_log('shutting down socket')
|
||||
if ws.is_ssl {
|
||||
ws.ssl_conn.shutdown()?
|
||||
ws.ssl_conn.shutdown()!
|
||||
} else {
|
||||
ws.conn.close()?
|
||||
ws.conn.close()!
|
||||
}
|
||||
}
|
||||
|
||||
// dial_socket connects tcp socket and initializes default configurations
|
||||
fn (mut ws Client) dial_socket() ?&net.TcpConn {
|
||||
fn (mut ws Client) dial_socket() !&net.TcpConn {
|
||||
tcp_address := '$ws.uri.hostname:$ws.uri.port'
|
||||
mut t := net.dial_tcp(tcp_address)?
|
||||
mut t := net.dial_tcp(tcp_address)!
|
||||
optval := int(1)
|
||||
t.sock.set_option_int(.keep_alive, optval)?
|
||||
t.sock.set_option_int(.keep_alive, optval)!
|
||||
t.set_read_timeout(ws.read_timeout)
|
||||
t.set_write_timeout(ws.write_timeout)
|
||||
if ws.is_ssl {
|
||||
ws.ssl_conn.connect(mut t, ws.uri.hostname)?
|
||||
ws.ssl_conn.connect(mut t, ws.uri.hostname)!
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
Reference in New Issue
Block a user