ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC/Async.pm
Revision: 1.17
Committed: Thu Sep 12 15:22:37 2019 UTC (7 years ago) by root
Branch: MAIN
Changes since 1.16: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 package AnyEvent::Fork::RPC::Async;
2    
3     use common::sense; # actually required to avoid spurious warnings...
4    
5 root 1.16 our $VERSION = 2; # protocol version
6    
7 root 1.1 use Errno ();
8    
9     use AnyEvent;
10    
11     # declare only
12     sub AnyEvent::Fork::RPC::event;
13    
14 root 1.9 sub do_exit { exit } # workaround for perl 5.14 and below
15    
16 root 1.1 sub run {
17 root 1.16 my %kv = splice @_, pop;
18    
19 root 1.4 my $rfh = shift;
20     my $wfh = fileno $rfh ? $rfh : *STDOUT;
21 root 1.1
22 root 1.16 my $function = delete $kv{function};
23     my $serialiser = delete $kv{serialiser};
24     my $rlen = delete $kv{rlen};
25     my $done = delete $kv{done};
26    
27 root 1.15 $0 =~ s/^(\d+).*$/$1 $function/s;
28 root 1.5
29 root 1.1 {
30     package main;
31 root 1.16 my $init = delete $kv{init};
32 root 1.1 &$init if length $init;
33     $function = \&$function; # resolve function early for extra speed
34     }
35    
36 root 1.17 %kv = (); # save some very small amount of memory
37    
38 root 1.1 my $busy = 1; # exit when == 0
39    
40 root 1.8 my ($f, $t) = eval $serialiser; AE::log fatal => $@ if $@;
41 root 1.1 my ($wbuf, $ww);
42    
43     my $wcb = sub {
44 root 1.4 my $len = syswrite $wfh, $wbuf;
45 root 1.1
46     unless (defined $len) {
47     if ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) {
48     undef $ww;
49 root 1.8 AE::log fatal => "AnyEvent::Fork::RPC: write error ($!), parent gone?";
50 root 1.1 }
51     }
52    
53     substr $wbuf, 0, $len, "";
54    
55     unless (length $wbuf) {
56     undef $ww;
57 root 1.3 unless ($busy) {
58 root 1.4 shutdown $wfh, 1;
59 root 1.6 @_ = (); goto &$done;
60 root 1.3 }
61 root 1.1 }
62     };
63    
64     my $write = sub {
65     $wbuf .= $_[0];
66 root 1.4 $ww ||= AE::io $wfh, 1, $wcb;
67 root 1.1 };
68    
69     *AnyEvent::Fork::RPC::event = sub {
70 root 1.4 $write->(pack "NN/a*", 0, &$f);
71 root 1.1 };
72    
73 root 1.16 my ($rbuf, $rw);
74 root 1.1
75 root 1.2 my $len;
76 root 1.1
77 root 1.4 $rw = AE::io $rfh, 0, sub {
78 root 1.1 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
79 root 1.4 $len = sysread $rfh, $rbuf, $rlen - length $rbuf, length $rbuf;
80 root 1.1
81     if ($len) {
82     while (8 <= length $rbuf) {
83 root 1.4 (my $id, $len) = unpack "NN", $rbuf;
84 root 1.1 8 + $len <= length $rbuf
85     or last;
86    
87     my @r = $t->(substr $rbuf, 8, $len);
88     substr $rbuf, 0, 8 + $len, "";
89    
90     ++$busy;
91     $function->(sub {
92     --$busy;
93 root 1.4 $write->(pack "NN/a*", $id, &$f);
94 root 1.1 }, @r);
95     }
96 root 1.7 } elsif (defined $len or $! == Errno::EINVAL) { # EINVAL is for microshit windoze
97 root 1.1 undef $rw;
98     --$busy;
99 root 1.4 $ww ||= AE::io $wfh, 1, $wcb;
100 root 1.1 } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) {
101     undef $rw;
102 root 1.8 AE::log fatal => "AnyEvent::Fork::RPC: read error in child: $!";
103 root 1.1 }
104     };
105    
106 root 1.14 $AnyEvent::MODEL eq "AnyEvent::Impl::EV"
107 root 1.10 ? EV::run ()
108 root 1.1 : AE::cv->recv;
109     }
110    
111     1
112