ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-Fork-RPC/RPC/Sync.pm
Revision: 1.21
Committed: Thu Sep 12 15:22:37 2019 UTC (7 years ago) by root
Branch: MAIN
Changes since 1.20: +2 -0 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 our $VERSION = 2; # protocol version
6
7 # declare only
8 sub AnyEvent::Fork::RPC::event;
9
10 sub do_exit { exit } # workaround for perl 5.14 and below
11
12 # the goal here is to keep this simple, small and efficient
13 sub run {
14 my %kv = splice @_, pop;
15
16 my $rfh = shift;
17 my $wfh = fileno $rfh ? $rfh : *STDOUT;
18
19 my $function = delete $kv{function};
20 my $serialiser = delete $kv{serialiser};
21 my $rlen = delete $kv{rlen};
22
23 $0 =~ s/^(\d+).*$/$1 $function/s;
24
25 {
26 package main;
27 my $init = delete $kv{init};
28 &$init if length $init;
29 $function = \&$function; # resolve function early for extra speed
30 }
31
32 %kv = (); # save some very small amount of memory
33
34 my ($f, $t) = eval $serialiser; die $@ if $@;
35
36 my $write = sub {
37 my $got = syswrite $wfh, $_[0];
38
39 while ($got < length $_[0]) {
40 my $len = syswrite $wfh, $_[0], 1<<30, $got;
41
42 defined $len
43 or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?";
44
45 $got += $len;
46 }
47 };
48
49 *AnyEvent::Fork::RPC::event = sub {
50 $write->(pack "NN/a*", 0, &$f);
51 };
52
53 my $rbuf;
54
55 while (sysread $rfh, $rbuf, $rlen - length $rbuf, length $rbuf) {
56 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
57
58 while () {
59 last if 4 > length $rbuf;
60 my $len = unpack "N", $rbuf;
61 last if 4 + $len > length $rbuf;
62
63 $write->(pack "NN/a*", 1, $f->($function->($t->(substr $rbuf, 4, $len))));
64
65 substr $rbuf, 0, 4 + $len, "";
66 }
67 }
68
69 shutdown $wfh, 1;
70 exit; # work around broken win32 perls
71 }
72
73 1
74