| 1 | # Copyright 1998-2013 Rocco Caputo <rcaputo@cpan.org>. All rights | ||||||||||||||||||||||||||||
| 2 | # reserved. This program is free software; you can redistribute it | ||||||||||||||||||||||||||||
| 3 | # and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package POE::Driver::SysRW; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 105 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 105 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 105 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 105 | ▶ | use Errno qw(EAGAIN EWOULDBLOCK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 105 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub OUTPUT_QUEUE () { 0 } | ||||||||||||||||||||||||||||
| 16 | sub CURRENT_OCTETS_DONE () { 1 } | ||||||||||||||||||||||||||||
| 17 | sub CURRENT_OCTETS_LEFT () { 2 } | ||||||||||||||||||||||||||||
| 18 | sub BLOCK_SIZE () { 3 } | ||||||||||||||||||||||||||||
| 19 | sub TOTAL_OCTETS_LEFT () { 4 } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 950 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 950 | my $type = shift; | |||||||||||||||||||||||||||
| 25 | 950 | my $self = bless [ | |||||||||||||||||||||||||||
| 26 | [ ], # OUTPUT_QUEUE | ||||||||||||||||||||||||||||
| 27 | 0, # CURRENT_OCTETS_DONE | ||||||||||||||||||||||||||||
| 28 | 0, # CURRENT_OCTETS_LEFT | ||||||||||||||||||||||||||||
| 29 | 65536, # BLOCK_SIZE | ||||||||||||||||||||||||||||
| 30 | 0, # TOTAL_OCTETS_LEFT | ||||||||||||||||||||||||||||
| 31 | ], $type; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 950 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 16 | ▶ | if (@_ % 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | croak "$type requires an even number of parameters, if any"; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 15 | my %args = @_; | |||||||||||||||||||||||||||
| 38 | 15 | ▶ | if (defined $args{BlockSize}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 14 | $self->[BLOCK_SIZE] = delete $args{BlockSize}; | |||||||||||||||||||||||||||
| 40 | 14 | ▶ | croak "$type BlockSize must be greater than 0" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | if ($self->[BLOCK_SIZE] <= 0); | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 14 | ▶ | if (keys %args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | my @bad_args = sort keys %args; | |||||||||||||||||||||||||||
| 45 | 1 | croak "$type has unknown parameter(s): @bad_args"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 947 | $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 474 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 474 | my ($self, $chunks) = @_; | |||||||||||||||||||||||||||
| 56 | 474 | my $old_queue_octets = $self->[TOTAL_OCTETS_LEFT]; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Need to check lengths in octets, not characters. | ||||||||||||||||||||||||||||
| 59 | 105 | ▶ | BEGIN { eval { require bytes } and bytes->import; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 474 | foreach (grep { length } @$chunks) { | |||||||||||||||||||||||||||
| 62 | 473 | $self->[TOTAL_OCTETS_LEFT] += length; | |||||||||||||||||||||||||||
| 63 | 473 | push @{$self->[OUTPUT_QUEUE]}, $_; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 474 | ▶ | if ($self->[TOTAL_OCTETS_LEFT] && (!$old_queue_octets)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->[4] and not $old_queue_octets
MC/DC100%2/2
$self->[4] and not $old_queue_octets
$self->[4]
not $old_queue_octets
Truth table100%3/3
A and not(B)
A= $self->[4]B= not $old_queue_octets
| |||||||||||||||||||||||||||||
| 67 | 427 | $self->[CURRENT_OCTETS_LEFT] = length($self->[OUTPUT_QUEUE]->[0]); | |||||||||||||||||||||||||||
| 68 | 427 | $self->[CURRENT_OCTETS_DONE] = 0; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 474 | $self->[TOTAL_OCTETS_LEFT]; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2385 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 2385 | my ($self, $handle) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2385 | my $result = sysread($handle, my $buffer = '', $self->[BLOCK_SIZE]); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # sysread() returned a positive number of octets. Return whatever | ||||||||||||||||||||||||||||
| 82 | # was read. | ||||||||||||||||||||||||||||
| 83 | 2385 | ▶ | return [ $buffer ] if $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # 18:01 <dngor> sysread() clears $! when it returns 0 for eof? | ||||||||||||||||||||||||||||
| 86 | # 18:01 <merlyn> nobody clears $! | ||||||||||||||||||||||||||||
| 87 | # 18:01 <merlyn> returning 0 is not an error | ||||||||||||||||||||||||||||
| 88 | # 18:01 <merlyn> returning -1 is an error, and sets $! | ||||||||||||||||||||||||||||
| 89 | # 18:01 <merlyn> eof is not an error. :) | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # 18:21 <dngor> perl -wle '$!=1; warn "\$!=",$!+0; \ | ||||||||||||||||||||||||||||
| 92 | # warn "sysread=",sysread(STDIN,my $x="",100); \ | ||||||||||||||||||||||||||||
| 93 | # die "\$!=",$!+0' < /dev/null | ||||||||||||||||||||||||||||
| 94 | # 18:23 <lathos> $!=1 at foo line 1. | ||||||||||||||||||||||||||||
| 95 | # 18:23 <lathos> sysread=0 at foo line 1. | ||||||||||||||||||||||||||||
| 96 | # 18:23 <lathos> $!=0 at foo line 1. | ||||||||||||||||||||||||||||
| 97 | # 18:23 <lathos> 5.6.0 on Darwin. | ||||||||||||||||||||||||||||
| 98 | # 18:23 <dngor> Same, 5.6.1 on fbsd 4.4-stable. | ||||||||||||||||||||||||||||
| 99 | # read(2) must be clearing errno or something. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # sysread() returned 0, signifying EOF. Although $! is magically | ||||||||||||||||||||||||||||
| 102 | # set to 0 on EOF, it may not be portable to rely on this. | ||||||||||||||||||||||||||||
| 103 | 779 | ▶ | if (defined $result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 715 | $! = 0; | |||||||||||||||||||||||||||
| 105 | 715 | return undef; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Nonfatal sysread() error. Return an empty list. | ||||||||||||||||||||||||||||
| 109 | 64 | ▶ | return [ ] if $! == EAGAIN or $! == EWOULDBLOCK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$! == 11 or $! == 11
MC/DC50%1/2
$! == 11 or $! == 11
$! == 11
$! == 11
Truth table66%2/3
A or B
A= $! == 11B= $! == 11
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # fatal sysread error | ||||||||||||||||||||||||||||
| 112 | 13 | undef; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 383 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 383 | my ($self, $handle) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Need to check lengths in octets, not characters. | ||||||||||||||||||||||||||||
| 121 | 105 | ▶ | BEGIN { eval { require bytes } and bytes->import; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Reset errno in case there is nothing to write. | ||||||||||||||||||||||||||||
| 124 | # https://rt.cpan.org/Public/Bug/Display.html?id=87721 | ||||||||||||||||||||||||||||
| 125 | 383 | $! = 0; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # syswrite() it, like we're supposed to | ||||||||||||||||||||||||||||
| 128 | 383 | ▶ | while (@{$self->[OUTPUT_QUEUE]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 435 | my $wrote_count = syswrite( | |||||||||||||||||||||||||||
| 130 | $handle, | ||||||||||||||||||||||||||||
| 131 | $self->[OUTPUT_QUEUE]->[0], | ||||||||||||||||||||||||||||
| 132 | $self->[CURRENT_OCTETS_LEFT], | ||||||||||||||||||||||||||||
| 133 | $self->[CURRENT_OCTETS_DONE], | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Errors only count if syswrite() failed. | ||||||||||||||||||||||||||||
| 137 | 435 | ▶ | $! = 0 if defined $wrote_count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 435 | ▶ | unless ($wrote_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 18 | ▶ | $! = 0 if $! == EAGAIN or $! == EWOULDBLOCK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$! == 11 or $! == 11
MC/DC50%1/2
$! == 11 or $! == 11
$! == 11
$! == 11
Truth table66%2/3
A or B
A= $! == 11B= $! == 11
| |||||||||||||||||||||||||||||
| 141 | 18 | last; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 417 | $self->[CURRENT_OCTETS_DONE] += $wrote_count; | |||||||||||||||||||||||||||
| 145 | 417 | $self->[TOTAL_OCTETS_LEFT] -= $wrote_count; | |||||||||||||||||||||||||||
| 146 | 417 | ▶ | unless ($self->[CURRENT_OCTETS_LEFT] -= $wrote_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 400 | shift(@{$self->[OUTPUT_QUEUE]}); | |||||||||||||||||||||||||||
| 148 | 400 | ▶ | if (@{$self->[OUTPUT_QUEUE]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 36 | $self->[CURRENT_OCTETS_DONE] = 0; | |||||||||||||||||||||||||||
| 150 | 36 | $self->[CURRENT_OCTETS_LEFT] = length($self->[OUTPUT_QUEUE]->[0]); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||
| 153 | 364 | $self->[CURRENT_OCTETS_DONE] = $self->[CURRENT_OCTETS_LEFT] = 0; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 383 | $self->[TOTAL_OCTETS_LEFT]; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 44 | ▶ | sub get_out_messages_buffered { | ||||||||||||||||||||||||||
|
Subroutine
get_out_messages_buffered: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 44 | scalar(@{$_[0]->[OUTPUT_QUEUE]}); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 105 | 1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||