mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
x.websocket: docs and cleanup (#7078)
This commit is contained in:
@ -2,11 +2,7 @@ module websocket
|
||||
|
||||
import net
|
||||
import time
|
||||
|
||||
interface WebsocketIO {
|
||||
socket_read(mut buffer []byte) ?int
|
||||
socket_write(bytes []byte) ?
|
||||
}
|
||||
import sync
|
||||
|
||||
// socket_read reads into the provided buffer with its length
|
||||
fn (mut ws Client) socket_read(mut buffer []byte) ?int {
|
||||
@ -15,7 +11,7 @@ fn (mut ws Client) socket_read(mut buffer []byte) ?int {
|
||||
return error('socket_read: trying to read a closed socket')
|
||||
}
|
||||
if ws.is_ssl {
|
||||
r := ws.ssl_conn.read_into(mut buffer)?
|
||||
r := ws.ssl_conn.read_into(mut buffer) ?
|
||||
return r
|
||||
} else {
|
||||
for {
|
||||
@ -31,14 +27,14 @@ fn (mut ws Client) socket_read(mut buffer []byte) ?int {
|
||||
}
|
||||
}
|
||||
|
||||
// socket_read reads into the provided byte pointer and length
|
||||
fn (mut ws Client) socket_read_ptr(buf_ptr byteptr, 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 {
|
||||
for {
|
||||
@ -62,7 +58,7 @@ fn (mut ws Client) socket_write(bytes []byte) ? {
|
||||
return error('socket_write: trying to write on a closed socket')
|
||||
}
|
||||
if ws.is_ssl {
|
||||
ws.ssl_conn.write(bytes)?
|
||||
ws.ssl_conn.write(bytes) ?
|
||||
} else {
|
||||
for {
|
||||
ws.conn.write(bytes) or {
|
||||
@ -77,26 +73,26 @@ fn (mut ws Client) socket_write(bytes []byte) ? {
|
||||
}
|
||||
}
|
||||
|
||||
// shutdown_socket, proper shutdown make PR in Emeliy repo
|
||||
// shutdown_socket, shut down socket properly closing the connection
|
||||
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() ?
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
// dial_socket, setup socket communication, options and timeouts
|
||||
fn (mut ws Client) dial_socket() ?net.TcpConn {
|
||||
mut t := net.dial_tcp('$ws.uri.hostname:$ws.uri.port')?
|
||||
mut t := net.dial_tcp('$ws.uri.hostname:$ws.uri.port') ?
|
||||
optval := int(1)
|
||||
t.sock.set_option_int(.keep_alive, optval)?
|
||||
t.sock.set_option_int(.keep_alive, optval) ?
|
||||
t.set_read_timeout(10 * time.millisecond)
|
||||
t.set_write_timeout(10 * time.millisecond)
|
||||
if ws.is_ssl {
|
||||
ws.ssl_conn.connect(mut t)?
|
||||
ws.ssl_conn.connect(mut t) ?
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
Reference in New Issue
Block a user