Rchan allows channels to be implemented in Tcl

The "rchan" command is now part of CritLib [L1 ] where it is called "rechan".


DKF: Tcl 8.5 supersedes (and indirectly builds on) this work with chan create.


This package defines an "rchan" command, which lets you create Tcl-style channels that are completely handled by Tcl scripts, instead of C code.

The rchan extension itself is written in C, and requires Critcl to turn it into an extension (on the fly)

Rchan must be called with two arguments to create a new channel:

    rchan cmd mode


    cmd = the name of the "callback" that will process requests
    mode = the open mode:
     2 is read only (not thoroughly tested yet)
     4 is write only (not thoroughly tested yet)
     6 is R/W, the normal case

The callback cmd can be one or more words (i.e. a list). Depending on the operation, a request name and some args will be tacked on before invoking it. The following requests are implemented:

$cmd seek $chan <offset> ?<seekmode>?called when a "seek $chan <offset> ?<seekmode>?" is done
$cmd read $chan <count>called when a "read $chan <count>" request is made
$cmd write $chan <data>called when a "write $chan <data>" request is made

Other operations are not supported (close, for example, so the caller must keep track of this channel and clean up as appropriate - which is messy).

Rchan is not all that capable, it does not work well with fileevents, and in general it definitely could use some re-work and modernization. But it has been invaluable in TclKit to implement its database-backed VFS design.

Without some sort of "reflection", the Virtual File System (whether the original one by Matt Newman or the C-based on in the core by Vince Darley) could not have been implemented. It is hoped that one day, some sort of channel reflection becomes part of standard Tcl, and included in the core.

Nov 2003 status - close is now also tracked, and file-event friendliness is ok now. (This is not new, but as this page appears to have been edited recently, I thought I'd update the facts about it as well) -jcw


PWQ 19 Feb 05, surely you jest, mode = 2,4,6?. You really mean mode = r,w,r+||w+ do you not?

Zarutian 7. mars 2005: it gives an error indicating that it expected an integer but not a char in every instance of r,w,r+||w+. I suspect the integer is treated like a set of binary flags of (to us) an unknown length.

PWQ, my comment wasn't a bug report, it was to say WTF?, this is TCL not C, we use strings not numbers for parameters. That is to say , IMO the code should use string arguments to open and not numbers.

Zarutian 4. june 2005: I am as perplexed as you but this is what the command accepts as parameters.

11jun05 jcw - Heh, so it took 3 years to expose my craziness, eh? ;) - I'm amazed by this response, actually, since it takes just one line of Tcl to wrap this logic into whatever API you prefer. Let me reveal a secret: I don't code in C to create a nice Tcl API: I use Tcl for that. This isn't flame bait, but you are welcome to point to the above as proof (at last!) that everything I do is madness <wink>