websocket: vfmt

pull/7634/head
Alexander Medvednikov 2020-12-28 01:06:57 +01:00
parent 597314322f
commit bd67b647f2
3 changed files with 10 additions and 8 deletions

View File

@ -69,6 +69,8 @@ const (
'vlib/strings/', 'vlib/strings/',
'vlib/time/', 'vlib/time/',
'vlib/vweb/', 'vlib/vweb/',
'vlib/x/websocket/websocket_server.v',
'vlib/x/websocket/websocket_client.v',
] ]
) )

View File

@ -1,5 +1,5 @@
// websocket module implements websocket client and a websocket server // websocket module implements websocket client and a websocket server
// attribution: @thecoderr the author of original websocket client // attribution: @thecoderr the author of original websocket client
module websocket module websocket
import net import net
@ -123,7 +123,7 @@ pub fn (mut ws Client) listen() ? {
if ws.state in [.closed, .closing] { if ws.state in [.closed, .closing] {
return return
} }
ws.debug_log('got message: $msg.opcode') ws.debug_log('got message: $msg.opcode')
match msg.opcode { match msg.opcode {
.text_frame { .text_frame {
ws.debug_log('read: text') ws.debug_log('read: text')
@ -245,7 +245,7 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
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 {
@ -263,7 +263,7 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
header[5] = masking_key[1] header[5] = masking_key[1]
header[6] = masking_key[2] header[6] = masking_key[2]
header[7] = masking_key[3] header[7] = masking_key[3]
} 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)}
@ -344,13 +344,13 @@ pub fn (mut ws Client) close(code int, message string) ? {
// send_control_frame sends a control frame to the server // send_control_frame sends a control frame to the server
fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []byte) ? { fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []byte) ? {
ws.debug_log('send control frame $code, frame_type: $frame_typ') ws.debug_log('send control frame $code, frame_type: $frame_typ')
if ws.state !in [.open, .closing] && ws.conn.sock.handle > 1 { if ws.state !in [.open, .closing] && ws.conn.sock.handle > 1 {
return error('socket is not connected') return error('socket is not connected')
} }
header_len := if ws.is_server { 2 } else { 6 } header_len := if ws.is_server { 2 } else { 6 }
frame_len := header_len + payload.len frame_len := header_len + payload.len
mut control_frame := []byte{len: frame_len} mut control_frame := []byte{len: frame_len}
mut masking_key := if !ws.is_server { create_masking_key() } else { empty_bytearr } mut masking_key := if !ws.is_server { create_masking_key() } else { empty_bytearr }
defer { defer {
unsafe { unsafe {

View File

@ -30,8 +30,8 @@ pub:
resource_name string // resource that the client access resource_name string // resource that the client access
client_key string // unique key of client client_key string // unique key of client
pub mut: pub mut:
server &Server server &Server
client &Client client &Client
} }
// new_server instance a new websocket server on provided port and route // new_server instance a new websocket server on provided port and route