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

# 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) = splice @_, -3, 3;
11 my $rfh = shift;
12 my $wfh = fileno $rfh ? $rfh : *STDOUT;
13
14 {
15 package main;
16 &$init if length $init;
17 $function = \&$function; # resolve function early for extra speed
18 }
19
20 my ($f, $t) = eval $serialiser; die $@ if $@;
21
22 my $write = sub {
23 my $got = syswrite $wfh, $_[0];
24
25 while ($got < length $_[0]) {
26 my $len = syswrite $wfh, $_[0], 1<<30, $got;
27
28 defined $len
29 or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?";
30
31 $got += $len;
32 }
33 };
34
35 *AnyEvent::Fork::RPC::event = sub {
36 $write->(pack "NN/a*", 0, &$f);
37 };
38
39 my ($rlen, $rbuf) = 512 - 16;
40
41 while (sysread $rfh, $rbuf, $rlen - length $rbuf, length $rbuf) {
42 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
43
44 while () {
45 last if 4 > length $rbuf;
46 my $len = unpack "N", $rbuf;
47 last if 4 + $len > length $rbuf;
48
49 $write->(pack "NN/a*", 1, $f->($function->($t->(substr $rbuf, 4, $len))));
50
51 substr $rbuf, 0, 4 + $len, "";
52 }
53 }
54
55 shutdown $wfh, 1;
56 exit; # work around broken win32 perls
57 }
58
59 1
60