File Coverage

lib/IOMux/Net/TCP.pm
Criterion Covered Total %
statement 21 56 37.5
branch 0 16 0.0
condition 0 17 0.0
subroutine 7 13 53.8
pod 5 6 83.3
total 33 108 30.5


line stmt bran cond sub pod time code
1             # Copyrights 2011-2020 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution IOMux. Meta-POD processed with OODoc
6             # into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package IOMux::Net::TCP;
10 1     1   846 use vars '$VERSION';
  1         2  
  1         67  
11             $VERSION = '1.01';
12              
13 1     1   39 use base 'IOMux::Handler::Read', 'IOMux::Handler::Write';
  1         3  
  1         105  
14              
15 1     1   6 use warnings;
  1         1  
  1         18  
16 1     1   4 use strict;
  1         1  
  1         16  
17              
18 1     1   4 use Log::Report 'iomux';
  1         1  
  1         4  
19 1     1   204 use Socket 'SOCK_STREAM';
  1         1  
  1         149  
20 1     1   412 use IO::Socket::INET;
  1         7076  
  1         6  
21              
22              
23             sub init($)
24 0     0 0   { my ($self, $args) = @_;
25              
26 0   0       $args->{Proto} ||= 'tcp';
27             my $socket = $args->{fh}
28 0   0       = (delete $args->{socket}) || $self->extractSocket($args);
29              
30 0   0       $args->{name} ||= "tcp ".$socket->peerhost.':'.$socket->peerport;
31              
32 0           $self->IOMux::Handler::Read::init($args);
33 0           $self->IOMux::Handler::Write::init($args);
34              
35 0           $self;
36             }
37              
38             #-------------------
39              
40 0     0 1   sub socket() {shift->fh}
41              
42             #-------------------
43              
44             sub shutdown($)
45 0     0 1   { my($self, $which) = @_;
46 0           my $socket = $self->socket;
47 0           my $mux = $self->mux;
48              
49 0 0         if($which!=1)
50             { # Shutdown for reading. We can do this now.
51 0           $socket->shutdown(0);
52 0           $self->{IMNT_shutread} = 1;
53             # The muxEOF hook must be run from the main loop to consume
54             # the rest of the inbuffer if there is anything left.
55             # It will also remove $fh from _readers.
56 0           $self->fdset(0, 1, 0, 0);
57             }
58 0 0         if($which!=0)
59             { # Shutdown for writing. Only do this now if there is no pending data.
60 0           $self->{IMNT_shutwrite} = 1;
61 0 0         unless($self->muxOutputWaiting)
62 0           { $socket->shutdown(1);
63 0           $self->fdset(0, 0, 1, 0);
64             }
65             }
66              
67             $self->close
68             if $self->{IMNT_shutread}
69 0 0 0       && $self->{IMNT_shutwrite} && !$self->muxOutputWaiting;
      0        
70             }
71              
72             sub close()
73 0     0 1   { my $self = shift;
74              
75             warning __x"closing {name} with read buffer", name => $self->name
76 0 0         if length $self->{ICMT_inbuf};
77              
78             warning __x"closing {name} with write buffer", name => $self->name
79 0 0         if $self->{ICMT_outbuf};
80              
81 0           $self->socket->close;
82 0           $self->SUPER::close;
83             }
84              
85             #-------------------------
86              
87             sub muxInit($)
88 0     0 1   { my ($self, $mux) = @_;
89 0           $self->SUPER::muxInit($mux);
90              
91             # we will not listen for write until we have something to write
92 0           $self->fdset(1, 1, 0, 1);
93             }
94              
95             sub muxOutbufferEmpty()
96 0     0 1   { my $self = shift;
97 0           $self->SUPER::muxOutbufferEmpty;
98              
99 0 0 0       if($self->{IMNT_shutwrite} && !$self->muxOutputWaiting)
100 0           { $self->socket->shutdown(1);
101 0           $self->fdset(0, 0, 1, 0);
102 0 0         $self->close if $self->{IMNT_shutread};
103             }
104             }
105              
106              
107             1;