examples: fix crash when running examples/ws/client.v outside of valgrind
parent
b0d76c59f7
commit
ebbc7bd471
|
@ -10,6 +10,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ws_test(uri string) {
|
fn ws_test(uri string) {
|
||||||
|
println('connecting to $uri ...')
|
||||||
mut ws := websocket.new(uri)
|
mut ws := websocket.new(uri)
|
||||||
ws.subscriber.subscribe('on_open', on_open)
|
ws.subscriber.subscribe('on_open', on_open)
|
||||||
ws.subscriber.subscribe('on_message', on_message)
|
ws.subscriber.subscribe('on_message', on_message)
|
||||||
|
|
|
@ -6,7 +6,7 @@ fn (mut ws Client) read_handshake(seckey string) {
|
||||||
max_buffer := 1024
|
max_buffer := 1024
|
||||||
buffer_size := 1
|
buffer_size := 1
|
||||||
mut buffer := malloc(max_buffer)
|
mut buffer := malloc(max_buffer)
|
||||||
for bytes_read <= max_buffer {
|
for bytes_read < max_buffer - 1 {
|
||||||
mut res := 0
|
mut res := 0
|
||||||
unsafe {
|
unsafe {
|
||||||
res = ws.read_from_server(buffer + bytes_read, buffer_size)
|
res = ws.read_from_server(buffer + bytes_read, buffer_size)
|
||||||
|
@ -21,7 +21,7 @@ fn (mut ws Client) read_handshake(seckey string) {
|
||||||
}
|
}
|
||||||
bytes_read += buffer_size
|
bytes_read += buffer_size
|
||||||
}
|
}
|
||||||
buffer[max_buffer + 1] = `\0`
|
buffer[max_buffer - 1] = `\0`
|
||||||
ws.handshake_handler(string(byteptr(buffer)), seckey)
|
ws.handshake_handler(string(byteptr(buffer)), seckey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,16 +3,21 @@ module websocket
|
||||||
fn (mut ws Client) write_to_server(buf voidptr, len int) int {
|
fn (mut ws Client) write_to_server(buf voidptr, len int) int {
|
||||||
mut bytes_written := 0
|
mut bytes_written := 0
|
||||||
ws.write_lock.m_lock()
|
ws.write_lock.m_lock()
|
||||||
bytes_written = if ws.is_ssl { C.SSL_write(ws.ssl, buf, len) } else { C.write(ws.socket.sockfd,
|
if ws.is_ssl {
|
||||||
buf, len) }
|
bytes_written = C.SSL_write(ws.ssl, buf, len)
|
||||||
|
} else {
|
||||||
|
bytes_written = C.write(ws.socket.sockfd, buf, len)
|
||||||
|
}
|
||||||
ws.write_lock.unlock()
|
ws.write_lock.unlock()
|
||||||
return bytes_written
|
return bytes_written
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (ws &Client) read_from_server(buffer byteptr, buffer_size int) int {
|
fn (ws &Client) read_from_server(buffer byteptr, buffer_size int) int {
|
||||||
return if ws.is_ssl {
|
mut res := 0
|
||||||
C.SSL_read(ws.ssl, buffer, buffer_size)
|
if ws.is_ssl {
|
||||||
|
res = C.SSL_read(ws.ssl, buffer, buffer_size)
|
||||||
} else {
|
} else {
|
||||||
C.read(ws.socket.sockfd, buffer, buffer_size)
|
res = C.read(ws.socket.sockfd, buffer, buffer_size)
|
||||||
}
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,13 +239,11 @@ pub fn (mut ws Client) close(code int, message string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write will send the payload to the websocket server. NB: *it will not free the payload*, the caller is responsible for that.
|
||||||
pub fn (mut ws Client) write(payload byteptr, payload_len int, code OPCode) int {
|
pub fn (mut ws Client) write(payload byteptr, payload_len int, code OPCode) int {
|
||||||
mut bytes_written := -1
|
mut bytes_written := -1
|
||||||
if ws.state != .open {
|
if ws.state != .open {
|
||||||
ws.send_error_event('WebSocket closed. Cannot write.')
|
ws.send_error_event('WebSocket closed. Cannot write.')
|
||||||
unsafe {
|
|
||||||
free(payload)
|
|
||||||
}
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
header_len := 6 + if payload_len > 125 { 2 } else { 0 } + if payload_len > 0xffff { 6 } else { 0 }
|
header_len := 6 + if payload_len > 125 { 2 } else { 0 } + if payload_len > 0xffff { 6 } else { 0 }
|
||||||
|
@ -305,7 +303,6 @@ pub fn (mut ws Client) write(payload byteptr, payload_len int, code OPCode) int
|
||||||
ws.log.debug('write: $bytes_written bytes written.')
|
ws.log.debug('write: $bytes_written bytes written.')
|
||||||
free_data:
|
free_data:
|
||||||
unsafe {
|
unsafe {
|
||||||
free(payload)
|
|
||||||
frame_buf.free()
|
frame_buf.free()
|
||||||
header.free()
|
header.free()
|
||||||
masking_key.free()
|
masking_key.free()
|
||||||
|
|
Loading…
Reference in New Issue