| 1 | package POE::Filter::Block; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use POE::Filter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | |||||||||||||||||||||||||||
| 8 | 4 | @ISA = qw(POE::Filter); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub FRAMING_BUFFER () { 0 } | ||||||||||||||||||||||||||||
| 13 | sub BLOCK_SIZE () { 1 } | ||||||||||||||||||||||||||||
| 14 | sub EXPECTED_SIZE () { 2 } | ||||||||||||||||||||||||||||
| 15 | sub ENCODER () { 3 } | ||||||||||||||||||||||||||||
| 16 | sub DECODER () { 4 } | ||||||||||||||||||||||||||||
| 17 | sub MAX_LENGTH () { 5 } | ||||||||||||||||||||||||||||
| 18 | sub MAX_BUFFER () { 6 } | ||||||||||||||||||||||||||||
| 19 | sub FIRST_UNUSED () { 7 } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 4 | our @EXPORT_OK = qw( FIRST_UNUSED ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 48 | ▶ | sub _default_decoder { | ||||||||||||||||||||||||||
|
Subroutine
_default_decoder: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 27 | 48 | my $stuff = shift; | |||||||||||||||||||||||||||
| 28 | 48 | ▶ | unless ($$stuff =~ s/^(\d+)\0//s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 20 | ▶ | warn length($1), " strange bytes removed from stream" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if $$stuff =~ s/^(\D+)//s; | ||||||||||||||||||||||||||||
| 31 | 20 | return; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 28 | return $1; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 30 | ▶ | sub _default_encoder { | ||||||||||||||||||||||||||
|
Subroutine
_default_encoder: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 30 | my $stuff = shift; | |||||||||||||||||||||||||||
| 38 | 30 | substr($$stuff, 0, 0) = length($$stuff) . "\0"; | |||||||||||||||||||||||||||
| 39 | 30 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 30 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 30 | my $type = shift; | |||||||||||||||||||||||||||
| 44 | 30 | ▶ | croak "$type must be given an even number of parameters" if @_ & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 30 | my %params = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 30 | my $max_buffer = $type->__param_max( MaxBuffer => 512*1024*1024, \%params ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 28 | my ($encoder, $decoder, $max_length); | |||||||||||||||||||||||||||
| 50 | 28 | my $block_size = delete $params{BlockSize}; | |||||||||||||||||||||||||||
| 51 | 28 | ▶ | if (defined $block_size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 7 | ▶ | croak "$type doesn't support zero or negative block sizes" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | if $block_size < 1; | ||||||||||||||||||||||||||||
| 54 | croak "Can't use both LengthCodec and BlockSize at the same time" | ||||||||||||||||||||||||||||
| 55 | 7 | ▶ | if exists $params{LengthCodec}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | croak "Can't use both MaxLength and BlockSize at the same time" | ||||||||||||||||||||||||||||
| 57 | 7 | ▶ | if exists $params{MaxLength}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 21 | my $codec = delete $params{LengthCodec}; | |||||||||||||||||||||||||||
| 61 | 21 | ▶ | if ($codec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | croak "LengthCodec must be an array reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | unless ref($codec) eq "ARRAY"; | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | croak "LengthCodec must contain two items" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | unless @$codec == 2; | ||||||||||||||||||||||||||||
| 66 | 1 | ($encoder, $decoder) = @$codec; | |||||||||||||||||||||||||||
| 67 | 1 | ▶ | croak "LengthCodec encoder must be a code reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | unless ref($encoder) eq "CODE"; | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | croak "LengthCodec decoder must be a code reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | unless ref($decoder) eq "CODE"; | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 20 | $encoder = \&_default_encoder; | |||||||||||||||||||||||||||
| 74 | 20 | $decoder = \&_default_decoder; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 21 | $max_length = $type->__param_max( MaxLength => 64*1024*1024, \%params ); | |||||||||||||||||||||||||||
| 77 | 19 | ▶ | croak "MaxBuffer is not large enough for MaxLength blocks" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | unless $max_buffer >= $max_length + length( $max_length ) + 1; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 25 | delete @params{qw(MaxLength MaxBuffer LengthCode BlockSize)}; | |||||||||||||||||||||||||||
| 82 | 25 | ▶ | carp("$type ignores unknown parameters: ", join(', ', sort keys %params)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | if scalar keys %params; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 25 | my $self = bless [ | |||||||||||||||||||||||||||
| 86 | '', # FRAMING_BUFFER | ||||||||||||||||||||||||||||
| 87 | $block_size, # BLOCK_SIZE | ||||||||||||||||||||||||||||
| 88 | undef, # EXPECTED_SIZE | ||||||||||||||||||||||||||||
| 89 | $encoder, # ENCODER | ||||||||||||||||||||||||||||
| 90 | $decoder, # DECODER | ||||||||||||||||||||||||||||
| 91 | $max_length, # MAX_LENGTH | ||||||||||||||||||||||||||||
| 92 | $max_buffer # MAX_BUFFER | ||||||||||||||||||||||||||||
| 93 | ], $type; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 25 | $self; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 100 | # get() is inherited from POE::Filter. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 103 | # 2001-07-27 RCC: The get_one() variant of get() allows Wheel::Xyz to | ||||||||||||||||||||||||||||
| 104 | # retrieve one filtered block at a time. This is necessary for filter | ||||||||||||||||||||||||||||
| 105 | # changing and proper input flow control. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 57 | ▶ | sub get_one_start { | ||||||||||||||||||||||||||
|
Subroutine
get_one_start: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 57 | my ($self, $stream) = @_; | |||||||||||||||||||||||||||
| 109 | 57 | $self->[FRAMING_BUFFER] .= join '', @$stream; | |||||||||||||||||||||||||||
| 110 | 57 | ▶ | die "Framing buffer exceeds the limit" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | if $self->[MAX_BUFFER] < length( $self->[FRAMING_BUFFER] ); | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 110 | ▶ | sub get_one { | ||||||||||||||||||||||||||
|
Subroutine
get_one: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 110 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Need to check lengths in octets, not characters. | ||||||||||||||||||||||||||||
| 118 | 4 | ▶ | BEGIN { eval { require bytes } and bytes->import; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # If a block size is specified, then pull off a block of that many | ||||||||||||||||||||||||||||
| 121 | # bytes. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 110 | ▶ | if (defined $self->[BLOCK_SIZE]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 48 | ▶ | return [ ] unless length($self->[FRAMING_BUFFER]) >= $self->[BLOCK_SIZE]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 26 | my $block = substr($self->[FRAMING_BUFFER], 0, $self->[BLOCK_SIZE]); | |||||||||||||||||||||||||||
| 126 | 26 | substr($self->[FRAMING_BUFFER], 0, $self->[BLOCK_SIZE]) = ''; | |||||||||||||||||||||||||||
| 127 | 26 | return [ $block ]; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Otherwise we're doing the variable-length block thing. Look for a | ||||||||||||||||||||||||||||
| 131 | # length marker, and then pull off a chunk of that length. Repeat. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 62 | ▶ | unless( defined($self->[EXPECTED_SIZE]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 58 | $self->[EXPECTED_SIZE] = $self->[DECODER]->(\$self->[FRAMING_BUFFER]); | |||||||||||||||||||||||||||
| 135 | 58 | ▶ | die "Expected size of next block exceeds the limit" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->[2] and $self->[2] > $self->[5]
MC/DC100%2/2
defined $self->[2] and $self->[2] > $self->[5]
defined $self->[2]
$self->[2] > $self->[5]
Truth table100%3/3
A and B
A= defined $self->[2]B= $self->[2] > $self->[5]
| |||||||||||||||||||||||||||||
| 136 | if defined($self->[EXPECTED_SIZE]) and | ||||||||||||||||||||||||||||
| 137 | $self->[EXPECTED_SIZE] > $self->[MAX_LENGTH]; | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 61 | ▶ | if ( defined($self->[EXPECTED_SIZE]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 36 | ▶ | return [ ] if length($self->[FRAMING_BUFFER]) < $self->[EXPECTED_SIZE]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Four-arg substr() would be better here, but it's not compatible | ||||||||||||||||||||||||||||
| 143 | # with Perl as far back as we support. | ||||||||||||||||||||||||||||
| 144 | 32 | my $block = substr($self->[FRAMING_BUFFER], 0, $self->[EXPECTED_SIZE]); | |||||||||||||||||||||||||||
| 145 | 32 | substr($self->[FRAMING_BUFFER], 0, $self->[EXPECTED_SIZE]) = ''; | |||||||||||||||||||||||||||
| 146 | 32 | $self->[EXPECTED_SIZE] = undef; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 32 | return [ $block ]; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 25 | return [ ]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 42 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 42 | my ($self, $blocks) = @_; | |||||||||||||||||||||||||||
| 158 | 42 | my @raw; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Need to check lengths in octets, not characters. | ||||||||||||||||||||||||||||
| 161 | 4 | ▶ | BEGIN { eval { require bytes } and bytes->import; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # If a block size is specified, then just assume the put is right. | ||||||||||||||||||||||||||||
| 164 | # This will cause quiet framing errors on the receiving side. Then | ||||||||||||||||||||||||||||
| 165 | # again, we'll have quiet errors if the block sizes on both ends | ||||||||||||||||||||||||||||
| 166 | # differ. Ah, well! | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 42 | ▶ | if (defined $self->[BLOCK_SIZE]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 16 | @raw = join '', @$blocks; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # No specified block size. Do the variable-length block thing. This | ||||||||||||||||||||||||||||
| 173 | # steals a lot of Artur's code from the Reference filter. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||
| 176 | 26 | @raw = @$blocks; | |||||||||||||||||||||||||||
| 177 | 26 | foreach (@raw) { | |||||||||||||||||||||||||||
| 178 | 38 | $self->[ENCODER]->(\$_); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 42 | \@raw; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 12 | ▶ | sub get_pending { | ||||||||||||||||||||||||||
|
Subroutine
get_pending: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 189 | 12 | ▶ | return undef unless length $self->[FRAMING_BUFFER]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 4 | [ $self->[FRAMING_BUFFER] ]; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 4 | 1; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||