/
Socket.pm
62 lines (49 loc) · 1.09 KB
/
Socket.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl6
class 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();
my $len = $received.chars;
my $buf;
while $len > 0 {
$buf = $!PIO.recv();
$received ~= $buf;
$len = $buf.chars;
}
return $received;
}
method send (Str $string) {
fail("Not connected") unless $!PIO;
return $!PIO.send($string);
}
method close () {
fail("Not connected!") unless $!PIO;
return $!PIO.close();
}
}