ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC.pm
(Generate patch)

Comparing AnyEvent-Fork-RPC/RPC.pm (file contents):
Revision 1.19 by root, Thu Apr 18 20:27:02 2013 UTC vs.
Revision 1.24 by root, Sat Apr 27 23:49:01 2013 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork 3AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork
4 4
5THE API IS NOT FINISHED, CONSIDER THIS A TECHNOLOGY DEMO 5THE API IS NOT FINISHED, CONSIDER THIS A BETA RELEASE
6 6
7=head1 SYNOPSIS 7=head1 SYNOPSIS
8 8
9 use AnyEvent::Fork::RPC; 9 use AnyEvent::Fork::RPC;
10 # use AnyEvent::Fork is not needed 10 # use AnyEvent::Fork is not needed
305 305
306use Errno (); 306use Errno ();
307use Guard (); 307use Guard ();
308 308
309use AnyEvent; 309use AnyEvent;
310# explicit version on next line, as some cpan-testers test with the 0.1 version,
311# ignoring dependencies, and this line will at least give a clear indication of that.
310use AnyEvent::Fork; # we don't actually depend on it, this is for convenience 312use AnyEvent::Fork 0.6; # we don't actually depend on it, this is for convenience
311 313
312our $VERSION = 0.1; 314our $VERSION = 0.2;
313 315
314=item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] 316=item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...]
315 317
316The traditional way to call it. But it is way cooler to call it in the 318The traditional way to call it. But it is way cooler to call it in the
317following way: 319following way:
524 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; 526 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
525 $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; 527 $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf;
526 528
527 if ($len) { 529 if ($len) {
528 while (8 <= length $rbuf) { 530 while (8 <= length $rbuf) {
529 ($id, $len) = unpack "LL", $rbuf; 531 ($id, $len) = unpack "NN", $rbuf;
530 8 + $len <= length $rbuf 532 8 + $len <= length $rbuf
531 or last; 533 or last;
532 534
533 my @r = $t->(substr $rbuf, 8, $len); 535 my @r = $t->(substr $rbuf, 8, $len);
534 substr $rbuf, 0, 8 + $len, ""; 536 substr $rbuf, 0, 8 + $len, "";
550 undef $rw; undef $ww; # it ends here 552 undef $rw; undef $ww; # it ends here
551 553
552 if (@rcb || %rcb) { 554 if (@rcb || %rcb) {
553 $on_error->("unexpected eof"); 555 $on_error->("unexpected eof");
554 } else { 556 } else {
555 $on_destroy->(); 557 $on_destroy->()
558 if $on_destroy;
556 } 559 }
557 } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { 560 } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) {
558 undef $rw; undef $ww; # it ends here 561 undef $rw; undef $ww; # it ends here
559 $on_error->("read: $!"); 562 $on_error->("read: $!");
560 } 563 }
576 $id = ($id == 0xffffffff ? 0 : $id) + 1; 579 $id = ($id == 0xffffffff ? 0 : $id) + 1;
577 $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops 580 $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops
578 581
579 $rcb{$id} = pop; 582 $rcb{$id} = pop;
580 583
581 $guard; # keep it alive 584 $guard if 0; # keep it alive
582 585
583 $wbuf .= pack "LL/a*", $id, &$f; 586 $wbuf .= pack "NN/a*", $id, &$f;
584 $ww ||= $fh && AE::io $fh, 1, $wcb; 587 $ww ||= $fh && AE::io $fh, 1, $wcb;
585 } 588 }
586 : sub { 589 : sub {
587 push @rcb, pop; 590 push @rcb, pop;
588 591
589 $guard; # keep it alive 592 $guard; # keep it alive
590 593
591 $wbuf .= pack "L/a*", &$f; 594 $wbuf .= pack "N/a*", &$f;
592 $ww ||= $fh && AE::io $fh, 1, $wcb; 595 $ww ||= $fh && AE::io $fh, 1, $wcb;
593 } 596 }
594} 597}
595 598
596=item $rpc->(..., $cb->(...)) 599=item $rpc->(..., $cb->(...))
772 775
773Of course, this might be blocking if you pass a lot of file descriptors, 776Of course, this might be blocking if you pass a lot of file descriptors,
774so you might want to look into L<AnyEvent::FDpasser> which can handle the 777so you might want to look into L<AnyEvent::FDpasser> which can handle the
775gory details. 778gory details.
776 779
780=head1 EXCEPTIONS
781
782There are no provisions whatsoever for catching exceptions at this time -
783in the child, exeptions might kill the process, causing calls to be lost
784and the parent encountering a fatal error. In the parent, exceptions in
785the result callback will not be caught and cause undefined behaviour.
786
777=head1 SEE ALSO 787=head1 SEE ALSO
778 788
779L<AnyEvent::Fork>, to create the processes in the first place. 789L<AnyEvent::Fork>, to create the processes in the first place.
780 790
781L<AnyEvent::Fork::Pool>, to manage whole pools of processes. 791L<AnyEvent::Fork::Pool>, to manage whole pools of processes.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines