net/openssl/websocket: implement io.Writer (#8980)
parent
f67bff1696
commit
d0a64f2da7
|
@ -74,11 +74,11 @@ pub fn new() FTP {
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut zftp FTP) write(data string) ? {
|
fn (mut zftp FTP) write(data string) ?int {
|
||||||
$if debug {
|
$if debug {
|
||||||
println('FTP.v >>> $data')
|
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) {
|
fn (mut zftp FTP) read() ?(int, string) {
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn (mut c TcpConn) close() ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_ptr blocks and attempts to write all data
|
// 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 ? {
|
$if trace_tcp ? {
|
||||||
eprintln(
|
eprintln(
|
||||||
'>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
|
'>>> 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
|
total_sent += sent
|
||||||
}
|
}
|
||||||
|
return total_sent
|
||||||
}
|
}
|
||||||
return none
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write blocks and attempts to write all data
|
// 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)
|
return c.write_ptr(bytes.data, bytes.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_str blocks and attempts to write all data
|
// 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)
|
return c.write_ptr(s.str, s.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,23 +45,23 @@ fn resolve_wrapper(raddr string) ?Addr {
|
||||||
return x
|
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 }
|
remote := c.sock.remote() or { return err_no_udp_remote }
|
||||||
return c.write_to_ptr(remote, b, len)
|
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)
|
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)
|
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)
|
res := C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len)
|
||||||
if res >= 0 {
|
if res >= 0 {
|
||||||
return none
|
return res
|
||||||
}
|
}
|
||||||
code := error_code()
|
code := error_code()
|
||||||
if code == int(error_ewouldblock) {
|
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
|
// 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)
|
return c.write_to_ptr(addr, buf.data, buf.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_to_string blocks and writes the buf to the remote addr specified
|
// 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)
|
return c.write_to_ptr(addr, s.str, s.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ pub fn (mut c StreamConn) close() ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_ptr blocks and attempts to write all data
|
// 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 ? {
|
$if trace_unix ? {
|
||||||
eprintln(
|
eprintln(
|
||||||
'>>> StreamConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
|
'>>> 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
|
total_sent += sent
|
||||||
}
|
}
|
||||||
|
return total_sent
|
||||||
}
|
}
|
||||||
return none
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write blocks and attempts to write all data
|
// 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)
|
return c.write_ptr(bytes.data, bytes.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_str blocks and attempts to write all data
|
// 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)
|
return c.write_ptr(s.str, s.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -186,7 +186,7 @@ pub fn (mut s SSLConn) read_into(mut buffer []byte) ?int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write number of bytes to SSL connection
|
// 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 {
|
unsafe {
|
||||||
mut ptr_base := byteptr(bytes.data)
|
mut ptr_base := byteptr(bytes.data)
|
||||||
mut total_sent := 0
|
mut total_sent := 0
|
||||||
|
@ -218,6 +218,7 @@ pub fn (mut s SSLConn) write(bytes []byte) ? {
|
||||||
}
|
}
|
||||||
total_sent += sent
|
total_sent += sent
|
||||||
}
|
}
|
||||||
|
return total_sent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
// 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 {
|
lock {
|
||||||
if ws.state == .closed || ws.conn.sock.handle <= 1 {
|
if ws.state == .closed || ws.conn.sock.handle <= 1 {
|
||||||
ws.debug_log('socket_write: Socket allready closed')
|
ws.debug_log('socket_write: Socket allready closed')
|
||||||
return error('socket_write: trying to write on a closed socket')
|
return error('socket_write: trying to write on a closed socket')
|
||||||
}
|
}
|
||||||
if ws.is_ssl {
|
if ws.is_ssl {
|
||||||
ws.ssl_conn.write(bytes) ?
|
return ws.ssl_conn.write(bytes)
|
||||||
} else {
|
} else {
|
||||||
for {
|
for {
|
||||||
ws.conn.write(bytes) or {
|
n := ws.conn.write(bytes) or {
|
||||||
if errcode == net.err_timed_out_code {
|
if errcode == net.err_timed_out_code {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return error(err)
|
return error(err)
|
||||||
}
|
}
|
||||||
return
|
return n
|
||||||
}
|
}
|
||||||
|
panic('reached unreachable code')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -226,7 +226,7 @@ pub fn (mut ws Client) pong() ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_ptr writes len bytes provided a byteptr with a websocket messagetype
|
// 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')
|
// ws.debug_log('write_ptr code: $code')
|
||||||
if ws.state != .open || ws.conn.sock.handle < 1 {
|
if ws.state != .open || ws.conn.sock.handle < 1 {
|
||||||
// todo: send error here later
|
// 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
|
frame_buf[header_len + i] ^= masking_key[i % 4] & 0xff
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ws.socket_write(frame_buf) ?
|
written_len := ws.socket_write(frame_buf) ?
|
||||||
unsafe {
|
unsafe {
|
||||||
frame_buf.free()
|
frame_buf.free()
|
||||||
masking_key.free()
|
masking_key.free()
|
||||||
header.free()
|
header.free()
|
||||||
}
|
}
|
||||||
|
return written_len
|
||||||
}
|
}
|
||||||
|
|
||||||
// write writes a byte array with a websocket messagetype to socket
|
// write writes a byte array with a websocket messagetype to socket
|
||||||
pub fn (mut ws Client) write(bytes []byte, code OPCode) ? {
|
pub fn (mut ws Client) write(bytes []byte, code OPCode) ?int {
|
||||||
ws.write_ptr(byteptr(bytes.data), bytes.len, code) ?
|
return ws.write_ptr(byteptr(bytes.data), bytes.len, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_str, writes a string with a websocket texttype to socket
|
// write_str, writes a string with a websocket texttype to socket
|
||||||
pub fn (mut ws Client) write_str(str string) ? {
|
pub fn (mut ws Client) write_str(str string) ?int {
|
||||||
ws.write_ptr(str.str, str.len, .text_frame) ?
|
return ws.write_ptr(str.str, str.len, .text_frame)
|
||||||
}
|
}
|
||||||
|
|
||||||
// close closes the websocket connection
|
// close closes the websocket connection
|
||||||
|
|
Loading…
Reference in New Issue