aboutsummaryrefslogtreecommitdiff
path: root/examples/nonblocking-gets.tcl
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2022-02-23 08:34:03 +1000
committerSteve Bennett <steveb@workware.net.au>2022-02-23 12:30:38 +1000
commit8faa0cb2805808e9f350acdc24d1f4de875f5aa3 (patch)
tree62eaab64548a06b6c8475f4c2de2fb2bd628f8a9 /examples/nonblocking-gets.tcl
parent043325531af41ba102ff79ea4e251dfc201e287b (diff)
downloadjimtcl-8faa0cb2805808e9f350acdc24d1f4de875f5aa3.zip
jimtcl-8faa0cb2805808e9f350acdc24d1f4de875f5aa3.tar.gz
jimtcl-8faa0cb2805808e9f350acdc24d1f4de875f5aa3.tar.bz2
aio: gets: Improve behaviour for non-blocking streams
Previously calling gets on a non-blocking stream could easily result in a partial line. Now if a partial line is read, return zero/empty to indicate that nothing is available while storing the partial line. The next call to gets (typically within a readable script) will continue appending to the previous partial line until a complete line can be returned. Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'examples/nonblocking-gets.tcl')
-rw-r--r--examples/nonblocking-gets.tcl38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/nonblocking-gets.tcl b/examples/nonblocking-gets.tcl
new file mode 100644
index 0000000..a9ac4bd
--- /dev/null
+++ b/examples/nonblocking-gets.tcl
@@ -0,0 +1,38 @@
+#!/usr/bin/env jimsh
+
+# Tests that 'gets' on a non-blocking socket
+# does not return partial lines
+
+lassign [socket pipe] r w
+
+if {[os.fork] == 0} {
+ # The child will be our client
+ $r close
+ # Output increasingly long lines
+ loop i 10000 {
+ $w puts [string repeat a $i]
+ }
+} else {
+ # The server reads lines with gets.
+ # Each one should be one longer than the last
+ $w close
+
+ set exp 0
+ $r ndelay 1
+ $r readable {
+ while {[$r gets buf] >= 0} {
+ set len [string length $buf]
+ if {$len != $exp} {
+ puts "Read line of length $len but expected $exp"
+ incr done
+ break
+ }
+ incr exp
+ }
+ if {[$r eof]} {
+ incr done
+ }
+ }
+
+ vwait done
+}