Created
March 8, 2011 10:57
-
-
Save Timbus/860141 to your computer and use it in GitHub Desktop.
A more perl5 IO::Socket::INET
This file contains 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
class IO::Socket::INET2 does IO::Socket { | |
has $.peeraddr; | |
has $.peerport = 80; | |
has $.localaddr; | |
has $.localport; | |
has $.listen; | |
has $.proto = PIO::PROTO_TCP; | |
has $.type = PIO::SOCK_STREAM; | |
method new (*%args is copy) { | |
%args<peeraddr> //= %args<peerhost>; | |
my ($peer, $port) = %args<peeraddr>.split(':', 2); | |
if $port { | |
%args<peerport> //= $port; | |
%args<peeraddr> = $peer; | |
} | |
if %args<localaddr> || %args<localhost> { | |
%args<localaddr> //= %args<localhost>; | |
($peer, $port) = %args<localaddr>.split(':', 2); | |
if $port { | |
%args<localport> //= $port; | |
%args<localaddr> = $peer; | |
} | |
} | |
fail "Nothing given for new socket to connect or bind to" unless %args<peeraddr> | %args<listen>; | |
self.bless(*, |%args); | |
} | |
submethod BUILD { | |
#Callsame first to get all the actual class composition+construction done. | |
callsame; | |
$!PIO = Q:PIR { %r = root_new ['parrot';'Socket'] }; | |
$!PIO.socket(PIO::PF_INET, $.type, $.proto); | |
#If Listen is defined then a listen socket is created, else if the socket type, | |
#which is derived from the protocol, is SOCK_STREAM then connect() is called. | |
if $.listen || $.localaddr || $.localport { | |
my $addr = $!PIO.sockaddr($.localaddr || "0.0.0.0", $.localport || 0); | |
$!PIO.bind(pir::descalarref__PP($addr)); | |
} | |
if !$.listen && $.proto == PIO::PROTO_TCP() { | |
my $addr = $!PIO.sockaddr($.peeraddr, $.peerport); | |
$!PIO.connect(pir::descalarref__PP($addr)); | |
} | |
} | |
method get { | |
$!PIO.readline; | |
} | |
method lines { | |
gather { take $!PIO.readline }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment