ci: run vfmt over websocket_client.v and websocket_server.v

pull/7640/head
Delyan Angelov 2020-12-28 07:13:25 +02:00
parent bd67b647f2
commit b65353794c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 25 additions and 27 deletions

View File

@ -31,7 +31,8 @@ pub:
id string // unique id of client id string // unique id of client
pub mut: pub mut:
conn net.TcpConn // underlying TCP socket connection conn net.TcpConn // underlying TCP socket connection
nonce_size int = 16 // size of nounce used for masking // size of nounce used for masking
nonce_size int = 16
panic_on_callback bool // set to true of callbacks can panic panic_on_callback bool // set to true of callbacks can panic
state State // current state of connection state State // current state of connection
logger &log.Log // logger used to log messages logger &log.Log // logger used to log messages
@ -41,7 +42,7 @@ pub mut:
// Flag represents different types of headers in websocket handshake // Flag represents different types of headers in websocket handshake
enum Flag { enum Flag {
has_accept // Webs has_accept // Webs
has_connection has_connection
has_upgrade has_upgrade
} }
@ -57,8 +58,8 @@ enum State {
// Message represents a whole message combined from 1 to n frames // Message represents a whole message combined from 1 to n frames
pub struct Message { pub struct Message {
pub: pub:
opcode OPCode // websocket frame type of this message opcode OPCode // websocket frame type of this message
payload []byte // payload of the message payload []byte // payload of the message
} }
// OPCode represents the supported websocket frame types // OPCode represents the supported websocket frame types
@ -128,12 +129,12 @@ pub fn (mut ws Client) listen() ? {
.text_frame { .text_frame {
ws.debug_log('read: text') ws.debug_log('read: text')
ws.send_message_event(msg) ws.send_message_event(msg)
unsafe {msg.free()} unsafe { msg.free() }
} }
.binary_frame { .binary_frame {
ws.debug_log('read: binary') ws.debug_log('read: binary')
ws.send_message_event(msg) ws.send_message_event(msg)
unsafe {msg.free()} unsafe { msg.free() }
} }
.ping { .ping {
ws.debug_log('read: ping, sending pong') ws.debug_log('read: ping, sending pong')
@ -146,7 +147,7 @@ pub fn (mut ws Client) listen() ? {
continue continue
} }
if msg.payload.len > 0 { if msg.payload.len > 0 {
unsafe {msg.free()} unsafe { msg.free() }
} }
} }
.pong { .pong {
@ -154,7 +155,7 @@ pub fn (mut ws Client) listen() ? {
ws.last_pong_ut = time.now().unix ws.last_pong_ut = time.now().unix
ws.send_message_event(msg) ws.send_message_event(msg)
if msg.payload.len > 0 { if msg.payload.len > 0 {
unsafe {msg.free()} unsafe { msg.free() }
} }
} }
.close { .close {
@ -182,14 +183,14 @@ pub fn (mut ws Client) listen() ? {
r := reason.bytestr() r := reason.bytestr()
ws.close(code, r) ? ws.close(code, r) ?
} }
unsafe {msg.free()} unsafe { msg.free() }
} else { } else {
if ws.state !in [.closing, .closed] { if ws.state !in [.closing, .closed] {
ws.debug_log('close with reason, no code') ws.debug_log('close with reason, no code')
// sending close back according to spec // sending close back according to spec
ws.close(1000, 'normal') ? ws.close(1000, 'normal') ?
} }
unsafe {msg.free()} unsafe { msg.free() }
} }
return return
} }
@ -242,11 +243,11 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
} else if payload_len > 125 && payload_len <= 0xffff { } else if payload_len > 125 && payload_len <= 0xffff {
len16 := C.htons(payload_len) len16 := C.htons(payload_len)
header[1] = 126 header[1] = 126
unsafe {C.memcpy(&header[2], &len16, 2)} unsafe { C.memcpy(&header[2], &len16, 2) }
} else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff { } else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff {
len_bytes := htonl64(u64(payload_len)) len_bytes := htonl64(u64(payload_len))
header[1] = 127 header[1] = 127
unsafe {C.memcpy(&header[2], len_bytes.data, 8)} unsafe { C.memcpy(&header[2], len_bytes.data, 8) }
} }
} else { } else {
if payload_len <= 125 { if payload_len <= 125 {
@ -258,7 +259,7 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
} else if payload_len > 125 && payload_len <= 0xffff { } else if payload_len > 125 && payload_len <= 0xffff {
len16 := C.htons(payload_len) len16 := C.htons(payload_len)
header[1] = (126 | 0x80) header[1] = (126 | 0x80)
unsafe {C.memcpy(&header[2], &len16, 2)} unsafe { C.memcpy(&header[2], &len16, 2) }
header[4] = masking_key[0] header[4] = masking_key[0]
header[5] = masking_key[1] header[5] = masking_key[1]
header[6] = masking_key[2] header[6] = masking_key[2]
@ -266,7 +267,7 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
} else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff { } else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff {
len64 := htonl64(u64(payload_len)) len64 := htonl64(u64(payload_len))
header[1] = (127 | 0x80) header[1] = (127 | 0x80)
unsafe {C.memcpy(&header[2], len64.data, 8)} unsafe { C.memcpy(&header[2], len64.data, 8) }
header[10] = masking_key[0] header[10] = masking_key[0]
header[11] = masking_key[1] header[11] = masking_key[1]
header[12] = masking_key[2] header[12] = masking_key[2]
@ -334,7 +335,7 @@ pub fn (mut ws Client) close(code int, message string) ? {
} }
ws.send_control_frame(.close, 'CLOSE', close_frame) ? ws.send_control_frame(.close, 'CLOSE', close_frame) ?
ws.send_close_event(code, message) ws.send_close_event(code, message)
unsafe {close_frame.free()} unsafe { close_frame.free() }
} else { } else {
ws.send_control_frame(.close, 'CLOSE', []) ? ws.send_control_frame(.close, 'CLOSE', []) ?
ws.send_close_event(code, '') ws.send_close_event(code, '')
@ -374,14 +375,14 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b
if payload.len >= 2 { if payload.len >= 2 {
if !ws.is_server { if !ws.is_server {
mut parsed_payload := []byte{len: payload.len + 1} mut parsed_payload := []byte{len: payload.len + 1}
unsafe {C.memcpy(parsed_payload.data, &payload[0], payload.len)} unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) }
parsed_payload[payload.len] = `\0` parsed_payload[payload.len] = `\0`
for i in 0 .. payload.len { for i in 0 .. payload.len {
control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff
} }
unsafe {parsed_payload.free()} unsafe { parsed_payload.free() }
} else { } else {
unsafe {C.memcpy(&control_frame[2], &payload[0], payload.len)} unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
} }
} }
} else { } else {
@ -393,7 +394,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b
} }
} else { } else {
if payload.len > 0 { if payload.len > 0 {
unsafe {C.memcpy(&control_frame[2], &payload[0], payload.len)} unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
} }
} }
} }
@ -464,7 +465,7 @@ fn (mut ws Client) debug_log(text string) {
// free handles manual free memory of Message struct // free handles manual free memory of Message struct
pub fn (m &Message) free() { pub fn (m &Message) free() {
unsafe {m.payload.free()} unsafe { m.payload.free() }
} }
// free handles manual free memory of Client struct // free handles manual free memory of Client struct

View File

@ -20,7 +20,8 @@ pub:
port int // port used as listen to incoming connections port int // port used as listen to incoming connections
is_ssl bool // true if secure connection (not supported yet on server) is_ssl bool // true if secure connection (not supported yet on server)
pub mut: pub mut:
ping_interval int = 30 // interval for sending ping to clients (seconds) ping_interval int = 30
// interval for sending ping to clients (seconds)
state State // current state of connection state State // current state of connection
} }
@ -57,9 +58,7 @@ pub fn (mut s Server) listen() ? {
s.set_state(.open) s.set_state(.open)
go s.handle_ping() go s.handle_ping()
for { for {
mut c := s.accept_new_client() or { mut c := s.accept_new_client() or { continue }
continue
}
go s.serve_client(mut c) go s.serve_client(mut c)
} }
s.logger.info('websocket server: end listen on port $s.port') s.logger.info('websocket server: end listen on port $s.port')
@ -88,9 +87,7 @@ fn (mut s Server) handle_ping() {
} }
if (time.now().unix - c.client.last_pong_ut) > s.ping_interval * 2 { if (time.now().unix - c.client.last_pong_ut) > s.ping_interval * 2 {
clients_to_remove << c.client.id clients_to_remove << c.client.id
c.client.close(1000, 'no pong received') or { c.client.close(1000, 'no pong received') or { continue }
continue
}
} }
} }
} }