1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
|
Originally at http://wiki.tcl.tk/2701, originally at SF. Belongs here.
**TODO**: Reformat for Fossil Wiki Markup, or Markdown.
Currently in Tcler's Wiki Markup Format.
```
HOWTO
A. Kupries
TclIODriver Andreas Computer Laboratories
(Me, myself and I)
November 14, 2000
HOWTO
A. Kupries
TclIODriver Andreas Computer Laboratories
(Me, myself and I)
November 14, 2000
```
'''The Tcl I/O system as seen by a driver (channel type)'''
The Tcl I/O system as seen by a driver (channel type)
# Abstract
This document describes the I/O system used in the Tcl core as it is seen from a driver implementing a channel type.
# Table of Contents
|
| ︙ | | |
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
-
+
-
+
|
# 1. Introduction
The main concept of the I/O system used by the Tcl core is the abstract notion
of channels unifying different paths for communication and the accompanying
split of this subsystem into two layers, one generic in nature, the other
handling the specialities of the various communication channels.
handling the specialties of the various communication channels.
It is this second layer which is the home of the drivers implementing channel
types and thus bridging the gap between the generic layer and the operating
system providing the actual facilities for communication. His interface to the
generic layer is what we will describe here.
Before embarking on this task some other things to note before. In the
beginning of the I/O system were there only drivers for things like files,
pipes and sockets. But with the inclusion of the stacked channel patch into
the core in 8.2 we now have the situation that two different types of drivers
can be written, one like the ones mentioned before, i.e. base (fundamental,
bottom) drivers, and transformations (also called filtering channels). Both
types will be described in this document but thereas the properties regarding
types will be described in this document but whereas the properties regarding
base drivers are valid across the various versions of the core the statements
regarding transformations will apply only to Tcl 8.4 and beyond. The reason
for this restriction is that the interface to transformations (and their
semantics) differ considerably between the various versions of the core and
while documenting the differences is not impossible (only tedious) I am
currently not in the mood for this boring task. People who want to know how to
support a transformation across version are hereby directed to take a look at
|
| ︙ | | |
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
-
+
|
interest to a channel driver.
A principal API used by all drivers is
* Tcl_NotifyChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
which allows a driver to communicate with the notification/event subsystem and
to post events when he is readable or writeable.
to post events when he is readable or writable.
For the creation of new channels two different APIs are available, one for
each type of drivers, base and transformation:
* Tcl_CreateChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm] and
* Tcl_StackChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm]
|
| ︙ | | |
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
-
+
+
+
-
+
-
+
-
+
|
the core will just pass the reference around. This allows us to associate the
specific state of the driver with the channel.
The following information should be present in the instance data, not
necessarily under the name I gave them, you are free to choose your own
identifiers:
''Tcl_Channel channel'';: is a backlink from the instanceData to the channel. Without this link the driver will be unable to access and manipulate its channel as all driver procedures are called only with the instance data as argument. The token to store is the result value of Tcl_CreateChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm] (or Tcl_StackChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm]). <<br>><<br>>All transformations need access to the channel below them so that they are able read and/or write from it. No additional item is required, just call Tcl_GetStackedChannel (Section 3.4) with this item as argument to obtain the necessary token.
__Tcl_Channel channel__: is a back-link from the instanceData to the channel. Without this link the driver will be unable to access and manipulate its channel as all driver procedures are called only with the instance data as argument. The token to store is the result value of Tcl_CreateChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm] (or Tcl_StackChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm]).
All transformations need access to the channel below them so that they are able read and/or write from it. No additional item is required, just call Tcl_GetStackedChannel (Section 3.4) with this item as argument to obtain the necessary token.
''Tcl_TimerToken timer'';: Such a timer is necessary if the driver is able to buffer processed data the generic I/O layer has no knowledge of. It will be used to flush out such data. See Section 3.5.9 for more explanations. Transformations usually do such buffering.
__Tcl_TimerToken timer__: Such a timer is necessary if the driver is able to buffer processed data the generic I/O layer has no knowledge of. It will be used to flush out such data. See Section 3.5.9 for more explanations. Transformations usually do such buffering.
''int flags'';: Transformations have to remember the current blocking mode to handle EOF on input right. See Section 3.5.7 for more explanations. Base channels on the other hand usually don't have a need for this as they can propagate this information to the operating system. An exception are channel types like memchan[http://www.purl.org/NET/akupries/soft/memchan/] which hold all their information in memory.
__int flags__: Transformations have to remember the current blocking mode to handle EOF on input right. See Section 3.5.7 for more explanations. Base channels on the other hand usually don't have a need for this as they can propagate this information to the operating system. An exception are channel types like memchan[http://www.purl.org/NET/akupries/soft/memchan/] which hold all their information in memory.
''int mask'';: Transformations have to remember the current interest in events; see Section 3.5.9 for more explanations. Other channel types may use this too to detect and skip calls which do not change the mask.
__int mask__: Transformations have to remember the current interest in events; see Section 3.5.9 for more explanations. Other channel types may use this too to detect and skip calls which do not change the mask.
## 3.2 Creation of channels
Every channel has to have a creation command at the tcl level and an
equivalent procedure at the C-level. Depending on the type of the channel the
procedure has to use either
Tcl_CreateChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm] or
|
| ︙ | | |
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
-
+
-
-
-
+
+
-
+
-
+
-
+
-
+
-
-
-
+
-
+
-
-
+
-
+
-
-
+
+
-
+
|
* create a name for the channel, like 'sockXX', 'fileN', etc. and
* initialize the specific channel structure.
After the creation of the channel it might be necessary to configure to
configure it. For example it might be non-blocking by default. Note that the
backlink to the channel has to be initialized with the result of the call to
back-link to the channel has to be initialized with the result of the call to
Tcl_CreateChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm].
Creation procedure skeleton for a base channel
```
int XX_CreateChannel(interp, objc, objv, cd) {
''process arguments'' /* objc, objv */
int XX_CreateChannel(interp, objc, objv, cd) {
process arguments'' /* objc, objv */
name = ''generate_name_for_new_channel()'';
name = generate_name_for_new_channel();
clientData = Tcl_Alloc(...);
''initialize clientData''... /* name, state, config ... */
initialize clientData... /* name, state, config ... */
clientData->channel = Tcl_CreateChannel(interp, &chan_type, clientData);
''configure the channel according to arguments, if necessary''
configure the channel according to arguments, if necessary
Tcl_SetResult(interp, name);
return TCL_OK;
}
}
```
The ''chan_type'' in the code above is the structure containing the
references to the driver procedures for the base channel.
The ```chan_type``` in the code above is the structure containing the references to the driver procedures for the base channel.
### 3.2.2 Creation of a transformation
In contrast to base channel types the creation procedure must not use
Tcl_CreateChannel[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm] as that
would create a new and separate channel, but use
Tcl_StackChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm]
instead. This procedure takes as one of its parameters a reference to an
existing channel and creates a new channel structure holding the state of the
transformation. A token for this new structure is returned. When later
accessing the old channel, i.e. the one the transformation was stacked upon,
via Tcl_Read/Write et. al. the system will automatically redirect such calls
to the top of the stack. In other words, all Tcl_Channel tokens stay valid,
independent of where they are in a stack, yet no backdoors are opened. The
latter is not completely true, but we will come to this later on.
Other things, like the creation and initialization of the necessary clientData
for the transformation, have to be done as usual.
The backlink to the channel of the transformation has to be initialized with
The back-link to the channel of the transformation has to be initialized with
the result of the call to
Tcl_StackChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm].
Creation procedure skeleton for a transformation
```
int XX_CreateTransformation(interp, objc, objv, cd) {
int XX_CreateTransformation(interp, objc, objv, cd) {
old_channel = find(handle(objv[[1]));
clientData = Tcl_Alloc (...);
''initialize clientData...''
initialize clientData...
clientData->channel = Tcl_StackChannel(interp, &trans_type, clientData, old_channel);
Tcl_SetResult(interp, old_channel->name);
return TCL_OK;
}
```
}
The ''trans_type'' in the code above is the structure containing the
The ```trans_type``` in the code above is the structure containing the
references to the driver procedures for the transformation.
## 3.3 Destruction of channels
Destruction of channels is done with either
Tcl_UnregisterChannel[http://www.tcl.tk/man/tcl8.4/TclLib/OpenFileChnl.htm#M11]
or Tcl_UnstackChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm]. Both
|
| ︙ | | |
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
|
3.5.6 of the transformation so that its driver may cleanup its data
structures.
## 3.4 Accessing the channel downstream
This section is relevant to transformations, but nothing else.
To accesss the channel below itself a driver just has to call
To access the channel below itself a driver just has to call
Tcl_GetStackedChannel[http://www.tcl.tk/man/tcl8.4/TclLib/ChnlStack.htm] with
the token of its own channel (the backlink we talked about in Section
the token of its own channel (the back-link we talked about in Section
3.1). The function will return the token for the channel we want. A
(Tcl_Channel) NULL indicates that the channel used as argument was at the
bottom of the stack.
## 3.5 The driver in detail
Now that the environment of the driver is a little more known we can explain
the operations of the various driver procedures in detail. Every description
will start with the general condition under which the procedure is called by
the generic I/O layer of the tcl core and proceeds to the specialities a
the generic I/O layer of the tcl core and proceeds to the specialties a
transformation has to take care of.
But before we come to this we have to talk about driver versions as well. The
core currently supports version 1 and version 2 drivers. The latter was
introduced with Tcl 8.3.2, during the second rewrite of the stacked channel
stuff. It was required as for some functionality new vectors were needed to
correctly support it and the core also had to had a way to determine wether
correctly support it and the core also had to had a way to determine whether
the new fields were valid or not and just accessing them is out of the
question as the older structures simply don't have them, i.e. we would get
random information and most likely crash later oon.
random information and most likely crash later on.
The structure for a channel type looks like this:
'''Tcl_ChannelType definition'''
### Tcl_ChannelType definition
```
typedef struct Tcl_ChannelType {
typedef struct Tcl_ChannelType {
char *typeName; /* The name of the channel type in Tcl
* commands. This storage is owned by
* the channel type. */
Tcl_ChannelTypeVersion version; /* Version of the channel type. */
Tcl_DriverCloseProc *closeProc; /* Procedure to call to close the
* channel, or TCL_CLOSE2PROC if the
* close2Proc should be used
|
| ︙ | | |
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
* Only valid in TCL_CHANNEL_VERSION_2 channels
*/
Tcl_DriverFlushProc *flushProc; /* Procedure to call to flush a
* channel. May be NULL. */
Tcl_DriverHandlerProc *handlerProc; /* Procedure to call to handle a
* channel event. This will be passed
* up the stacked channel chain. */
} Tcl_ChannelType;
```
} Tcl_ChannelType;
For this discussion are only the four fields 'version', 'blockModeProc',
'flushProc' and 'handlerProc' of relevance.
For this discussion are only the four fields ```version```, ```blockModeProc```,
```flushProc``` and ```handlerProc``` of relevance.
All possible values are acceptable for 'version', but 'TCL_CHANNEL_VERSION_1'
and 'TCL_CHANNEL_VERSION_2' are special as they explictly spell out the
and 'TCL_CHANNEL_VERSION_2' are special as they explicitly spell out the
version of the driver. Any other value will cause the system to assume that
the structure describes a version 1 driver and that the 'version' field
actually contains the reference to 'blockModeProc'. And when looking at older
versions of tcl.h one will find exactly this definition of the structure. If
the special values are used the 'blockModeProc' must be found in the field of
that name, and for a version 2 driver 'flushProc' and 'handlerProc' are valid
as well.
Now that I have explained the complex logic I should also note that code which
has just to read the various fields in a
Tcl_ChannelType[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm#M6] has to
use the accessor functions in the following list. These functions have the
use the access functions in the following list. These functions have the
logic above written into them and will return the correct value independent of
the driver they access. This is especially of value for transformation
channels. Someone setting up a
Tcl_ChannelType[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm#M6]
structure for a new driver still has to know the rules, though.
o Tcl_ChannelBlockModeProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelBlockModeProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelCloseProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelCloseProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelClose2Proc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelClose2Proc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelInputProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelInputProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelOutputProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelOutputProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelSeekProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelSeekProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelSetOptionProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelSetOptionProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelGetOptionProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelGetOptionProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelWatchProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelWatchProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelGetHandleProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelGetHandleProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelFlushProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelFlushProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
o Tcl_ChannelHandlerProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
* Tcl_ChannelHandlerProc[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm]
Some questions of policy:
When should one write a version 1 driver, when one for version 2? And what is
the 'best' way to write a version 1 driver?
the __best__ way to write a version 1 driver?
When defining a transformation writing a version 2 driver is recommended as
only that version has the best support for integrating transformations and
eevent processing. The disadvantage is that thiis will restrict the driver to
event processing. The disadvantage is that this will restrict the driver to
Tcl 8.3.2, 8.4 and up. On the other hand, someone dead set at writing a
transformation supporting all (or some) versions of the core with stacked
channels should take a good look at
Trf[http://www.purl.org/NET/akupries/soft/trf/] for the necessary voodoo to
make such a beast work.
When writing a base channel it is on the other hand recommended to use a
|
| ︙ | | |
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
-
+
-
+
-
-
-
+
+
-
+
-
-
+
-
-
+
-
+
-
-
-
+
+
+
-
+
-
+
-
+
-
|
This procedure is called by the generic I/O layer whenever
Tcl_SetChannelOption[http://www.tcl.tk/man/tcl8.4/TclLib/OpenFileChnl.htm#M21]
is used (for example by 'fconfigure') and a non-standard option was specified
as argument.
For base channel the handling is straight forward. If there are no options to
set, just set the asssociated field in
set, just set the associated field in
Tcl_ChannelType[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm#M6] to
NULL. Else compare the specified name against the options supported by the
driver and act accordingly. If the option is not known use
Tcl_BadChannelOption[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm#M17]
to generate the error message.
Transformation channels are basically the same, except for unknown options.
They have the additional option to delegate the call to the channel
downstream. I personally recommend to delegate the call. Because of this I
also recommend to implement this function even if the transformation has no
options by itself.
'''SetOptionProc skeleton for a transformation'''
#### SetOptionProc skeleton for a transformation
```
static int SetOptionProc(clientData, interp, optionName, value) {
''... handle your own options''
static int SetOptionProc(clientData, interp, optionName, value) {
... handle your own options
/* delegate unknown options downstream */
Tcl_Channel parent = Tcl_GetStackedChannel(clientData->channel);
Tcl_DriverSetOptionProc *setOptionProc =
Tcl_ChannelSetOptionProc(Tcl_GetChannelType(parent));
if (setOptionProc == NULL) {
return TCL_ERROR;
}
return setOptionProc(Tcl_GetChannelInstanceData(parent),
interp, optionName, value);
}
}
```
### 3.5.3 GetOptionProc
This procedure is called by the generic I/O layer whenever
Tcl_GetChannelOption[http://www.tcl.tk/man/tcl8.4/TclLib/OpenFileChnl.htm#M20]
is used (f.e. by 'fconfigure') to query the value of a non-standard (or all)
option(s).
The channel type has to implement everything from Section 3.5.2 and some more.
The latter not only because read-only options make sense (write-only not so
much) but also because there is a special case which asks the channel for the
values of all of its options.
For transformations it is again possible to delegate options unknown to it to
the underlying channel. In the case of a query for all options such a
delegation will generate a mighty long result. Pruning the unnecessary
options values from the result of the underlying channel (-encoding,
-buffering, -translation) is possible, but tedious (We are working with
DStrings, not Tcl_Obj'ects, and especially no ListObj'ects).
'''GetOptionProc skeleton for a transformation'''
#### GetOptionProc skeleton for a transformation
```
static int GetOptionProc(clientData, interp, optionName, dsPtr) {
static int GetOptionProc(clientData, interp, optionName, dsPtr) {
if (query for all options) {
''... add our own options to the result''
... add our own options to the result
''/* add the options of the channel downstream''
'' * to the result''
'' */''
/* add the options of the channel downstream
* to the result
*/
Tcl_Channel parent = Tcl_GetStackedChannel(clientData->channel);
Tcl_DriverGetOptionProc *getOptionProc =
Tcl_ChannelGetOptionProc(Tcl_GetChannelType(parent));
if (getOptionProc == NULL) {
return TCL_OK;
}
return getOptionProc(Tcl_GetChannelInstanceData(parent),
interp, optionName, dsPtr);
} else {
''... handle your own options.''
... handle your own options.
''/* delegate queries for unknown options downstream */''
/* delegate queries for unknown options downstream */
Tcl_Channel parent = Tcl_GetStackedChannel(clientData->channel);
Tcl_DriverGetOptionProc *getOptionProc =
Tcl_ChannelGetOptionProc(Tcl_GetChannelType(parent));
if (getOptionProc == NULL) {
return TCL_ERROR;
}
return getOptionProc(Tcl_GetChannelInstanceData(parent),
interp, optionName, dsPtr);
}
}
}
```
P.S. Given the similarities in the way the delegation is handled by the two
branches of the if statement above it makes sense to factor this code into a
separate procedure.
### 3.5.4 SeekProc
|
| ︙ | | |
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
-
+
-
-
-
-
+
+
+
-
+
-
+
-
-
+
-
-
-
+
+
-
+
-
+
-
-
+
+
-
|
For new a base channel the implementation of this procedure should be straight
forward. Unseekable channels (like [udp]) and forward seekable channels just
don't implement it, the other types either forward the call to the OS or
simply manipulate their internal state. See
memchan[http://www.purl.org/NET/akupries/soft/memchan/] for an example of the
latter.
'''SeekProc skeleton for OS associated channels'''
#### SeekProc skeleton for OS associated channels
```
static int SeekProc(clientdata, offset, mode, errorCodePtr) {
''... flush possibly waiting output''
''... discard possibly waiting input''
static int SeekProc(clientdata, offset, mode, errorCodePtr) {
... flush possibly waiting output
... discard possibly waiting input
''... compute some value from offset, mode and current location, then forward this to the OS.''
... compute some value from offset, mode and current location, then forward this to the OS.
*errorCodePtr = (result == -1) ? Tcl_GetErrno () : 0;
return result;
}
}
```
'''SeekProc skeleton for non-OS channels'''
#### SeekProc skeleton for non-OS channels
```
static int SeekProc(clientdata, offset, mode, errorCodePtr) {
''... compute the new location X from offset, mode and current location''
static int SeekProc(clientdata, offset, mode, errorCodePtr) {
... compute the new location X from offset, mode and current location
if (''X out of bounds'') {
if (X out of bounds) {
*errorCodePtr = EINVALID;
return -1;
}
clientdata->state = ''X'';
clientdata->state = X;
*errorCodePtr = 0;
return ''X'';
}
return X;
}
```
For transformations seeking is a hard problem. Should they seek using their
own notion of access point? Or should they use the notion of the underlying
channel and then try to adapt their own state for fine-positioning? Should
they allow seeking at all?
Depending on the transformation both the first two can be impossible. Nice
|
| ︙ | | |
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
-
+
-
-
-
-
+
+
+
-
-
+
+
|
Remark: It is possible to deal even with non-reversible state, by recording
all read/write calls and maintaining an exact image of the information
read/written so far, but this is, ah, memory-extensive, to understate this a
little. Also note that the notion of channels with non-reversible state is
equivalent to the notion of forward seekable channels.
'''SeekProc skeleton with 1:1 pass'''
#### SeekProc skeleton with 1:1 pass
```
static int SeekProc(clientdata, offset, mode, errorCodePtr) {
''... flush waiting output''
''... flush waiting input, if possible (f.e. into a configured variable!)''
static int SeekProc(clientdata, offset, mode, errorCodePtr) {
... flush waiting output
... flush waiting input, if possible (f.e. into a configured variable!)
/* Chain the call */
Tcl_Channel parent = Tcl_GetStackedChannel(clientData->channel);
Tcl_ChannelType *parentType = Tcl_GetChannelType(parent);
Tcl_DriverSeekProc *parentSeekProc = Tcl_ChannelSeekProc(parentType);
int errorCode;
if (parentSeekProc == NULL) {
return -1;
}
return parentSeekProc(Tcl_GetChannelInstanceData(parent),
offset, mode, &errorCode);
}
```
}
As a last note, Trf[http://www.purl.org/NET/akupries/soft/trf/] implements a
much more complex seeking model for transformations, but describing it is
beyond the scope of this document. Go to its documentation instead.
### 3.5.5 BlockModeProc
|
| ︙ | | |
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
-
+
-
-
+
-
+
-
-
+
-
-
-
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
+
-
+
-
-
+
|
(non-)blocking behaviour.
A transformation channel however just has to remember this information in its
instance data so that InputProc (Section 3.5.7) is able to deal correctly with
empty reads on the downstream channel. The generic layer takes care of
notifying all channels in a stack so that all have the same information.
'''BlockModeProc skeleton'''
#### BlockModeProc skeleton
```
static int BlockModeProc(clientdata, mode) {
static int BlockModeProc(clientdata, mode) {
if (mode == TCL_MODE_NONBLOCKING) {
clientdata->flags |= ASYNC;
} else {
clientdata->flags &= ~ASYNC;
}
}
}
```
### 3.5.6 CloseProc
This procedure is called by generic the I/O layer to tell a channel that it is
about to be destroyed. See Section 3.3 for the procedures which can invoke it.
It is the responsibility of the procedure to clean up any data structures held
by the channel. Another task is the removal of all event related things, like
ChannelHandlers and Timers, although this could be billeted under 'clean up of
any data structures held by the channel' too.
Transformations have the additional responsibility to complete the conversion
of all incomplete information sitting in its internal write buffers and to
write the result into the downstream channel to ensure a clean closure.
'''CloseProc skeleton'''
#### CloseProc skeleton
```
int CloseProc(clientdata, interp) {
''... delete timer, if any. See 'WatchProc' too.''
int CloseProc(clientdata, interp) {
... delete timer, if any. See 'WatchProc' too.
'''|'''... do last minute conversions on r/w buffers and try to
'''|''' flush their results to the underlying channel.
'''|''' See below
|... do last minute conversions on r/w buffers and try to
| flush their results to the underlying channel.
| See below
'''|'''
'''|''' parent = Tcl_GetStackedChannel (clientdata->channel);
'''|''' Tcl_WriteRaw (parent, buffer, bufsize);
|
| parent = Tcl_GetStackedChannel (clientdata->channel);
| Tcl_WriteRaw (parent, buffer, bufsize);
''... free data structures on the heap.''
... free data structures on the heap.
return TCL_OK
}
}
```
The part marked with '''|''' is specific to transformations and not required
The part marked with __|__ is specific to transformations and not required
for a base channel.
### 3.5.7 InputProc
This procedure is called by the generic I/O layer whenever some input is
required. Entrypoints which can cause this are
Tcl_Read[http://www.tcl.tk/man/tcl8.4/TclLib/OpenFileChnl.htm#M13],
|
| ︙ | | |
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
|
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
|
-
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
-
-
-
-
+
+
+
+
+
-
+
-
+
-
-
+
-
-
+
-
+
-
|
3.5.10 is also of essence.
Two things have to be done.
1. Propagation of the mask information to the channel downstream. All transformations have to cooperate in this or else the base channel at the bottom won't register with the notifier for events. The contrasting example to this is Section 3.5.5 where the generic layer automatically notifies all channels in the tsack itself. Here we decided against such an automatism because the way it is now allows the various transformations to modify the mask before passing it down, i.e. to add the events they are interested in beyond the interest of the script level without any changes to the structure of Tcl_ChannelType[http://www.tcl.tk/man/tcl8.4/TclLib/CrtChannel.htm#M6] or the sematics of this vector. This would have been the case if we had changed this to an automatic chaining in the generic part of the I/O system.
1. Setting up and destroying the timer used to flush out the internal read buffers. This needs a bit more explanation, which will be given after the skeleton code for a WatchProc.
'''WatchProc skeleton'''
#### WatchProc skeleton
```
static void WatchProc(clientdata, mask) {
''/* Pass the mask to the channel downstream, possibly''
'' * modified. Remember the mask internally.''
'' */''
static void WatchProc(clientdata, mask) {
/* Pass the mask to the channel downstream, possibly
* modified. Remember the mask internally.
*/
Tcl_Channel parent = Tcl_GetStackedChannel(clientdata->channel);
Tcl_DriverWatchProc *watchProc = Tcl_ChannelWatchProc(Tcl_GetChannelType(parent));
trans->watchMask = mask;
watchProc(Tcl_GetChannelInstanceData(parent), mask);
/* Manage the timer */
if (!(mask & TCL_READABLE) || (''no pending converted information''))) {
''/* A pending timer may exist, but either is''
'' * there no (more) interest in the events it''
'' * generates or nothing is available for''
'' * reading. Remove it, if existing.''
'' */''
if (!(mask & TCL_READABLE) || (no pending converted information))) {
/* A pending timer may exist, but either is
* there no (more) interest in the events it
* generates or nothing is available for
* reading. Remove it, if existing.
*/
''... kill the timer''
... kill the timer
} else {
''/* There might be no pending timer, but there''
'' * is interest in readable events and we''
'' * actually have data waiting, so generate a''
'' * timer to flush that if it does not exist.''
'' */''
/* There might be no pending timer, but there
* is interest in readable events and we
* actually have data waiting, so generate a
* timer to flush that if it does not exist.
*/
''... create the timer.''
... create the timer.
}
}
}
```
The handler procedure for the timer handled above looks like below. It
basically proclaims that the transformation channel is readable. There is no
need to recreate the timer here, because the generic layer will call Section
3.5.9 after it has handled the event and that vector will then do the right
thing (see above).
'''Timer handler skeleton'''
#### Timer handler skeleton
```
static void ChannelHandlerTimer(clientData) {
static void ChannelHandlerTimer(clientData) {
clientdata->timer = NULL;
Tcl_NotifyChannel(clientdata->channel, TCL_READABLE);
}
}
```
Now the promised explanation about the necessity of the timer.
Consider this scenario:
1. A transformation is stacked upon a socket and its internal read buffer is empty. The transformation does not merge lines.
1. A [fileevent] script is set up and waiting for calls.
|
| ︙ | | |
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
|
-
+
|
1. In the end the generic I/O buffers are empty. Now what, again ?
1. Nothing. No events, and no processing of the remaining line(s) stored in the transformation.
1. Why? Because the socket has an EOF pending and will not generate events anymore. and the generic layer has empty buffers and ceases to generate events too. It has no knowledge about the buffers inside the driver, i.e the transformation. So the script will not wake up again, neither ask for the line, nor detect the pending EOF. We are hung.
The solution to this lock is the same one used by the generic layer, but from
the inside of the transformation this time:
: The transformation has to check itself for data waiting to be read and then use a timer to generate the necessary 'readable' events. And that is what the timer shown in the WatchProc will do.
* The transformation has to check itself for data waiting to be read and then use a timer to generate the necessary 'readable' events. And that is what the timer shown in the WatchProc will do.
### 3.5.10 HandlerProc
This vector is the second part of the integration of transformations with the
notifier. It is called by the generic layer whenever an event happens at the
channel downstream. This also means that it is never called for the channel at
the bottom of a stack. In other words, base channels don't have to implement
|
| ︙ | | |
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
|
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
-
+
-
-
+
-
+
-
-
-
+
+
-
+
-
+
-
+
-
+
|
Because of this a transformation is able to absorb and handle events without
the channel (or script) above being aware of them and the associated
processing. The TLS transformation[http://www.sensus.org/tcl/] for example
uses this facility to handle the whole negotiation phase. Only after the
encryption is setup events are passed unchanged to the higher layers.
'''HandleProc skeleton'''
#### HandleProc skeleton
```
static int HandleProc(clientdata, mask) {
static int HandleProc(clientdata, mask) {
/*
* An event occured in the underlying channel. This
* transformation doesn't process such events thus
* returns the incoming mask unchanged.
*
* We do delete an existing timer. It was not fired,
* yet we are here, so the channel below generated
* such an event and we don't have to. The renewal of
* the interest after the execution of channel
* handlers will eventually cause us to recreate the
* timer
*/
... kill timer
return interestMask;
}
}
```
### 3.5.11 FlushProc
This vector is currently not used by the generic layer of the I/O system. In
the future it might be used to separate the actions of flushing and writing
data.
# Author's Address
Andreas Kupries
Andreas Computer Laboratories (Me, myself and I)
Andreas Kupries
Andreas Computer Laboratories (Me, myself and I)
EMail: [email protected]
EMail: [email protected]
# Appendix A. Glossary
A little glossary of terms used in the paper, but so far without much of an
explanation (or none).
Tcl_Channel: An opaque token for channels, and used by all interfaces accessing channels. Internally it is a pointer to the relevant data structures (Channel*).
__Tcl_Channel__: An opaque token for channels, and used by all interfaces accessing channels. Internally it is a pointer to the relevant data structures (Channel*).
stack: If one or more transformations are stacked upon an arbitrary other channel I use this word to refer to the whole group of channels.
__stack__: If one or more transformations are stacked upon an arbitrary other channel I use this word to refer to the whole group of channels.
(un)cover: Placing a transformation on a channel C "covers" C, removing the transformation "uncovers" it again.
__(un)cover__: Placing a transformation on a channel C "covers" C, removing the transformation "uncovers" it again.
# Appendix B. Acknowledgements
This HOWTO was written in XML using the DTD developed by [Marshall T. Rose]
for writing RFC's and I-D's, see [RFC]
2629[http://www.rfc-editor.org/rfc/rfc2629.txt], and converted to text and
HTML with his tool, ''[xml2rfc]''.
|