| 1 | module websocket |
| 2 | |
| 3 | import net |
| 4 | import net.http |
| 5 | |
| 6 | // socket_read reads from socket into the provided buffer |
| 7 | fn (mut ws Client) socket_read(mut buffer []u8) !int { |
| 8 | lock { |
| 9 | if ws.get_state() in [.closed, .closing] || ws.conn.sock.handle <= 1 { |
| 10 | return error('socket_read: trying to read a closed socket') |
| 11 | } |
| 12 | if ws.is_ssl { |
| 13 | r := ws.ssl_conn.read(mut buffer) or { return error('none') } |
| 14 | return r |
| 15 | } else { |
| 16 | r := ws.conn.read(mut buffer) or { return error('none') } |
| 17 | return r |
| 18 | } |
| 19 | } |
| 20 | return error('none') |
| 21 | } |
| 22 | |
| 23 | // socket_read reads from socket into the provided byte pointer and length |
| 24 | fn (mut ws Client) socket_read_ptr(buf_ptr &u8, len int) !int { |
| 25 | lock { |
| 26 | if ws.get_state() in [.closed, .closing] || ws.conn.sock.handle <= 1 { |
| 27 | return error('socket_read_ptr: trying to read a closed socket') |
| 28 | } |
| 29 | if ws.is_ssl { |
| 30 | r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len)! |
| 31 | return r |
| 32 | } else { |
| 33 | r := ws.conn.read_ptr(buf_ptr, len)! |
| 34 | return r |
| 35 | } |
| 36 | } |
| 37 | return error('none') |
| 38 | } |
| 39 | |
| 40 | // socket_write writes the provided byte array to the socket |
| 41 | fn (mut ws Client) socket_write(bytes []u8) !int { |
| 42 | lock { |
| 43 | if ws.get_state() == .closed || ws.conn.sock.handle <= 1 { |
| 44 | ws.debug_log('socket_write: Socket already closed') |
| 45 | return error('socket_write: trying to write on a closed socket') |
| 46 | } |
| 47 | if ws.is_ssl { |
| 48 | return ws.ssl_conn.write(bytes) |
| 49 | } else { |
| 50 | for { |
| 51 | n := ws.conn.write(bytes) or { |
| 52 | if err.code() == net.err_timed_out_code { |
| 53 | continue |
| 54 | } |
| 55 | return err |
| 56 | } |
| 57 | return n |
| 58 | } |
| 59 | panic('reached unreachable code') |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // shutdown_socket shuts down the socket properly when connection is closed |
| 65 | fn (mut ws Client) shutdown_socket() ! { |
| 66 | ws.debug_log('shutting down socket') |
| 67 | if ws.is_ssl { |
| 68 | ws.ssl_conn.shutdown()! |
| 69 | } else { |
| 70 | ws.conn.close()! |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // dial_socket connects tcp socket and initializes default configurations |
| 75 | fn (mut ws Client) dial_socket() !&net.TcpConn { |
| 76 | tcp_address := '${ws.uri.hostname}:${ws.uri.port}' |
| 77 | mut t := if ws.proxy_url == '' { |
| 78 | net.dial_tcp(tcp_address)! |
| 79 | } else { |
| 80 | http.dial_tcp_via_proxy(ws.proxy_url, tcp_address)! |
| 81 | } |
| 82 | optval := int(1) |
| 83 | t.sock.set_option_int(.keep_alive, optval)! |
| 84 | t.set_read_timeout(ws.read_timeout) |
| 85 | t.set_write_timeout(ws.write_timeout) |
| 86 | if ws.is_ssl { |
| 87 | ws.ssl_conn.connect(mut t, ws.uri.hostname)! |
| 88 | } |
| 89 | return t |
| 90 | } |
| 91 | |