Data-FastPack

 view release on metacpan or  search on metacpan

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

           $ns->[N2E]->%*=();
           $ns->[I2E]->%*=();
           $ns->[FREE_ID]->@*=();
           $ns->[NEXT_ID]=1;
        }
        $_ids[$i++]=$_->[FP_MSG_ID];
        # If the msg id is 0 this  is passed thourgh un modified and not name translated. 
        #
        #warn 'FastPack encode: Ignoring message. Named FastPack Messages cannot be named 0 or "0" or undefined';
      }

    }
    $i=0;
    for(@$inputs){
      $padding=((length($_->[FP_MSG_PAYLOAD]//""))%8);
      $padding= 8-$padding  if $padding;

      my $s=pack("d V V/a*", $_->[FP_MSG_TIME], $_ids[$i++], $_->[FP_MSG_PAYLOAD]//"" );
      $tmp=$s.substr $pbuf, 0, $padding;
      $bytes+=length $tmp;
      $buf.=$tmp;
      last if ++$processed == $limit;
    }
  }
  else {
    $i=0;
    for(@$inputs){
      $padding=((length $_->[FP_MSG_PAYLOAD])%8);
      $padding= 8-$padding  if $padding;

      my $s=pack("d V V/a*", @$_);
      $tmp=$s.substr $pbuf, 0, $padding;
      $bytes+=length $tmp;
      $buf.=$tmp;
      last if ++$processed == $limit;
    }

  }

  # Remove the messages from the input array
  splice @$inputs, 0, $processed;
  
  $bytes;	
}

*encode_fastpack=\&encode_message;

# Decode a message from a buffer. Buffer is aliased
=over

=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 

=cut

sub decode_message {
  \my $buf=\$_[0]; shift;
  my $output=shift;
  my $limit=shift//4096;
  my $ns=shift;

  my $byte_count=0;
  for(1..$limit){
    # Minimum message length 8 bytes long (header)
    last if length($buf)<16;

    # Decode  header. Leave length for in buffer
    my @message= unpack "d V V", substr($buf, 0, 16);



    # Calculate pad. Payload in message here is actuall just length atm
    my $pad= $message[FP_MSG_PAYLOAD]%8;
    $pad=8-$pad if $pad;

    # Calculate total length
    my $total=$message[FP_MSG_PAYLOAD]+16+$pad;

    last if(length($buf)<$total);




    $byte_count+=$total;


    ($message[FP_MSG_PAYLOAD],undef)=unpack "V/a* ", substr($buf,12);
    push @message, $total;

    # remove from buffer
    substr($buf, 0, $total,"");

    # Process name space definitions and lookups
    if($ns ){
      #print STDERR __PACKAGE__. "-------- ID of $message[FP_MSG_ID]=> $message[FP_MSG_PAYLOAD]\n";
      if($message[FP_MSG_ID]){
        # This is a non system message which needs conversion 
        #
        my $id=$message[FP_MSG_ID];
        my $name= $ns->[I2E]{$id};
        unless($name){
          # id not found, so this is the first message for this id
          # The payload contains the name



( run in 0.458 second using v1.01-cache-2.11-cpan-df04353d9ac )