2020-08-22 00:50:38 +02:00
|
|
|
module websocket
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
import net
|
2020-08-22 00:50:38 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
interface WebsocketIO {
|
2020-11-15 21:54:47 +01:00
|
|
|
socket_read(mut buffer []byte) ?int
|
2020-08-22 00:50:38 +02:00
|
|
|
socket_write(bytes []byte) ?
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
// socket_read reads into the provided buffer with its length
|
|
|
|
fn (mut ws Client) socket_read(mut buffer []byte) ?int {
|
2020-08-22 00:50:38 +02:00
|
|
|
lock {
|
|
|
|
if ws.is_ssl {
|
|
|
|
r := ws.ssl_conn.read_into(mut buffer)?
|
|
|
|
return r
|
|
|
|
} else {
|
|
|
|
for {
|
2020-11-15 21:54:47 +01:00
|
|
|
r := ws.conn.read(mut buffer) or {
|
2020-08-22 00:50:38 +02:00
|
|
|
if errcode == net.err_timed_out_code {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
fn (mut ws Client) socket_read_ptr(buf_ptr byteptr, len int) ?int {
|
2020-08-22 00:50:38 +02:00
|
|
|
lock {
|
|
|
|
if ws.is_ssl {
|
|
|
|
r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)?
|
|
|
|
return r
|
|
|
|
} else {
|
|
|
|
for {
|
2020-11-15 21:54:47 +01:00
|
|
|
r := ws.conn.read_ptr(buf_ptr, len) or {
|
2020-08-22 00:50:38 +02:00
|
|
|
if errcode == net.err_timed_out_code {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// socket_write, writes the whole byte array provided to the socket
|
|
|
|
fn (mut ws Client) socket_write(bytes []byte) ? {
|
|
|
|
lock {
|
|
|
|
if ws.state == .closed || ws.conn.sock.handle <= 1 {
|
|
|
|
ws.debug_log('write: Socket allready closed')
|
|
|
|
return error('Socket allready closed')
|
|
|
|
}
|
|
|
|
if ws.is_ssl {
|
|
|
|
ws.ssl_conn.write(bytes)?
|
|
|
|
} else {
|
|
|
|
for {
|
|
|
|
ws.conn.write(bytes) or {
|
|
|
|
if errcode == net.err_timed_out_code {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shutdown_socket, proper shutdown make PR in Emeliy repo
|
|
|
|
fn (mut ws Client) shutdown_socket() ? {
|
|
|
|
ws.debug_log('shutting down socket')
|
|
|
|
if ws.is_ssl {
|
|
|
|
ws.ssl_conn.shutdown()?
|
|
|
|
} else {
|
|
|
|
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')?
|
|
|
|
optval := int(1)
|
|
|
|
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)?
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|