| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Copyrights 2011 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 1.07. | 
| 5 | 1 |  |  | 1 |  | 1277 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 37 |  | 
| 6 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 48 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | package IOMux::Net::TCP; | 
| 9 | 1 |  |  | 1 |  | 4 | use vars '$VERSION'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 10 |  |  |  |  |  |  | $VERSION = '0.12'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 6 | use base 'IOMux::Handler::Read', 'IOMux::Handler::Write'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 115 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 1 |  |  | 1 |  | 5 | use Log::Report 'iomux'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 15 | 1 |  |  | 1 |  | 309 | use Socket      'SOCK_STREAM'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 277 |  | 
| 16 | 1 |  |  | 1 |  | 974 | use IO::Socket::INET; | 
|  | 1 |  |  |  |  | 11719 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub init($) | 
| 20 | 0 |  |  | 0 | 0 |  | {   my ($self, $args) = @_; | 
| 21 | 0 |  | 0 |  |  |  | my $socket = $args->{fh} | 
| 22 |  |  |  |  |  |  | = (delete $args->{socket}) || $self->extractSocket($args); | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 |  | 0 |  |  |  | $args->{name} ||= "tcp ".$socket->peerhost.':'.$socket->peerport; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 |  |  |  |  |  | $self->IOMux::Handler::Read::init($args); | 
| 27 | 0 |  |  |  |  |  | $self->IOMux::Handler::Write::init($args); | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 0 |  |  |  |  |  | $self; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | #------------------- | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  | 0 | 1 |  | sub socket() {shift->fh} | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | #------------------- | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub shutdown($) | 
| 39 | 0 |  |  | 0 | 1 |  | {   my($self, $which) = @_; | 
| 40 | 0 |  |  |  |  |  | my $socket = $self->socket; | 
| 41 | 0 |  |  |  |  |  | my $mux    = $self->mux; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 | 0 |  |  |  |  | if($which!=1) | 
| 44 |  |  |  |  |  |  | {   # Shutdown for reading.  We can do this now. | 
| 45 | 0 |  |  |  |  |  | $socket->shutdown(0); | 
| 46 | 0 |  |  |  |  |  | $self->{IMNT_shutread} = 1; | 
| 47 |  |  |  |  |  |  | # The mux_eof hook must be run from the main loop to consume | 
| 48 |  |  |  |  |  |  | # the rest of the inbuffer if there is anything left. | 
| 49 |  |  |  |  |  |  | # It will also remove $fh from _readers. | 
| 50 | 0 |  |  |  |  |  | $self->fdset(0, 1, 0, 0); | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 0 | 0 |  |  |  |  | if($which!=0) | 
| 53 |  |  |  |  |  |  | {   # Shutdown for writing.  Only do this now if there is no pending data. | 
| 54 | 0 |  |  |  |  |  | $self->{IMNT_shutwrite} = 1; | 
| 55 | 0 | 0 |  |  |  |  | unless($self->mux_output_waiting) | 
| 56 | 0 |  |  |  |  |  | {   $socket->shutdown(1); | 
| 57 | 0 |  |  |  |  |  | $self->fdset(0, 0, 1, 0); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | $self->close | 
| 62 | 0 | 0 | 0 |  |  |  | if $self->{IMNT_shutread} | 
|  |  |  | 0 |  |  |  |  | 
| 63 |  |  |  |  |  |  | && $self->{IMNT_shutwrite} && !$self->mux_output_waiting; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub close() | 
| 67 | 0 |  |  | 0 | 1 |  | {   my $self = shift; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 | 0 |  |  |  |  | warning __x"closing {name} with read buffer", name => $self->name | 
| 70 |  |  |  |  |  |  | if length $self->{ICMT_inbuf}; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 | 0 |  |  |  |  | warning __x"closing {name} with write buffer", name => $self->name | 
| 73 |  |  |  |  |  |  | if $self->{ICMT_outbuf}; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | $self->socket->close; | 
| 76 | 0 |  |  |  |  |  | $self->SUPER::close; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | #------------------------- | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub mux_init($) | 
| 82 | 0 |  |  | 0 | 1 |  | {   my ($self, $mux) = @_; | 
| 83 | 0 |  |  |  |  |  | $self->SUPER::mux_init($mux); | 
| 84 | 0 |  |  |  |  |  | $self->fdset(1, 1, 1, 0); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub mux_remove() | 
| 88 | 0 |  |  | 0 | 1 |  | {   my $self = shift; | 
| 89 | 0 |  |  |  |  |  | $self->SUPER::mux_remove; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | sub mux_outbuffer_empty() | 
| 93 | 0 |  |  | 0 | 1 |  | {   my $self = shift; | 
| 94 | 0 |  |  |  |  |  | $self->SUPER::mux_outbuffer_empty; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 | 0 | 0 |  |  |  | if($self->{IMNT_shutwrite} && !$self->mux_output_waiting) | 
| 97 | 0 |  |  |  |  |  | {   $self->socket->shutdown(1); | 
| 98 | 0 |  |  |  |  |  | $self->fdset(0, 0, 1, 0); | 
| 99 | 0 | 0 |  |  |  |  | $self->close if $self->{IMNT_shutread}; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | 1; |