File Coverage

blib/lib/Riemann/Client/Transport/TCP.pm
Criterion Covered Total %
statement 6 27 22.2
branch 0 12 0.0
condition n/a
subroutine 2 4 50.0
pod 0 2 0.0
total 8 45 17.7


line stmt bran cond sub pod time code
1             package # hide from CPAN
2             Riemann::Client::Transport::TCP;
3              
4 2     2   14 use Moo;
  2         6  
  2         15  
5              
6 2     2   1642 use Riemann::Client::Protocol;
  2         9  
  2         580  
7              
8             extends 'Riemann::Client::Transport';
9              
10             sub send {
11 0     0 0   my ($self, $msg) = @_;
12              
13             # Encode the message
14 0           my $encoded = Msg->encode($msg);
15 0           my $e_length = length $encoded;
16              
17             # Prepend the length to the binary message
18 0           my $to_send = pack('N', $e_length) . $encoded;
19 0           my $sock = $self->socket;
20 0 0         unless ($sock->connected) {
21 0           $self->clear_socket;
22 0           $sock = $self->socket;
23             }
24              
25             # Write to the socket
26 0 0         print $sock $to_send or die $!;
27              
28             # Read 4 bytes of the response to get the length
29 0           my $res_length;
30 0           my $r = read $sock, $res_length, 4;
31 0 0         die $! unless defined $r;
32 0           $res_length = unpack('N', $res_length);
33              
34             # Something went really wrong. Maybe the connection was closed
35 0 0         die "Did not receive a response" unless $res_length;
36              
37             # Read the actual response
38 0           my $recv;
39 0           $r = read $sock, $recv, $res_length;
40 0 0         die $! unless defined $r;
41              
42             # Decode the message and check for errors
43 0           my $res = Msg->decode($recv);
44 0 0         die $res->{error} unless $res->{ok};
45              
46 0           return $res;
47             }
48              
49             sub DEMOLISH {
50             # Close sockets properly on destroy
51 0     0 0   close shift->socket;
52             }
53              
54             1;