Tcl_CreateChannelHandler(3)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– Tcl_CreateChannelHandler(3) β€” Tcl Library Procedures πŸ’–

okay bestie, so this is a page out of the Tcl Library Procedures manual, and it's all about two little functions that go so hard: Tcl_CreateChannelHandler and Tcl_DeleteChannelHandler. they're the ones who let you say "hey, ping me the second this channel gets readable or writable." callback girlies, this one's for you 🫢


✨ NAME ✨

Tcl_CreateChannelHandler, Tcl_DeleteChannelHandler β€” call a procedure when a channel becomes readable or writable. that's it. that's the vibe. πŸ’…


🌸 SYNOPSIS 🌸

#include <tcl.h>

Tcl_CreateChannelHandler(channel, mask, proc, clientData)

Tcl_DeleteChannelHandler(channel, proc, clientData)

πŸŽ€ ARGUMENTS πŸŽ€

  • Tcl_Channel channel (in) β€” a Tcl channel, like the ones Tcl_CreateChannel hands you. your main character channel, if you will.
  • int mask (in) β€” the conditions under which proc should be called: an OR-ed combination of TCL_READABLE, TCL_WRITABLE and TCL_EXCEPTION. and listen β€” if you pass a zero value, that temporarily disables an existing handler. yes, zero is a valid mood. πŸ™ˆ
  • Tcl_ChannelProc *proc (in) β€” the procedure to invoke whenever the channel indicated by channel meets the conditions specified by mask. the whole point, honestly.
  • void *clientData (in) β€” an arbitrary one-word value to pass to proc. like a lil note you tuck into its pocket. πŸŽ€

πŸ’– DESCRIPTION πŸ’–

Tcl_CreateChannelHandler arranges for proc to be called in the future whenever input or output becomes possible on the channel identified by channel, or whenever an exceptional condition exists for channel. the conditions of interest under which proc will be invoked are specified by the mask argument. for a precise description of what it even means for a channel to be readable or writable, go read the manual entry for fileevent. (iykyk ✨)

and proc has to conform to this exact prototype, no notes:

typedef void Tcl_ChannelProc(
        void *clientData,
        int mask);

the clientData argument is the same as the value passed to Tcl_CreateChannelHandler when the handler was created. typically, clientData points to a data structure containing application-specific information about the channel. mask is an integer mask indicating which of the requested conditions actually exists for the channel; it will contain a subset of the bits from the mask argument to Tcl_CreateChannelHandler when the handler was created. so like, a subset. not the whole thing. don't be surprised, bestie. πŸ’œ


each channel handler is identified by a unique combination of channel, proc and clientData. there may be many handlers for a given channel β€” as long as they do not have the same channel, proc, and clientData. multitasking queen behavior, we love it. πŸ‘‘

if Tcl_CreateChannelHandler is invoked when there is already a handler for channel, proc, and clientData, then no new handler is created; instead, the mask is changed for the existing handler. not a duplicate, just a glow-up. πŸ’…

Tcl_DeleteChannelHandler deletes a channel handler identified by channel, proc and clientData; if no such handler exists, the call has no effect. (ghosted, and honestly? nothing happens. very chill.) πŸ¦‹


channel handlers are invoked via the Tcl event mechanism, so they are only useful in applications that are event-driven. that's the whole deal, no cap.

⚠️ also note: the conditions specified in the mask argument to proc may no longer exist when proc is invoked. for example, if there are two handlers for TCL_READABLE on the same channel, the first handler could consume all of the available input so that the channel is no longer readable when the second handler is invoked. 😭

for this reason it may be useful to use nonblocking I/O on channels for which there are event handlers. do with that what you will, but like… do it. 🌈


πŸ‘€ SEE ALSO πŸ‘€

Notifier(3), Tcl_CreateChannel(3), Tcl_OpenFileChannel(3), vwait(n)


⭐ KEYWORDS ⭐

blocking, callback, channel, events, handler, nonblocking


and that's the whole page, bestie!! this one came from the Tcl manual, version 7.5, and the page itself is Tcl_CreateChannelHandler(3) β€” go forth and handle those channels, you absolute icon πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from