Net/NATS/Connection.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 168 CC 19 SCAR 36.9
CC
19
Cov
0%
nb_getline
72.0
nb_read
72.0
upgrade
12.0
CRAP 40.1
1 package Net::NATS::Connection;
2 use strict;
3  
4 use Class::XSAccessor {
5 constructors => [ '_new' ],
6 accessors => [
7 'socket_args',
8 '_socket',
9 ],
10 lvalue_accessors => [
11 'buffer',
12 'eobuf',
13 'eof',
14 'error',
15 ],
16 };
17  
18 use IO::Socket::INET;
19 use Errno qw(EAGAIN EINTR EWOULDBLOCK);
20  
21 sub new {
22 my $class = shift;
23 my $self = $class->_new(@_);
24  
25 $self->socket_args->{Proto} = 'tcp';
26  
27 my $socket = IO::Socket::INET->new(%{$self->socket_args})
28 or return;
29 $self->_socket($socket);
30 $socket->blocking(0);
31 $self->flush();
32  
33 return $self;
34 }
35  
36 sub upgrade {
37 my $self = shift;
38  
39 unless ($IO::Socket::SSL::VERSION) {
40 eval { require IO::Socket::SSL };
41 die $@ if $@;
42 }
43  
44 my $socket = IO::Socket::SSL->start_SSL($self->_socket, %{$self->socket_args})
45 or return;
46  
47 $self->_socket($socket);
48 $socket->blocking(0);
49 $self->flush();
50 }
51  
52 # clear out the buffered data
53 sub flush {
54 my $self = shift;
55  
56 $self->buffer = '';
57 $self->eobuf = 0;
58 $self->eof = 0;
59 $self->error = '';
60 }
61  
62 # test if we have data on the handle, with optional timeout.
63 sub can_read {
64 my $self = shift;
65  
66 return IO::Select->new($self->_socket)->can_read(@_);
67 }
68  
69 # block until the handle is ready to write, with optional timeout.
70 sub can_write {
71 my $self = shift;
72  
73 return IO::Select->new($self->_socket)->can_write(@_);
74 }
75  
76  
77 # implement non-blocking getline() function by managing our own data buffer
78 # based on sample code from "Network Programming with Perl" by L.D. Stein.
79  
80 # $bytes = $self->nb_getline($data);
81 # data is stored in $data, returns number of bytes on success
82 # returns undef on error and sets $self->error, $data has any partial read
83 # returns 0 on EOF, $data has partial read
84 # returns 0E0 if would block (ie, not a full line read), data is unchanged.
85  
86 sub nb_getline {
87 my $self = shift;
88  
89 return 0 if $self->eof; # previous read reached EOF
90 return undef if $self->error; # previous read encountered error
91  
92 # look up position of EOL in the buffer
93 my $idx = index($self->buffer, $/);
94 if ($idx < 0) {
95 # EOL was not found, so suck in more data if we can
96 $self->eobuf = length $self->buffer;
97 # append to our buffer from the file handle if any data is there.
98 my $count = sysread($self->_socket,$self->buffer,1024,$self->eobuf);
99  
100 if (!defined $count) {
101 return '0E0' if $! == EWOULDBLOCK; # we handle this error
102  
103 $self->error = $!; # remember the error for later
104 $_[0] = $self->buffer; # return whatever we read
105 return length($_[0]);
106 }
107 elsif ($count == 0) { # EOF
108 $self->eof = 1; # remember for later
109 $_[0] = $self->buffer; # return whatever we read
110 return length($_[0]);
111 }
112 else {
113 # look for EOL again in the newly read data
114 $idx = index($self->buffer, $/, $self->eobuf);
115 # if not found, pretend this was EWOULDBLOCK
116 if ($idx < 0) {
117 $self->eobuf = length $self->buffer;
118 return '0E0';
119 }
120 }
121 }
122  
123 # we successfully read what we needed up to a new line
124 $_[0] = substr($self->buffer,0,$idx + length($/));
125 substr($self->buffer,0,$idx + length($/)) = '';
126 $self->eobuf = length $self->buffer;
127 return length($_[0]);
128 }
129  
130  
131 # implement read() function upon our data buffer.
132 sub nb_read {
133 my $self = shift;
134 my $length = $_[1];
135  
136 return 0 if $self->eof; # previous read reached EOF
137 return undef if $self->error; # previous read encountered error
138  
139 # do we have enough data?
140 if (length $self->buffer < $length) {
141 # not enough, so suck in more data if we can
142 $self->eobuf = length $self->buffer;
143 # append to our buffer from the file handle if any data is there.
144 my $count = sysread($self->_socket,$self->buffer,1024,$self->eobuf);
145  
146 if (!defined $count) {
147 return '0E0' if $! == EWOULDBLOCK; # we handle this error
148  
149 $self->error = $!; # remember the error for later
150 $_[0] = $self->buffer; # return whatever we read
151 return length($_[0]);
152 }
153 elsif ($count == 0) { # EOF
154 $self->eof = 1; # remember for later
155 $_[0] = $self->buffer; # return whatever we read
156 return length($_[0]);
157 }
158 else {
159 # check length again
160 $self->eobuf = length $self->buffer;
161 # if not, pretend this was EWOULDBLOCK
162 if ($self->eobuf < $length) {
163 return '0E0';
164 }
165 }
166 }
167  
168 # we successfully read what we needed
169 $_[0] = substr($self->buffer,0,$length);
170 substr($self->buffer,0,$length) = '';
171 $self->eobuf = length $self->buffer;
172 return length($_[0]);
173 }
174  
175 # blocking send
176 # return 1 on success, undef on failure
177 sub send {
178 my $self = shift;
179 my $msg = "$_[0]\r\n";
180  
181 my $len = length $msg;
182 my $offset = 0;
183 while ($len) {
184 my $written = syswrite $self->_socket, $msg, $len, $offset;
185 if (defined $written) {
186 $len -= $written;
187 $offset += $written;
188 } else {
189 if ($! == EAGAIN || $! == EINTR) { # retry sending
190 #warn "waiting for socket ready after $!\n";
191 $self->can_write(); # block until ready to write
192 }
193 else {
194 $self->error = $!;
195 return; # can't do anything with failed write. socket likely closed.
196 }
197 }
198 }
199 return 1;
200 }
201  
202 1;