On Sat, Jun 05, 2004 at 07:57:33 -0400, muppet wrote:
On Saturday, June 5, 2004, at 06:30 AM, Jan Hudec wrote:On Fri, Jun 04, 2004 at 16:38:03 -0400, muppet wrote:On Friday, June 4, 2004, at 03:18 PM, Jan Hudec wrote:I really wish perl IO descriptors had a 'buffer' method. It would return the internal buffer as a (read-only) string.there is one, at the C level --- PerlIO_get_base(). see perlapio.
I only had a very brief look at that when I wanted to know how unicode works. I'll give it a better look. Thanks for pointer.
Then we could have Glib::IO watchers properly generating 'in' event when the perlio descriptor (not the underlying file) have some data to read!how would you have it work? while the buffer is non-empty, keep firing the 'in' condition? (that is, multiple 'in' dispatches for a single file descriptor read.) or return the new data to the sub?
If you have a file descriptor (OS level), don't read all the data ready and call select/poll on it, it immediately returns with 'in' condition on that descriptor. I want to mimic that behaviour on buffered descriptors.
even if we see how many bytes are actually available, it's up the the handler to decide if and how to read them, so the handler will still have to do filehandle io carefully. also, how would you handle the output situation? (e.g., the 'out' condition, when it's safe to write to the socket.)
There are two options: 1) The simple one: Output to socket/pipe must not be delayed. There is output condition only if there is output condition on the underlying descriptor. flush is called at end of dispatch routine (so you don't have to set autoflush). 2) The complicated one: Output condition is always on. When poll actualy tells it's there, check method calls flush. This is probably useless and I am not sure how glib would like it.
the old gtk-perl used some interesting MAGIC vtable tricks to allow subs to be called whenever a scalar is changed. perhaps we should modify Glib::IO::add_watch() to use a similar mechanism?The vtable MAGIC was about the scalars. The interface to glib is not magic at all. And for watching perlio objects, you don't need the vtable magic. On the glib side, it means to implement an object based on GSource structure. It has a "vtable" GSourceFuncs with four functions -- prepare, check, dispatch and finalize.yeah, i understand all of that. what i was referring to was in Gtk-Perl/Gtk/xs/Gtk.xs, the implementation of Gtk::watch_add(), which creates a GSource containing functions that watch magical SVs. this caused the main loop to wake up whenever anything happened to that scalar.
Yes. But you don't need to enchant perlio objects to watch them.
i forgot that the old version of Glib::IO::add_watch() was Gtk::Gdk::input_add(); for some reason i thought that the magical scalar implementation of watch_add would handle io events as well. note that the old version (input_add()) used plain file descriptors, as well.
Of course. It's simpler to implement and after all, perl's built-in select only works with sysread/syswrite as well, so programmers are used to that anyway.
prepare is called before poll. It must look whether there are data in the buffer. This is actualy the hardest part, at least for me, since I don't understand internals of perlio. prepare returns true if the source is ready (buffer not empty).there's a macro to get the number of bytes in the buffer, PerlIO_get_cnt().
Perfect. Then we could have Glib::IO::add_watch handle perl scalars and handle them well. Though, I don't think it's worth. What would be worth is getting perlio work with nonblocking descriptors gracefuly. That is, making proper difference between EOF and EAGAIN codintions. With careful programming, you wouldn't need any support from poll/select -- just make sure to read everything available. I believe this approach works cleanly with stdio. I have, however, tried it in perl and it was not helpful over sysread/syswrite in any way. ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb ucw cz>
Attachment:
signature.asc
Description: Digital signature