From 8faa0cb2805808e9f350acdc24d1f4de875f5aa3 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 23 Feb 2022 08:34:03 +1000 Subject: 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 --- examples/nonblocking-gets.tcl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/nonblocking-gets.tcl (limited to 'examples/nonblocking-gets.tcl') 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 +} -- cgit v1.1