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.35 by root, Wed Nov 20 16:17:22 2013 UTC vs.
Revision 1.36 by root, Sat Nov 30 17:41:46 2013 UTC

494 494
495All arguments, result data and event data have to be serialised to be 495All arguments, result data and event data have to be serialised to be
496transferred between the processes. For this, they have to be frozen and 496transferred between the processes. For this, they have to be frozen and
497thawed in both parent and child processes. 497thawed in both parent and child processes.
498 498
499By default, only octet strings can be passed between the processes, which 499By default, only octet strings can be passed between the processes,
500is reasonably fast and efficient and requires no extra modules. 500which is reasonably fast and efficient and requires no extra modules
501(the C<AnyEvent::Fork::RPC> distribution does not provide these extra
502serialiser modules).
501 503
502For more complicated use cases, you can provide your own freeze and thaw 504For more complicated use cases, you can provide your own freeze and thaw
503functions, by specifying a string with perl source code. It's supposed to 505functions, by specifying a string with perl source code. It's supposed to
504return two code references when evaluated: the first receives a list of 506return two code references when evaluated: the first receives a list of
505perl values and must return an octet string. The second receives the octet 507perl values and must return an octet string. The second receives the octet
525 ( 527 (
526 sub { pack "(w/a*)*", @_ }, 528 sub { pack "(w/a*)*", @_ },
527 sub { unpack "(w/a*)*", shift } 529 sub { unpack "(w/a*)*", shift }
528 ) 530 )
529 531
530=item json - C<$AnyEvent::Fork::RPC::CBOR_XS_SERIALISER> 532=item cbor - C<$AnyEvent::Fork::RPC::CBOR_XS_SERIALISER>
531 533
532This serialiser creates CBOR::XS arrays - you have to make sure the 534This serialiser creates CBOR::XS arrays - you have to make sure the
533L<CBOR::XS> module is installed for this serialiser to work. It can be 535L<CBOR::XS> module is installed for this serialiser to work. It can be
534beneficial for sharing when you preload the L<CBOR::XS> module in a template 536beneficial for sharing when you preload the L<CBOR::XS> module in a template
535process. 537process.
537L<CBOR::XS> is about as fast as the octet string serialiser, but supports 539L<CBOR::XS> is about as fast as the octet string serialiser, but supports
538complex data structures (similar to JSON) and is faster than any of the 540complex data structures (similar to JSON) and is faster than any of the
539other serialisers. If you have the L<CBOR::XS> module available, it's the 541other serialisers. If you have the L<CBOR::XS> module available, it's the
540best choice. 542best choice.
541 543
542Note that the CBOR::XS module supports some extensions to encode cyclic 544The encoder enables C<allow_sharing> (so this serialisation method can
543and self-referencing data structures, which are not enabled. You need to 545encode cyclic and self-referencing data structures).
544write your own serialiser to take advantage of these.
545 546
546Implementation: 547Implementation:
547 548
548 use CBOR::XS (); 549 use CBOR::XS ();
549 ( 550 (
550 sub { CBOR::XS::encode_cbor \@_ }, 551 sub { CBOR::XS::encode_cbor_sharing \@_ },
551 sub { @{ CBOR::XS::decode_cbor shift } } 552 sub { @{ CBOR::XS::decode_cbor shift } }
552 ) 553 )
553 554
554=item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> 555=item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER>
555 556
607examples. 608examples.
608 609
609=cut 610=cut
610 611
611our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; 612our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })';
612our $CBOR_XS_SERIALISER = 'use CBOR::XS (); (sub { CBOR::XS::encode_cbor \@_ }, sub { @{ CBOR::XS::decode_cbor shift } })'; 613our $CBOR_XS_SERIALISER = 'use CBOR::XS (); (sub { CBOR::XS::encode_cbor_sharing \@_ }, sub { @{ CBOR::XS::decode_cbor shift } })';
613our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; 614our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })';
614our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; 615our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })';
615our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })'; 616our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })';
616 617
617sub run { 618sub run {
618 my ($self, $function, %arg) = @_; 619 my ($self, $function, %arg) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines