ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC/Sync.pm
Revision: 1.14
Committed: Sat Aug 31 16:35:33 2013 UTC (13 years ago) by root
Branch: MAIN
CVS Tags: rel-1_2
Changes since 1.13: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package AnyEvent::Fork::RPC::Sync;
2
3 use common::sense; # actually required to avoid spurious warnings...
4
5 # declare only
6 sub AnyEvent::Fork::RPC::event;
7
8 # the goal here is to keep this simple, small and efficient
9 sub run {
10 my ($function, $init, $serialiser, undef) = splice @_, -4, 4;
11 my $rfh = shift;
12 my $wfh = fileno $rfh ? $rfh : *STDOUT;
13
14 $0 =~ s/^AnyEvent::Fork::RPC::Sync::run of /$function of /;
15
16 {
17 package main;
18 &$init if length $init;
19 $function = \&$function; # resolve function early for extra speed
20 }
21
22 my ($f, $t) = eval $serialiser; die $@ if $@;
23
24 my $write = sub {
25 my $got = syswrite $wfh, $_[0];
26
27 while ($got < length $_[0]) {
28 my $len = syswrite $wfh, $_[0], 1<<30, $got;
29
30 defined $len
31 or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?";
32
33 $got += $len;
34 }
35 };
36
37 *AnyEvent::Fork::RPC::event = sub {
38 $write->(pack "NN/a*", 0, &$f);
39 };
40
41 my ($rlen, $rbuf) = 512 - 16;
42
43 while (sysread $rfh, $rbuf, $rlen - length $rbuf, length $rbuf) {
44 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
45
46 while () {
47 last if 4 > length $rbuf;
48 my $len = unpack "N", $rbuf;
49 last if 4 + $len > length $rbuf;
50
51 $write->(pack "NN/a*", 1, $f->($function->($t->(substr $rbuf, 4, $len))));
52
53 substr $rbuf, 0, 4 + $len, "";
54 }
55 }
56
57 shutdown $wfh, 1;
58 exit; # work around broken win32 perls
59 }
60
61 1
62