Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added server bits to IO::Socket::INET
- Loading branch information
Carl Masak
committed
Apr 19, 2009
1 parent
cf80109
commit 6161852
Showing
2 changed files
with
56 additions
and
42 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |