Jim Tcl
Check-in [d0d277697e]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:examples/tip.tcl: Use 'writable' when sending output

Avoids situations where the output might block

Signed-off-by: Steve Bennett <steveb@workware.net.au>

Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:d0d277697e8f5bef7847e2ce9aff50e2dfac1142
User & Date: steveb@workware.net.au 2017-09-21 06:40:25
Context
2017-09-21
06:40
examples/tip.tcl: Use 'writable' when sending output

Avoids situations where the output might block

Signed-off-by: Steve Bennett <steveb@workware.net.au> Leaf check-in: d0d277697e user: steveb@workware.net.au tags: trunk

06:40
configure: Remove reference to ensemble extension

It is not pushed to mainline yet.

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: 4d2900cfd6 user: steveb@workware.net.au tags: trunk

Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to examples/tip.tcl.

102
103
104
105
106
107
108


109
110





111
112



113
























114
115
116
117
118
119
120
121

122
123
124
125
126
127
128
129
130
131
132
133
134
...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
stdin tty input raw
stdin ndelay 1

stdout tty output raw
stdout buffering none

set status ""



# I/O loop






set tilde 0




























$f readable {
	set c [$f read]
	if {[$f eof]} {
		set status "$device: disconnected"
		incr done
	} else {
		stdout puts -nonewline $c
	}

}

proc tilde_timeout {} {
	global tilde f
	if {$tilde} {
		$f puts -nonewline ~
		set tilde 0
	}
}

stdin readable {
	set c [stdin read]
	# may receive more than one char here, but only need to consider
................................................................................
		if {$tilde} {
			after cancel tilde_timeout
			set tilde 0
			if {$c eq "."} {
				incr done
				return
			}
			$f puts -nonewline ~
		}
		$f puts -nonewline $c
	}
}

vwait done

# restore previous settings
stdin tty {*}$stdin_save
stdout tty {*}$stdout_save

puts $status







>
>

<
>
>
>
>
>

<
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





|
<

>





|







 







|

|










102
103
104
105
106
107
108
109
110
111

112
113
114
115
116
117

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
stdin tty input raw
stdin ndelay 1

stdout tty output raw
stdout buffering none

set status ""
set tilde 0
set tosend {}


# To avoid sending too much data and blocking,
# this sends str in chunks of 1000 bytes via writable
proc output-on-writable {fh str} {
	# Add it to the buffer to send
	append ::tosend($fh) $str


	if {[string length [$fh writable]] == 0} {
		# Start the writable event handler
		$fh writable [list output-is-writable $fh]
	}
}

# This is the writable callback
proc output-is-writable {fh} {
	global tosend
	set buf $tosend($fh)
	if {[string bytelength $buf] >= 1000} {
		set tosend($fh) [string byterange $buf 1000 end]
		set buf [string byterange $buf 0 999]
	} else {
		set tosend($fh) {}
		# All sent, so cancel the writable event handler
		$fh writable {}
	}
	$fh puts -nonewline $buf
}

proc bgerror {args} {
	set status $args
	incr ::done
}

# I/O loop

$f readable {
	set c [$f read]
	if {[$f eof]} {
		set status "$device: disconnected"
		incr done
		break

	}
	output-on-writable stdout $c
}

proc tilde_timeout {} {
	global tilde f
	if {$tilde} {
		output-on-writable $f ~
		set tilde 0
	}
}

stdin readable {
	set c [stdin read]
	# may receive more than one char here, but only need to consider
................................................................................
		if {$tilde} {
			after cancel tilde_timeout
			set tilde 0
			if {$c eq "."} {
				incr done
				return
			}
			output-on-writable $f ~
		}
		output-on-writable $f $c
	}
}

vwait done

# restore previous settings
stdin tty {*}$stdin_save
stdout tty {*}$stdout_save

puts $status