ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC/Sync.pm
Revision: 1.8
Committed: Wed Apr 17 20:19:41 2013 UTC (13 years, 5 months ago) by root
Branch: MAIN
Changes since 1.7: +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     my $data = pack "L/a*", &$f;
23    
24     my $got = syswrite $master, $data;
25    
26     while ($got < length $data) {
27     my $len = syswrite $master, $data, 1<<30, $got;
28    
29     defined $len
30     or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?";
31    
32     $got += $len;
33     }
34     };
35    
36 root 1.4 *AnyEvent::Fork::RPC::event = sub {
37 root 1.7 $write->(@_, 1);
38 root 1.4 };
39    
40     my ($rlen, $rbuf) = 512 - 16;
41 root 1.1
42 root 1.4 while (sysread $master, $rbuf, $rlen - length $rbuf, length $rbuf) {
43     $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
44    
45 root 1.2 while () {
46 root 1.8 last if 4 > length $rbuf;
47 root 1.2 my $len = unpack "L", $rbuf;
48     last if 4 + $len > length $rbuf;
49     my @r = $t->(substr $rbuf, 4, $len);
50     substr $rbuf, 0, 4 + $len, "";
51 root 1.1
52 root 1.7 $write->($function->(@r), "");
53 root 1.2 }
54 root 1.1 }
55    
56     shutdown $master, 1;
57     }
58    
59     1
60