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