| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | |||
| 7 | our $session_stream; | ||
| 8 | |||
| 9 | |||
| 10 | my ( $host, $port ) = @ARGV[ 0, 1 ]; | ||
| 11 | $host //= '127.0.0.1'; | ||
| 12 | $port //= 9000; | ||
| 13 | |||
| 14 | my $loop; | ||
| 15 | my $timer; | ||
| 16 | my $tty; | ||
| 17 | |||
| 18 | my $verbose = 0; | ||
| 19 | |||
| 20 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 21 | use IO::Async::Loop; | ||
| 22 | $loop = IO::Async::Loop->new; | ||
| 23 | |||
| 24 | use IO::Async::Timer::Periodic; | ||
| 25 | $timer = IO::Async::Timer::Periodic->new( | ||
| 26 | first_interval => 0, | ||
| 27 | interval => 2, | ||
| 28 | |||
| 29 | on_tick => sub { | ||
| 30 | # warn ">>@_<<<< Retrying"; | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | unless( $session_stream && $session_stream->loop ) { | ||
| 34 | create_dbg_session( $loop, $host, $port ); | ||
| 35 | } else { | ||
| 36 | # warn "Stopping the timer"; | ||
| 37 | $self->stop; | ||
| 38 | } | ||
| 39 | }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 45 | use IO::Async::Stream; | ||
| 46 | $tty = IO::Async::Stream->new( | ||
| 47 | read_handle => \*STDIN, | ||
| 48 | write_handle => \*STDOUT, | ||
| 49 | on_read => \&tty_read, | ||
| 50 | ); | ||
| 51 | |||
| 52 | |||
| 53 | sub tty_read { | ||
| 54 | my ( $self, $buffref, $eof ) = @_; | ||
| 55 | |||
| 56 | if( $$buffref =~ s/-v\n// ) { | ||
| 57 | $verbose = $verbose ? 0 : 1 ; | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | if( $$buffref =~ s/-vv\n// ) { | ||
| 61 | $verbose = 2; | ||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | |||
| 65 | if( $session_stream && $session_stream->loop ) { | ||
| 66 | $session_stream->write( $$buffref ); | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $tty->write( "No debug session is active\n" ); | ||
| 70 | } | ||
| 71 | |||
| 72 | $$buffref = ''; | ||
| 73 | |||
| 74 | if( $eof ) { | ||
| 75 | warn "STDIN EOF"; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | $loop->add( $tty ); | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 87 | sub handle_write_eof { | ||
| 88 | warn "Write EOF: >>@_<<"; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub handle_write_error { | ||
| 93 | warn "Write error: >>@_<<"; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub handle_closed { | ||
| 98 | warn "Closed >>@_<<$session_stream<<"; | ||
| 99 | |||
| 100 | # Q: It is so restrictive... I think, it sould be enough just warn about | ||
| 101 | # that timer is already running, because that is not so critical | ||
| 102 | $timer->start unless $timer->is_running; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | # Why this is called after 'handle_closed'? | ||
| 107 | sub handle_read_eof { | ||
| 108 | warn "Read EOF: >>@_<<"; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub handle_read_error { | ||
| 113 | warn "Read error: >>@_<<"; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub handle_read { | ||
| 118 | my( $self, $buffref, $eof ) = @_; | ||
| 119 | |||
| 120 | on_data( $buffref ); | ||
| 121 | $$buffref = ''; | ||
| 122 | |||
| 123 | if( $eof ) { | ||
| 124 | warn "GOT EOF at $self"; | ||
| 125 | $self->close_when_empty(); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | return 0; | ||
| 130 | }; | ||
| 131 | |||
| 132 | |||
| 133 | sub on_data { | ||
| 134 | my( $data ) = @_; | ||
| 135 | |||
| 136 | $tty->write( "New data(DC)\n" ) if $verbose; | ||
| 137 | $data = $$data; | ||
| 138 | $data = ' -'x20 ."\n" .$data ." -"x20 ."\n" if $verbose > 1; | ||
| 139 | $tty->write( $data ); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 145 | sub on_dbg_session { | ||
| 146 | ( $session_stream ) = @_; | ||
| 147 | |||
| 148 | |||
| 149 | $session_stream->configure( | ||
| 150 | close_on_read_eof => 0 | ||
| 151 | ,autoflush => 1 | ||
| 152 | |||
| 153 | ,on_write_eof => \&handle_write_eof | ||
| 154 | ,on_write_error => \&handle_write_error | ||
| 155 | ,on_closed => \&handle_closed | ||
| 156 | |||
| 157 | ,on_read_eof => \&handle_read_eof | ||
| 158 | ,on_read_error => \&handle_read_error | ||
| 159 | ,on_read => \&handle_read | ||
| 160 | ); | ||
| 161 | |||
| 162 | |||
| 163 | $loop->add( $session_stream ); | ||
| 164 | |||
| 165 | $session_stream->write( "l 0\n" ); | ||
| 166 | warn "DBG session activated"; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | # Подключение по протоколу TCP к хосту на порт | ||
| 171 | sub create_dbg_session { | ||
| 172 | my( $loop, $host, $port ) = @_; | ||
| 173 | |||
| 174 | |||
| 175 | $loop->connect( | ||
| 176 | host => $host, | ||
| 177 | service => $port, | ||
| 178 | socktype => 'stream', | ||
| 179 | |||
| 180 | on_resolve_error => sub { die "Cannot resolve - >>@_<<\n"; }, | ||
| 181 | on_connect_error => sub { 0 && warn "Cannot connect\n"; }, | ||
| 182 | |||
| 183 | # Успешное подключение | ||
| 184 | on_stream => \&on_dbg_session | ||
| 185 | ); | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 191 | $timer->start; | ||
| 192 | $loop->add( $timer ); | ||
| 193 | $loop->run; | ||
| 194 | |||
| 195 | |||
| 196 | __END__ |