Data-FastPack

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        Returns the number of bytes encoded

    decode_message
    decode_fastpack
        Consumes data from an input buffer and decodes it into 0 or more
        messages. Buffer is aliased and is an in/out parameter Decoded
        messages are added to the dereferenced output array An optional
        limit of message count can be specified.

        Returns the number of bytes consumed during decoding. I a message
        could not be decoded, 0 bytes are consumed.

          buffer (aliased) 
          output (array ref)
          limit (numeric)

          return (byte count)

README.md  view on Meta::CPAN


- decode\_message
- decode\_fastpack

    Consumes data from an input buffer and decodes it into 0 or more messages.
    Buffer is aliased and is an in/out parameter
    Decoded messages are added to the dereferenced output array
    An optional limit of message count can be specified.

    Returns the number of bytes consumed during decoding. I a message could not be
    decoded, 0 bytes are consumed.

    ```
    buffer (aliased) 
    output (array ref)
    limit (numeric)

    return (byte count)
    ```

lib/Data/FastPack.pm  view on Meta::CPAN

=item decode_message

=item decode_fastpack

Consumes data from an input buffer and decodes it into 0 or more messages.
Buffer is aliased and is an in/out parameter
Decoded messages are added to the dereferenced output array
An optional limit of message count can be specified.

Returns the number of bytes consumed during decoding. I a message could not be
decoded, 0 bytes are consumed.

  buffer (aliased) 
  output (array ref)
  limit (numeric)

  return (byte count)


=back 

lib/Data/FastPack.pod  view on Meta::CPAN



=head3 decode_fastpack

  decode_fastpack $buf,$output, $limit, $namespace


Decodes FastPack messages from C<$buf>, consuming it as it progresses. C<$buf>
is aliases so new messages can be added to it externally.

C<$output> is a reference to an array to store the decoded messages. The
messages are decoded into  C<[time, id, payload]>.

If C<$limit> is provided, this is the maximum number of messages to decode
during a single call. If not provided or undef, 4096 is the default.


C<$namespace> if provided enables named ids. decoded messages have the id
mapped to a name stored in the names space. Special namespace update messages
manipulate the namespace table to ensure it is copy of the  encoding end of the
stream


=head2 Namspaces

=head3 create_namespace

  create_namespace

lib/Data/FastPack/Meta.pm  view on Meta::CPAN


my $mp=Data::MessagePack->new();
$mp->prefer_integer(1);
$mp->utf8(1);

# Arguments: $payload, force_mp Forcing message pack decode is only needed if
# the encoded data is not of map or array type. Otherise automatic decoding is
# best
sub decode_meta_payload {
	my ($payload,$force_mp)=@_;
	my $decodedMessage;
  
	for(unpack("C", $payload)) {
		if (!$force_mp and ($_== 0x5B || $_== 0x7B)) {
			#JSON encoded string
			$decodedMessage=decode_json($payload);
		}
    else { 
			#msgpack encoded
			$decodedMessage=$mp->unpack($payload);
		}
	}
	$decodedMessage;
}

# Arguments: payload, force_mp
sub encode_meta_payload {
  $_[1]?$mp->encode($_[0]):encode_json($_[0]);
}
1;

t/01-fastpack.t  view on Meta::CPAN

# Decode, gives length
my @output;
my $limit=10;
my $byte_count=decode_message($buffer, \@output, $limit);
#say STDERR Dumper @input;
#say STDERR Dumper @output;
ok length($buffer)==0, "Length ok";

ok @input_copy==@output, "Same message count";

# Test decoded messages are identical
for(0..$#input_copy){
  ok $input_copy[$_][0]==$output[$_][0];
  ok $input_copy[$_][1]==$output[$_][1];
  ok $input_copy[$_][2] eq $output[$_][2];
}


# Meta data
my $data={this=>1, is=>"text"};
my $json_payload=encode_meta_payload($data);



( run in 0.467 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )