Skip to content

Commit

Permalink
added server bits to IO::Socket::INET
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Masak committed Apr 19, 2009
1 parent cf80109 commit 6161852
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
36 changes: 2 additions & 34 deletions src/setting/IO/Socket.pm
@@ -1,40 +1,8 @@
role IO::Socket {
has %.options;
has Bool $.Listener;
use v6;

role IO::Socket {
has $!PIO;

method open (Str $hostname, Int $port) {

Q:PIR {
.include "socket.pasm"
.local pmc sock
.local pmc address
.local string hostname
.local int port
.local string buf
.local int ret
$P0 = find_lex "$hostname"
hostname = $P0
$P0 = find_lex "$port"
port = $P0
# Create the socket handle
sock = new 'Socket'
unless sock goto ERR
sock.'socket'(.PIO_PF_INET, .PIO_SOCK_STREAM, .PIO_PROTO_TCP)
# Pack a sockaddr_in structure with IP and port
address = sock.'sockaddr'(hostname, port)
sock.'connect'(address)
setattribute self, '$!PIO', sock
ERR:
.return (0)
}
}

method recv () {
fail('Socket not available') unless $!PIO;
my $received = $!PIO.recv();
Expand Down
62 changes: 54 additions & 8 deletions src/setting/IO/Socket/INET.pm
@@ -1,9 +1,55 @@
class IO::Socket::INET {

has Int $.Version = 4; # Whether to use IPv4 or IPv6
has Str $.Protocol = 'TCP';
has Str $.RemoteHost;
has Int $.RemotePort;
has Str $.LocalHost;
has Int $.LocalPort;
class IO::Socket::INET does IO::Socket {

has $!listener;

method open (Str $hostname, Int $port) {

Q:PIR {
.include "socket.pasm"
.local pmc sock
.local pmc address
.local string hostname
.local int port
.local string buf
.local int ret
$P0 = find_lex "$hostname"
hostname = $P0
$P0 = find_lex "$port"
port = $P0
# Create the socket handle
sock = new 'Socket'
unless sock goto ERR
sock.'socket'(.PIO_PF_INET, .PIO_SOCK_STREAM, .PIO_PROTO_TCP)
# Pack a sockaddr_in structure with IP and port
address = sock.'sockaddr'(hostname, port)
sock.'connect'(address)
setattribute self, '$!PIO', sock
ERR:
.return (0)
}
}

method socket(Int $domain, Int $type, Int $protocol) {
my $listener := Q:PIR {{ %r = new 'Socket' }};
$listener.socket($domain, $type, $protocol);
return IO::Socket::INET.new( :listener($listener) );
}

method bind($host, $port) {
$!listener.bind($!listener.sockaddr($host, $port));
return self;
}

method listen() {
$!listener.listen(1);
return self;
}

method accept() {
return $!listener.accept();
}
}

0 comments on commit 6161852

Please sign in to comment.