AMF-Connection

 view release on metacpan or  search on metacpan

lib/AMF/Connection/InputStream.pm  view on Meta::CPAN

	my $proto = shift;
	my $class = ref($proto) || $proto;

	my ($stream, $storable_amf_options) = @_;

	croak "Input stream must be a valid string"
		if(ref($stream));
	
	my $self = {
		'stream' => $stream,
		'cursor' => 0
		};

	if (defined $storable_amf_options)
	  {
	    if ($Storable::AMF::VERSION < 0.84)
	      {
	        croak "Storable::AMF 0.84 or newer needed to set stream options\n";
	      }
	    $self->{'options'} = Storable::AMF::parse_option ($storable_amf_options);
	  }

	return bless($self, $class);
	};

sub readBuffer {
	my ($class, $length) = @_;

	croak "Buffer underrun at position: ". $class->{'cursor'} . ". Trying to fetch ". $length . " bytes from buffer total length ".length($class->{'stream'})
		if($length + $class->{'cursor'} > length($class->{'stream'}));

        my $data = substr($class->{'stream'},$class->{'cursor'},$length);
	$class->{'cursor'}+=$length;
	
	return $data;
	};

sub readByte {
	my ($class) = @_;

	return ord($class->readBuffer(1));
	};

lib/AMF/Connection/InputStream.pm  view on Meta::CPAN

	};

# deparse out the next avail AMF entity
# TODO - make sure ref counts are reset/preserved between calls in the scope of the same InputStream - study Storable::AMF API
sub readAMFData {
	my ($class) = @_;

	my $type = $class->readByte();

	# Storable::AMF will take care of deparsing the right AMF format
	$class->{'cursor'}--;

	local $@ = undef;

        my ($obj, $len);
	my $encoding=0;
	if($type == 0x11) {
		$encoding=3;
		$class->{'cursor'}++;
		if ($storable_with_options  == 0
		    || not defined $class->{'options'})
		  {
        	    ($obj, $len) = Storable::AMF3::deparse_amf( substr($class->{'stream'},$class->{'cursor'}));
		  }
  		else
		  {
        	    ($obj, $len) = Storable::AMF3::deparse_amf( substr($class->{'stream'},$class->{'cursor'}), $class->{'options'});
		  }
	} else {
		# NOTE: Storable::AMF0 seems not needing extra readByte() before deparse

		if ($storable_with_options  == 0
		    || not defined $class->{'options'})
		  {
        	    ($obj, $len) = Storable::AMF0::deparse_amf( substr($class->{'stream'},$class->{'cursor'}));
		  }
		else
		  {
        	    ($obj, $len) = Storable::AMF0::deparse_amf( substr($class->{'stream'},$class->{'cursor'}), $class->{'options'});
		  }
		};

	croak "Can not read AMF".$encoding." data starting from position ".$class->{'cursor'}." of input - reason: ".$@ ."\n"
		if($@);

	if(defined $obj) {
		$class->{'cursor'}+=$len
			unless( $len + $class->{'cursor'} > length($class->{'stream'}) );	
		};

	return $obj;
	};

1;
__END__

=head1 NAME

lib/AMF/Connection/OutputStream.pm  view on Meta::CPAN

		    || not defined $class->{'options'})
                  {
		    $bytes = Storable::AMF0::freeze($data);
		  }
		else
		  {
		    $bytes = Storable::AMF0::freeze($data, $class->{'options'});
		  }
                };

	croak "Can not write AMF".$encoding." data starting from position ".$class->{'cursor'}." of input - reason: ".$@ ."\n"
		if($@);

	$class->writeBuffer($bytes);
        };

1;
__END__

=head1 NAME



( run in 0.417 second using v1.01-cache-2.11-cpan-a5abf4f5562 )