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