Skip to content

Commit

Permalink
use even less code
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 13, 2017
1 parent fcb9107 commit 7b789aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
24 changes: 11 additions & 13 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -19,10 +19,8 @@ my $NDN = HAS_NDN ? Net::DNS::Native->new(pool => 5, extra_thread => 1) : undef;
use constant HAS_SOCKS => $ENV{MOJO_NO_SOCKS}
? 0
: eval 'use IO::Socket::Socks 0.64 (); 1';
use constant SOCKS_READ => HAS_SOCKS ? IO::Socket::Socks::SOCKS_WANT_READ() : 0;
use constant SOCKS_WRITE => HAS_SOCKS
? IO::Socket::Socks::SOCKS_WANT_WRITE()
: 0;
use constant READ => HAS_SOCKS ? IO::Socket::Socks::SOCKS_WANT_READ() : 0;
use constant WRITE => HAS_SOCKS ? IO::Socket::Socks::SOCKS_WANT_WRITE() : 0;

has reactor => sub { Mojo::IOLoop->singleton->reactor };

Expand Down Expand Up @@ -85,7 +83,7 @@ sub _connect {
}
$handle->blocking(0);

$self->_wait($handle, $args);
$self->_wait('_ready', $handle, $args);
}

sub _port { $_[0]{socks_port} || $_[0]{port} || ($_[0]{tls} ? 443 : 80) }
Expand All @@ -98,7 +96,7 @@ sub _ready {
unless ($handle->connect) {
return $self->emit(error => $!) unless $! == EINPROGRESS;
$self->reactor->remove($handle);
return $self->_wait($handle, $args);
return $self->_wait('_ready', $handle, $args);
}

return $self->emit(error => $! || 'Not connected') unless $handle->connected;
Expand All @@ -118,9 +116,9 @@ sub _socks {

# Switch between reading and writing
my $err = $IO::Socket::Socks::SOCKS_ERROR;
if ($err == SOCKS_READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == SOCKS_WRITE) { $self->reactor->watch($handle, 1, 1) }
else { $self->emit(error => $err) }
if ($err == READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == WRITE) { $self->reactor->watch($handle, 1, 1) }
else { $self->emit(error => $err) }
}

sub _try_socks {
Expand All @@ -140,8 +138,8 @@ sub _try_socks {
$reactor->remove($handle);
return $self->emit(error => 'SOCKS upgrade failed')
unless IO::Socket::Socks->start_SOCKS($handle, %options);
weaken $self;
$reactor->io($handle => sub { $self->_socks($args) })->watch($handle, 0, 1);

$self->_wait('_socks', $handle, $args);
}

sub _try_tls {
Expand All @@ -161,9 +159,9 @@ sub _try_tls {
}

sub _wait {
my ($self, $handle, $args) = @_;
my ($self, $next, $handle, $args) = @_;
weaken $self;
$self->reactor->io($handle => sub { $self->_ready($args) })
$self->reactor->io($handle => sub { $self->$next($args) })
->watch($handle, 0, 1);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/IOLoop/TLS.pm
Expand Up @@ -8,8 +8,8 @@ use Mojo::File 'path';
use constant HAS_TLS => $ENV{MOJO_NO_TLS}
? 0
: eval 'use IO::Socket::SSL 1.94 (); 1';
use constant TLS_READ => HAS_TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant TLS_WRITE => HAS_TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;
use constant READ => HAS_TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant WRITE => HAS_TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;

has reactor => sub { Mojo::IOLoop->singleton->reactor };

Expand Down Expand Up @@ -74,8 +74,8 @@ sub _tls {

# Switch between reading and writing
my $err = $IO::Socket::SSL::SSL_ERROR;
if ($err == TLS_READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == TLS_WRITE) { $self->reactor->watch($handle, 1, 1) }
if ($err == READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == WRITE) { $self->reactor->watch($handle, 1, 1) }
}

1;

0 comments on commit 7b789aa

Please sign in to comment.