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