aboutsummaryrefslogtreecommitdiff
path: root/lib/Arctica/Core/JABus
diff options
context:
space:
mode:
authorVadim Troshchinskiy <vtroshchinskiy@qindel.com>2017-09-18 10:42:23 +0200
committerVadim Troshchinskiy <vtroshchinskiy@qindel.com>2017-09-18 10:42:23 +0200
commitfb59da853fab3545c9531367e097babc87e53101 (patch)
treeb66223bd3f27a1aa73bf760bce2fe022829463f2 /lib/Arctica/Core/JABus
parent85667be79db5df8bbf9082cb560d0cd46e4b196f (diff)
downloadperl-Arctica-Core-JABus-Socket-fb59da853fab3545c9531367e097babc87e53101.tar.gz
perl-Arctica-Core-JABus-Socket-fb59da853fab3545c9531367e097babc87e53101.tar.bz2
perl-Arctica-Core-JABus-Socket-fb59da853fab3545c9531367e097babc87e53101.zip
Fix issue with partial JSON reads
It's possible for sysread to be interrupted by events such as a signal. Since the protocol requires whole lines to be read, keep on looping until we get a newline.
Diffstat (limited to 'lib/Arctica/Core/JABus')
-rw-r--r--lib/Arctica/Core/JABus/Socket.pm33
1 files changed, 31 insertions, 2 deletions
diff --git a/lib/Arctica/Core/JABus/Socket.pm b/lib/Arctica/Core/JABus/Socket.pm
index 2ec46fe..fe9e12f 100644
--- a/lib/Arctica/Core/JABus/Socket.pm
+++ b/lib/Arctica/Core/JABus/Socket.pm
@@ -514,7 +514,20 @@ sub _server_handle_conn {
} elsif ( $client_conn_cond >= 'in' ) {
if ($self->{'clients'}{$client_id}{'io_obj'}) {
- my $bytes_read = sysread($self->{'clients'}{$client_id}{'io_obj'},my $in_data,16384);
+ my $bytes_read = 0;
+ my $in_data = "";
+
+ BugOUT(9,"JABus Server _handle_conn()->READ starting");
+ while($in_data !~ /\n$/s) {
+ my $rb = sysread($self->{'clients'}{$client_id}{'io_obj'}, $in_data, 16384, length($in_data));
+ last if ($rb == 0 && $in_data eq "");
+
+ $bytes_read += $rb;
+
+ BugOUT(9,"JABus Server _handle_conn()->BUFFER: '$in_data'");
+ }
+
+ BugOUT(9,"JABus Server _handle_conn()->READ completed, $bytes_read bytes read.");
$self->{'total_bytes_read'} += $bytes_read;
$self->{'clients'}{$client_id}{'bytes_read'}
@@ -709,7 +722,21 @@ sub _client_handle_conn {
return 0;
} elsif ( $connection_cond >= 'in' ) {
if ($self->{'the_socket'}) {
- my $bytes_read = sysread($self->{'the_socket'},my $in_data,16384);
+ my $bytes_read = 0;
+ my $in_data = "";
+
+ BugOUT(9,"JABus Server _handle_conn()->READ starting");
+ while($in_data !~ /\n$/s) {
+ my $rb = sysread($self->{'the_socket'}, $in_data, 16384, length($in_data));
+ last if ($rb == 0 && $in_data eq "");
+
+ $bytes_read += $rb;
+
+ BugOUT(9,"JABus Server _handle_conn()->BUFFER: '$in_data'");
+ }
+
+ BugOUT(9,"JABus Server _handle_conn()->READ completed, $bytes_read bytes read.");
+
$self->{'bytes_read'} += $bytes_read;
@@ -720,6 +747,8 @@ foreach my $in_data_line (split(/\n/,$in_data)) {
if ($in_data_line =~ /JAB/) {
my $jData;
+ BugOUT(9,"JABus::Socket _client_handle_conn()->Received JSON: '$in_data_line'");
+
eval {
$jData = decode_json($in_data_line);
} or warn("JABus _client_handle_conn()->DONE (Got some garbage instead of JSON!)");