File Coverage

lib/IOMux/Service/TCP.pm
Criterion Covered Total %
statement 21 46 45.6
branch 0 8 0.0
condition 0 13 0.0
subroutine 7 12 58.3
pod 4 5 80.0
total 32 84 38.1


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::Service::TCP;
10 1     1   729 use vars '$VERSION';
  1         2  
  1         40  
11             $VERSION = '1.01';
12              
13 1     1   4 use base 'IOMux::Handler::Service';
  1         2  
  1         72  
14              
15 1     1   5 use warnings;
  1         1  
  1         27  
16 1     1   4 use strict;
  1         2  
  1         19  
17              
18 1     1   3 use Log::Report 'iomux';
  1         2  
  1         3  
19 1     1   215 use IOMux::Net::TCP ();
  1         2  
  1         14  
20              
21 1     1   3 use Socket 'SOCK_STREAM';
  1         1  
  1         361  
22              
23              
24             sub init($)
25 0     0 0   { my ($self, $args) = @_;
26              
27 0   0       $args->{Proto} ||= 'tcp';
28 0   0       my $socket = delete $args->{socket} || $self->extractSocket($args);
29 0 0         $socket->socktype eq SOCK_STREAM
30             or error __x"{pkg} needs STREAM protocol socket", pkg => ref $self;
31 0           $args->{fh} = $socket;
32              
33 0           my $sockaddr = $socket->sockhost.':'.$socket->sockport;
34 0   0       $args->{name} ||= "listen tcp $sockaddr";
35              
36 0           $self->SUPER::init($args);
37              
38             my $ct = $self->{IMST_conn_type} = $args->{conn_type}
39 0 0         or error __x"a conn_type for incoming request is need by {name}"
40             , name => $self->name;
41              
42 0   0       $self->{IMST_conn_opts} = $args->{conn_opts} || [];
43 0   0       $self->{IMST_hostname} = $args->{hostname} || $sockaddr;
44 0           $self;
45             }
46              
47             #------------------------
48              
49 0     0 1   sub clientType() {shift->{IMST_conn_type}}
50 0     0 1   sub socket() {shift->fh}
51 0     0 1   sub hostname() {shift->{IMST_hostname}}
52              
53             #-------------------------
54              
55             # The read flag is set on the socket, which means that a new connection
56             # attempt is made.
57              
58              
59             sub muxReadFlagged()
60 0     0 1   { my $self = shift;
61              
62 0           my $client = $self->socket->accept;
63 0 0         unless($client)
64 0           { alert __x"accept for socket {name} failed", name => $self->name;
65 0           return;
66             }
67              
68             # create an object which handles this connection
69 0           my $ct = $self->clientType;
70 0           my $opts = $self->{IMST_conn_opts};
71 0 0         my $handler = ref $ct eq 'CODE'
72             ? $ct-> (socket => $client, Proto => 'tcp', @$opts)
73             : $ct->new(socket => $client, Proto => 'tcp', @$opts);
74              
75             # add the new socket to the mux, to be watched
76 0           $self->mux->add($handler);
77              
78 0           $self->muxConnection($client);
79             }
80              
81             1;