| 1 | package IO::Mux::Packet ; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use IO::Handle ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | our $VERSION = '0.08' ; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 84 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 84 | my $class = shift ; | |||||||||||||||||||||||||||
| 13 | 84 | my $id = shift ; | |||||||||||||||||||||||||||
| 14 | 84 | my $data = shift ; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 84 | my $this = {} ; | |||||||||||||||||||||||||||
| 17 | 84 | $this->{id} = $id ; | |||||||||||||||||||||||||||
| 18 | 84 | $this->{data} = $data ; | |||||||||||||||||||||||||||
| 19 | 84 | $this->{type} = 'D' ; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 84 | return bless($this, $class) ; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 152 | ▶ | sub get_length { | ||||||||||||||||||||||||||
|
Subroutine
get_length: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 152 | my $this = shift ; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 152 | ▶ | return (defined($this->{data}) ? length($this->{data}) : 0) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 81 | ▶ | sub get_data { | ||||||||||||||||||||||||||
|
Subroutine
get_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 81 | my $this = shift ; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 81 | return $this->{data} ; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 164 | ▶ | sub get_id { | ||||||||||||||||||||||||||
|
Subroutine
get_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 164 | my $this = shift ; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 164 | return $this->{id} ; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 112 | ▶ | sub get_type { | ||||||||||||||||||||||||||
|
Subroutine
get_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 112 | my $this = shift ; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 112 | return $this->{type} ; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 62 | ▶ | sub is_eof { | ||||||||||||||||||||||||||
|
Subroutine
is_eof: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 62 | my $this = shift ; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 62 | return $this->get_type() eq 'E' ; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 26 | ▶ | sub make_eof { | ||||||||||||||||||||||||||
|
Subroutine
make_eof: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 26 | my $this = shift ; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 26 | $this->{type} = 'E' ; | |||||||||||||||||||||||||||
| 64 | 26 | $this->{data} = 0 ; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 50 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 50 | my $this = shift ; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 50 | my $len = length( | |||||||||||||||||||||||||||
| 72 | $this->get_id()) | ||||||||||||||||||||||||||||
| 73 | + 3 | ||||||||||||||||||||||||||||
| 74 | + $this->get_length() ; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # We place the length in between 2 0x1 bytes in order to attempt | ||||||||||||||||||||||||||||
| 77 | # to detect invalid data appearing in the filehandle. | ||||||||||||||||||||||||||||
| 78 | 50 | return pack("CLC", 1, $len, 1) . | |||||||||||||||||||||||||||
| 79 | $this->get_id() | ||||||||||||||||||||||||||||
| 80 | . "\t" . $this->get_type() . "\t" | ||||||||||||||||||||||||||||
| 81 | . $this->get_data() ; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 47 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 47 | my $this = shift ; | |||||||||||||||||||||||||||
| 87 | 47 | my $fh = shift ; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # We do not write empty packets, but we still return success. | ||||||||||||||||||||||||||||
| 90 | 47 | ▶ | return 1 if ! $this->get_length() ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 46 | my $ret = print $fh $this->serialize() ; | |||||||||||||||||||||||||||
| 93 | 46 | ▶ | if ($ret){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 34 | $ret = $this->get_length() ; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 46 | return $ret ; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 44 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 44 | my $class = shift ; | |||||||||||||||||||||||||||
| 103 | 44 | my $fh = shift ; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 44 | my $len = '' ; | |||||||||||||||||||||||||||
| 106 | 44 | ▶ | while (length($len) < 6){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 45 | my $rc = $fh->sysread($len, 6 - length($len), length($len)) ; | |||||||||||||||||||||||||||
| 108 | 45 | ▶ | if (! defined($rc)){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | 0 | return undef ; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | elsif (! $rc){ | ||||||||||||||||||||||||||||
| 112 | 5 | ▶ | return 0 if ! length($len) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | croak("Unexpected EOF (incomplete packet length)") ; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 39 | my ($mb, $me) = () ; | |||||||||||||||||||||||||||
| 117 | 39 | ($mb, $len, $me) = unpack("CLC", $len) ; | |||||||||||||||||||||||||||
| 118 | 39 | ▶ | if (($mb != 1)||($me != 1)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mb != 1 or $me != 1
MC/DC100%2/2
$mb != 1 or $me != 1
$mb != 1
$me != 1
Truth table100%3/3
A or B
A= $mb != 1B= $me != 1
| |||||||||||||||||||||||||||||
| 119 | # We have bad data on the handle | ||||||||||||||||||||||||||||
| 120 | 3 | croak("Marker mismatch ($mb,$me) != (1,1): someone writing directly on IO::Mux Handle?") ; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 36 | my $buf = '' ; | |||||||||||||||||||||||||||
| 124 | 36 | ▶ | while (length($buf) < $len){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 37 | my $rc = $fh->sysread($buf, $len - length($buf), length($buf)) ; | |||||||||||||||||||||||||||
| 126 | 37 | ▶ | if (! defined($rc)){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 127 | 0 | return undef ; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | elsif (! $rc){ | ||||||||||||||||||||||||||||
| 130 | 1 | croak("Unexpected EOF (incomplete packet id or data)") ; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 35 | ▶ | if ($buf =~ s/^(.*?)\t([DE])\t//){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 34 | my $id = $1 ; | |||||||||||||||||||||||||||
| 136 | 34 | my $type = $2 ; | |||||||||||||||||||||||||||
| 137 | 34 | my $p = new IO::Mux::Packet($id, $buf) ; | |||||||||||||||||||||||||||
| 138 | 34 | ▶ | if ($type eq 'E'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 7 | $p->make_eof() ; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 34 | return $p ; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | 1 | croak("Malformed packet: $buf") ; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 5 | 1 ; | |||||||||||||||||||||||||||