ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC/Sync.pm
Revision: 1.5
Committed: Wed Apr 17 20:05:13 2013 UTC (13 years, 5 months ago) by root
Branch: MAIN
Changes since 1.4: +0 -7 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 root 1.4 our $master;
9    
10 root 1.1 sub xwrite($) {
11     my $got = syswrite $master, $_[0];
12    
13     while ($got < length $_[0]) {
14     my $len = syswrite $master, $_[0], 1<<30, $got;
15    
16     defined $len
17     or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?";
18    
19     $got += $len;
20     }
21     }
22    
23     # the goal here is to keep this simple, small and efficient
24     sub run {
25 root 1.4 my ($function, $init, $serialiser) = splice @_, -3, 3,;
26    
27 root 1.1 $master = shift;
28    
29     {
30     package main;
31     &$init if length $init;
32     $function = \&$function; # resolve function early for extra speed
33     }
34    
35 root 1.4 my ($f, $t) = eval $serialiser; die $@ if $@;
36 root 1.1
37 root 1.4 *AnyEvent::Fork::RPC::event = sub {
38     xwrite pack "L/a*", $f->(@_, 1);
39     };
40    
41     my ($rlen, $rbuf) = 512 - 16;
42 root 1.1
43 root 1.2 # the read length here is not optimal, but since we re-use $rbuf,
44     # its allocated size should eventually auto-adjust.
45 root 1.4 while (sysread $master, $rbuf, $rlen - length $rbuf, length $rbuf) {
46     $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
47    
48 root 1.2 while () {
49     last if 5 > length $rbuf;
50     my $len = unpack "L", $rbuf;
51     last if 4 + $len > length $rbuf;
52     my @r = $t->(substr $rbuf, 4, $len);
53     substr $rbuf, 0, 4 + $len, "";
54 root 1.1
55 root 1.2 xwrite pack "L/a*", $f->($function->(@r), "");
56     }
57 root 1.1 }
58    
59     shutdown $master, 1;
60     }
61    
62     1
63