Posted to tcl by colin at Sun Nov 15 12:04:19 GMT 2009view pretty

# coroutine-enabled read
    proc read {socket size} {
	# read a chunk of $size bytes
	Debug.HttpdLow {[info coroutine] reading}
	set chunk ""
	set gone [catch {chan eof $socket} eof]

	while {$size && !$gone && !$eof} {
       	    set result [yield]
	    set chunklet [chan read $socket $size]
	    incr size -[string length $chunklet]
	    append chunk $chunklet
	    set gone [catch {chan eof $socket} eof]
	}
	
	set gone [catch {chan eof $socket} eof]
	if {$gone || $eof} {
	    Debug.HttpdLow {[info coroutine] eof in read}
	    terminate "eof in reading entity - $size"	;# check the socket for closure
	}
	
	# return the chunk
	Debug.HttpdLow {[info coroutine] read: '$chunk'}
    	return $chunk
    }