Created
July 21, 2014 12:46
-
-
Save sschober/4f1aac742f04f6511837 to your computer and use it in GitHub Desktop.
SSLSocket.pm - A perl wrapper for the standard Thrift::Socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Modern::Perl '2014'; | |
use IO::Socket::SSL; | |
use Thrift; | |
use Thrift::Socket; | |
=head1 NAME | |
Thrift::SSLSocket - A SSL wrapper around the standard C<Thrift::Socket> | |
=head1 SYNOPSIS | |
Use just like you would use the standard C<Thrift::Socket> | |
=cut | |
package Thrift::SSLSocket; | |
use base('Thrift::Socket'); | |
sub open { | |
my $self = shift; | |
my $sock = IO::Socket::SSL->new( | |
SSL_verify_mode => 'SSL_VERIFY_NONE', | |
PeerAddr => $self->{host}, | |
PeerPort => $self->{port}, | |
Proto => 'tcp', | |
Timeout => $self->{sendTimeout}/1000) | |
|| do { | |
my $error = 'TSocket: Could not connect to '.$self->{host}.':'.$self->{port}.' ('.$!.')'; | |
if ($self->{debug}) { | |
$self->{debugHandler}->($error); | |
} | |
die new Thrift::TException($error); | |
}; | |
$self->{handle} = new IO::Select( $sock ); | |
} | |
sub write { | |
my $self = shift; | |
my $buf = shift; | |
return unless defined $self->{handle}; | |
#check for timeout | |
my @sockets = $self->{handle}->can_write( $self->{sendTimeout} / 1000 ); | |
if(@sockets == 0){ | |
die new Thrift::TException('TSocket: timed out writing to bytes from '. | |
$self->{host}.':'.$self->{port}); | |
} | |
my $sock = $sockets[0]; | |
print $sock $buf; | |
} | |
sub read { | |
my $self = shift; | |
my $len = shift; | |
my @sockets = $self->{handle}->can_read( $self->{recvTimeout} / 1000 ); | |
if(@sockets == 0){ | |
die new Thrift::TException('TSocket: timed out reading '.$len.' bytes from '. | |
$self->{host}.':'.$self->{port}); | |
} | |
my $sock = $sockets[0]; | |
my ($buf,$sz); | |
read $sock, $buf, $len; | |
if (!defined $buf || $buf eq '') { | |
die new TException('TSocket: Could not read '.$len.' bytes from '. | |
$self->{host}.':'.$self->{port}); | |
} | |
return $buf; | |
} | |
sub readAll{ | |
my $self = shift; | |
my $len = shift; | |
return unless defined $self->{handle}; | |
my $pre = ""; | |
while (1) { | |
#check for timeout | |
my @sockets = $self->{handle}->can_read( $self->{recvTimeout} / 1000 ); | |
if(@sockets == 0){ | |
die new Thrift::TException('TSocket: timed out reading '.$len.' bytes from '. | |
$self->{host}.':'.$self->{port}); | |
} | |
my $sock = $sockets[0]; | |
my ($buf,$sz); | |
CORE::read $sock,$buf,$len; | |
if (!defined $buf || $buf eq '') { | |
die new Thrift::TException('TSocket: Could not read '.$len.' bytes from '. | |
$self->{host}.':'.$self->{port}); | |
} elsif (($sz = length($buf)) < $len) { | |
$pre .= $buf; | |
$len -= $sz; | |
} else { | |
return $pre.$buf; | |
} | |
} | |
} | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment