[PDD] Add docs for the Parrot_PMC_push_* and Parrot_PMC_pop_* functions
[parrot.git] / docs / pdds / pdd22_io.pod
blob337543f61e233c636432c950676be8d3a54882de
1 # Copyright (C) 2001-2009, Parrot Foundation.
2 # $Id$
4 =head1 PDD 22: I/O
6 =head2 Abstract
8 Parrot's I/O subsystem.
10 =head2 Version
12 $Revision$
14 =head2 Definitions
16 A "stream" allows input or output operations on a source/destination
17 such as a file, keyboard, or text console. Streams are also called
18 "filehandles", though only some of them have anything to do with files.
20 =head2 Description
22 =over 4
24 =item - Parrot I/O objects support both streams and network I/O.
26 =item - Parrot has both synchronous and asynchronous I/O operations.
28 =item - Asynchronous operations must interact safely with Parrot's other
29 concurrency models.
31 =back
33 =head2 Implementation
35 =head3 Platform Implementation
37 Parrot uses a series of macros and conditional compilation to generate
38 code to handle IO on various platforms. There is a "portable" version of
39 the IO system that is used for most systems that use a common POSIX API
40 for IO, and there are platform-specific files that can be used for systems
41 that do not subscribe, or don't subscribe faithfully, to POSIX.
43 A series of macros and conditional compilation are used to determine which
44 files get added to the build, and which functions are called to implement
45 each operation.
47 =head3 Concurrency Model for Asynchronous I/O
49 Currently, Parrot only implements synchronous I/O operations. Initially,
50 the asynchronous operations will be implemented separately from the
51 synchronous ones. There may be an implementation that uses one variant
52 to implement the other someday, but it's not an immediate priority.
54 Synchronous opcodes are differentiated from asynchronous opcodes by the
55 presence of a callback argument in the asynchronous calls.  Asynchronous
56 calls that don't supply callbacks (perhaps if the user wants to manually
57 check later if the operation succeeded) are enough of a fringe case that
58 they don't need opcodes. They can access the functionality via methods
59 on ParrotIO objects.
61 Asynchronous operations use a lightweight concurrency model. At the user
62 level, Parrot follows the callback function model of asynchronous I/O.
63 At the interpreter level, each asynchronous operation registers a task
64 with the interpreter's concurrency scheduler. The registered task could
65 represent a simple Parrot asynchronous I/O operation, a platform-native
66 asynchronous I/O call, or even synchronous code in a full Parrot thread
67 (rare but possibly useful for prototyping new features, or for mock
68 objects in testing).
70 Communication between the calling code and the asynchronous operation
71 task is handled by a shared status object. The operation task updates
72 the status object whenever the status changes, and the calling code can
73 check the status object at any time.  The status object contains a
74 reference to the returned result of an asynchronous I/O call. In order
75 to allow sharing of the status object, asynchronous ops both pass the
76 status object to the callback PMC, and return it to the calling code.
78 The lightweight tasks typically used by the asynchronous I/O system
79 capture no state other than the arguments passed to the I/O call, and
80 share no variables with the calling code other than the status object.
82 =head3 FileHandle PMC API
84 Methods
86 =over 4
88 =item C<new>
90 =begin PIR_FRAGMENT
92   $P0 = new [ 'FileHandle' ]
94 =end PIR_FRAGMENT
96 Creates a new I/O stream object. [Note that this is usually performed
97 via the C<open> opcode.]
99 =item C<open>
101 =begin PIR_FRAGMENT
103   $P0 = $P1.'open'()
104   $P0 = $P1.'open'($S2)
105   $P0 = $P1.'open'($S2, $S3)
107 =end PIR_FRAGMENT
109 Opens a stream on an existing I/O stream object, and returns a status
110 object. With no arguments, it can be used to reopen a previously opened
111 I/O stream. $S2 is a file path and $S3 is an optional mode for the
112 stream (read, write, read/write, etc), using the same format as the
113 C<open> opcode: 'r' for read, 'w' for write, 'a' for append, and 'p' for
114 pipe. When the optional mode argument is not passed, the default is read mode.
115 When the mode is set to write or append, a file is created without warning if
116 none exists. When the mode is read (without write), a nonexistent file is an
117 error.
119 The asynchronous version takes a PMC callback as an additional final
120 argument. When the open operation is complete, it invokes the callback
121 with a single argument: a status object containing the opened stream
122 object.
124 =item C<close>
126 =begin PIR_FRAGMENT
128   $P0 = $P1.'close'()
129   $P0 = $P1.'close'($P2)
131 =end PIR_FRAGMENT
133 Closes an I/O stream, but leaves destruction of the I/O object to the
134 GC. The C<close> method returns a PMC status object.
136 The asynchronous version takes an additional final PMC callback argument
137 $P1. When the close operation is complete, it invokes the callback,
138 passing it a status object. [There's not really much advantage in this
139 over just leaving the object for the GC to clean-up, but it does give
140 you the option of executing an action when the stream has been closed.]
142 =item C<print>
144 =begin PIR_FRAGMENT
146   $P0 = $P1.'print'($I2)
147   $P0 = $P1.'print'($N2)
148   $P0 = $P1.'print'($S2)
149   $P0 = $P1.'print'($P2)
150   $P0 = $P1.'print'($I2, $P3)
151   $P0 = $P1.'print'($N2, $P3)
152   $P0 = $P1.'print'($S2, $P3)
153   $P0 = $P1.'print'($P2, $P3)
155 =end PIR_FRAGMENT
157 Writes an integer, float, string, or PMC value to an I/O stream object.
158 Returns a PMC status object.
160 The asynchronous version takes an additional final PMC callback
161 argument $P2. When the print operation is complete, it invokes the callback,
162 passing it a status object.
164 =item C<read>
166 =begin PIR_FRAGMENT
168   $S0 = $P1.'read'($I2)
169   $P0 = $P1.'read'($I2, $P3)
171 =end PIR_FRAGMENT
173 Retrieves a specified number of bytes ($I2) from a stream $P1 into a
174 string $S0. By default it reads in bytes, but the FileHandle object can
175 be configured to read in code points instead, by setting the character
176 set and encoding on the filehandle object. If there are fewer bytes
177 remaining in the stream than specified in the read request, it returns
178 the remaining bytes (with no error).
180 The asynchronous version takes an additional final PMC callback argument
181 $P3, and only returns a status object $P0. When the read operation is
182 complete, it invokes the callback, passing it a status object. The
183 status object contains the return value: a string that may be in bytes
184 or codepoints depending on the read mode of the I/O object. [The
185 callback doesn't need to know the read mode of the original operation,
186 as the information about the character encoding of the return value is
187 contained in the string.]
189 =item C<readline>
191 =begin PIR_FRAGMENT
193   $S0 = $P1.'readline'()
194   $P0 = $P1.'readline'($P2)
196 =end PIR_FRAGMENT
198 Retrieves a single line from a stream $P1 into a string $S1. Calling
199 C<readline> flags the stream as operating in line-buffer mode (see the
200 C<buffer_type> method below). The C<readline> operation respects the
201 read mode of the I/O object the same as C<read> does. Newlines are not
202 removed from the end of the string.
204 The asynchronous version takes an additional final PMC callback argument
205 $P2, and only returns a status object $P0. When the readline operation
206 is complete, it invokes the callback, passing it a status object and a
207 string of bytes.
209 =item C<readall>
211 =begin PIR_FRAGMENT
213   $S0 = $P1.'readall'()
214   $P0 = $P1.'readall'($P2)
216   $S0 = $P1.'readall'($S2)
217   $P0 = $P1.'readall'($S2, $P3)
219 =end PIR_FRAGMENT
221 Retrieves the entire contents from a stream $P1 into a string $S0. On a
222 previously opened stream, C<readall> will read the entire contents of
223 the file, and return them as a string. It will respect the read mode of
224 the I/O object the same as C<read> does.
226 On an unopened stream, if a string file path is passed in $S2,
227 C<readall> will open a stream on the file, read the contents and close
228 it again. 
230 The asynchronous version takes an additional final PMC callback argument
231 ($P2 or $P3), and only returns a status object $P0. When the readall operation
232 is complete, it invokes the callback, passing it a status object and a
233 string.
235 =item C<record_separator>
237 =begin PIR_FRAGMENT
239   $S0 = $P1.'record_separator'()
240   $P0.'record_separator'($S1)
242 =end PIR_FRAGMENT
244 Accessor (get and set) for the I/O stream's record separator attribute.
245 The default value is a newline (CR, LF, CRLF, etc. depending on the
246 platform).
248 =item C<buffer_type>
250 =begin PIR_FRAGMENT
252   $S0 = $P1.'buffer_type'()
253   $P0.'buffer_type'($S1)
255 =end PIR_FRAGMENT
257 Accessor (get and set) for the I/O stream's buffer type attribute. The
258 attribute is set or returned as a string value of 'unbuffered' (bytes sent as
259 soon as possible), 'line-buffered' (bytes sent when record separator is
260 encountered), or 'full-buffered' (bytes sent when the buffer is full).
262 =item C<buffer_size>
264 =begin PIR_FRAGMENT
266   $I0 = $P1.'buffer_size'()
267   $P0.'buffer_size'($I1)
269 =end PIR_FRAGMENT
271 Accessor (get and set) for the I/O stream's buffer size attribute. The
272 size is specified in bytes (positive integer value), though the buffer
273 may hold a varying number of characters when dealing with an encoding of
274 multi-byte codepoints.  The code that implements the handling of a
275 particular character set must provide the logic that marks the buffer as
276 "full" when it can't hold the next codepoint even if there are empty
277 bytes in the buffer.
279 Setting the buffer size turns on full buffering mode for the I/O stream.
280 The set buffer size is taken as a minimum, the I/O subsystem may round
281 it up to a standard block size.
283 The buffer is automatically flushed when the buffer size is changed. If
284 the new size was larger than the existing data in the buffer, a size
285 change would be non-disruptive, but if the new size was smaller,
286 resizing it without flushing would truncate the buffer.
288 =item C<mode>
290 =begin PIR_FRAGMENT
292   $S0 = $P1.'mode'()
294 =end PIR_FRAGMENT
296 Accessor (get only) for the I/O stream's read mode. This returns the mode
297 string used to open the I/O stream.
299 =item C<encoding>
301 =begin PIR_FRAGMENT
303   $S0 = $P1.'encoding'()
304   $P0.'encoding'($S1)
306 =end PIR_FRAGMENT
308 Accessor (get and set) for the I/O stream's encoding attribute. Currently,
309 the only valid value to set is 'utf8' which turns on UTF-8 reading/writing
310 mode for the stream. The default behavior is fixed-width 8-bit characters.
312 =item C<get_fd>
314 =begin PIR_FRAGMENT
316   $I0 = $P1.'get_fd'()
318 =end PIR_FRAGMENT
320 For stream objects that are simple wrappers around a Unix filehandle,
321 C<get_fd> retrieves the Unix integer file descriptor of the object.
322 This method will simply return -1 on stream objects that aren't Unix
323 filehandles.
325 No asynchronous version.
327 =back
329 =head3 Status Object PMC API
331 =over 4
333 =item C<get_integer> (vtable)
335 =begin PIR_FRAGMENT
337   $I0 = $P1
339 =end PIR_FRAGMENT
341 Returns an integer status for the status object, 1 for successful
342 completion, -1 for an error, and 0 while still running. [Discuss: This
343 is largely to preserve current expectations of -1 for an error. If we
344 move away from that, is there a better representation?]
346 =item C<get_bool> (vtable)
348 =begin PIR_FRAGMENT
350   if $P0 goto L1
351   # ...
352   L1:
354 =end PIR_FRAGMENT
356 Returns a boolean status for the status object, C<true> for successful
357 completion or while still running, C<false> for an error.
359 =item C<return>
361 =begin PIR_FRAGMENT
363   $P0 = $P1.'return'()
365 =end PIR_FRAGMENT
367 Retrieves the return value of the asynchronous operation from the status
368 object. Returns a NULL PMC while still running, or if the operation had
369 no return value.
371 =item C<error>
373 =begin PIR_FRAGMENT
375   $P0 = $P1.'error'()
377 =end PIR_FRAGMENT
379 Retrieves the error object from the status object, if the execution of
380 the asynchronous operation terminated with an error. The error object is
381 derived from Exception, and can be thrown from the callback. If there
382 was no error, or the asynchronous operation is still running, returns a
383 null PMC.
385 =item C<throw>
387 =begin PIR_FRAGMENT
389   $P0.'throw'()
391 =end PIR_FRAGMENT
393 Throw an exception from the status object if it contains an error
394 object, otherwise do nothing.
396 =back
398 =head3 I/O Iterator PMC API
400 [Implementation NOTE: this may either be the default Iterator object
401 applied to a FileHandle or Socket object, a separate Iterator object for
402 I/O objects, or an Iterator role applied to I/O objects.]
404 =over 4
406 =item C<new>
408 =begin PIR_FRAGMENT
410     new $P0, [ 'Iterator' ], $P1
412 =end PIR_FRAGMENT
414 Create a new iterator object $P0 from I/O object $P1.
416 =item C<shift>
418 =begin PIR_FRAGMENT
420       shift $S0, $P1
422 =end PIR_FRAGMENT
424 Retrieve the next line/block $S0 from the I/O iterator $P1. The amount
425 of data retrieved in each iteration is determined by the I/O object's
426 C<buffer_type> setting: unbuffered, line-buffered, or fully-buffered.
428 =item C<get_bool> (vtable)
430 =begin PIR_FRAGMENT
432   unless $P0 goto iter_end
434 =end PIR_FRAGMENT
436 Returns a boolean value for the iterator, C<true> if there is more data
437 to pull from the I/O object, C<false> if the iterator has reached the
438 end of the data. [NOTE: this means that an iterator always checks for
439 the next line/block of data when it retrieves the current one.]
441 =back
443 =head3 I/O Opcodes
445 The signatures for the asynchronous operations are nearly identical to
446 the synchronous operations, but the asynchronous operations take an
447 additional argument for a callback, and the only return value from the
448 asynchronous operations is a status object. When the callbacks are invoked,
449 they are passed the status object as their sole argument. Any return
450 values from the operation are stored within the status object.
452 The listing below says little about whether the opcodes return error
453 information. For now assume that they can either return a status object,
454 or return nothing. Error handling is discussed more thoroughly below in
455 L<Error Handling>.
457 =head3 I/O Stream Opcodes
459 =head4 Opening and closing streams
461 =over 4
463 =item C<open>
465 =begin PIR_FRAGMENT_INVALID
467   $P0 = open $S1
468   $P0 = open $S1, $S2
469   $P0 = open $P1
470   $P0 = open $P1, $S2
472 =end PIR_FRAGMENT_INVALID
474 Opens a stream object based on a file path in $S1 and returns it.  The
475 stream object defaults to read/write mode. The optional string argument
476 $S2 specifies the mode of the stream (read, write, append, read/write,
477 etc.).  Currently the mode of the stream is set with a string argument
478 similar to Perl 5 syntax, but a language-agnostic mode string is
479 preferable, using 'r' for read, 'w' for write, 'a' for append, and 'p'
480 for pipe.
482 The asynchronous version takes a PMC callback as an additional final
483 argument. When the open operation is complete, it invokes the callback
484 with a single argument: a status object containing the opened stream
485 object.
487 =item C<close>
489 =begin PIR_FRAGMENT_INVALID
491   close $P0
492   close $P0, $P1
494 =end PIR_FRAGMENT_INVALID
496 Closes a stream object. It takes a single string object argument and
497 returns a status object.
499 The asynchronous version takes an additional final PMC callback
500 argument. When the close operation is complete, it invokes the callback,
501 passing it a status object.
503 =back
505 =head4 Retrieving existing streams
507 These opcodes do not have asynchronous variants.
509 =over 4
511 =item *
513 C<getstdin>, C<getstdout>, and C<getstderr> return a stream object for
514 standard input, standard output, and standard error, respectively.
516 =item *
518 C<fdopen> converts an existing and already open UNIX integer file
519 descriptor into a stream object. It also takes a string argument to
520 specify the mode.
522 =back
524 =head4 Writing to streams
526 =over 4
528 =item C<print>
530 =begin PIR_FRAGMENT_INVALID
532   print $I0
533   print $N0
534   print $S0
535   print $P0
536   print $P0, $I1
537   print $P0, $N1
538   print $P0, $S1
539   print $P0, $P1
540   print $P0, $I1, $P2
541   print $P0, $N1, $P2
542   print $P0, $S1, $P2
543   print $P0, $P1, $P2
545 =end PIR_FRAGMENT_INVALID
547 Writes an integer, float, string, or PMC value to a stream.  It
548 writes to standard output by default, but optionally takes a PMC
549 argument to select another stream to write to.
551 The asynchronous version takes an additional final PMC callback
552 argument. When the print operation is complete, it invokes the callback,
553 passing it a status object.
555 =item C<printerr>
557 =begin PIR_FRAGMENT
559   printerr $I0
560   printerr $N0
561   printerr $S0
562   printerr $P0
564 =end PIR_FRAGMENT
566 Writes an integer, float, string, or PMC value to standard error.
568 There is no asynchronous variant of C<printerr>. [It's just a shortcut.
569 If they want an asynchronous version, they can use C<print>.]
571 =back
573 =head4 Reading from streams
575 =over 4
577 =item C<read>
579 =begin PIR_FRAGMENT_INVALID
581   $S0 = read $I1
582   $S0 = read $P1, $I2
583   $P0 = read $P1, $I2, $P3
585 =end PIR_FRAGMENT_INVALID
587 Retrieves a specified number of bytes, $I2, from a stream, $P2, into a
588 string, $S0. [Note this is bytes, not codepoints.] By default it reads
589 from standard input, but it also takes an alternate stream object source
590 as an optional argument.
592 The asynchronous version takes an additional final PMC callback
593 argument, and only returns a status object. When the read operation is
594 complete, it invokes the callback, passing it a status object and a
595 string of bytes.
597 =item C<readline>
599 =begin PIR_FRAGMENT_INVALID
601   $S0 = readline $P1
602   $P0 = readline $P1, $P2
604 =end PIR_FRAGMENT_INVALID
606 Retrieves a single line from a stream into a string. Calling
607 C<readline> flags the stream as operating in line-buffer mode.
609 The asynchronous version takes an additional final PMC callback
610 argument, and only returns a status object. When the readline operation
611 is complete, it invokes the callback, passing it a status object and a
612 string of bytes.
614 =item C<peek>
616 =begin PIR_FRAGMENT
618   $S0 = peek
619   $S0 = peek $P1
621 =end PIR_FRAGMENT
623 [C<peek>, C<seek>, C<tell>, and C<poll> are all candidates for moving from
624 opcodes to FileHandle object methods.]
626 C<peek> retrieves the next byte from a stream into a string, but doesn't
627 remove it from the stream. By default it reads from standard input, but
628 it also takes a stream object argument for an alternate source.
630 There is no asynchronous version of C<peek>. [Does anyone have a line
631 of reasoning why one might be needed? The concept of "next byte" seems
632 to be a synchronous one.]
634 =back
636 =head4 Retrieving and setting stream properties
638 =over 4
640 =item C<seek>
642 =begin PIR_FRAGMENT_INVALID
644   seek $P0, $I1, $I2
645   seek $P0, $I1, $I2, $I3
646   seek $P0, $I1, $I2, $P3
647   seek $P0, $I1, $I2, $I3, $P4
649 =end PIR_FRAGMENT_INVALID
651 Sets the current file position of a stream object, $P0, to an integer
652 byte offset, $I1, from an integer starting position, $I2, (0 for the
653 start of the file, 1 for the current position, and 2 for the end of the
654 file). It also has a 64-bit variant that sets the byte offset by two
655 integer arguments, $I1 and $I2, (one for the first 32 bits of the 64-bit
656 offset, and one for the second 32 bits). [The two-register emulation for
657 64-bit integers may be deprecated in the future.]
659 The asynchronous version takes an additional final PMC callback
660 argument. When the seek operation is complete, it invokes the callback,
661 passing it a status object and the stream object it was called on.
663 =item C<tell>
665 =begin PIR_FRAGMENT_INVALID
667   $I0 = tell $P1
668   ($I0, $I1) = tell $P2
670 =end PIR_FRAGMENT_INVALID
672 Retrieves the current file position of a stream object.  It also has a
673 64-bit variant that returns the byte offset as two integers (one for the
674 first 32 bits of the 64-bit offset, and one for the second 32 bits).
675 [The two-register emulation for 64-bit integers may be deprecated in the
676 future.]
678 No asynchronous version.
680 =item C<poll>
682 =begin PIR_FRAGMENT_INVALID
684   $I0 = poll $P1, $I2, $I3, $I4
686 =end PIR_FRAGMENT_INVALID
688 Polls a stream or socket object for particular types of events (an
689 integer flag) at a frequency set by seconds and microseconds (the final
690 two integer arguments). [At least, that's what the documentation in
691 src/io/io.c says. In actual fact, the final two arguments seem to be
692 setting the timeout, exactly the same as the corresponding argument to
693 the system version of C<poll>.]
695 See the system documentation for C<poll> to see the constants for event
696 types and return status.
698 This opcode is inherently synchronous (poll is "synchronous I/O
699 multiplexing"), but it can retrieve status information from a stream or
700 socket object whether the object is being used synchronously or
701 asynchronously.
703 =back
705 =head3 Filesystem Opcodes
707 [Okay, I'm seriously considering moving most of these to methods on the
708 ParrotIO object. More than that, moving them into a role that is
709 composed into the ParrotIO object when needed. For the ones that have
710 the form C<opcodename io_object, arguments>, I can't see that it's
711 much less effort than C<io_object.'methodname'(arguments)> for either
712 manually writing PIR or generating PIR. The slowest thing about I/O is
713 I/O, so I can't see that we're getting much speed gain out of making
714 them opcodes. The ones to keep as opcodes are C<unlink>, C<rmdir>, and
715 C<opendir>.]
717 =over 4
719 =item *
721 C<stat> retrieves information about a file on the filesystem. It takes a
722 string filename or an integer argument of a UNIX file descriptor [or an
723 already opened stream object?], and an integer flag for the type of
724 information requested. It returns an integer containing the requested
725 information.  The following constants are defined for the type of
726 information requested (see F<runtime/parrot/include/stat.pasm>):
728   0    STAT_EXISTS
729            Whether the file exists.
730   1    STAT_FILESIZE
731            The size of the file.
732   2    STAT_ISDIR
733            Whether the file is a directory.
734   3    STAT_ISDEV
735            Whether the file is a device such as a terminal or a disk.
736   4    STAT_CREATETIME
737            The time the file was created.
738            (Currently just returns -1.)
739   5    STAT_ACCESSTIME
740            The last time the file was accessed.
741   6    STAT_MODIFYTIME
742            The last time the file data was changed.
743   7    STAT_CHANGETIME
744            The last time the file metadata was changed.
745   8    STAT_BACKUPTIME
746            The last time the file was backed up.
747            (Currently just returns -1.)
748   9    STAT_UID
749            The user ID of the file.
750   10   STAT_GID
751            The group ID of the file.
753 The asynchronous version takes an additional final PMC callback
754 argument, and only returns a status object. When the stat operation is
755 complete, it invokes the callback, passing it a status object and an
756 integer containing the status information.
758 =item *
760 C<unlink> deletes a file from the filesystem. It takes a single string
761 argument of a filename (including the path).
763 The asynchronous version takes an additional final PMC callback
764 argument. When the unlink operation is complete, it invokes the
765 callback, passing it a status object.
767 =item *
769 C<rmdir> deletes a directory from the filesystem if that directory is
770 empty. It takes a single string argument of a directory name (including
771 the path).
773 The asynchronous version takes an additional final PMC callback
774 argument. When the rmdir operation is complete, it invokes the callback,
775 passing it a status object.
777 =item *
779 C<opendir> opens a stream object for a directory. It takes a single
780 string argument of a directory name (including the path) and returns a
781 stream object.
783 The asynchronous version takes an additional final PMC callback
784 argument, and only returns a status object. When the opendir operation
785 is complete, it invokes the callback, passing it a status object and a
786 newly created stream object.
788 =item *
790 C<readdir> reads a single item from an open directory stream object. It
791 takes a single stream object argument and returns a string containing
792 the path and filename/directory name of the current item. (i.e. the
793 directory stream object acts as an iterator.)
795 The asynchronous version takes an additional final PMC callback
796 argument, and only returns a status object. When the readdir operation
797 is complete, it invokes the callback, passing it a status object and the
798 string result.
800 =item *
802 C<telldir> returns the current position of C<readdir> operations on a
803 directory stream object.
805 No asynchronous version.
807 =item *
809 C<seekdir> sets the current position of C<readdir> operations on a
810 directory stream object. It takes a stream object argument and an
811 integer for the position. [The system C<seekdir> requires that the
812 position argument be the result of a previous C<telldir> operation.]
814 The asynchronous version takes an additional final PMC callback
815 argument. When the seekdir operation is complete, it invokes the
816 callback, passing it a status object and the directory stream object it
817 was called on.
819 =item *
821 C<rewinddir> sets the current position of C<readdir> operations on a
822 directory stream object back to the beginning of the directory. It takes
823 a stream object argument.
825 No asynchronous version.
827 =item *
829 C<closedir> closes a directory stream object. It takes a single stream
830 object argument.
832 The asynchronous version takes an additional final PMC callback
833 argument. When the closedir operation is complete, it invokes the
834 callback, passing it a status object.
836 =back
838 =head3 Network I/O Methods
840 Most of these methods take a default set of arguments the same as the standard
841 UNIX interface. Many also take additional optional arguments. There are no
842 network-specific opcodes.
844 =over 4
846 =item *
848 C<socket> returns a new socket object from a given address family,
849 socket type, and protocol number (all integers). The socket object's
850 boolean value can be tested for whether the socket was created.
852 The asynchronous version takes an additional final PMC callback
853 argument, and only returns a status object. When the socket operation is
854 complete, it invokes the callback, passing it a status object and a new
855 socket object.
857 =item *
859 C<sockaddr> returns an object representing a socket address, generated
860 from a port number (integer) and an address (string).
862 No asynchronous version.
864 =item *
866 C<connect> connects a socket object to an address.
868 The asynchronous version takes an additional final PMC callback
869 argument, and only returns a status object. When the socket operation is
870 complete, it invokes the callback, passing it a status object and the
871 socket object it was called on. [If you want notification when a connect
872 operation is completed, you probably want to do something with that
873 connected socket object.]
875 =item *
877 C<recv> receives a message from a connected socket object. It returns
878 the message in a string.
880 The asynchronous version takes an additional final PMC callback
881 argument, and only returns a status object. When the recv operation is
882 complete, it invokes the callback, passing it a status object and a
883 string containing the received message.
885 =item *
887 C<send> sends a message string to a connected socket object.
889 The asynchronous version takes an additional final PMC callback
890 argument, and only returns a status object. When the send operation is
891 complete, it invokes the callback, passing it a status object.
893 =item *
895 C<sendto> sends a message string to an address specified in an address
896 object (first connecting to the address).
898 The asynchronous version takes an additional final PMC callback
899 argument, and only returns a status object. When the sendto operation is
900 complete, it invokes the callback, passing it a status object.
903 =item *
905 C<bind> binds a socket object to the port and address specified by an
906 address object (the packed result of C<sockaddr>).
908 The asynchronous version takes an additional final PMC callback
909 argument, and only returns a status object. When the bind operation is
910 complete, it invokes the callback, passing it a status object and the
911 socket object it was called on. [If you want notification when a bind
912 operation is completed, you probably want to do something with that
913 bound socket object.]
915 =item *
917 C<listen> specifies that a socket object is willing to accept incoming
918 connections. The integer argument gives the maximum size of the queue
919 for pending connections.
921 There is no asynchronous version. C<listen> marks a set of attributes on
922 the socket object.
924 =item *
926 C<accept> accepts a new connection on a given socket object, and returns
927 a newly created socket object for the connection.
929 The asynchronous version takes an additional final PMC callback
930 argument, and only returns a status object. When the accept operation
931 receives a new connection, it invokes the callback, passing it a status
932 object and a newly created socket object for the connection. [While the
933 synchronous C<accept> has to be called repeatedly in a loop (once for
934 each connection received), the asynchronous version is only called once,
935 but continues to send new connection events until the socket is closed.]
937 =item *
939 C<shutdown> closes a socket object for reading, for writing, or for all
940 I/O. It takes a socket object argument and an integer argument for the
941 type of shutdown:
943   0    PIOSHUTDOWN_READ
944            Close the socket object for reading.
945   1    PIOSHUTDOWN_WRITE
946            Close the socket object for writing.
947   2    PIOSHUTDOWN
948            Close the socket object.
950 =back
952 =head3 Error Handling
954 Currently some of the networking opcodes (C<connect>, C<recv>, C<send>,
955 C<poll>, C<bind>, and C<listen>) return an integer indicating the status
956 of the call, -1 or a system error code if unsuccessful. Other I/O
957 opcodes (such as C<accept>) have various different
958 strategies for error notification, and others have no way of marking
959 errors at all. We want to unify all I/O opcodes so they use a consistent
960 strategy for error notification.
962 =head4 Synchronous operations
964 Synchronous I/O operations return an integer status code indicating
965 success or failure in addition to their ordinary return value(s). This
966 approach has the advantage of being lightweight: returning a single
967 additional integer is cheap.
969 [Discuss: should synchronous operations take the same error handling
970 strategy as asynchronous ones?]
973 =head4 Asynchronous operations
975 Asynchronous I/O operations return a status object. The status object
976 contains an integer status code, string status/error message, and
977 boolean success value.
979 An error callback may be set on a status object, though it isn't
980 required. This callback will be invoked if the asynchronous operation
981 terminates in an error condition. The error callback takes one argument,
982 which is the status object containing all information about the failed
983 call. If no error callback is set, then the standard callback will be
984 invoked, and the user will need to check for error conditions in the
985 status object as the first operation of the handler code.
987 =head4 Exceptions
989 At some point in the future, I/O objects may also provide a way to throw
990 exceptions on error conditions. This feature will be enabled by calling
991 a method on the I/O object to set an internal flag.  The exception
992 throwing will be implemented as a method call on the status object.
994 Note that exception handlers for asynchronous I/O operations will likely
995 have to be set at a global scope because execution will have left the
996 dynamic scope of the I/O call by the time the error occurs.
998 =head3 IPv6 Support
1000 The transition from IPv4 to IPv6 is in progress, though not likely to be
1001 complete anytime soon. Most operating systems today offer at least
1002 dual-stack IPv6 implementations, so they can use either IPv4 or IPv6,
1003 depending on what's available. Parrot also needs to support either
1004 protocol. For the most part, the network I/O opcodes should internally
1005 handle either addressing scheme, without requiring the user to specify
1006 which scheme is being used.
1008 IETF recommends defaulting to IPv6 connections and falling back to IPv4
1009 connections when IPv6 fails. This would give us more solid testing of
1010 Parrot's compatibility IPv6, but may be too slow. Either way, it's a
1011 good idea to make setting the default (or selecting one exclusively) an
1012 option when compiling Parrot.
1014 The most important issues for Parrot to consider with IPv6 are:
1016 =over 4
1018 =item *
1020 Support 128 bit addresses. IPv6 addresses are colon-separated
1021 hexadecimal numbers, such as C<20a:95ff:fef5:7e5e>.
1023 =item *
1025 Any address parsing should be able to support the address separated from
1026 a port number or prefix/length by brackets: C<[20a:95ff:fef5:7e5e]:80>
1027 and C<[20a:95ff::]/64>.
1029 =item *
1031 Packed addresses, such as the result of the C<sockaddr> opcode, should
1032 be passed around as an object (or at least a structure) rather than as a
1033 string.
1035 =back
1037 See the relevant IETF RFCs: "Application Aspects of IPv6 Transition"
1038 (http://www.ietf.org/rfc/rfc4038.txt) and "Basic Socket Interface
1039 Extensions for IPv6" (http://www.ietf.org/rfc/rfc3493.txt).
1041 =head2 Attachments
1043 None.
1045 =head2 Footnotes
1047 None.
1049 =head2 Links
1051 L<http://en.wikipedia.org/wiki/Asynchronous_I/O> for a relatively
1052 comprehensive list of asynchronous I/O implementation options.
1054 =head2 References
1056   F<src/io/core.c>
1057   F<src/ops/io.ops>
1058   F<include/parrot/io.h>
1059   F<runtime/parrot/library/Stream/*>
1060   F<src/io/unix.c>
1061   F<src/io/win32.c>
1062   Perl 5's IO::AIO
1063   Perl 5's POE
1065 =cut
1067 __END__
1068 Local Variables:
1069   fill-column:78
1070 End:
1071 vim: expandtab shiftwidth=4: