… | |
… | |
485 | my $self = shift; |
485 | my $self = shift; |
486 | |
486 | |
487 | # ideally, we would want to use "a (w/a)*" as format string, but perl |
487 | # ideally, we would want to use "a (w/a)*" as format string, but perl |
488 | # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack |
488 | # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack |
489 | # it. |
489 | # it. |
490 | push @{ $self->[QUEUE] }, pack "a L/a*", $_[0], $_[1]; |
490 | push @{ $self->[QUEUE] }, pack "a N/a*", $_[0], $_[1]; |
491 | |
491 | |
492 | $self->[WW] ||= AE::io $self->[FH], 1, sub { |
492 | $self->[WW] ||= AE::io $self->[FH], 1, sub { |
493 | do { |
493 | do { |
494 | # send the next "thing" in the queue - either a reference to an fh, |
494 | # send the next "thing" in the queue - either a reference to an fh, |
495 | # or a plain string. |
495 | # or a plain string. |
… | |
… | |
876 | |
876 | |
877 | There are a number of limitations though: C<send_fh> will only work if the |
877 | There are a number of limitations though: C<send_fh> will only work if the |
878 | L<IO::FDPass> module is loadable by the remote perl and the two processes |
878 | L<IO::FDPass> module is loadable by the remote perl and the two processes |
879 | are connected in a way that let's L<IO::FDPass> do it's work. |
879 | are connected in a way that let's L<IO::FDPass> do it's work. |
880 | |
880 | |
881 | This will therefore not work over a network conenction. From this follows |
881 | This will therefore not work over a network connection. From this follows |
882 | that C<fork> will also not work under these circumstances, as it relies on |
882 | that C<fork> will also not work under these circumstances, as it relies on |
883 | C<send_fh> internally. |
883 | C<send_fh> internally. |
|
|
884 | |
|
|
885 | Although not a limitation of this module, keep in mind that the |
|
|
886 | "communications socket" is simply C<STDIN>, and depending on how you |
|
|
887 | started F<perl> (e.g. via F<ssh>), it might only be half-duplex. This is |
|
|
888 | fine for C<AnyEvent::Fork>, but your C<run> function might want to use |
|
|
889 | C<STDIN> (or the "communications socket") for input and C<STDOUT> for |
|
|
890 | output. |
|
|
891 | |
|
|
892 | You can support both cases by checking the C<fileno> of the handle passed |
|
|
893 | to your run function: |
|
|
894 | |
|
|
895 | sub run { |
|
|
896 | my ($rfh) = @_; |
|
|
897 | |
|
|
898 | my $wfh = fileno $rfh ? $rfh : *STDOUT; |
|
|
899 | |
|
|
900 | # now use $rfh for reading and $wfh for writing |
|
|
901 | } |
884 | |
902 | |
885 | =cut |
903 | =cut |
886 | |
904 | |
887 | sub new_from_stdio { |
905 | sub new_from_stdio { |
888 | my ($class, $fh) = @_; |
906 | my ($class, $fh) = @_; |
… | |
… | |
891 | |
909 | |
892 | # send startup code |
910 | # send startup code |
893 | push @{ $self->[QUEUE] }, |
911 | push @{ $self->[QUEUE] }, |
894 | (do "AnyEvent/Fork/serve.pl") |
912 | (do "AnyEvent/Fork/serve.pl") |
895 | . <<'EOF'; |
913 | . <<'EOF'; |
896 | { |
|
|
897 | open my $fh, "+<&0" |
|
|
898 | or die "AnyEvent::Fork::Serve::stdio: unable to open communications socket: $!\n"; |
|
|
899 | open STDIN , ">&2"; |
|
|
900 | open STDOUT, ">&2"; |
|
|
901 | |
914 | |
902 | $OWNER = "another process"; |
915 | $OWNER = "another process"; |
903 | $0 = "AnyEvent::Fork/stdio of $OWNER"; |
916 | $0 = "AnyEvent::Fork/stdio of $OWNER"; |
904 | |
917 | |
905 | @_ = $fh; |
918 | serve *STDIN; |
906 | } |
|
|
907 | |
|
|
908 | &serve; |
|
|
909 | __END__ |
919 | __END__ |
910 | EOF |
920 | EOF |
911 | |
921 | |
912 | # the data is only sent when the user requests additional things, which |
922 | # the data is only sent when the user requests additional things, which |
913 | # is likely early enough for our purposes. |
923 | # is likely early enough for our purposes. |