2021-07-03 02:56:00 +03:00
|
|
|
// websocket module implements websocket client and a websocket server
|
|
|
|
// attribution: @thecoderr the author of original websocket client
|
|
|
|
[manualfree]
|
|
|
|
module websocket
|
|
|
|
|
|
|
|
import net
|
|
|
|
import net.http
|
2022-09-22 16:50:34 +03:00
|
|
|
import net.ssl
|
2021-07-03 02:56:00 +03:00
|
|
|
import net.urllib
|
|
|
|
import time
|
|
|
|
import log
|
|
|
|
import rand
|
|
|
|
|
|
|
|
const (
|
2022-04-15 15:35:35 +03:00
|
|
|
empty_bytearr = []u8{} // used as empty response to avoid allocation
|
2021-07-03 02:56:00 +03:00
|
|
|
)
|
|
|
|
|
2023-05-18 12:27:00 +03:00
|
|
|
pub struct ClientState {
|
2023-05-24 19:30:37 +03:00
|
|
|
pub mut:
|
2023-05-18 12:27:00 +03:00
|
|
|
state State = .closed // current state of connection
|
|
|
|
}
|
|
|
|
|
2021-07-03 02:56:00 +03:00
|
|
|
// Client represents websocket client
|
|
|
|
pub struct Client {
|
|
|
|
is_server bool
|
|
|
|
mut:
|
2022-09-22 16:50:34 +03:00
|
|
|
ssl_conn &ssl.SSLConn = unsafe { nil } // secure connection used when wss is used
|
|
|
|
flags []Flag // flags used in handshake
|
|
|
|
fragments []Fragment // current fragments
|
2021-07-03 02:56:00 +03:00
|
|
|
message_callbacks []MessageEventHandler // all callbacks on_message
|
|
|
|
error_callbacks []ErrorEventHandler // all callbacks on_error
|
|
|
|
open_callbacks []OpenEventHandler // all callbacks on_open
|
|
|
|
close_callbacks []CloseEventHandler // all callbacks on_close
|
|
|
|
pub:
|
2022-07-05 06:40:23 +03:00
|
|
|
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
|
2021-07-03 02:56:00 +03:00
|
|
|
pub mut:
|
|
|
|
header http.Header // headers that will be passed when connecting
|
2022-09-15 07:59:31 +03:00
|
|
|
conn &net.TcpConn = unsafe { nil } // underlying TCP socket connection
|
2021-07-03 02:56:00 +03:00
|
|
|
nonce_size int = 16 // size of nounce used for masking
|
2023-05-18 12:27:00 +03:00
|
|
|
panic_on_callback bool // set to true of callbacks can panic
|
|
|
|
client_state shared ClientState // current state of connection
|
2022-09-22 08:00:03 +03:00
|
|
|
// logger used to log messages
|
|
|
|
logger &log.Logger = &log.Logger(&log.Log{
|
|
|
|
level: .info
|
|
|
|
})
|
|
|
|
resource_name string // name of current resource
|
|
|
|
last_pong_ut i64 // last time in unix time we got a pong message
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flag represents different types of headers in websocket handshake
|
|
|
|
enum Flag {
|
|
|
|
has_accept // Webs
|
|
|
|
has_connection
|
|
|
|
has_upgrade
|
|
|
|
}
|
|
|
|
|
|
|
|
// State represents the state of the websocket connection.
|
|
|
|
pub enum State {
|
|
|
|
connecting = 0
|
|
|
|
open
|
|
|
|
closing
|
|
|
|
closed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message represents a whole message combined from 1 to n frames
|
|
|
|
pub struct Message {
|
|
|
|
pub:
|
|
|
|
opcode OPCode // websocket frame type of this message
|
2022-04-15 18:25:45 +03:00
|
|
|
payload []u8 // payload of the message
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// OPCode represents the supported websocket frame types
|
|
|
|
pub enum OPCode {
|
|
|
|
continuation = 0x00
|
|
|
|
text_frame = 0x01
|
|
|
|
binary_frame = 0x02
|
|
|
|
close = 0x08
|
|
|
|
ping = 0x09
|
|
|
|
pong = 0x0A
|
|
|
|
}
|
|
|
|
|
2022-07-05 06:40:23 +03:00
|
|
|
[params]
|
|
|
|
pub struct ClientOpt {
|
|
|
|
read_timeout i64 = 30 * time.second
|
|
|
|
write_timeout i64 = 30 * time.second
|
2022-07-09 17:39:07 +03:00
|
|
|
logger &log.Logger = &log.Logger(&log.Log{
|
|
|
|
level: .info
|
|
|
|
})
|
2022-07-05 06:40:23 +03:00
|
|
|
}
|
|
|
|
|
2021-07-03 02:56:00 +03:00
|
|
|
// new_client instance a new websocket client
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn new_client(address string, opt ClientOpt) !&Client {
|
|
|
|
uri := parse_uri(address)!
|
2021-07-03 02:56:00 +03:00
|
|
|
return &Client{
|
|
|
|
conn: 0
|
|
|
|
is_server: false
|
2022-10-16 09:28:57 +03:00
|
|
|
ssl_conn: ssl.new_ssl_conn()!
|
2021-07-03 02:56:00 +03:00
|
|
|
is_ssl: address.starts_with('wss')
|
2022-07-09 17:39:07 +03:00
|
|
|
logger: opt.logger
|
2021-07-03 02:56:00 +03:00
|
|
|
uri: uri
|
2023-05-18 12:27:00 +03:00
|
|
|
client_state: ClientState{
|
|
|
|
state: .closed
|
|
|
|
}
|
2021-07-03 02:56:00 +03:00
|
|
|
id: rand.uuid_v4()
|
|
|
|
header: http.new_header()
|
2022-07-05 06:40:23 +03:00
|
|
|
read_timeout: opt.read_timeout
|
|
|
|
write_timeout: opt.write_timeout
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect connects to remote websocket server
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) connect() ! {
|
|
|
|
ws.assert_not_connected()!
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.set_state(.connecting)
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.logger.info('connecting to host ${ws.uri}')
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.conn = ws.dial_socket()!
|
|
|
|
ws.handshake()!
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.set_state(.open)
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.logger.info('successfully connected to host ${ws.uri}')
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.send_open_event()
|
|
|
|
}
|
|
|
|
|
|
|
|
// listen listens and processes incoming messages
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) listen() ! {
|
2023-02-03 10:09:41 +03:00
|
|
|
mut log_msg := 'Starting client listener, server(${ws.is_server})...'
|
|
|
|
ws.logger.info(log_msg)
|
|
|
|
unsafe { log_msg.free() }
|
2021-07-03 02:56:00 +03:00
|
|
|
defer {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.logger.info('Quit client listener, server(${ws.is_server})...')
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() == .open {
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.close(1000, 'closed by client') or {}
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 12:27:00 +03:00
|
|
|
for ws.get_state() == .open {
|
2021-07-03 02:56:00 +03:00
|
|
|
msg := ws.read_next_message() or {
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() in [.closed, .closing] {
|
2021-07-03 02:56:00 +03:00
|
|
|
return
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.debug_log('failed to read next message: ${err}')
|
|
|
|
ws.send_error_event('failed to read next message: ${err}')
|
2021-07-03 02:56:00 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() in [.closed, .closing] {
|
2021-07-03 02:56:00 +03:00
|
|
|
return
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.debug_log('got message: ${msg.opcode}')
|
2021-07-03 02:56:00 +03:00
|
|
|
match msg.opcode {
|
|
|
|
.text_frame {
|
2023-02-03 10:09:41 +03:00
|
|
|
log_msg = 'read: text'
|
|
|
|
ws.debug_log(log_msg)
|
|
|
|
unsafe { log_msg.free() }
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.send_message_event(msg)
|
|
|
|
unsafe { msg.free() }
|
|
|
|
}
|
|
|
|
.binary_frame {
|
|
|
|
ws.debug_log('read: binary')
|
|
|
|
ws.send_message_event(msg)
|
|
|
|
unsafe { msg.free() }
|
|
|
|
}
|
|
|
|
.ping {
|
|
|
|
ws.debug_log('read: ping, sending pong')
|
|
|
|
ws.send_control_frame(.pong, 'PONG', msg.payload) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.logger.error('error in message callback sending PONG: ${err}')
|
|
|
|
ws.send_error_event('error in message callback sending PONG: ${err}')
|
2021-07-03 02:56:00 +03:00
|
|
|
if ws.panic_on_callback {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if msg.payload.len > 0 {
|
|
|
|
unsafe { msg.free() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.pong {
|
|
|
|
ws.debug_log('read: pong')
|
|
|
|
ws.last_pong_ut = time.now().unix
|
|
|
|
ws.send_message_event(msg)
|
|
|
|
if msg.payload.len > 0 {
|
|
|
|
unsafe { msg.free() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.close {
|
2023-02-03 10:09:41 +03:00
|
|
|
log_msg = 'read: close'
|
|
|
|
ws.debug_log(log_msg)
|
|
|
|
unsafe { log_msg.free() }
|
2021-07-03 02:56:00 +03:00
|
|
|
defer {
|
|
|
|
ws.manage_clean_close()
|
|
|
|
}
|
|
|
|
if msg.payload.len > 0 {
|
|
|
|
if msg.payload.len == 1 {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.close(1002, 'close payload cannot be 1 byte')!
|
2021-07-03 02:56:00 +03:00
|
|
|
return error('close payload cannot be 1 byte')
|
|
|
|
}
|
2021-11-29 03:48:49 +03:00
|
|
|
code := u16(msg.payload[0]) << 8 | u16(msg.payload[1])
|
2021-07-03 02:56:00 +03:00
|
|
|
if code in invalid_close_codes {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.close(1002, 'invalid close code: ${code}')!
|
|
|
|
return error('invalid close code: ${code}')
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
2022-04-15 15:35:35 +03:00
|
|
|
reason := if msg.payload.len > 2 { msg.payload[2..] } else { []u8{} }
|
2021-07-03 02:56:00 +03:00
|
|
|
if reason.len > 0 {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.validate_utf_8(.close, reason)!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() !in [.closing, .closed] {
|
2021-07-03 02:56:00 +03:00
|
|
|
// sending close back according to spec
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.debug_log('close with reason, code: ${code}, reason: ${reason}')
|
2021-07-03 02:56:00 +03:00
|
|
|
r := reason.bytestr()
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.close(code, r)!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
unsafe { msg.free() }
|
|
|
|
} else {
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() !in [.closing, .closed] {
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.debug_log('close with reason, no code')
|
|
|
|
// sending close back according to spec
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.close(1000, 'normal')!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
unsafe { msg.free() }
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
.continuation {
|
|
|
|
ws.logger.error('unexpected opcode continuation, nothing to continue')
|
|
|
|
ws.send_error_event('unexpected opcode continuation, nothing to continue')
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.close(1002, 'nothing to continue')!
|
2021-07-03 02:56:00 +03:00
|
|
|
return error('unexpected opcode continuation, nothing to continue')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// manage_clean_close closes connection in a clean websocket way
|
|
|
|
fn (mut ws Client) manage_clean_close() {
|
|
|
|
ws.send_close_event(1000, 'closed by client')
|
|
|
|
}
|
|
|
|
|
|
|
|
// ping sends ping message to server
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) ping() ! {
|
|
|
|
ws.send_control_frame(.ping, 'PING', [])!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pong sends pong message to server,
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) pong() ! {
|
|
|
|
ws.send_control_frame(.pong, 'PONG', [])!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write_ptr writes len bytes provided a byteptr with a websocket messagetype
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
|
2021-07-03 02:56:00 +03:00
|
|
|
// ws.debug_log('write_ptr code: $code')
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() != .open || ws.conn.sock.handle < 1 {
|
2021-07-03 02:56:00 +03:00
|
|
|
// todo: send error here later
|
|
|
|
return error('trying to write on a closed socket!')
|
|
|
|
}
|
|
|
|
mut header_len := 2 + if payload_len > 125 { 2 } else { 0 } +
|
|
|
|
if payload_len > 0xffff { 6 } else { 0 }
|
|
|
|
if !ws.is_server {
|
|
|
|
header_len += 4
|
|
|
|
}
|
2022-04-15 15:35:35 +03:00
|
|
|
mut header := []u8{len: header_len, init: `0`} // [`0`].repeat(header_len)
|
2022-04-15 14:58:56 +03:00
|
|
|
header[0] = u8(int(code)) | 0x80
|
2021-07-03 02:56:00 +03:00
|
|
|
masking_key := create_masking_key()
|
|
|
|
if ws.is_server {
|
|
|
|
if payload_len <= 125 {
|
2022-04-15 14:58:56 +03:00
|
|
|
header[1] = u8(payload_len)
|
2021-07-03 02:56:00 +03:00
|
|
|
} else if payload_len > 125 && payload_len <= 0xffff {
|
|
|
|
len16 := C.htons(payload_len)
|
|
|
|
header[1] = 126
|
|
|
|
unsafe { C.memcpy(&header[2], &len16, 2) }
|
|
|
|
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
|
|
|
|
len_bytes := htonl64(u64(payload_len))
|
|
|
|
header[1] = 127
|
|
|
|
unsafe { C.memcpy(&header[2], len_bytes.data, 8) }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if payload_len <= 125 {
|
2022-04-15 14:58:56 +03:00
|
|
|
header[1] = u8(payload_len | 0x80)
|
2021-07-03 02:56:00 +03:00
|
|
|
header[2] = masking_key[0]
|
|
|
|
header[3] = masking_key[1]
|
|
|
|
header[4] = masking_key[2]
|
|
|
|
header[5] = masking_key[3]
|
|
|
|
} else if payload_len > 125 && payload_len <= 0xffff {
|
|
|
|
len16 := C.htons(payload_len)
|
|
|
|
header[1] = (126 | 0x80)
|
|
|
|
unsafe { C.memcpy(&header[2], &len16, 2) }
|
|
|
|
header[4] = masking_key[0]
|
|
|
|
header[5] = masking_key[1]
|
|
|
|
header[6] = masking_key[2]
|
|
|
|
header[7] = masking_key[3]
|
|
|
|
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
|
|
|
|
len64 := htonl64(u64(payload_len))
|
|
|
|
header[1] = (127 | 0x80)
|
|
|
|
unsafe { C.memcpy(&header[2], len64.data, 8) }
|
|
|
|
header[10] = masking_key[0]
|
|
|
|
header[11] = masking_key[1]
|
|
|
|
header[12] = masking_key[2]
|
|
|
|
header[13] = masking_key[3]
|
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.close(1009, 'frame too large')!
|
2021-07-03 02:56:00 +03:00
|
|
|
return error('frame too large')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len := header.len + payload_len
|
2022-04-15 15:35:35 +03:00
|
|
|
mut frame_buf := []u8{len: len}
|
2021-07-03 02:56:00 +03:00
|
|
|
unsafe {
|
2022-04-15 14:58:56 +03:00
|
|
|
C.memcpy(&frame_buf[0], &u8(header.data), header.len)
|
2021-07-03 02:56:00 +03:00
|
|
|
if payload_len > 0 {
|
|
|
|
C.memcpy(&frame_buf[header.len], bytes, payload_len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ws.is_server {
|
|
|
|
for i in 0 .. payload_len {
|
|
|
|
frame_buf[header_len + i] ^= masking_key[i % 4] & 0xff
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
written_len := ws.socket_write(frame_buf)!
|
2021-07-03 02:56:00 +03:00
|
|
|
unsafe {
|
|
|
|
frame_buf.free()
|
|
|
|
masking_key.free()
|
|
|
|
header.free()
|
|
|
|
}
|
|
|
|
return written_len
|
|
|
|
}
|
|
|
|
|
|
|
|
// write writes a byte array with a websocket messagetype to socket
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) write(bytes []u8, code OPCode) !int {
|
2022-04-15 14:58:56 +03:00
|
|
|
return ws.write_ptr(&u8(bytes.data), bytes.len, code)
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write_str, writes a string with a websocket texttype to socket
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) write_string(str string) !int {
|
2021-07-03 02:56:00 +03:00
|
|
|
return ws.write_ptr(str.str, str.len, .text_frame)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close closes the websocket connection
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut ws Client) close(code int, message string) ! {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.debug_log('sending close, ${code}, ${message}')
|
2023-05-18 12:27:00 +03:00
|
|
|
ws_state := ws.get_state()
|
|
|
|
if ws_state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
|
|
|
|
ws.debug_log('close: Websocket already closed (${ws_state}), ${message}, ${code} handle(${ws.conn.sock.handle})')
|
2023-03-06 11:05:17 +03:00
|
|
|
err_msg := 'Socket already closed: ${code}'
|
2021-07-03 02:56:00 +03:00
|
|
|
return error(err_msg)
|
|
|
|
}
|
|
|
|
defer {
|
|
|
|
ws.shutdown_socket() or {}
|
2022-09-22 16:50:34 +03:00
|
|
|
ws.reset_state() or {}
|
2023-02-13 15:28:41 +03:00
|
|
|
ws.send_close_event(code, message)
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
ws.set_state(.closing)
|
|
|
|
// mut code32 := 0
|
|
|
|
if code > 0 {
|
|
|
|
code_ := C.htons(code)
|
|
|
|
message_len := message.len + 2
|
2022-04-15 15:35:35 +03:00
|
|
|
mut close_frame := []u8{len: message_len}
|
2022-04-15 14:58:56 +03:00
|
|
|
close_frame[0] = u8(code_ & 0xFF)
|
|
|
|
close_frame[1] = u8(code_ >> 8)
|
2021-07-03 02:56:00 +03:00
|
|
|
// code32 = (close_frame[0] << 8) + close_frame[1]
|
|
|
|
for i in 0 .. message.len {
|
|
|
|
close_frame[i + 2] = message[i]
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.send_control_frame(.close, 'CLOSE', close_frame)!
|
2021-07-03 02:56:00 +03:00
|
|
|
unsafe { close_frame.free() }
|
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.send_control_frame(.close, 'CLOSE', [])!
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
ws.fragments = []
|
|
|
|
}
|
|
|
|
|
|
|
|
// send_control_frame sends a control frame to the server
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []u8) ! {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.debug_log('send control frame ${code}, frame_type: ${frame_typ}')
|
2023-05-18 12:27:00 +03:00
|
|
|
if ws.get_state() !in [.open, .closing] && ws.conn.sock.handle > 1 {
|
2021-07-03 02:56:00 +03:00
|
|
|
return error('socket is not connected')
|
|
|
|
}
|
|
|
|
header_len := if ws.is_server { 2 } else { 6 }
|
|
|
|
frame_len := header_len + payload.len
|
2022-04-15 15:35:35 +03:00
|
|
|
mut control_frame := []u8{len: frame_len}
|
2021-07-03 02:56:00 +03:00
|
|
|
mut masking_key := if !ws.is_server { create_masking_key() } else { websocket.empty_bytearr }
|
|
|
|
defer {
|
|
|
|
unsafe {
|
|
|
|
control_frame.free()
|
|
|
|
if masking_key.len > 0 {
|
|
|
|
masking_key.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 14:58:56 +03:00
|
|
|
control_frame[0] = u8(int(code) | 0x80)
|
2021-07-03 02:56:00 +03:00
|
|
|
if !ws.is_server {
|
2022-04-15 14:58:56 +03:00
|
|
|
control_frame[1] = u8(payload.len | 0x80)
|
2021-07-03 02:56:00 +03:00
|
|
|
control_frame[2] = masking_key[0]
|
|
|
|
control_frame[3] = masking_key[1]
|
|
|
|
control_frame[4] = masking_key[2]
|
|
|
|
control_frame[5] = masking_key[3]
|
|
|
|
} else {
|
2022-04-15 14:58:56 +03:00
|
|
|
control_frame[1] = u8(payload.len)
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
if code == .close {
|
|
|
|
if payload.len >= 2 {
|
|
|
|
if !ws.is_server {
|
2022-04-15 15:35:35 +03:00
|
|
|
mut parsed_payload := []u8{len: payload.len + 1}
|
2021-07-03 02:56:00 +03:00
|
|
|
unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) }
|
|
|
|
parsed_payload[payload.len] = `\0`
|
|
|
|
for i in 0 .. payload.len {
|
|
|
|
control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff
|
|
|
|
}
|
|
|
|
unsafe { parsed_payload.free() }
|
|
|
|
} else {
|
|
|
|
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !ws.is_server {
|
|
|
|
if payload.len > 0 {
|
|
|
|
for i in 0 .. payload.len {
|
|
|
|
control_frame[header_len + i] = (payload[i] ^ masking_key[i % 4]) & 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if payload.len > 0 {
|
|
|
|
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.socket_write(control_frame) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('send_control_frame: error sending ${frame_typ} control frame.')
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse_uri parses the url to a Uri
|
2022-10-16 09:28:57 +03:00
|
|
|
fn parse_uri(url string) !&Uri {
|
|
|
|
u := urllib.parse(url)!
|
2021-07-03 02:56:00 +03:00
|
|
|
request_uri := u.request_uri()
|
|
|
|
v := request_uri.split('?')
|
|
|
|
mut port := u.port()
|
|
|
|
uri := u.str()
|
|
|
|
if port == '' {
|
|
|
|
port = if uri.starts_with('ws://') {
|
|
|
|
'80'
|
|
|
|
} else if uri.starts_with('wss://') {
|
|
|
|
'443'
|
|
|
|
} else {
|
|
|
|
u.port()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystring := if v.len > 1 { '?' + v[1] } else { '' }
|
|
|
|
return &Uri{
|
|
|
|
url: url
|
|
|
|
hostname: u.hostname()
|
|
|
|
port: port
|
|
|
|
resource: v[0]
|
|
|
|
querystring: querystring
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_state sets current state of the websocket connection
|
2023-05-18 12:27:00 +03:00
|
|
|
pub fn (mut ws Client) set_state(state State) {
|
|
|
|
lock ws.client_state {
|
|
|
|
ws.client_state.state = state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_state return the current state of the websocket connection
|
|
|
|
pub fn (ws Client) get_state() State {
|
|
|
|
return rlock ws.client_state {
|
|
|
|
ws.client_state.state
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert_not_connected returns error if the connection is not connected
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (ws Client) assert_not_connected() ! {
|
2023-05-18 12:27:00 +03:00
|
|
|
match ws.get_state() {
|
2021-07-03 02:56:00 +03:00
|
|
|
.connecting { return error('connect: websocket is connecting') }
|
|
|
|
.open { return error('connect: websocket already open') }
|
|
|
|
.closing { return error('connect: reconnect on closing websocket not supported, please use new client') }
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset_state resets the websocket and initialize default settings
|
2023-05-18 12:27:00 +03:00
|
|
|
pub fn (mut ws Client) reset_state() ! {
|
|
|
|
lock ws.client_state {
|
|
|
|
ws.client_state.state = .closed
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.ssl_conn = ssl.new_ssl_conn()!
|
2021-07-03 02:56:00 +03:00
|
|
|
ws.flags = []
|
|
|
|
ws.fragments = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// debug_log handles debug logging output for client and server
|
|
|
|
fn (mut ws Client) debug_log(text string) {
|
|
|
|
if ws.is_server {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.logger.debug('server-> ${text}')
|
2021-07-03 02:56:00 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
ws.logger.debug('client-> ${text}')
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free handles manual free memory of Message struct
|
|
|
|
pub fn (m &Message) free() {
|
|
|
|
unsafe { m.payload.free() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// free handles manual free memory of Client struct
|
|
|
|
pub fn (c &Client) free() {
|
|
|
|
unsafe {
|
|
|
|
c.flags.free()
|
|
|
|
c.fragments.free()
|
|
|
|
c.message_callbacks.free()
|
|
|
|
c.error_callbacks.free()
|
|
|
|
c.open_callbacks.free()
|
|
|
|
c.close_callbacks.free()
|
|
|
|
c.header.free()
|
|
|
|
}
|
|
|
|
}
|