| 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.4 |
while (sysread $master, $rbuf, $rlen - length $rbuf, length $rbuf) { |
| 44 |
|
|
$rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
| 45 |
|
|
|
| 46 |
root |
1.2 |
while () { |
| 47 |
|
|
last if 5 > length $rbuf; |
| 48 |
|
|
my $len = unpack "L", $rbuf; |
| 49 |
|
|
last if 4 + $len > length $rbuf; |
| 50 |
|
|
my @r = $t->(substr $rbuf, 4, $len); |
| 51 |
|
|
substr $rbuf, 0, 4 + $len, ""; |
| 52 |
root |
1.1 |
|
| 53 |
root |
1.2 |
xwrite pack "L/a*", $f->($function->(@r), ""); |
| 54 |
|
|
} |
| 55 |
root |
1.1 |
} |
| 56 |
|
|
|
| 57 |
|
|
shutdown $master, 1; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
1 |
| 61 |
|
|
|