Depreciated feature: the transfer is treated as a binary transfer, if the encoding profile is set to “tcl8” and the input encoding matches the output encoding. In this case, eventual encoding errors are not handled. An eventually given size is in bytes in this case.
You are not allowed to do other input operations with inchan, or output operations with outchan, during a background fcopy. The converse is entirely legitimate, as exhibited by the bidirectional fcopy example below.
If either inchan or outchan get closed while the copy is in progress, the current copy is stopped and the command callback is not made. If inchan is closed, then all data already queued for outchan is written out.
Note that inchan can become readable during a background copy. You should turn off any fileevent handlers during a background copy so those handlers do not interfere with the copy. Any wrong-sided I/O attempted (by a fileevent handler or otherwise) will get a “channel busy” error.
Fcopy may throw encoding errors (error code EILSEQ), if input or output channel is configured to the “strict” encoding profile.
If an encoding error arises on the input channel, any data before the error byte is written to the output channel. The input file pointer is located just before the values causing the encoding error. Error inspection or recovery is possible by changing the encoding parameters and invoking a file command (read, fcopy).
If an encoding error arises on the output channel, the erroneous data is lost. To make the difference between the input error case and the output error case, only the error message may be inspected (read or write), as both throw the error code EILSEQ.
fconfigure $in -translation binary fconfigure $out -translation binary fcopy $in $out
This second example shows how the callback gets passed the number of bytes transferred. It also uses vwait to put the application into the event loop. Of course, this simplified example could be done without the command callback.
proc Cleanup {in out bytes {error {}}} { global total set total $bytes close $in close $out if {[string length $error] != 0} { # error occurred during the copy } } set in [open $file1] set out [socket $server $port] fcopy $in $out -command [list Cleanup $in $out] vwait total
The third example copies in chunks and tests for end of file in the command callback.
proc CopyMore {in out chunk bytes {error {}}} { global total done incr total $bytes if {([string length $error] != 0) || [eof $in]} { set done $total close $in close $out } else { fcopy $in $out -size $chunk \ -command [list CopyMore $in $out $chunk] } } set in [open $file1] set out [socket $server $port] set chunk 1024 set total 0 fcopy $in $out -size $chunk \ -command [list CopyMore $in $out $chunk] vwait done
The fourth example starts an asynchronous, bidirectional fcopy between two sockets. Those could also be pipes from two [open "|hal 9000" r+] (though their conversation would remain secret to the script, since all four fileevent slots are busy).
set flows 2 proc Done {dir args} { global flows done puts "$dir is over." incr flows -1 if {$flows<=0} {set done 1} } fcopy $sok1 $sok2 -command [list Done UP] fcopy $sok2 $sok1 -command [list Done DOWN] vwait done