aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/net/websocket/io.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/vlib/net/websocket/io.v
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/old/vlib/net/websocket/io.v')
-rw-r--r--v_windows/v/old/vlib/net/websocket/io.v100
1 files changed, 100 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/net/websocket/io.v b/v_windows/v/old/vlib/net/websocket/io.v
new file mode 100644
index 0000000..5408a4e
--- /dev/null
+++ b/v_windows/v/old/vlib/net/websocket/io.v
@@ -0,0 +1,100 @@
+module websocket
+
+import net
+import time
+
+// socket_read reads from socket into the provided buffer
+fn (mut ws Client) socket_read(mut buffer []byte) ?int {
+ lock {
+ if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
+ return error('socket_read: trying to read a closed socket')
+ }
+ if ws.is_ssl {
+ r := ws.ssl_conn.read_into(mut buffer) ?
+ return r
+ } else {
+ for {
+ r := ws.conn.read(mut buffer) or {
+ if err.code == net.err_timed_out_code {
+ continue
+ }
+ return err
+ }
+ return r
+ }
+ }
+ }
+ return none
+}
+
+// socket_read reads from socket into the provided byte pointer and length
+fn (mut ws Client) socket_read_ptr(buf_ptr &byte, len int) ?int {
+ lock {
+ if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
+ return error('socket_read_ptr: trying to read a closed socket')
+ }
+ if ws.is_ssl {
+ r := ws.ssl_conn.socket_read_into_ptr(buf_ptr, len) ?
+ return r
+ } else {
+ for {
+ r := ws.conn.read_ptr(buf_ptr, len) or {
+ if err.code == net.err_timed_out_code {
+ continue
+ }
+ return err
+ }
+ return r
+ }
+ }
+ }
+ return none
+}
+
+// socket_write writes the provided byte array to the socket
+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 {
+ return ws.ssl_conn.write(bytes)
+ } else {
+ for {
+ n := ws.conn.write(bytes) or {
+ if err.code == net.err_timed_out_code {
+ continue
+ }
+ return err
+ }
+ return n
+ }
+ panic('reached unreachable code')
+ }
+ }
+}
+
+// shutdown_socket shuts down the socket properly when connection is closed
+fn (mut ws Client) shutdown_socket() ? {
+ ws.debug_log('shutting down socket')
+ if ws.is_ssl {
+ ws.ssl_conn.shutdown() ?
+ } else {
+ ws.conn.close() ?
+ }
+}
+
+// dial_socket connects tcp socket and initializes default configurations
+fn (mut ws Client) dial_socket() ?&net.TcpConn {
+ tcp_address := '$ws.uri.hostname:$ws.uri.port'
+ mut t := net.dial_tcp(tcp_address) ?
+ optval := int(1)
+ t.sock.set_option_int(.keep_alive, optval) ?
+ t.set_read_timeout(30 * time.second)
+ t.set_write_timeout(30 * time.second)
+ if ws.is_ssl {
+ ws.ssl_conn.connect(mut t, ws.uri.hostname) ?
+ }
+ return t
+}