1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.websocket: allow timeout to be configured (#14941)

This commit is contained in:
Ken 2022-07-05 12:40:23 +09:00 committed by GitHub
parent 5523c11dd4
commit a50e0f0522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 28 deletions

View File

@ -1,7 +1,6 @@
module websocket
import net
import time
// socket_read reads from socket into the provided buffer
fn (mut ws Client) socket_read(mut buffer []u8) ?int {
@ -13,15 +12,8 @@ fn (mut ws Client) socket_read(mut buffer []u8) ?int {
r := ws.ssl_conn.read(mut buffer)?
return r
} else {
for {
r := ws.conn.read(mut buffer) or {
if err.code() == net.err_timed_out_code {
continue
}
return err
}
return r
}
r := ws.conn.read(mut buffer)?
return r
}
}
return none
@ -37,15 +29,8 @@ fn (mut ws Client) socket_read_ptr(buf_ptr &u8, len int) ?int {
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)?
return r
} else {
for {
r := ws.conn.read_ptr(buf_ptr, len) or {
if err.code() == net.err_timed_out_code {
continue
}
return err
}
return r
}
r := ws.conn.read_ptr(buf_ptr, len)?
return r
}
}
return none
@ -91,8 +76,8 @@ fn (mut ws Client) dial_socket() ?&net.TcpConn {
mut t := net.dial_tcp(tcp_address)?
optval := int(1)
t.sock.set_option_int(.keep_alive, optval)?
t.set_read_timeout(30 * time.second)
t.set_write_timeout(30 * time.second)
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)?
}

View File

@ -27,9 +27,11 @@ mut:
open_callbacks []OpenEventHandler // all callbacks on_open
close_callbacks []CloseEventHandler // all callbacks on_close
pub:
is_ssl bool // true if secure socket is used
uri Uri // uri of current connection
id string // unique id of client
is_ssl bool // true if secure socket is used
uri Uri // uri of current connection
id string // unique id of client
read_timeout i64
write_timeout i64
pub mut:
header http.Header // headers that will be passed when connecting
conn &net.TcpConn // underlying TCP socket connection
@ -73,8 +75,14 @@ pub enum OPCode {
pong = 0x0A
}
[params]
pub struct ClientOpt {
read_timeout i64 = 30 * time.second
write_timeout i64 = 30 * time.second
}
// new_client instance a new websocket client
pub fn new_client(address string) ?&Client {
pub fn new_client(address string, opt ClientOpt) ?&Client {
uri := parse_uri(address)?
return &Client{
conn: 0
@ -88,6 +96,8 @@ pub fn new_client(address string) ?&Client {
state: .closed
id: rand.uuid_v4()
header: http.new_header()
read_timeout: opt.read_timeout
write_timeout: opt.write_timeout
}
}
@ -97,9 +107,6 @@ pub fn (mut ws Client) connect() ? {
ws.set_state(.connecting)
ws.logger.info('connecting to host $ws.uri')
ws.conn = ws.dial_socket()?
// Todo: make setting configurable
ws.conn.set_read_timeout(time.second * 30)
ws.conn.set_write_timeout(time.second * 30)
ws.handshake()?
ws.set_state(.open)
ws.logger.info('successfully connected to host $ws.uri')