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

  1. # coroutine-enabled read
  2. proc read {socket size} {
  3. # read a chunk of $size bytes
  4. Debug.HttpdLow {[info coroutine] reading}
  5. set chunk ""
  6. set gone [catch {chan eof $socket} eof]
  7.  
  8. while {$size && !$gone && !$eof} {
  9. set result [yield]
  10. set chunklet [chan read $socket $size]
  11. incr size -[string length $chunklet]
  12. append chunk $chunklet
  13. set gone [catch {chan eof $socket} eof]
  14. }
  15.  
  16. set gone [catch {chan eof $socket} eof]
  17. if {$gone || $eof} {
  18. Debug.HttpdLow {[info coroutine] eof in read}
  19. terminate "eof in reading entity - $size" ;# check the socket for closure
  20. }
  21.  
  22. # return the chunk
  23. Debug.HttpdLow {[info coroutine] read: '$chunk'}
  24. return $chunk
  25. }