2020-08-22 01:50:38 +03:00
|
|
|
// The module websocket implements the websocket server capabilities
|
|
|
|
module websocket
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
import net
|
2020-08-22 01:50:38 +03:00
|
|
|
import x.openssl
|
|
|
|
import log
|
|
|
|
import sync
|
|
|
|
import time
|
|
|
|
import rand
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// Server holds state of websocket server connection
|
2020-08-22 01:50:38 +03:00
|
|
|
pub struct Server {
|
|
|
|
mut:
|
2020-12-02 06:02:53 +03:00
|
|
|
clients map[string]&ServerClient // Clients connected to this server
|
|
|
|
logger &log.Log // Logger used to log
|
|
|
|
ls net.TcpListener // TCpLister used to get incoming connection to socket
|
|
|
|
accept_client_callbacks []AcceptClientFn // Accept client callback functions
|
|
|
|
message_callbacks []MessageEventHandler // New message callback functions
|
|
|
|
close_callbacks []CloseEventHandler // Close message callback functions
|
2020-08-22 01:50:38 +03:00
|
|
|
pub:
|
2020-12-02 06:02:53 +03:00
|
|
|
port int // Port used as listen to incoming connections
|
|
|
|
is_ssl bool // True if secure connection (not supported yet on server)
|
2020-08-22 01:50:38 +03:00
|
|
|
pub mut:
|
2020-12-02 06:02:53 +03:00
|
|
|
ping_interval int = 30
|
|
|
|
// Interval for automatic sending ping to connected clients in seconds
|
|
|
|
state State // Current state of connection
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// ServerClient has state of connected clients
|
2020-08-22 01:50:38 +03:00
|
|
|
struct ServerClient {
|
|
|
|
pub:
|
2020-12-02 06:02:53 +03:00
|
|
|
resource_name string // The resource that the client access
|
|
|
|
client_key string // Unique key of client
|
2020-08-22 01:50:38 +03:00
|
|
|
pub mut:
|
2020-12-02 06:02:53 +03:00
|
|
|
server &Server // The server instance
|
|
|
|
client &Client // The client instance
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// new_server instance new websocket server on port and route
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn new_server(port int, route string) &Server {
|
|
|
|
return &Server{
|
|
|
|
port: port
|
|
|
|
logger: &log.Log{
|
|
|
|
level: .info
|
|
|
|
}
|
|
|
|
state: .closed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// set_ping_interval sets the interval that the server will send ping messages to clients
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) set_ping_interval(seconds int) {
|
|
|
|
s.ping_interval = seconds
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// listen, start listen to incoming connections
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) listen() ? {
|
|
|
|
s.logger.info('websocket server: start listen on port $s.port')
|
2020-12-02 06:02:53 +03:00
|
|
|
s.ls = net.listen_tcp(s.port) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
s.set_state(.open)
|
|
|
|
go s.handle_ping()
|
|
|
|
for {
|
2020-09-27 11:09:20 +03:00
|
|
|
mut c := s.accept_new_client() or {
|
2020-08-22 01:50:38 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
go s.serve_client(mut c)
|
|
|
|
}
|
|
|
|
s.logger.info('websocket server: end listen on port $s.port')
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// Close server (not implemented)
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) close() {
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// handle_ping sends ping to all clients every set interval
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) handle_ping() {
|
|
|
|
mut clients_to_remove := []string{}
|
|
|
|
for s.state == .open {
|
|
|
|
time.sleep(s.ping_interval)
|
|
|
|
for _, cli in s.clients {
|
|
|
|
mut c := cli
|
|
|
|
if c.client.state == .open {
|
|
|
|
c.client.ping() or {
|
|
|
|
s.logger.debug('server-> error sending ping to client')
|
2020-12-02 06:02:53 +03:00
|
|
|
c.client.close(1002, 'Closing connection: ping send error') or {
|
2020-08-22 01:50:38 +03:00
|
|
|
// we want to continue even if error
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
clients_to_remove << c.client.id
|
|
|
|
}
|
|
|
|
if (time.now().unix - c.client.last_pong_ut) > s.ping_interval * 2 {
|
|
|
|
clients_to_remove << c.client.id
|
|
|
|
c.client.close(1000, 'no pong received') or {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO replace for with s.clients.delete_all(clients_to_remove) if (https://github.com/vlang/v/pull/6020) merges
|
|
|
|
for client in clients_to_remove {
|
|
|
|
lock {
|
|
|
|
s.clients.delete(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clients_to_remove.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// serve_client accepts incoming connection and setup the websocket handshake
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) serve_client(mut c Client) ? {
|
|
|
|
c.logger.debug('server-> Start serve client ($c.id)')
|
|
|
|
defer {
|
|
|
|
c.logger.debug('server-> End serve client ($c.id)')
|
|
|
|
}
|
2020-12-02 06:02:53 +03:00
|
|
|
mut handshake_response, mut server_client := s.handle_server_handshake(mut c) ?
|
|
|
|
accept := s.send_connect_event(mut server_client) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
if !accept {
|
|
|
|
s.logger.debug('server-> client not accepted')
|
2020-12-02 06:02:53 +03:00
|
|
|
c.shutdown_socket() ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// The client is accepted
|
2020-12-02 06:02:53 +03:00
|
|
|
c.socket_write(handshake_response.bytes()) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
lock {
|
|
|
|
s.clients[server_client.client.id] = server_client
|
|
|
|
}
|
|
|
|
s.setup_callbacks(mut server_client)
|
|
|
|
c.listen() or {
|
|
|
|
s.logger.error(err)
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// setup_callbacks initialize all callback functions
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) setup_callbacks(mut sc ServerClient) {
|
|
|
|
if s.message_callbacks.len > 0 {
|
|
|
|
for cb in s.message_callbacks {
|
|
|
|
if cb.is_ref {
|
|
|
|
sc.client.on_message_ref(cb.handler2, cb.ref)
|
|
|
|
} else {
|
|
|
|
sc.client.on_message(cb.handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.close_callbacks.len > 0 {
|
|
|
|
for cb in s.close_callbacks {
|
|
|
|
if cb.is_ref {
|
|
|
|
sc.client.on_close_ref(cb.handler2, cb.ref)
|
|
|
|
} else {
|
|
|
|
sc.client.on_close(cb.handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set standard close so we can remove client if closed
|
|
|
|
sc.client.on_close_ref(fn (mut c Client, code int, reason string, mut sc ServerClient) ? {
|
|
|
|
c.logger.debug('server-> Delete client')
|
|
|
|
lock {
|
|
|
|
sc.server.clients.delete(sc.client.id)
|
|
|
|
}
|
2020-09-27 11:09:20 +03:00
|
|
|
}, sc)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// accept_new_client creates a new client instance for client connects to socket
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) accept_new_client() ?&Client {
|
2020-12-02 06:02:53 +03:00
|
|
|
mut new_conn := s.ls.accept() ?
|
2020-08-22 01:50:38 +03:00
|
|
|
c := &Client{
|
|
|
|
is_server: true
|
|
|
|
conn: new_conn
|
|
|
|
ssl_conn: openssl.new_ssl_conn()
|
|
|
|
logger: s.logger
|
|
|
|
state: .open
|
|
|
|
last_pong_ut: time.now().unix
|
|
|
|
id: rand.uuid_v4()
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_state sets current state in a thread safe way
|
|
|
|
fn (mut s Server) set_state(state State) {
|
|
|
|
lock {
|
|
|
|
s.state = state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// free, manual free memory of Server instance
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) free() {
|
|
|
|
unsafe {
|
|
|
|
s.clients.free()
|
|
|
|
s.accept_client_callbacks.free()
|
|
|
|
s.message_callbacks.free()
|
|
|
|
s.close_callbacks.free()
|
|
|
|
}
|
|
|
|
}
|