/
Socket.pm6
140 lines (108 loc) · 3.7 KB
/
Socket.pm6
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use v6;
use MongoDB;
#-------------------------------------------------------------------------------
unit package MongoDB:auth<github:MARTIMM>;
#-------------------------------------------------------------------------------
class Server::Socket {
has IO::Socket::INET $!sock;
has Int $.thread-id;
has Int $.time-last-used;
# has Bool $!must-authenticate;
has Bool $.is-open;
has MongoDB::ServerType $.server;
#-----------------------------------------------------------------------------
submethod BUILD ( MongoDB::ServerType:D :$!server ) {
$!is-open = True;
$!thread-id = $*THREAD.id;
$!time-last-used = time;
#todo try connect ipv4/ipv6 in parallel
trace-message("open socket $!server.server-name(), $!server.server-port()");
try {
$!sock .= new( :host($!server.server-name), :port($!server.server-port));
CATCH {
default {
# Retry for ipv6
$!sock .= new(
:host($!server.server-name),
:port($!server.server-port),
:family(PF_INET6)
);
}
}
}
};
#-----------------------------------------------------------------------------
method check ( --> Bool ) {
my Bool $is-closed = False;
if (time - $!time-last-used) > MAX-SOCKET-UNUSED-OPEN {
debug-message("close socket, timeout after {time - $!time-last-used} sec");
$!sock.close if $!sock.defined;
$!is-open = False;
$is-closed = True;
}
else {
$is-closed = ! $!is-open;
}
$is-closed;
}
#-----------------------------------------------------------------------------
method send ( Buf:D $b --> Nil ) {
fatal-message("thread $*THREAD.id() is not owner of this socket")
unless $.thread-id == $*THREAD.id();
fatal-message("Socket not opened") unless $!sock.defined;
trace-message("socket send, size: $b.elems()");
$!sock.write($b);
$!time-last-used = time;
}
#-----------------------------------------------------------------------------
method receive ( int $nbr-bytes --> Buf ) {
fatal-message("thread $*THREAD.id() is not owner of this socket")
unless $.thread-id == $*THREAD.id();
fatal-message("socket not opened") unless $!sock.defined;
my Buf $b = $!sock.read($nbr-bytes);
$!time-last-used = time;
trace-message("socket receive, request size $nbr-bytes, received size $b.elems()");
$b;
}
#-----------------------------------------------------------------------------
method close ( ) {
fatal-message("thread $*THREAD.id() is not owner of this socket")
unless $.thread-id == $*THREAD.id();
$!sock.close if $!sock.defined;
$!sock = Nil;
trace-message("Close socket");
$!is-open = False;
$!time-last-used = time;
}
#-----------------------------------------------------------------------------
method close-on-fail ( ) {
# An Exception can be thrown and caught in another thread. When then a
# socket must close it should be able to do so
#fatal-message("thread $*THREAD.id() is not owner of this socket")
# unless $.thread-id == $*THREAD.id();
warn-message("close exception where thread $*THREAD.id() is not owner of this socket")
unless $.thread-id == $*THREAD.id();
trace-message("'close' socket on failure");
$!sock = Nil;
$!is-open = False;
$!time-last-used = time;
}
#-----------------------------------------------------------------------------
method cleanup ( ) {
# Close can have exceptions
try {
if $!sock.defined {
$!sock.close;
$!sock = Nil;
}
else {
$!sock = Nil;
}
$!is-open = False;
CATCH {
$!sock = Nil;
$!is-open = False;
}
}
}
}