aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/net/tcp_read_line.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/tcp_read_line.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/tcp_read_line.v')
-rw-r--r--v_windows/v/old/vlib/net/tcp_read_line.v55
1 files changed, 55 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/net/tcp_read_line.v b/v_windows/v/old/vlib/net/tcp_read_line.v
new file mode 100644
index 0000000..7641866
--- /dev/null
+++ b/v_windows/v/old/vlib/net/tcp_read_line.v
@@ -0,0 +1,55 @@
+module net
+
+const (
+ crlf = '\r\n'
+ msg_peek = 0x02
+ max_read = 400
+)
+
+// read_line is a *simple*, *non customizable*, blocking line reader.
+// It will *always* return a line, ending with CRLF, or just '', on EOF.
+// NB: if you want more control over the buffer, please use a buffered IO
+// reader instead: `io.new_buffered_reader({reader: io.make_reader(con)})`
+pub fn (mut con TcpConn) read_line() string {
+ mut buf := [net.max_read]byte{} // where C.recv will store the network data
+ mut res := '' // The final result, including the ending \n.
+ for {
+ mut line := '' // The current line. Can be a partial without \n in it.
+ n := C.recv(con.sock.handle, &buf[0], net.max_read - 1, net.msg_peek | msg_nosignal)
+ if n == -1 {
+ return res
+ }
+ if n == 0 {
+ return res
+ }
+ buf[n] = `\0`
+ mut eol_idx := -1
+ for i in 0 .. n {
+ if int(buf[i]) == `\n` {
+ eol_idx = i
+ // Ensure that tos_clone(buf) later,
+ // will return *only* the first line (including \n),
+ // and ignore the rest
+ buf[i + 1] = `\0`
+ break
+ }
+ }
+ line = unsafe { tos_clone(&buf[0]) }
+ if eol_idx > 0 {
+ // At this point, we are sure that recv returned valid data,
+ // that contains *at least* one line.
+ // Ensure that the block till the first \n (including it)
+ // is removed from the socket's receive queue, so that it does
+ // not get read again.
+ C.recv(con.sock.handle, &buf[0], eol_idx + 1, msg_nosignal)
+ res += line
+ break
+ }
+ // recv returned a buffer without \n in it .
+ C.recv(con.sock.handle, &buf[0], n, msg_nosignal)
+ res += line
+ res += net.crlf
+ break
+ }
+ return res
+}