Ext/MsgPack/RPC/EventLoop.pm

filter: Statementstmt 84.6% 127 / 150 Branchbran 56.6% 17 / 30 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 86.1% 31 / 36 Podpod 100% 11 / 11 totaltotal 76.7% 191 / 249 CC 23 SCAR 12.8
CC
23
Cov
77%
__ANON__
12.0
connect_socket
8.1
new
6.6
CRAP 3.6
« Ext/MsgPack/RPC/Response.pm ↑ Summary Ext/Common.pm »
1 package Neovim::Ext::MsgPack::RPC::EventLoop;
2 56 $Neovim::Ext::MsgPack::RPC::EventLoop::VERSION = '0.01';
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use base qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 ▶use IPC::Open3 qw/open3/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 56 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use IO::Async::Signal;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 56 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 56 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 56 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 56 ▶use Time::HiRes qw/usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 56 ▶use Socket qw/SOCK_STREAM/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 56 __PACKAGE__->mk_accessors (qw/loop stream data_cb _transport_type _can_close _pid signals/);
16  
17  
18 sub new
19 {
20 52 ▶ my $this = shift;
Subroutine new: called CC=6 SCAR=18.9
Pod documented
21 52 my $transport_type = shift;
22  
23 52 ▶ my $class = ref ($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0520
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
24 52 my $self =
25 {
26 loop => IO::Async::Loop->new,
27 signals => [],
28 _transport_type => $transport_type,
29 };
30  
31 52 my $obj = bless $self, $class;
32 52 ▶ if ($transport_type eq 'stdio')
Branch: 4 branches
BranchTrueFalseTotal
if ($transport_type eq 'stdio') { } 0 52 52
elsif ($transport_type eq 'tcp') { } 50 2 52
elsif ($transport_type eq 'socket') { } 1 1 2
elsif ($transport_type eq 'child') { } 1 0 1
33 {
34 0 $obj->connect_stdio (@_);
35 }
36 elsif ($transport_type eq 'tcp')
37 {
38 50 $obj->connect_tcp (@_);
39 }
40 elsif ($transport_type eq 'socket')
41 {
42 1 $obj->connect_socket (@_);
43 }
44 elsif ($transport_type eq 'child')
45 {
46 1 $obj->connect_child (@_);
47 }
48 else
49 {
50 0 die "Unsupported transport type: $transport_type\n";
51 }
52  
53 43 return $obj;
54 }
55  
56  
57  
58 sub DESTROY
59 {
60 9 ▶ my ($this) = @_;
Subroutine DESTROY: called CC=2 SCAR=8.1
61  
62 9 ▶ if ($this->_pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->_pid) 0 9 9
63 {
64 0 waitpid ($this->_pid, 0);
65 }
66 }
67  
68  
69  
70 sub connect_stdio
71 {
72 0 ▶ my ($this) = @_;
Subroutine connect_stdio: not called CC=1 SCAR=6.9
Pod documented
73  
74 0 binmode STDIN;
75 0 binmode STDOUT;
76  
77 0 $this->_create_stream (read_handle => \*STDIN, write_handle => \*STDOUT);
78 }
79  
80  
81  
82 sub connect_tcp
83 {
84 50 ▶ my ($this, $address, $port, $retries, $retryInterval) = @_;
Subroutine connect_tcp: called CC=5 SCAR=16.1
Pod documented
85  
86 50 ▶ $retries //= 0;
Condition50%1/2
$retries //= 0
l!l
500
MC/DC0%0/1
$retries //= 0
$retries
Truth table50%1/2
A
A= $retries
Aresult
11
00
87 50 ▶ $retryInterval //= 100;
Condition50%1/2
$retryInterval //= 100
l!l
500
MC/DC0%0/1
$retryInterval //= 100
$retryInterval
Truth table50%1/2
A
A= $retryInterval
Aresult
11
00
88  
89 500 AGAIN:
90 my $socket = IO::Socket::INET->new
91 (
92 PeerAddr => $address,
93 PeerPort => $port,
94 Proto => 'tcp',
95 Type => SOCK_STREAM
96 );
97  
98 500 ▶ if (!$socket)
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 459 41 500
99 {
100 459 ▶ if ($retries == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($retries == 0) 9 450 459
101 {
102 9 die "Couldn't connect to $address:$port: $!\n";
103 }
104  
105 450 --$retries;
106 450 usleep ($retryInterval*1000);
107 450 goto AGAIN;
108 }
109  
110 41 $this->_create_stream (handle => $socket);
111 41 $this->_can_close (1);
112 }
113  
114  
115  
116 sub connect_socket
117 {
118 1 ▶ my ($this, $path, $retries, $retryInterval) = @_;
Subroutine connect_socket: called CC=5 SCAR=20.9
Pod documented
119  
120 1 ▶ $retries //= 0;
Condition50%1/2
$retries //= 0
l!l
10
MC/DC0%0/1
$retries //= 0
$retries
Truth table50%1/2
A
A= $retries
Aresult
11
00
121 1 ▶ $retryInterval //= 100;
Condition50%1/2
$retryInterval //= 100
l!l
10
MC/DC0%0/1
$retryInterval //= 100
$retryInterval
Truth table50%1/2
A
A= $retryInterval
Aresult
11
00
122  
123 1 AGAIN:
124 my $socket = IO::Socket::UNIX->new
125 (
126 Type => SOCK_STREAM,
127 Peer => $path,
128 );
129  
130 1 ▶ if (!$socket)
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 0 1 1
131 {
132 0 ▶ if ($retries == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($retries == 0) 0 0 0
133 {
134 0 die "Couldn't connect to $path: $!\n";
135 }
136  
137 0 --$retries;
138 0 usleep ($retryInterval*1000);
139 0 goto AGAIN;
140 }
141  
142 1 $this->_create_stream (handle => $socket);
143 1 $this->_can_close (1);
144 }
145  
146  
147  
148 sub connect_child
149 {
150 1 ▶ my ($this, $argv) = @_;
Subroutine connect_child: called CC=2 SCAR=7.2
Pod documented
151  
152 1 ▶ if ($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
153 {
154 0 die "Not supported!";
155 }
156  
157 1 $this->_pid (open3 (\*CHILD_IN, \*CHILD_OUT, \*ERR, @$argv));
158 1 $this->_create_stream (read_handle => \*CHILD_OUT, write_handle => \*CHILD_IN);
159 1 $this->_can_close (1);
160 }
161  
162  
163  
164 sub _create_stream
165 {
166 43 ▶ my ($this, %options) = @_;
Subroutine _create_stream: called CC=1 SCAR=0.0
167  
168 43 $this->stream (IO::Async::Stream->new
169 (
170 %options,
171 on_read => $this->_on_read(),
172 on_read_error => $this->_on_read_error(),
173 on_read_eof => $this->_on_read_eof(),
174 )
175 );
176  
177 43 $this->loop->add ($this->stream);
178 }
179  
180  
181  
182 sub _on_read
183 {
184 43 ▶ my ($this) = @_;
Subroutine _on_read: called CC=1 SCAR=0.0
185  
186 43 my $loop = $this;
187 43 weaken ($loop);
188  
189 return sub
190 {
191 84 ▶ my ($self, $buffref, $eof) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
192  
193 # Consume all the data
194 84 my $data = $$buffref;
195 84 $$buffref = '';
196  
197 84 $loop->data_cb->($data);
198  
199 84 return 0;
200 43 };
201 }
202  
203  
204  
205 sub _on_read_error
206 {
207 43 ▶ my ($this) = @_;
Subroutine _on_read_error: called CC=1 SCAR=0.0
208  
209 43 my $loop = $this;
210 43 weaken ($loop);
211  
212 return sub
213 {
214 10 ▶ $loop->stop();
Subroutine __ANON__: called CC=1 SCAR=0.0
215 10 die "read error\n";
216 43 };
217 }
218  
219  
220  
221 sub _on_read_eof
222 {
223 43 ▶ my ($this) = @_;
Subroutine _on_read_eof: called CC=1 SCAR=0.0
224  
225 43 my $loop = $this;
226 43 weaken ($loop);
227  
228 return sub
229 {
230 33 ▶ $loop->stop();
Subroutine __ANON__: called CC=1 SCAR=0.0
231 33 die "read eof\n";
232 43 };
233 }
234  
235  
236  
237 sub _on_signal
238 {
239 122 ▶ my ($this) = @_;
Subroutine _on_signal: called CC=1 SCAR=2.9
240  
241 122 my $loop = $this;
242 122 weaken ($loop);
243  
244 return sub
245 {
246 0 ▶ my ($signal) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
247  
248 0 ▶ if ($loop->_transport_type eq 'stdio' && $signal eq 'INT')
Branch: 1 branch
BranchTrueFalseTotal
if ($loop->_transport_type eq "stdio" and $signal eq "INT") 0 0 0
Condition0%0/3
$loop->_transport_type eq "stdio" and $signal eq "INT"
!ll&&!rl&&r
000
MC/DC0%0/2
$loop->_transport_type eq "stdio" and $signal eq "INT"
$loop->_transport_type eq "stdio" $signal eq "INT"
Truth table0%0/3
A and B
A= $loop->_transport_type eq "stdio"B= $signal eq "INT"
ABresult
0X0
100
111
249 {
250 # Probably running as a nvim child process, we don't want
251 # to be killed by ctrl+C
252 0 return;
253 }
254  
255 0 $loop->stop();
256 122 };
257 }
258  
259  
260  
261 sub send
262 {
263 104 ▶ my ($this, $data) = @_;
Subroutine send: called CC=1 SCAR=0.0
Pod documented
264 104 $this->stream->write ($data);
265 }
266  
267  
268  
269 sub create_future
270 {
271 61 ▶ my ($this) = @_;
Subroutine create_future: called CC=1 SCAR=0.0
Pod documented
272 61 return $this->loop->new_future;
273 }
274  
275  
276  
277 sub await
278 {
279 0 ▶ my ($this, $future) = @_;
Subroutine await: not called CC=1 SCAR=6.9
Pod documented
280  
281 0 my @result = $this->loop->await ($future);
282 0 return @result;
283 }
284  
285  
286  
287 sub _setup_signals
288 {
289 61 ▶ my ($this, @signals) = @_;
Subroutine _setup_signals: called CC=3 SCAR=11.2
290  
291 61 ▶ return if ($^O eq 'MSWin32');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 61 61
292  
293 61 foreach my $signal (@signals)
294 {
295 122 my $handler = $this->_on_signal();
296  
297 my $signal = IO::Async::Signal->new
298 (
299 name => $signal,
300 on_receipt => sub
301 {
302 0 ▶ $handler->($signal);
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 }
304 122 );
305  
306 122 $this->loop->add ($signal);
307 122 push @{$this->signals}, $signal;
308 }
309 }
310  
311  
312  
313 sub _teardown_signals
314 {
315 18 ▶ my ($this) = @_;
Subroutine _teardown_signals: called CC=2 SCAR=6.9
316  
317 18 ▶ while (my $signal = shift @{$this->signals})
Branch: 1 branch
BranchTrueFalseTotal
if (my $signal = shift @{$this->signals;}) 36 18 54
318 {
319 36 $this->loop->remove ($signal);
320 }
321 }
322  
323  
324  
325 sub run
326 {
327 61 ▶ my ($this, $data_cb, $setup_cb) = @_;
Subroutine run: called CC=2 SCAR=7.0
Pod documented
328  
329 61 ▶ $this->loop->later ($setup_cb) if ($setup_cb);
Branch: 1 branch
BranchTrueFalseTotal
if $setup_cb 0 61 61
330  
331 61 $this->data_cb ($data_cb);
332  
333 61 $this->_setup_signals ('INT', 'TERM');
334 61 $this->loop->run;
335 18 $this->_teardown_signals();
336 }
337  
338  
339  
340 sub stop
341 {
342 61 ▶ my ($this) = @_;
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
343 61 $this->loop->stop;
344 }
345  
346  
347  
348 sub close
349 {
350 0 ▶ my ($this) = @_;
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
351  
352 0 ▶ $this->stream->close_now if ($this->_can_close);
Branch: 1 branch
BranchTrueFalseTotal
if $this->_can_close 0 0 0
353 }
354  
355 =head1 NAME
356  
357 Neovim::Ext::MsgPack::RPC::EventLoop - Neovim::Ext::MsgPack::RPC::EventLoop class
358  
359 =head1 VERSION
360  
361 version 0.01
362  
363 =head1 SYNOPSIS
364  
365 use Neovim::Ext;
366  
367 =head1 METHODS
368  
369 =head2 connect_stdio( )
370  
371 Connect using stdin/stdout.
372  
373 =head2 connect_tcp( $address, $port )
374  
375 Connect to tcp/ip C<$address>:C<port>.
376  
377 =head2 connect_socket( $path )
378  
379 Connect to socket at C<$path>.
380  
381 =head2 connect_child( \@argv )
382  
383 Connect to a new Nvim instance. Uses C<\@argv> as the argument vector to
384 spawn an embedded Nvim. This isn't support on Windows.
385  
386 =head2 create_future( )
387  
388 Create a future.
389  
390 =head2 await( $future)
391  
392 Wait for C<$future> to complete.
393  
394 =head2 close( )
395  
396 Stop the event loop.
397  
398 =head2 send( $data )
399  
400 Queue C<$data> for sending to Nvim.
401  
402 =head2 run( $data_cb )
403  
404 Run the event loop, calling C<$data_cb> for each message received.
405  
406 =head2 stop( )
407  
408 Stop the event loop.
409  
410 =cut
411  
412 56 1;
« Ext/MsgPack/RPC/Response.pm ↑ Summary Ext/Common.pm »