Data-FastPack

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    If the message has and ID of "0", and a payload with no length, this
    resets the mappings in the namespace and is output as normal

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

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

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

    If $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.

    $namespace if provided, int enables named ids. Decoded messages have the
    id mapped to a name stored in the names space. If its the id has not
    been encountered before, it uses the payload as the name to update the
    internal mapping. This message is not sent to the output, as it is
    intended to update the mapping only.

README.md  view on Meta::CPAN


### decode\_fastpack

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

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

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

If `$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.

`$namespace` if provided, int  enables named ids. Decoded messages have the id
mapped to a name stored in the names space. If its the id has not been
encountered before, it uses the payload as the name to update the internal
mapping. This message is not sent to the output, as it is intended to update
the mapping only.

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, int  enables named ids. Decoded messages have the id
mapped to a name stored in the names space. If its the id has not been
encountered before, it uses the payload as the name to update the internal
mapping. This message is not sent to the output, as it is intended to update
the mapping only.

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

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";

#use Data::Dumper;
# Test decoded messages are identical
for(0..$#input_copy){
  #print STDERR Dumper $input_copy[$_];
  #print STDERR Dumper $output[$_];
  ok $input_copy[$_][0]==$output[$_][0], "time ok";
  ok $input_copy[$_][1]==$output[$_][1], "name/id ok";
  ok $input_copy[$_][2] eq $output[$_][2], "payload ok";
}


# Meta data



( run in 0.936 second using v1.01-cache-2.11-cpan-e9daa2b36ef )