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:
parent
5523c11dd4
commit
a50e0f0522
@ -1,7 +1,6 @@
|
|||||||
module websocket
|
module websocket
|
||||||
|
|
||||||
import net
|
import net
|
||||||
import time
|
|
||||||
|
|
||||||
// socket_read reads from socket into the provided buffer
|
// 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 {
|
||||||
@ -13,15 +12,8 @@ fn (mut ws Client) socket_read(mut buffer []u8) ?int {
|
|||||||
r := ws.ssl_conn.read(mut buffer)?
|
r := ws.ssl_conn.read(mut buffer)?
|
||||||
return r
|
return r
|
||||||
} else {
|
} else {
|
||||||
for {
|
r := ws.conn.read(mut buffer)?
|
||||||
r := ws.conn.read(mut buffer) or {
|
return r
|
||||||
if err.code() == net.err_timed_out_code {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return none
|
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)?
|
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)?
|
||||||
return r
|
return r
|
||||||
} else {
|
} else {
|
||||||
for {
|
r := ws.conn.read_ptr(buf_ptr, len)?
|
||||||
r := ws.conn.read_ptr(buf_ptr, len) or {
|
return r
|
||||||
if err.code() == net.err_timed_out_code {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return none
|
return none
|
||||||
@ -91,8 +76,8 @@ fn (mut ws Client) dial_socket() ?&net.TcpConn {
|
|||||||
mut t := net.dial_tcp(tcp_address)?
|
mut t := net.dial_tcp(tcp_address)?
|
||||||
optval := int(1)
|
optval := int(1)
|
||||||
t.sock.set_option_int(.keep_alive, optval)?
|
t.sock.set_option_int(.keep_alive, optval)?
|
||||||
t.set_read_timeout(30 * time.second)
|
t.set_read_timeout(ws.read_timeout)
|
||||||
t.set_write_timeout(30 * time.second)
|
t.set_write_timeout(ws.write_timeout)
|
||||||
if ws.is_ssl {
|
if ws.is_ssl {
|
||||||
ws.ssl_conn.connect(mut t, ws.uri.hostname)?
|
ws.ssl_conn.connect(mut t, ws.uri.hostname)?
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,11 @@ mut:
|
|||||||
open_callbacks []OpenEventHandler // all callbacks on_open
|
open_callbacks []OpenEventHandler // all callbacks on_open
|
||||||
close_callbacks []CloseEventHandler // all callbacks on_close
|
close_callbacks []CloseEventHandler // all callbacks on_close
|
||||||
pub:
|
pub:
|
||||||
is_ssl bool // true if secure socket is used
|
is_ssl bool // true if secure socket is used
|
||||||
uri Uri // uri of current connection
|
uri Uri // uri of current connection
|
||||||
id string // unique id of client
|
id string // unique id of client
|
||||||
|
read_timeout i64
|
||||||
|
write_timeout i64
|
||||||
pub mut:
|
pub mut:
|
||||||
header http.Header // headers that will be passed when connecting
|
header http.Header // headers that will be passed when connecting
|
||||||
conn &net.TcpConn // underlying TCP socket connection
|
conn &net.TcpConn // underlying TCP socket connection
|
||||||
@ -73,8 +75,14 @@ pub enum OPCode {
|
|||||||
pong = 0x0A
|
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
|
// 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)?
|
uri := parse_uri(address)?
|
||||||
return &Client{
|
return &Client{
|
||||||
conn: 0
|
conn: 0
|
||||||
@ -88,6 +96,8 @@ pub fn new_client(address string) ?&Client {
|
|||||||
state: .closed
|
state: .closed
|
||||||
id: rand.uuid_v4()
|
id: rand.uuid_v4()
|
||||||
header: http.new_header()
|
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.set_state(.connecting)
|
||||||
ws.logger.info('connecting to host $ws.uri')
|
ws.logger.info('connecting to host $ws.uri')
|
||||||
ws.conn = ws.dial_socket()?
|
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.handshake()?
|
||||||
ws.set_state(.open)
|
ws.set_state(.open)
|
||||||
ws.logger.info('successfully connected to host $ws.uri')
|
ws.logger.info('successfully connected to host $ws.uri')
|
||||||
|
Loading…
Reference in New Issue
Block a user