1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net/openssl/websocket: implement io.Writer (#8980)

This commit is contained in:
zakuro
2021-02-27 17:29:18 +09:00
committed by GitHub
parent f67bff1696
commit d0a64f2da7
7 changed files with 32 additions and 29 deletions

View File

@@ -52,24 +52,25 @@ fn (mut ws Client) socket_read_ptr(buf_ptr byteptr, len int) ?int {
}
// socket_write writes the provided byte array to the socket
fn (mut ws Client) socket_write(bytes []byte) ? {
fn (mut ws Client) socket_write(bytes []byte) ?int {
lock {
if ws.state == .closed || ws.conn.sock.handle <= 1 {
ws.debug_log('socket_write: Socket allready closed')
return error('socket_write: trying to write on a closed socket')
}
if ws.is_ssl {
ws.ssl_conn.write(bytes) ?
return ws.ssl_conn.write(bytes)
} else {
for {
ws.conn.write(bytes) or {
n := ws.conn.write(bytes) or {
if errcode == net.err_timed_out_code {
continue
}
return error(err)
}
return
return n
}
panic('reached unreachable code')
}
}
}

View File

@@ -226,7 +226,7 @@ pub fn (mut ws Client) pong() ? {
}
// write_ptr writes len bytes provided a byteptr with a websocket messagetype
pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ? {
pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?int {
// ws.debug_log('write_ptr code: $code')
if ws.state != .open || ws.conn.sock.handle < 1 {
// todo: send error here later
@@ -293,22 +293,23 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
frame_buf[header_len + i] ^= masking_key[i % 4] & 0xff
}
}
ws.socket_write(frame_buf) ?
written_len := ws.socket_write(frame_buf) ?
unsafe {
frame_buf.free()
masking_key.free()
header.free()
}
return written_len
}
// write writes a byte array with a websocket messagetype to socket
pub fn (mut ws Client) write(bytes []byte, code OPCode) ? {
ws.write_ptr(byteptr(bytes.data), bytes.len, code) ?
pub fn (mut ws Client) write(bytes []byte, code OPCode) ?int {
return ws.write_ptr(byteptr(bytes.data), bytes.len, code)
}
// write_str, writes a string with a websocket texttype to socket
pub fn (mut ws Client) write_str(str string) ? {
ws.write_ptr(str.str, str.len, .text_frame) ?
pub fn (mut ws Client) write_str(str string) ?int {
return ws.write_ptr(str.str, str.len, .text_frame)
}
// close closes the websocket connection