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
pub mut:
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
state State // current state of connection
logger &log.Log // logger used to log messages
@ -41,7 +42,7 @@ pub mut:
// Flag represents different types of headers in websocket handshake
enum Flag {
has_accept // Webs
has_accept // Webs
has_connection
has_upgrade
}
@ -57,8 +58,8 @@ enum State {
// Message represents a whole message combined from 1 to n frames
pub struct Message {
pub:
opcode OPCode // websocket frame type of this message
payload []byte // payload of the message
opcode OPCode // websocket frame type of this message
payload []byte // payload of the message
}
// OPCode represents the supported websocket frame types
@ -128,12 +129,12 @@ pub fn (mut ws Client) listen() ? {
.text_frame {
ws.debug_log('read: text')
ws.send_message_event(msg)
unsafe {msg.free()}
unsafe { msg.free() }
}
.binary_frame {
ws.debug_log('read: binary')
ws.send_message_event(msg)
unsafe {msg.free()}
unsafe { msg.free() }
}
.ping {
ws.debug_log('read: ping, sending pong')
@ -146,7 +147,7 @@ pub fn (mut ws Client) listen() ? {
continue
}
if msg.payload.len > 0 {
unsafe {msg.free()}
unsafe { msg.free() }
}
}
.pong {
@ -154,7 +155,7 @@ pub fn (mut ws Client) listen() ? {
ws.last_pong_ut = time.now().unix
ws.send_message_event(msg)
if msg.payload.len > 0 {
unsafe {msg.free()}
unsafe { msg.free() }
}
}
.close {
@ -182,14 +183,14 @@ pub fn (mut ws Client) listen() ? {
r := reason.bytestr()
ws.close(code, r) ?
}
unsafe {msg.free()}
unsafe { msg.free() }
} else {
if ws.state !in [.closing, .closed] {
ws.debug_log('close with reason, no code')
// sending close back according to spec
ws.close(1000, 'normal') ?
}
unsafe {msg.free()}
unsafe { msg.free() }
}
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 {
len16 := C.htons(payload_len)
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 {
len_bytes := htonl64(u64(payload_len))
header[1] = 127
unsafe {C.memcpy(&header[2], len_bytes.data, 8)}
unsafe { C.memcpy(&header[2], len_bytes.data, 8) }
}
} else {
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 {
len16 := C.htons(payload_len)
header[1] = (126 | 0x80)
unsafe {C.memcpy(&header[2], &len16, 2)}
unsafe { C.memcpy(&header[2], &len16, 2) }
header[4] = masking_key[0]
header[5] = masking_key[1]
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 {
len64 := htonl64(u64(payload_len))
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[11] = masking_key[1]
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_close_event(code, message)
unsafe {close_frame.free()}
unsafe { close_frame.free() }
} else {
ws.send_control_frame(.close, 'CLOSE', []) ?
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 !ws.is_server {
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`
for i in 0 .. payload.len {
control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff
}
unsafe {parsed_payload.free()}
unsafe { parsed_payload.free() }
} else {
unsafe {C.memcpy(&control_frame[2], &payload[0], payload.len)}
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
}
}
} else {
@ -393,7 +394,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b
}
} else {
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
pub fn (m &Message) free() {
unsafe {m.payload.free()}
unsafe { m.payload.free() }
}
// free handles manual free memory of Client struct

View File

@ -20,7 +20,8 @@ pub:
port int // port used as listen to incoming connections
is_ssl bool // true if secure connection (not supported yet on server)
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
}
@ -57,9 +58,7 @@ pub fn (mut s Server) listen() ? {
s.set_state(.open)
go s.handle_ping()
for {
mut c := s.accept_new_client() or {
continue
}
mut c := s.accept_new_client() or { continue }
go s.serve_client(mut c)
}
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 {
clients_to_remove << c.client.id
c.client.close(1000, 'no pong received') or {
continue
}
c.client.close(1000, 'no pong received') or { continue }
}
}
}