From 201b8a9ac971a3039f101ed0bbb87a6ceec78f91 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 24 Jan 2017 13:49:41 +0100 Subject: [PATCH] check if reader tcp connection is alive if it's not, we will exit 1 and expect something else to restart us (while true loop in shell, or goto in bat file) --- lib/Biblio/RFID/Reader/INET.pm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Biblio/RFID/Reader/INET.pm b/lib/Biblio/RFID/Reader/INET.pm index ed0887b..a17e280 100644 --- a/lib/Biblio/RFID/Reader/INET.pm +++ b/lib/Biblio/RFID/Reader/INET.pm @@ -7,6 +7,8 @@ use IO::Socket::INET; use Time::HiRes qw(ualarm); use Data::Dump qw(dump); +my $debug = $ENV{DEBUG}; + =head1 NAME Biblio::RFID::Reader::INET - emulate serial port over TCP socket @@ -15,6 +17,8 @@ Biblio::RFID::Reader::INET - emulate serial port over TCP socket sub write { my $self = shift; + $self->_check_connected; + warn ">> write ",dump(@_) if $debug; my $count = $self->SUPER::print(@_); $self->flush; #warn "XX ",ref($self), " write response: $count ", dump(@_); @@ -25,9 +29,10 @@ our $read_char_time = 1; sub read_char_time { $read_char_time = $_[1] * 1_000 || 1_000_000 }; sub read_const_time {}; -sub read { +sub read(*\$$;$) { my $self = shift; my $len = shift || die "no length?"; + #warn "XX ",ref($self), " read $len timeout $read_char_time"; my $buffer; eval { @@ -44,10 +49,20 @@ sub read { $len = 0; } - #warn "## read $len ",dump($buffer); + $self->_check_connected; + + warn "<< read $len ",dump($buffer) if $debug; return ( $len, $buffer ); } +sub _check_connected { + my $self = shift; + return if $self->connected; + + warn "LOST TCP Connection"; + exit 1; +} + 1; __END__ -- 2.20.1