FFI/SocketBase.pm

filter: Statementstmt 20.0% 33 / 165 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.6% 9 / 38 Podpod 0.0% 0 / 22 totaltotal 16.7% 42 / 251 CC 17 SCAR 19.1
CC
17
Cov
17%
send_multipart
30.0
recv_multipart
12.0
_unpack
12.0
CRAP 6.7
↑ Summary FFI/Util.pm »
1 package ZMQ::FFI::SocketBase;
2 1 $ZMQ::FFI::SocketBase::VERSION = '0.16';
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no if $] >= 5.018, warnings => "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use FFI::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 use Math::Int64 qw(
14 int64_to_native native_to_int64
15 uint64_to_native native_to_uint64
16 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use ZMQ::FFI::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 with qw(
21 ZMQ::FFI::SocketRole
22 ZMQ::FFI::ErrorHandler
23 ZMQ::FFI::Versioner
24 );
25  
26 1 has _ffi => (
27 is => 'ro',
28 init_arg => undef,
29 lazy => 1,
30 builder => '_init_ffi',
31 );
32  
33 # real underlying zmq ctx pointer
34 has _ctx => (
35 is => 'ro',
36 lazy => 1,
37 0 ▶ default => sub { shift->ctx->_ctx },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 1 );
39  
40 # real underlying zmq socket pointer
41 1 has _socket => (
42 is => 'rw',
43 default => -1,
44 );
45  
46 sub BUILD {
47 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=2 SCAR=17.9
Pod undocumented
48  
49 0 $self->_socket( $self->_ffi->{zmq_socket}->($self->_ctx, $self->type) );
50  
51 try {
52 0 ▶ $self->check_null('zmq_socket', $self->_socket);
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 }
54 catch {
55 0 ▶ $self->_socket(-1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 die $_;
57 0 };
58  
59 # ensure clean edge state
60 0 ▶ while ( $self->has_pollin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pollin) 0 0 0
61 0 $self->recv();
62 }
63 }
64  
65 sub connect {
66 0 ▶ my ($self, $endpoint) = @_;
Subroutine connect: not called CC=2 SCAR=17.9
Pod undocumented
67  
68 0 ▶ unless ($endpoint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($endpoint) 0 0 0
69 0 croak 'usage: $socket->connect($endpoint)'
70 }
71  
72 $self->check_error(
73 'zmq_connect',
74 0 $self->_ffi->{zmq_connect}->($self->_socket, $endpoint)
75 );
76 }
77  
78 sub disconnect {
79 0 ▶ croak 'unimplemented in base class';
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod undocumented
80 }
81  
82 sub bind {
83 0 ▶ my ($self, $endpoint) = @_;
Subroutine bind: not called CC=2 SCAR=17.9
Pod undocumented
84  
85 0 ▶ unless ($endpoint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($endpoint) 0 0 0
86 0 croak 'usage: $socket->bind($endpoint)'
87 }
88  
89 $self->check_error(
90 'zmq_bind',
91 0 $self->_ffi->{zmq_bind}->($self->_socket, $endpoint)
92 );
93 }
94  
95 sub unbind {
96 0 ▶ croak 'unimplemented in base class';
Subroutine unbind: not called CC=1 SCAR=6.9
Pod undocumented
97 }
98  
99 sub send {
100 0 ▶ croak 'unimplemented in base class';
Subroutine send: not called CC=1 SCAR=6.9
Pod undocumented
101 }
102  
103 sub send_multipart {
104 0 ▶ my ($self, $partsref, $flags) = @_;
Subroutine send_multipart: not called CC=5 SCAR=34.0
Pod undocumented
105  
106 0 ▶ $flags //= 0;
Condition0%0/2
$flags //= 0
l!l
00
MC/DC0%0/1
$flags //= 0
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
107  
108 0 ▶ my @parts = @{$partsref // []};
Condition0%0/2
$partsref // []
l!l
00
MC/DC0%0/1
$partsref // []
$partsref
Truth table0%0/2
A
A= $partsref
Aresult
11
00
109 0 ▶ unless (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@parts) 0 0 0
110 0 croak 'usage: send_multipart($parts, $flags)';
111 }
112  
113 0 for my $i (0..$#parts-1) {
114 0 $self->send($parts[$i], $flags | ZMQ_SNDMORE);
115 }
116  
117 0 $self->send($parts[$#parts], $flags);
118 }
119  
120 sub recv {
121 0 ▶ croak 'unimplemented in base class';
Subroutine recv: not called CC=1 SCAR=6.9
Pod undocumented
122 }
123  
124 sub recv_multipart {
125 0 ▶ my ($self, $flags) = @_;
Subroutine recv_multipart: not called CC=3 SCAR=24.8
Pod undocumented
126  
127 0 my @parts = ( $self->recv($flags) );
128  
129 0 my ($major) = $self->version;
130 0 ▶ my $type = $major == 2 ? 'int64_t' : 'int';
Branch: 1 branch
BranchTrueFalseTotal
$major == 2 ? : 0 0 0
131  
132 0 ▶ while ( $self->get(ZMQ_RCVMORE, $type) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get(ZMQ_RCVMORE(), $type)) 0 0 0
133 0 push @parts, $self->recv($flags);
134 }
135  
136 0 return @parts;
137 }
138  
139 sub get_fd {
140 0 ▶ my $self = shift;
Subroutine get_fd: not called CC=1 SCAR=6.9
Pod undocumented
141  
142 0 return $self->get(ZMQ_FD, 'int');
143 }
144  
145 sub set_linger {
146 0 ▶ my ($self, $linger) = @_;
Subroutine set_linger: not called CC=1 SCAR=6.9
Pod undocumented
147  
148 0 $self->set(ZMQ_LINGER, 'int', $linger);
149 }
150  
151 sub get_linger {
152 0 ▶ return shift->get(ZMQ_LINGER, 'int');
Subroutine get_linger: not called CC=1 SCAR=6.9
Pod undocumented
153 }
154  
155 sub set_identity {
156 0 ▶ my ($self, $id) = @_;
Subroutine set_identity: not called CC=1 SCAR=6.9
Pod undocumented
157  
158 0 $self->set(ZMQ_IDENTITY, 'binary', $id);
159 }
160  
161 sub get_identity {
162 0 ▶ return shift->get(ZMQ_IDENTITY, 'binary');
Subroutine get_identity: not called CC=1 SCAR=6.9
Pod undocumented
163 }
164  
165 sub subscribe {
166 0 ▶ my ($self, $topic) = @_;
Subroutine subscribe: not called CC=1 SCAR=6.9
Pod undocumented
167  
168 0 $self->set(ZMQ_SUBSCRIBE, 'binary', $topic);
169 }
170  
171 sub unsubscribe {
172 0 ▶ my ($self, $topic) = @_;
Subroutine unsubscribe: not called CC=1 SCAR=6.9
Pod undocumented
173  
174 0 $self->set(ZMQ_UNSUBSCRIBE, 'binary', $topic);
175 }
176  
177 sub has_pollin {
178 0 ▶ my $self = shift;
Subroutine has_pollin: not called CC=1 SCAR=6.9
Pod undocumented
179  
180 0 my $zmq_events = $self->get(ZMQ_EVENTS, 'int');
181 0 return $zmq_events & ZMQ_POLLIN;
182 }
183  
184 sub has_pollout {
185 0 ▶ my $self = shift;
Subroutine has_pollout: not called CC=1 SCAR=6.9
Pod undocumented
186  
187 0 my $zmq_events = $self->get(ZMQ_EVENTS, 'int');
188 0 return $zmq_events & ZMQ_POLLOUT;
189 }
190  
191 sub get {
192 0 ▶ my ($self, $opt, $opt_type) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
193  
194 0 my $optval;
195 my $optval_ptr;
196 0 my $optval_len;
197  
198 0 for ($opt_type) {
199 0 when (/^(binary|string)$/) {
200 # ZMQ_IDENTITY uses binary type and
201 # can be at most 255 bytes long
202  
203 # ZMQ_LAST_ENDPOINT uses string type and
204 # expects a buffer large enough to hold an endpoint string
205  
206 # so for both cases 256 should be sufficient (including \0)
207 0 my $buflen = 256;
208  
209 0 $optval_ptr = FFI::Raw::memptr($buflen);
210 0 $optval_len = pack 'L!', $buflen;
211 }
212  
213 0 default {
214 # zeroed memory region
215 0 $optval = $self->_pack($opt_type, 0);
216  
217 0 $optval_ptr = unpack('L!', pack('P', $optval));
218 0 $optval_len = pack 'L!', length($optval);
219 }
220 }
221  
222 0 my $optval_len_ptr = unpack('L!', pack('P', $optval_len));
223  
224 $self->check_error(
225 'zmq_getsockopt',
226 0 $self->_ffi->{zmq_getsockopt}->(
227 $self->_socket,
228 $opt,
229 $optval_ptr,
230 $optval_len_ptr
231 )
232 );
233  
234 0 $optval = $self->_unpack($opt_type, $optval, $optval_ptr, $optval_len);
235 0 return $optval;
236 }
237  
238 sub set {
239 0 ▶ my ($self, $opt, $opt_type, $opt_val) = @_;
Subroutine set: not called CC=2 SCAR=17.9
Pod undocumented
240  
241 0 my $ffi = $self->_ffi;
242  
243 0 ▶ if ($opt_type =~ m/^(binary|string$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_type =~ /^(binary|string$)/) { } 0 0 0
244 $self->check_error(
245 'zmq_setsockopt',
246 0 $ffi->{str_zmq_setsockopt}->(
247 $self->_socket,
248 $opt,
249 $opt_val,
250 length($opt_val)
251 )
252 );
253 }
254 else {
255 0 my $packed = $self->_pack($opt_type, $opt_val);
256  
257 0 my $opt_ptr = unpack('L!', pack('P', $packed));
258 0 my $opt_len = length($packed);
259  
260 $self->check_error(
261 'zmq_setsockopt',
262 0 $ffi->{int_zmq_setsockopt}->(
263 $self->_socket,
264 $opt,
265 $opt_ptr,
266 $opt_len
267 )
268 );
269 }
270 }
271  
272 sub _pack {
273 0 ▶ my ($self, $opt_type, $val) = @_;
Subroutine _pack: not called CC=2 SCAR=17.9
274  
275 0 my $packed;
276 0 for ($opt_type) {
277 0 when (/^int64_t$/) { $packed = int64_to_native($val) }
278 0 when (/^uint64_t$/) { $packed = uint64_to_native($val) }
279  
280 0 default {
281 0 $packed = pack $self->_pack_type($opt_type), $val;
282 }
283 }
284  
285 0 return $packed;
286 }
287  
288 sub _unpack {
289 0 ▶ my ($self, $opt_type, $optval, $optval_ptr, $optval_len) = @_;
Subroutine _unpack: not called CC=3 SCAR=24.8
290  
291 0 for ($opt_type) {
292 0 when (/^binary$/) {
293 0 $optval_len = unpack 'L!', $optval_len;
294  
295 0 ▶ if ($optval_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optval_len == 0) 0 0 0
296 0 return;
297 }
298  
299 0 $optval = $optval_ptr->tostr($optval_len);
300 }
301  
302 0 when (/^string$/) {
303 0 $optval = $optval_ptr->tostr();
304 }
305  
306 0 when (/^int64_t$/) { $optval = native_to_int64($optval) }
307 0 when (/^uint64_t$/) { $optval = native_to_uint64($optval) }
308  
309 0 default {
310 0 $optval = unpack $self->_pack_type($opt_type), $optval;
311 }
312 }
313  
314 0 return $optval;
315 }
316  
317 sub _pack_type {
318 0 ▶ my ($self, $zmqtype) = @_;
Subroutine _pack_type: not called CC=2 SCAR=17.9
319  
320 # opts we use native perl packing for (currently just int)
321 0 ▶ if ( $zmqtype eq 'int' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zmqtype eq "int") 0 0 0
322 0 return 'i!';
323 }
324  
325 0 confess "unsupported type '$zmqtype'";
326 }
327  
328 sub close {
329 0 ▶ my $self = shift;
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
330  
331 $self->check_error(
332 'zmq_close',
333 0 $self->_ffi->{zmq_close}->($self->_socket)
334 );
335  
336 0 $self->_socket(-1);
337 }
338  
339 sub _init_ffi {
340 0 ▶ my $self = shift;
Subroutine _init_ffi: not called CC=1 SCAR=6.9
341  
342 0 my $soname = $self->soname;
343  
344 0 my $ffi = {};
345 0 $ffi->{zmq_socket} = FFI::Raw->new(
346 $soname => 'zmq_socket',
347 FFI::Raw::ptr, # returns socket ptr
348 FFI::Raw::ptr, # takes ctx ptr
349 FFI::Raw::int # socket type
350 );
351  
352 0 $ffi->{zmq_getsockopt} = FFI::Raw->new(
353 $soname => 'zmq_getsockopt',
354 FFI::Raw::int, # retval
355 FFI::Raw::ptr, # socket ptr,
356 FFI::Raw::int, # option constant
357 FFI::Raw::ptr, # buf for option value
358 FFI::Raw::ptr # buf for size of option value
359 );
360  
361 0 $ffi->{int_zmq_setsockopt} = FFI::Raw->new(
362 $soname => 'zmq_setsockopt',
363 FFI::Raw::int, # retval
364 FFI::Raw::ptr, # socket ptr,
365 FFI::Raw::int, # option constant
366 FFI::Raw::ptr, # ptr to value int
367 FFI::Raw::int # size of option value
368 );
369  
370 0 $ffi->{str_zmq_setsockopt} = FFI::Raw->new(
371 $soname => 'zmq_setsockopt',
372 FFI::Raw::int, # retval
373 FFI::Raw::ptr, # socket ptr,
374 FFI::Raw::int, # option constant
375 FFI::Raw::str, # ptr to value string
376 FFI::Raw::int # size of option value
377 );
378  
379 0 $ffi->{zmq_connect} = FFI::Raw->new(
380 $soname => 'zmq_connect',
381 FFI::Raw::int,
382 FFI::Raw::ptr,
383 FFI::Raw::str
384 );
385  
386 0 $ffi->{zmq_bind} = FFI::Raw->new(
387 $soname => 'zmq_bind',
388 FFI::Raw::int,
389 FFI::Raw::ptr,
390 FFI::Raw::str
391 );
392  
393 0 $ffi->{zmq_msg_init} = FFI::Raw->new(
394 $soname => 'zmq_msg_init',
395 FFI::Raw::int, # retval
396 FFI::Raw::ptr, # zmq_msg_t ptr
397 );
398  
399 0 $ffi->{zmq_msg_init_size} = FFI::Raw->new(
400 $soname => 'zmq_msg_init_size',
401 FFI::Raw::int,
402 FFI::Raw::ptr,
403 FFI::Raw::int
404 );
405  
406 0 $ffi->{zmq_msg_size} = FFI::Raw->new(
407 $soname => 'zmq_msg_size',
408 FFI::Raw::int, # returns msg size in bytes
409 FFI::Raw::ptr # msg ptr
410 );
411  
412 0 $ffi->{zmq_msg_data} = FFI::Raw->new(
413 $soname => 'zmq_msg_data',
414 FFI::Raw::ptr, # msg data ptr
415 FFI::Raw::ptr # msg ptr
416 );
417  
418 0 $ffi->{zmq_msg_close} = FFI::Raw->new(
419 $soname => 'zmq_msg_data',
420 FFI::Raw::int, # retval
421 FFI::Raw::ptr # msg ptr
422 );
423  
424 0 $ffi->{zmq_close} = FFI::Raw->new(
425 $soname => 'zmq_close',
426 FFI::Raw::int,
427 FFI::Raw::ptr,
428 );
429  
430 0 $ffi->{memcpy} = FFI::Raw->new(
431 undef, 'memcpy',
432 FFI::Raw::ptr, # dest filled
433 FFI::Raw::ptr, # dest buf
434 FFI::Raw::ptr, # src
435 FFI::Raw::int # buf size
436 );
437  
438 0 return $ffi;
439 }
440  
441 sub DEMOLISH {
442 0 ▶ my $self = shift;
Subroutine DEMOLISH: not called CC=2 SCAR=17.9
Pod undocumented
443  
444 0 ▶ unless ($self->_socket == -1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_socket == -1) 0 0 0
445 0 $self->close();
446 }
447 }
448  
449 1 __PACKAGE__->meta->make_immutable();
450  
451 __END__
↑ Summary FFI/Util.pm »