From 3e35395f8064917ca18fdbf5d5f1f42de0c8da9e Mon Sep 17 00:00:00 2001 From: Aranya Sen Date: Sun, 31 Mar 2019 11:39:09 +0530 Subject: [PATCH] Make sure HL7 sender honors timeout value --- src/HL7/Connection.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/HL7/Connection.php b/src/HL7/Connection.php index 50f6a3e..9560798 100644 --- a/src/HL7/Connection.php +++ b/src/HL7/Connection.php @@ -35,6 +35,7 @@ class Connection { protected $socket; + protected $timeout; protected $MESSAGE_PREFIX; protected $MESSAGE_SUFFIX; @@ -51,6 +52,7 @@ public function __construct(string $host, int $port, int $timeout = 10) $this->setSocket($host, $port, $timeout); $this->MESSAGE_PREFIX = "\013"; $this->MESSAGE_SUFFIX = "\034\015"; + $this->timeout = $timeout; } /** @@ -123,11 +125,15 @@ public function send(Message $req, string $responseCharEncoding = 'UTF-8'): Mess $data = null; + $startTime = time(); while (($buf = socket_read($this->socket, 1024)) !== false) { // Read ACK / NACK from server $data .= $buf; if (preg_match('/' . $this->MESSAGE_SUFFIX . '$/', $data)) { break; } + if ((time() - $startTime) > $this->timeout) { + throw new HL7ConnectionException("Timed out listening for response from server"); + } } // Remove message prefix and suffix