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