ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC/Sync.pm
Revision: 1.15
Committed: Tue Oct 15 09:15:59 2013 UTC (12 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-1_21, rel-1_22
Changes since 1.14: +2 -0 lines
Log Message:
1.21

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