2021-07-03 02:56:00 +03:00
|
|
|
module websocket
|
|
|
|
|
|
|
|
import net
|
|
|
|
|
|
|
|
// socket_read reads from socket into the provided buffer
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut ws Client) socket_read(mut buffer []u8) !int {
|
2022-10-19 12:18:21 +03:00
|
|
|
lock {
|
2021-07-03 02:56:00 +03:00
|
|
|
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 {
|
2022-10-16 09:28:57 +03:00
|
|
|
r := ws.ssl_conn.read(mut buffer) or { return error('none') }
|
2021-07-03 02:56:00 +03:00
|
|
|
return r
|
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
r := ws.conn.read(mut buffer) or { return error('none') }
|
2022-07-05 06:40:23 +03:00
|
|
|
return r
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// socket_read reads from socket into the provided byte pointer and length
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut ws Client) socket_read_ptr(buf_ptr &u8, len int) !int {
|
2022-10-19 12:18:21 +03:00
|
|
|
lock {
|
2021-07-03 02:56:00 +03:00
|
|
|
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 {
|
2022-10-16 09:28:57 +03:00
|
|
|
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)!
|
2021-07-03 02:56:00 +03:00
|
|
|
return r
|
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
r := ws.conn.read_ptr(buf_ptr, len)!
|
2022-07-05 06:40:23 +03:00
|
|
|
return r
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// socket_write writes the provided byte array to the socket
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut ws Client) socket_write(bytes []u8) !int {
|
2022-10-19 12:18:21 +03:00
|
|
|
lock {
|
2021-07-03 02:56:00 +03:00
|
|
|
if ws.state == .closed || ws.conn.sock.handle <= 1 {
|
2023-03-06 11:05:17 +03:00
|
|
|
ws.debug_log('socket_write: Socket already closed')
|
2021-07-03 02:56:00 +03:00
|
|
|
return error('socket_write: trying to write on a closed socket')
|
|
|
|
}
|
|
|
|
if ws.is_ssl {
|
|
|
|
return ws.ssl_conn.write(bytes)
|
|
|
|
} else {
|
|
|
|
for {
|
|
|
|
n := ws.conn.write(bytes) or {
|
2022-04-12 13:38:40 +03:00
|
|
|
if err.code() == net.err_timed_out_code {
|
2021-07-03 02:56:00 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
panic('reached unreachable code')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shutdown_socket shuts down the socket properly when connection is closed
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut ws Client) shutdown_socket() ! {
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.debug_log('shutting down socket')
|
|
|
|
if ws.is_ssl {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.ssl_conn.shutdown()!
|
2021-07-03 02:56:00 +03:00
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.conn.close()!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial_socket connects tcp socket and initializes default configurations
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut ws Client) dial_socket() !&net.TcpConn {
|
2022-11-15 16:53:13 +03:00
|
|
|
tcp_address := '${ws.uri.hostname}:${ws.uri.port}'
|
2022-10-16 09:28:57 +03:00
|
|
|
mut t := net.dial_tcp(tcp_address)!
|
2021-07-03 02:56:00 +03:00
|
|
|
optval := int(1)
|
2022-10-16 09:28:57 +03:00
|
|
|
t.sock.set_option_int(.keep_alive, optval)!
|
2022-07-05 06:40:23 +03:00
|
|
|
t.set_read_timeout(ws.read_timeout)
|
|
|
|
t.set_write_timeout(ws.write_timeout)
|
2021-07-03 02:56:00 +03:00
|
|
|
if ws.is_ssl {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.ssl_conn.connect(mut t, ws.uri.hostname)!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|