File: FlatMarshaller.pm

package info (click to toggle)
libio-async-perl 0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 684 kB
  • ctags: 239
  • sloc: perl: 6,439; makefile: 2
file content (80 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2007 -- leonerd@leonerd.org.uk

package # hide from CPAN
  IO::Async::DetachedCode::FlatMarshaller;

use strict;
use warnings;

use Carp;

use constant LENGTH_OF_I => length( pack( "I", 0 ) );

sub new
{
   my $class = shift;
   return bless {}, $class;
}

sub marshall_args
{
   my ( $self ) = shift;
   my ( $id, $args ) = @_;

   my $buffer = "";

   foreach( @$args ) {
      croak "Cannot marshall a ".ref($_)." using the 'flat' marshaller" if ref $_;

      $buffer .= defined $_ ?
         pack( "i", length( $_ ) ) . $_ :
         pack( "i", -1 );
   }

   return $buffer;
}

sub unmarshall_args
{
   my ( $self ) = shift;
   my ( $id, $record ) = @_;

   my @args;

   while( length $record ) {
      my $arglen = unpack( "i", $record );
      substr( $record, 0, LENGTH_OF_I, "" );

      if( $arglen == -1 ) {
         push @args, undef;
      }
      else {
         push @args, substr( $record, 0, $arglen, "" );
      }
   }

   return ( \@args );
}

sub marshall_ret
{
   my $self = shift;
   my ( $id, $ret ) = @_;

   return $self->marshall_args( $id, $ret );
}

sub unmarshall_ret
{
   my $self = shift;
   my ( $id, $record ) = @_;

   return $self->unmarshall_args( $id, $record );
}


# Keep perl happy; keep Britain tidy
1;