2020-08-22 01:50:38 +03:00
|
|
|
module websocket
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
import net
|
2020-08-22 01:50:38 +03:00
|
|
|
import time
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// socket_read reads from socket into the provided buffer
|
2020-11-15 23:54:47 +03:00
|
|
|
fn (mut ws Client) socket_read(mut buffer []byte) ?int {
|
2020-08-22 01:50:38 +03:00
|
|
|
lock {
|
2020-11-21 16:45:45 +03:00
|
|
|
if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
|
|
|
|
return error('socket_read: trying to read a closed socket')
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
if ws.is_ssl {
|
2020-12-02 06:02:53 +03:00
|
|
|
r := ws.ssl_conn.read_into(mut buffer) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return r
|
|
|
|
} else {
|
|
|
|
for {
|
2020-11-15 23:54:47 +03:00
|
|
|
r := ws.conn.read(mut buffer) or {
|
2020-08-22 01:50:38 +03:00
|
|
|
if errcode == net.err_timed_out_code {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// socket_read reads from socket into the provided byte pointer and length
|
2020-11-15 23:54:47 +03:00
|
|
|
fn (mut ws Client) socket_read_ptr(buf_ptr byteptr, len int) ?int {
|
2020-08-22 01:50:38 +03:00
|
|
|
lock {
|
2020-11-21 16:45:45 +03:00
|
|
|
if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
|
|
|
|
return error('socket_read_ptr: trying to read a closed socket')
|
2020-12-02 06:02:53 +03:00
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
if ws.is_ssl {
|
2020-12-02 06:02:53 +03:00
|
|
|
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return r
|
|
|
|
} else {
|
|
|
|
for {
|
2020-11-15 23:54:47 +03:00
|
|
|
r := ws.conn.read_ptr(buf_ptr, len) or {
|
2020-08-22 01:50:38 +03:00
|
|
|
if errcode == net.err_timed_out_code {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// socket_write writes the provided byte array to the socket
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) socket_write(bytes []byte) ? {
|
|
|
|
lock {
|
|
|
|
if ws.state == .closed || ws.conn.sock.handle <= 1 {
|
2020-11-21 16:45:45 +03:00
|
|
|
ws.debug_log('socket_write: Socket allready closed')
|
|
|
|
return error('socket_write: trying to write on a closed socket')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
if ws.is_ssl {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.ssl_conn.write(bytes) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
} else {
|
|
|
|
for {
|
|
|
|
ws.conn.write(bytes) or {
|
|
|
|
if errcode == net.err_timed_out_code {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// shutdown_socket shuts down the socket properly when connection is closed
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) shutdown_socket() ? {
|
|
|
|
ws.debug_log('shutting down socket')
|
|
|
|
if ws.is_ssl {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.ssl_conn.shutdown() ?
|
2020-08-22 01:50:38 +03:00
|
|
|
} else {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.conn.close() ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// dial_socket connects tcp socket and initializes default configurations
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) dial_socket() ?net.TcpConn {
|
2020-12-11 03:04:12 +03:00
|
|
|
tcp_address := '$ws.uri.hostname:$ws.uri.port'
|
|
|
|
mut t := net.dial_tcp(tcp_address) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
optval := int(1)
|
2020-12-02 06:02:53 +03:00
|
|
|
t.sock.set_option_int(.keep_alive, optval) ?
|
2020-12-11 03:04:12 +03:00
|
|
|
t.set_read_timeout(30 * time.second)
|
|
|
|
t.set_write_timeout(30 * time.second)
|
2020-08-22 01:50:38 +03:00
|
|
|
if ws.is_ssl {
|
2020-12-11 03:04:12 +03:00
|
|
|
ws.ssl_conn.connect(mut t, ws.uri.hostname) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|