AMF-Connection
view release on metacpan or search on metacpan
lib/AMF/Connection/Message.pm view on Meta::CPAN
package AMF::Connection::Message;
use strict;
use Carp;
use AMF::Connection::OutputStream;
use AMF::Connection::InputStream;
use AMF::Connection::MessageBody;
use AMF::Connection::MessageHeader;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {
'encoding' => 0, # default is AMF0 encoding
'bodies' => [],
'headers' => []
};
return bless($self, $class);
};
sub serialize {
my ($class, $stream) = @_;
croak "Stream $stream is not a valid output stream"
unless(ref($stream) and $stream->isa("AMF::Connection::OutputStream"));
# we default to AMF0 encoding
$stream->writeByte(0x00);
$stream->writeByte($class->getEncoding());
$stream->writeInt(scalar(@{$class->{'headers'}}));
foreach my $header (@{$class->{'headers'}}) {
my $name =$header->getName();
$stream->writeInt(length($name));
$stream->writeBuffer($name);
$stream->writeByte($header->isRequired());
$stream->writeLong(-1);
# TODO - make sure Storable::AMF does not store string "true" as boolean - or make sure value is right typed
$stream->writeAMFData( $class->getEncoding(), $header->getValue() );
};
$stream->writeInt(scalar(@{$class->{'bodies'}}));
foreach my $body (@{$class->{'bodies'}}) {
my $target = $body->getTarget();
$stream->writeInt(length($target));
$stream->writeBuffer($target);
my $response = $body->getResponse();
$stream->writeInt(length($response));
$stream->writeBuffer($response);
$stream->writeLong(-1);
$stream->writeAMFData( $class->getEncoding(), $body->getData() );
};
};
sub deserialize {
my ($class, $stream) = @_;
$class->{'headers'} = [];
$class->{'bodies'} = [];
$stream->readByte();
my $sent_encoding = $stream->readByte();
# need to make AMF1 returned encoding the same as AMF0 - see more about the bug at http://balazs.sebesteny.com/footprints-in-blazeds/
$class->setEncoding( ( $sent_encoding!=0 and $sent_encoding!=3 ) ? 0 : $sent_encoding );
my $totalHeaders = $stream->readInt();
for(my $i=0;$i<$totalHeaders;$i++) {
my $header = new AMF::Connection::MessageHeader();
my $strLen = $stream->readInt();
$header->setName( $stream->readBuffer($strLen) );
( run in 1.560 second using v1.01-cache-2.11-cpan-39bf76dae61 )