From d0a64f2da7a3ee40345f823861fb2b4c8f432557 Mon Sep 17 00:00:00 2001 From: zakuro Date: Sat, 27 Feb 2021 17:29:18 +0900 Subject: [PATCH] net/openssl/websocket: implement io.Writer (#8980) --- vlib/net/ftp/ftp.v | 4 ++-- vlib/net/tcp.v | 8 ++++---- vlib/net/udp.v | 14 +++++++------- vlib/net/unix/stream_nix.v | 8 ++++---- vlib/x/openssl/openssl.v | 5 +++-- vlib/x/websocket/io.v | 9 +++++---- vlib/x/websocket/websocket_client.v | 13 +++++++------ 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/vlib/net/ftp/ftp.v b/vlib/net/ftp/ftp.v index ff4b312094..f27ebb537a 100644 --- a/vlib/net/ftp/ftp.v +++ b/vlib/net/ftp/ftp.v @@ -74,11 +74,11 @@ pub fn new() FTP { return f } -fn (mut zftp FTP) write(data string) ? { +fn (mut zftp FTP) write(data string) ?int { $if debug { println('FTP.v >>> $data') } - zftp.conn.write('$data\r\n'.bytes()) ? + return zftp.conn.write('$data\r\n'.bytes()) } fn (mut zftp FTP) read() ?(int, string) { diff --git a/vlib/net/tcp.v b/vlib/net/tcp.v index ea7d5464b6..3bb62e988e 100644 --- a/vlib/net/tcp.v +++ b/vlib/net/tcp.v @@ -33,7 +33,7 @@ pub fn (mut c TcpConn) close() ? { } // write_ptr blocks and attempts to write all data -pub fn (mut c TcpConn) write_ptr(b byteptr, len int) ? { +pub fn (mut c TcpConn) write_ptr(b byteptr, len int) ?int { $if trace_tcp ? { eprintln( '>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' + @@ -57,17 +57,17 @@ pub fn (mut c TcpConn) write_ptr(b byteptr, len int) ? { } total_sent += sent } + return total_sent } - return none } // write blocks and attempts to write all data -pub fn (mut c TcpConn) write(bytes []byte) ? { +pub fn (mut c TcpConn) write(bytes []byte) ?int { return c.write_ptr(bytes.data, bytes.len) } // write_str blocks and attempts to write all data -pub fn (mut c TcpConn) write_str(s string) ? { +pub fn (mut c TcpConn) write_str(s string) ?int { return c.write_ptr(s.str, s.len) } diff --git a/vlib/net/udp.v b/vlib/net/udp.v index b119d34e3f..fac1320d59 100644 --- a/vlib/net/udp.v +++ b/vlib/net/udp.v @@ -45,23 +45,23 @@ fn resolve_wrapper(raddr string) ?Addr { return x } -pub fn (mut c UdpConn) write_ptr(b byteptr, len int) ? { +pub fn (mut c UdpConn) write_ptr(b byteptr, len int) ?int { remote := c.sock.remote() or { return err_no_udp_remote } return c.write_to_ptr(remote, b, len) } -pub fn (mut c UdpConn) write(buf []byte) ? { +pub fn (mut c UdpConn) write(buf []byte) ?int { return c.write_ptr(buf.data, buf.len) } -pub fn (mut c UdpConn) write_str(s string) ? { +pub fn (mut c UdpConn) write_str(s string) ?int { return c.write_ptr(s.str, s.len) } -pub fn (mut c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ? { +pub fn (mut c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ?int { res := C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len) if res >= 0 { - return none + return res } code := error_code() if code == int(error_ewouldblock) { @@ -74,12 +74,12 @@ pub fn (mut c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ? { } // write_to blocks and writes the buf to the remote addr specified -pub fn (mut c UdpConn) write_to(addr Addr, buf []byte) ? { +pub fn (mut c UdpConn) write_to(addr Addr, buf []byte) ?int { return c.write_to_ptr(addr, buf.data, buf.len) } // write_to_string blocks and writes the buf to the remote addr specified -pub fn (mut c UdpConn) write_to_string(addr Addr, s string) ? { +pub fn (mut c UdpConn) write_to_string(addr Addr, s string) ?int { return c.write_to_ptr(addr, s.str, s.len) } diff --git a/vlib/net/unix/stream_nix.v b/vlib/net/unix/stream_nix.v index 50a3ea1cc2..120d689851 100644 --- a/vlib/net/unix/stream_nix.v +++ b/vlib/net/unix/stream_nix.v @@ -170,7 +170,7 @@ pub fn (mut c StreamConn) close() ? { } // write_ptr blocks and attempts to write all data -pub fn (mut c StreamConn) write_ptr(b byteptr, len int) ? { +pub fn (mut c StreamConn) write_ptr(b byteptr, len int) ?int { $if trace_unix ? { eprintln( '>>> StreamConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' + @@ -194,17 +194,17 @@ pub fn (mut c StreamConn) write_ptr(b byteptr, len int) ? { } total_sent += sent } + return total_sent } - return none } // write blocks and attempts to write all data -pub fn (mut c StreamConn) write(bytes []byte) ? { +pub fn (mut c StreamConn) write(bytes []byte) ?int { return c.write_ptr(bytes.data, bytes.len) } // write_str blocks and attempts to write all data -pub fn (mut c StreamConn) write_str(s string) ? { +pub fn (mut c StreamConn) write_str(s string) ?int { return c.write_ptr(s.str, s.len) } diff --git a/vlib/x/openssl/openssl.v b/vlib/x/openssl/openssl.v index 963c51be55..67e330499f 100644 --- a/vlib/x/openssl/openssl.v +++ b/vlib/x/openssl/openssl.v @@ -94,7 +94,7 @@ pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ? { // TODO: Fix option to enable/disable checks for valid // certificates to allow both secure and self signed - // for now the checks are not done at all to comply + // for now the checks are not done at all to comply // to current autobahn tests // C.SSL_CTX_set_verify_depth(s.sslctx, 4) @@ -186,7 +186,7 @@ pub fn (mut s SSLConn) read_into(mut buffer []byte) ?int { } // write number of bytes to SSL connection -pub fn (mut s SSLConn) write(bytes []byte) ? { +pub fn (mut s SSLConn) write(bytes []byte) ?int { unsafe { mut ptr_base := byteptr(bytes.data) mut total_sent := 0 @@ -218,6 +218,7 @@ pub fn (mut s SSLConn) write(bytes []byte) ? { } total_sent += sent } + return total_sent } } diff --git a/vlib/x/websocket/io.v b/vlib/x/websocket/io.v index eba4f66b2d..c34ec02b99 100644 --- a/vlib/x/websocket/io.v +++ b/vlib/x/websocket/io.v @@ -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') } } } diff --git a/vlib/x/websocket/websocket_client.v b/vlib/x/websocket/websocket_client.v index b19f987212..56c79f987a 100644 --- a/vlib/x/websocket/websocket_client.v +++ b/vlib/x/websocket/websocket_client.v @@ -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