Artifact a4fa80eb66eb2ecaf9cd222a1b0126084cf4f5ff6c0e5f09b89ec8cb9fe4b6bb:
- File doc/markdown/refchan.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 16012) [annotate] [blame] [check-ins using]
CommandName: refchan ManualSection: n Version: 8.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- chan(n)
- transchan(n) Keywords:
- API
- channel
- ensemble
- prefix
- reflection Copyright:
- Copyright (c) 2006 Andreas Kupries [email protected] ---
Name
refchan - command handler API of reflected channels
Synopsis
::: {.synopsis} ::: [chan]{.cmd} [create]{.sub} [mode]{.arg} [cmdPrefix]{.arg}
[cmdPrefix]{.ins} [blocking]{.sub} [channel]{.arg} [mode]{.arg} [cmdPrefix]{.ins} [cget]{.sub} [channel]{.arg} [option]{.arg} [cmdPrefix]{.ins} [cgetall]{.sub} [channel]{.arg} [cmdPrefix]{.ins} [configure]{.sub} [channel]{.arg} [option]{.arg} [value]{.arg} [cmdPrefix]{.ins} [finalize]{.sub} [channel]{.arg} [cmdPrefix]{.ins} [initialize]{.sub} [channel]{.arg} [mode]{.arg} [cmdPrefix]{.ins} [read]{.sub} [channel]{.arg} [count]{.arg} [cmdPrefix]{.ins} [seek]{.sub} [channel]{.arg} [offset]{.arg} [base]{.arg} [cmdPrefix]{.ins} [watch]{.sub} [channel]{.arg} [eventspec]{.arg} [cmdPrefix]{.ins} [write]{.sub} [channel]{.arg} [data]{.arg} :::
Description
The Tcl-level handler for a reflected channel has to be a command with subcommands (termed an fIensemblefR, as it is a command such as that created by fBnamespace ensemblefR fBcreatefR, though the implementation of handlers for reflected channel fIis notfR tied to fBnamespace ensemblefRs in any way; see fBEXAMPLEfR below for how to build an fBoo::classfR that supports the API). Note that fIcmdPrefixfR is whatever was specified in the call to fBchan createfR, and may consist of multiple arguments; this will be expanded to multiple words in place of the prefix.
Of all the possible subcommands, the handler fImustfR support fBinitializefR, fBfinalizefR, and fBwatchfR. Support for the other subcommands is optional.
Mandatory subcommands
cmdPrefix fBinitializefR fIchannel modefR : An invocation of this subcommand will be the first call the fIcmdPrefixfR will receive for the specified new fIchannelfR. It is the responsibility of this subcommand to set up any internal data structures required to keep track of the channel and its state. The return value of the method has to be a list containing the names of all subcommands supported by the fIcmdPrefixfR. This also tells the Tcl core which version of the API for reflected channels is used by this command handler. Any error thrown by the method will abort the creation of the channel and no channel will be created. The thrown error will appear as error thrown by fBchan createfR. Any exception other than an fBerrorfR (e.g., fBbreakfR, etc.) is treated as (and converted to) an error. Note: If the creation of the channel was aborted due to failures here, then the fBfinalizefR subcommand will not be called. The fImodefR argument tells the handler whether the channel was opened for reading, writing, or both. It is a list containing any of the strings fBreadfR or fBwritefR. The list may be empty, but will usually contain at least one element. The subcommand must throw an error if the chosen mode is not supported by the fIcmdPrefixfR.
cmdPrefix fBfinalizefR fIchannelfR : An invocation of this subcommand will be the last call the fIcmdPrefixfR will receive for the specified fIchannelfR. It will be generated just before the destruction of the data structures of the channel held by the Tcl core. The command handler fImust notfR access the fIchannelfR anymore in no way. Upon this subcommand being called, any internal resources allocated to this channel must be cleaned up. The return value of this subcommand is ignored. If the subcommand throws an error the command which caused its invocation (usually fBchan closefR) will appear to have thrown this error. Any exception beyond fBerrorfR (e.g., fBbreakfR, etc.) is treated as (and converted to) an error. This subcommand is not invoked if the creation of the channel was aborted during fBinitializefR (See above).
cmdPrefix fBwatchfR fIchannel eventspecfR : This subcommand notifies the fIcmdPrefixfR that the specified fIchannelfR is interested in the events listed in the fIeventspecfR. This argument is a list containing any of fBreadfR and fBwritefR. The list may be empty, which signals that the channel does not wish to be notified of any events. In that situation, the handler should disable event generation completely. Warning: Any return value of the subcommand is ignored. This includes all errors thrown by the subcommand, fBbreakfR, fBcontinuefR, and custom return codes. This subcommand interacts with fBchan posteventfR. Trying to post an event which was not listed in the last call to fBwatchfR will cause fBchan posteventfR to throw an error.
Optional subcommands
cmdPrefix fBreadfR fIchannel countfR : This fIoptionalfR subcommand is called when the user requests data from the channel fIchannelfR. fIcountfR specifies how many fIbytesfR have been requested. If the subcommand is not supported then it is not possible to read from the channel handled by the command. The return value of this subcommand is taken as the requested data fIbytesfR. If the returned data contains more bytes than requested, an error will be signaled and later thrown by the command which performed the read (usually fBgetsfR or fBreadfR). However, returning fewer bytes than requested is acceptable. Note that returning nothing (0 bytes) is a signal to the higher layers that fBEOFfR has been reached on the channel. To signal that the channel is out of data right now, but has not yet reached fBEOFfR, it is necessary to throw the error "EAGAIN", i.e. to either
```
return -code error EAGAIN
```
or
```
error EAGAIN
```
For extensibility any error whose value is a negative integer number will cause the higher layers to set the C-level variable "\fBerrno\fR" to the absolute value of this number, signaling a system error. However, note that the exact mapping between these error numbers and their meanings is operating system dependent.
For example, while on Linux both
```
return -code error -11
```
and
```
error -11
```
are equivalent to the examples above, using the more readable string "EAGAIN", this is not true for BSD, where the equivalent number is -35.
The symbolic string however is the same across systems, and internally translated to the correct number. No other error value has such a mapping to a symbolic string.
If the subcommand throws any other error, the command which caused its invocation (usually \fBgets\fR, or \fBread\fR) will appear to have thrown this error. Any exception beyond \fBerror\fR, (e.g.,\ \fBbreak\fR, etc.) is treated as and converted to an error.
cmdPrefix fBwritefR fIchannel datafR : This fIoptionalfR subcommand is called when the user writes data to the channel fIchannelfR. The fIdatafR argument contains fIbytesfR, not characters. Any type of transformation (EOL, encoding) configured for the channel has already been applied at this point. If this subcommand is not supported then it is not possible to write to the channel handled by the command. The return value of the subcommand is taken as the number of bytes written by the channel. Anything non-numeric will cause an error to be signaled and later thrown by the command which performed the write. A negative value implies that the write failed. Returning a value greater than the number of bytes given to the handler, or zero, is forbidden and will cause the Tcl core to throw an error. To signal that the channel is not able to accept data for writing right now, it is necessary to throw the error "EAGAIN", i.e. to either
```
return -code error EAGAIN
```
or
```
error EAGAIN
```
For extensibility any error whose value is a negative integer number will cause the higher layers to set the C-level variable "\fBerrno\fR" to the absolute value of this number, signaling a system error. However, note that the exact mapping between these error numbers and their meanings is operating system dependent.
For example, while on Linux both
```
return -code error -11
```
and
```
error -11
```
are equivalent to the examples above, using the more readable string "EAGAIN", this is not true for BSD, where the equivalent number is -35.
The symbolic string however is the same across systems, and internally translated to the correct number. No other error value has such a mapping to a symbolic string.
If the subcommand throws any other error the command which caused its invocation (usually \fBputs\fR) will appear to have thrown this error. Any exception beyond \fBerror\fR (e.g.,\ \fBbreak\fR, etc.) is treated as and converted to an error.
cmdPrefix fBseekfR fIchannel offset basefR : This fIoptionalfR subcommand is responsible for the handling of fBchan seekfR and fBchan tellfR requests on the channel fIchannelfR. If it is not supported then seeking will not be possible for the channel. The fIbasefR argument is the same as the equivalent argument of the builtin fBchan seekfR, namely:
start : Seeking is relative to the beginning of the channel.
current : Seeking is relative to the current seek position.
end : Seeking is relative to the end of the channel.
The \fIoffset\fR is an integer number specifying the amount of \fBbytes\fR to seek forward or backward. A positive number should seek forward, and a negative number should seek backward. A channel may provide only limited seeking. For example sockets can seek forward, but not backward.
The return value of the subcommand is taken as the (new) location of the channel, counted from the start. This has to be an integer number greater than or equal to zero. If the subcommand throws an error the command which caused its invocation (usually \fBchan seek\fR, or \fBchan tell\fR) will appear to have thrown this error. Any exception beyond \fBerror\fR (e.g.,\ \fBbreak\fR, etc.) is treated as and converted to an error.
The offset/base combination of 0/\fBcurrent\fR signals a \fBchan tell\fR request, i.e.,\ seek nothing relative to the current location, making the new location identical to the current one, which is then returned.
cmdPrefix fBconfigurefR fIchannel option valuefR : This fIoptionalfR subcommand is for setting the type-specific options of channel fIchannelfR. The fIoptionfR argument indicates the option to be written, and the fIvaluefR argument indicates the value to set the option to. This subcommand will never try to update more than one option at a time; that is behavior implemented in the Tcl channel core. The return value of the subcommand is ignored. If the subcommand throws an error the command which performed the (re)configuration or query (usually fBfconfigurefR or fBchan configurefR) will appear to have thrown this error. Any exception beyond fBerrorfR (e.g., fBbreakfR, etc.) is treated as and converted to an error.
cmdPrefix fBcgetfR fIchannel optionfR : This fIoptionalfR subcommand is used when reading a single type-specific option of channel fIchannelfR. If this subcommand is supported then the subcommand fBcgetallfR must be supported as well. The subcommand should return the value of the specified fIoptionfR. If the subcommand throws an error, the command which performed the (re)configuration or query (usually fBfconfigurefR or fBchan configurefR) will appear to have thrown this error. Any exception beyond fIerrorfR (e.g., fBbreakfR, etc.) is treated as and converted to an error.
cmdPrefix fBcgetallfR fIchannelfR : This fIoptionalfR subcommand is used for reading all type-specific options of channel fIchannelfR. If this subcommand is supported then the subcommand fBcgetfR has to be supported as well. The subcommand should return a list of all options and their values. This list must have an even number of elements. If the subcommand throws an error the command which performed the (re)configuration or query (usually fBfconfigurefR or fBchan configurefR) will appear to have thrown this error. Any exception beyond fBerrorfR (e.g., fBbreakfR, etc.) is treated as and converted to an error.
cmdPrefix fBblockingfR fIchannel modefR : This fIoptionalfR subcommand handles changes to the blocking mode of the channel fIchannelfR. The fImodefR is a boolean flag. A true value means that the channel has to be set to blocking, and a false value means that the channel should be non-blocking. The return value of the subcommand is ignored. If the subcommand throws an error the command which caused its invocation (usually fBfconfigurefR or fBchan configurefR) will appear to have thrown this error. Any exception beyond fBerrorfR (e.g., fBbreakfR, etc.) is treated as and converted to an error.
cmdPrefix fBtruncatefR fIchannel lengthfR : This fIoptionalfR subcommand handles changing the length of the underlying data stream for the channel fIchannelfR. Its length gets set to fIlengthfR. If the subcommand throws an error the command which caused its invocation (usually fBchan truncatefR) will appear to have thrown this error. Any exception beyond fBerrorfR (e.g., fBbreakfR, etc.) is treated as and converted to an error.
Notes
Some of the functions supported in channels defined in Tcl's C interface are not available to channels reflected to the Tcl level.
The function fBTcl_DriverGetHandleProcfR is not supported; i.e., reflected channels do not have OS specific handles.
The function fBTcl_DriverHandlerProcfR is not supported. This driver function is relevant only for stacked channels, i.e., transformations. Reflected channels are always base channels, not transformations.
The function fBTcl_DriverFlushProcfR is not supported. This is because the current generic I/O layer of Tcl does not use this function anywhere at all. Therefore support at the Tcl level makes no sense either. This may be altered in the future (through extending the API defined here and changing its version number) should the function be used at some time in the future.
Example
This demonstrates how to make a channel that reads from a string.
oo::class create stringchan {
variable data pos
constructor {string {encoding {}}} {
if {$encoding eq ""} {set encoding [encoding system]}
set data [encoding convertto $encoding $string]
set pos 0
}
method initialize {ch mode} {
return "initialize finalize watch read seek"
}
method finalize {ch} {
my destroy
}
method watch {ch events} {
# Must be present but we ignore it because we do not
# post any events
}
# Must be present on a readable channel
method read {ch count} {
set d [string range $data $pos [expr {$pos+$count-1}]]
incr pos [string length $d]
return $d
}
# This method is optional, but useful for the example below
method seek {ch offset base} {
switch $base {
start {
set pos $offset
}
current {
incr pos $offset
}
end {
set pos [string length $data]
incr pos $offset
}
}
if {$pos < 0} {
set pos 0
} elseif {$pos > [string length $data]} {
set pos [string length $data]
}
return $pos
}
}
# Now we create an instance...
set string "The quick brown fox jumps over the lazy dog.\\n"
set ch [chan create read [stringchan new $string]]
puts [gets $ch]; # Prints the whole string
seek $ch -5 end;
puts [read $ch]; # Prints just the last word