| 1 | package MYDan::Util::TcpServer; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | use POSIX ":sys_wait_h"; | ||
| 10 | use Time::HiRes qw(time); | ||
| 11 | use AnyEvent; | ||
| 12 | use AnyEvent::Impl::Perl; | ||
| 13 | use AnyEvent::Socket; | ||
| 14 | use AnyEvent::Handle; | ||
| 15 | use Fcntl qw(:flock SEEK_END); | ||
| 16 | |||
| 17 | use MYDan; | ||
| 18 | |||
| 19 | our %index; | ||
| 20 | |||
| 21 | sub new | ||
| 22 | { | ||
| 23 | my ( $class, %this ) = @_; | ||
| 24 | |||
| 25 | map{ die "$_ unkown" unless $this{$_} && $this{$_} =~ /^\d+$/ }qw( port max ); | ||
| 26 | die "no file:$this{'exec'}\n" unless $this{'exec'} && -e $this{'exec'}; | ||
| 27 | |||
| 28 | $this{tmp} ||= sprintf "%s/var/run/agent", $MYDan::PATH; | ||
| 29 | |||
| 30 | system( "mkdir -p '$this{tmp}'" ) unless -d $this{tmp}; | ||
| 31 | map{ unlink $_ if $_ =~ /\/\d+$/ || $_ =~ /\/\d+\.out$/ }glob "$this{tmp}/*"; | ||
| 32 | |||
| 33 | map{ $this{$_} ||= $this{buf} }qw( rbuf wbuf ); | ||
| 34 | |||
| 35 | bless \%this, ref $class || $class; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub run | ||
| 39 | { | ||
| 40 | my $this = shift; | ||
| 41 | my ( $port, $max, $exec, $tmp, $rbuf, $wbuf ) = @$this{qw( port max exec tmp rbuf wbuf )}; | ||
| 42 | |||
| 43 | my $version = $MYDan::VERSION; $version =~ s/\./0/g; | ||
| 44 | |||
| 45 | $SIG{'USR1'} = sub { | ||
| 46 | print Dumper \%index; | ||
| 47 | }; | ||
| 48 | |||
| 49 | $SIG{INT} = $SIG{TERM} = sub { | ||
| 50 | map{ kill 'TERM', $_->{pid} if $_->{pid}; }values %index; | ||
| 51 | die "kill.\n"; | ||
| 52 | }; | ||
| 53 | |||
| 54 | $SIG{'CHLD'} = sub { | ||
| 55 | while((my $pid = waitpid(-1, WNOHANG)) >0) | ||
| 56 | { | ||
| 57 | my $code = ( $? == -1 || $? & 127 ) ? 110 : $? >> 8; | ||
| 58 | |||
| 59 | |||
| 60 | print "chld: $pid exit $code.\n";; | ||
| 61 | |||
| 62 | my ( $index ) = grep{ $index{$_}{pid} && $index{$_}{pid} eq $pid }keys %index; | ||
| 63 | next unless my $data = delete $index{$index}; | ||
| 64 | |||
| 65 | if( $data->{handle}->fh ) | ||
| 66 | { | ||
| 67 | $data->{handle}->push_write( "*#*MYDan_$version*#" ); | ||
| 68 | |||
| 69 | my $size = $wbuf ? ( stat "$tmp/$index.out" )[7] || 0 : 0; | ||
| 70 | |||
| 71 | if ( ( ! $wbuf || ( $wbuf && $size <= $wbuf ) ) && open my $tmp_handle, '<', "$tmp/$index.out" ) | ||
| 72 | { | ||
| 73 | #seek( $tmp_handle, -16384, SEEK_END ); | ||
| 74 | while(<$tmp_handle>) | ||
| 75 | { | ||
| 76 | $data->{handle}->push_write($_) if $data->{handle}->fh; | ||
| 77 | } | ||
| 78 | $data->{handle}->push_write("--- $code\n") if $data->{handle}->fh; | ||
| 79 | } | ||
| 80 | $data->{handle}->destroy() if $data->{handle}->fh; | ||
| 81 | } | ||
| 82 | |||
| 83 | map{ unlink "$tmp/$_" }( $index, "$index.out" ); | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | my ( $i, $cv ) = ( 0, AnyEvent->condvar ); | ||
| 88 | |||
| 89 | |||
| 90 | tcp_server undef, $port, sub { | ||
| 91 | my ( $fh, $tip, $tport ) = @_ or die "tcp_server: $!"; | ||
| 92 | |||
| 93 | printf "index: %s\n", ++ $i; | ||
| 94 | my $index = $i; | ||
| 95 | |||
| 96 | my $len = keys %index; | ||
| 97 | printf "tcpserver: status: $len/$max\n"; | ||
| 98 | |||
| 99 | if( $len >= $max ) | ||
| 100 | { | ||
| 101 | printf "connection limit reached, from %s:%s\n", $tip, $tport; | ||
| 102 | close $fh; | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | my $tmp_handle; | ||
| 107 | unless( open $tmp_handle, '>', "$tmp/$index" ) | ||
| 108 | { | ||
| 109 | print "open '$tmp/$index' fail:$!\n"; | ||
| 110 | close $fh; | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $handle; $handle = new AnyEvent::Handle( | ||
| 115 | fh => $fh, | ||
| 116 | keepalive => 1, | ||
| 117 | on_eof => sub{ | ||
| 118 | close $tmp_handle; | ||
| 119 | |||
| 120 | if ( my $pid = fork() ) | ||
| 121 | { | ||
| 122 | $index{$index}{pid} = $pid; | ||
| 123 | } | ||
| 124 | else | ||
| 125 | { | ||
| 126 | $tip = '0.0.0.0' unless $tip && $tip =~ /^\d+\.\d+\.\d+\.\d+$/; | ||
| 127 | $tport = '0' unless $tport && $tport =~ /^\d+$/; | ||
| 128 | |||
| 129 | $ENV{TCPREMOTEIP} = $tip; | ||
| 130 | $ENV{TCPREMOTEPORT} = $port; | ||
| 131 | |||
| 132 | open STDIN, '<', "$tmp/$index" or die "Can't open '$tmp/$index': $!"; | ||
| 133 | open STDOUT, '>', "$tmp/$index.out" or die "Can't open '$tmp/$index.out': $!"; | ||
| 134 | exec $exec; | ||
| 135 | } | ||
| 136 | }, | ||
| 137 | on_read => sub { | ||
| 138 | my $self = shift; | ||
| 139 | $index{$index}{rbuf} += length $self->{rbuf}; | ||
| 140 | |||
| 141 | $self->unshift_read ( | ||
| 142 | chunk => length $self->{rbuf}, | ||
| 143 | sub { print $tmp_handle $_[1] if ! $rbuf || ( $rbuf && $index{$index}{rbuf} <= $rbuf ); } | ||
| 144 | ); | ||
| 145 | }, | ||
| 146 | on_error => sub { | ||
| 147 | close $tmp_handle; | ||
| 148 | |||
| 149 | close $fh; | ||
| 150 | delete $handle->{fh}; | ||
| 151 | |||
| 152 | $handle->destroy(); | ||
| 153 | |||
| 154 | my $pid = $index{$index}{pid} if $index{$index}; | ||
| 155 | if( $pid ) { kill 15, $pid; } | ||
| 156 | else | ||
| 157 | { | ||
| 158 | unlink "$tmp/$index"; | ||
| 159 | } | ||
| 160 | }, | ||
| 161 | ); | ||
| 162 | $index{$index}{handle} = $handle; | ||
| 163 | |||
| 164 | }; | ||
| 165 | my $t = AnyEvent->timer( | ||
| 166 | after => 1, | ||
| 167 | interval => 1, | ||
| 168 | cb => sub { | ||
| 169 | map{ | ||
| 170 | $_->{handle}->push_write('*') if $_->{handle} && $_->{handle}->fh; | ||
| 171 | }values %index; | ||
| 172 | } | ||
| 173 | ); | ||
| 174 | |||
| 175 | $cv->recv; | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; |