ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-Fork-RPC/RPC/Sync.pm
Revision: 1.20
Committed: Thu Sep 12 15:15:49 2019 UTC (7 years ago) by root
Branch: MAIN
Changes since 1.19: +10 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 package AnyEvent::Fork::RPC::Sync;
2    
3     use common::sense; # actually required to avoid spurious warnings...
4    
5 root 1.20 our $VERSION = 2; # protocol version
6    
7 root 1.4 # declare only
8     sub AnyEvent::Fork::RPC::event;
9 root 1.1
10 root 1.15 sub do_exit { exit } # workaround for perl 5.14 and below
11    
12 root 1.1 # the goal here is to keep this simple, small and efficient
13     sub run {
14 root 1.20 my %kv = splice @_, pop;
15    
16 root 1.12 my $rfh = shift;
17     my $wfh = fileno $rfh ? $rfh : *STDOUT;
18 root 1.1
19 root 1.20 my $function = delete $kv{function};
20     my $serialiser = delete $kv{serialiser};
21     my $rlen = delete $kv{rlen};
22    
23 root 1.19 $0 =~ s/^(\d+).*$/$1 $function/s;
24 root 1.13
25 root 1.1 {
26     package main;
27 root 1.20 my $init = delete $kv{init};
28 root 1.1 &$init if length $init;
29     $function = \&$function; # resolve function early for extra speed
30     }
31    
32 root 1.4 my ($f, $t) = eval $serialiser; die $@ if $@;
33 root 1.1
34 root 1.7 my $write = sub {
35 root 1.12 my $got = syswrite $wfh, $_[0];
36 root 1.7
37 root 1.9 while ($got < length $_[0]) {
38 root 1.12 my $len = syswrite $wfh, $_[0], 1<<30, $got;
39 root 1.7
40     defined $len
41     or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?";
42    
43     $got += $len;
44     }
45     };
46    
47 root 1.4 *AnyEvent::Fork::RPC::event = sub {
48 root 1.12 $write->(pack "NN/a*", 0, &$f);
49 root 1.4 };
50    
51 root 1.20 my $rbuf;
52 root 1.1
53 root 1.12 while (sysread $rfh, $rbuf, $rlen - length $rbuf, length $rbuf) {
54 root 1.4 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
55    
56 root 1.2 while () {
57 root 1.8 last if 4 > length $rbuf;
58 root 1.12 my $len = unpack "N", $rbuf;
59 root 1.2 last if 4 + $len > length $rbuf;
60 root 1.9
61 root 1.12 $write->(pack "NN/a*", 1, $f->($function->($t->(substr $rbuf, 4, $len))));
62 root 1.9
63 root 1.2 substr $rbuf, 0, 4 + $len, "";
64     }
65 root 1.1 }
66    
67 root 1.12 shutdown $wfh, 1;
68 root 1.11 exit; # work around broken win32 perls
69 root 1.1 }
70    
71     1
72