From e08f1246afc7fcabe068d1b867dd386eca43ac38 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 14 Apr 2006 13:42:19 +0000 Subject: [PATCH] added a flush_socket method to the jabber client code to allow a unix child process to drop any pending socket data before continuing on git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@690 9efc2488-bf62-4759-914b-345cdb29e865 --- .../OpenSRF/Transport/SlimJabber/Client.pm | 31 ++++++++++++++++++++++ src/perlmods/OpenSRF/UnixServer.pm | 20 +++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/perlmods/OpenSRF/Transport/SlimJabber/Client.pm b/src/perlmods/OpenSRF/Transport/SlimJabber/Client.pm index 810d9d8..2ef75e6 100644 --- a/src/perlmods/OpenSRF/Transport/SlimJabber/Client.pm +++ b/src/perlmods/OpenSRF/Transport/SlimJabber/Client.pm @@ -563,4 +563,35 @@ sub process { } +# -------------------------------------------------------------- +# Sets the socket to O_NONBLOCK, reads all of the data off of +# the socket, the restores the sockets flags +# Returns 1 on success, 0 if the socket isn't connected +# -------------------------------------------------------------- +sub flush_socket { + + my $self = shift; + my $socket = $self->{_socket}; + + if( $socket and $socket->connected() ) { + + my $buf; + my $flags = fcntl($socket, F_GETFL, 0); + + fcntl($socket, F_SETFL, $flags | O_NONBLOCK); + while( my $n = sysread( $socket, $buf, 8192 ) ) { + $logger->debug("flush_socket dropped $n bytes of data"); + } + fcntl($socket, F_SETFL, $flags); + + return 1; + + } else { + + return 0; + } +} + + + 1; diff --git a/src/perlmods/OpenSRF/UnixServer.pm b/src/perlmods/OpenSRF/UnixServer.pm index 1b57562..b80a9b5 100644 --- a/src/perlmods/OpenSRF/UnixServer.pm +++ b/src/perlmods/OpenSRF/UnixServer.pm @@ -12,8 +12,12 @@ use OpenSRF::Utils::SettingsClient; use Time::HiRes qw(time); use JSON; use vars qw/@ISA $app/; +use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); use Carp; +use IO::Socket::INET; +use IO::Socket::UNIX; + # XXX Need to add actual logging statements in the code my $logger = "OpenSRF::Utils::Logger"; @@ -84,10 +88,20 @@ sub process_request { $logger->debug( "Error closing Unix socket: $!", ERROR ); } - my $app = $self->app(); $logger->transport( "UnixServer for $app received $data", INTERNAL ); + # -------------------------------------------------------------- + # Drop all data from the socket before coninuting to process + # -------------------------------------------------------------- + my $ph = OpenSRF::Transport::PeerHandle->retrieve; + if(!$ph->flush_socket()) { + $logger->error("We received a request ". + "and we are no longer connected to the jabber network. ". + "We will go away and drop this request: $data"); + exit; + } + my $app_session = OpenSRF::Transport->handler( $self->app(), $data ); if(!ref($app_session)) { @@ -141,8 +155,6 @@ sub process_request { $app_session->kill_me if ($app_session); $0 = $orig; - - } @@ -243,8 +255,8 @@ sub child_init_hook { OpenSRF::Application->application_implementation->child_init if (OpenSRF::Application->application_implementation->can('child_init')); - return OpenSRF::Transport::PeerHandle->retrieve; + return OpenSRF::Transport::PeerHandle->retrieve; } 1; -- 2.11.0